Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix types in configs for v2.8 #912

Merged
merged 4 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/docker-feature.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
pull-requests: write
strategy:
matrix:
servis: [api, checker, cli, notifier, filter]
services: [api, checker, cli, notifier, filter]
runs-on: ubuntu-22.04
if: ${{github.event.issue.pull_request != null && startsWith(github.event.comment.body, '/build') && github.event.comment.author_association == 'MEMBER'}}
steps:
Expand Down Expand Up @@ -64,12 +64,12 @@ jobs:
uses: docker/build-push-action@v4
with:
context: "https://github.com/${{fromJSON(steps.get-pr.outputs.result).head.repo.full_name}}.git#${{fromJSON(steps.get-pr.outputs.result).head.ref}}"
file: ./Dockerfile.${{matrix.servis}}
file: ./Dockerfile.${{matrix.services}}
build-args: |
MoiraVersion=${{ env.DOCKER_TAG }}
GIT_COMMIT=${{ fromJSON(steps.get-pr.outputs.result).head.sha }}
push: true
tags: moira/${{matrix.servis}}-unstable:${{env.DOCKER_TAG}}
tags: moira/${{matrix.services}}-unstable:${{env.DOCKER_TAG}}

- name: Comment PR with build tag
uses: mshick/add-pr-comment@v2
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/docker-nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-22.04
strategy:
matrix:
servis: [api, checker, cli, notifier, filter]
services: [api, checker, cli, notifier, filter]
steps:

- name: Set up Docker Buildx
Expand All @@ -28,12 +28,12 @@ jobs:
- name: Build and push
uses: docker/build-push-action@v4
with:
file: ./Dockerfile.${{matrix.servis}}
file: ./Dockerfile.${{matrix.services}}
build-args: |
MoiraVersion=${{env.DOCKER_TAG}}
GIT_COMMIT=${{github.sha}}
push: true
tags: moira/${{matrix.servis}}-nightly:${{env.DOCKER_TAG}}
tags: moira/${{matrix.services}}-nightly:${{env.DOCKER_TAG}}

- name: Comment PR with build tag
uses: mshick/add-pr-comment@v2
Expand Down
7 changes: 3 additions & 4 deletions .github/workflows/docker-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-22.04
strategy:
matrix:
servis: [api, checker, cli, notifier, filter]
services: [api, checker, cli, notifier, filter]
steps:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
Expand All @@ -27,10 +27,9 @@ jobs:
- name: Build and push
uses: docker/build-push-action@v4
with:
file: ./Dockerfile.${{matrix.servis}}
file: ./Dockerfile.${{matrix.services}}
build-args: |
MoiraVersion=${{env.DOCKER_TAG}}
GIT_COMMIT=${{github.sha}}
push: true
tags: moira/${{matrix.servis}}:${{env.DOCKER_TAG}},moira/${{matrix.servis}}:latest

tags: moira/${{matrix.services}}:${{env.DOCKER_TAG}}
15 changes: 10 additions & 5 deletions cmd/cli/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,19 @@ var tagSubscriptionsKeyPrefixNew = "{moira-tag-subscriptions}:"
var tagTriggersKeyKeyPrefixOld = "moira-tag-triggers:"
var tagTriggersKeyKeyPrefixNew = "{moira-tag-triggers}:"

func renameKey(database moira.Database, oldKey string, newKey string) error {
func renameKey(database moira.Database, oldValue, newValue string) error {
switch d := database.(type) {
case *redis.DbConnector:
err := d.Client().Rename(d.Context(), oldKey, newKey).Err()
pipe := d.Client().TxPipeline()
iter := d.Client().Scan(d.Context(), 0, oldValue, 0).Iterator()
for iter.Next(d.Context()) {
oldKey := iter.Val()
newKey := strings.Replace(iter.Val(), oldValue, newValue, 1)
pipe.Rename(d.Context(), oldKey, newKey)
}
_, err := pipe.Exec(d.Context())
if err != nil {
if err.Error() != noSuchKeyError {
return err
}
return err
}
}

Expand Down
107 changes: 107 additions & 0 deletions cmd/cli/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/moira-alert/moira"

rds "github.com/go-redis/redis/v8"
. "github.com/smartystreets/goconvey/convey"
)

Expand Down Expand Up @@ -295,3 +296,109 @@ func createDataWithNewKeys(database moira.Database) {
client.SAdd(ctx, "{moira-tag-triggers}:tag3", "triggerID-0000000000003")
}
}

func Test_renameKey(t *testing.T) {
logger, _ := logging.GetLogger("Test Worker")
oldKey := "my_test_key"
newKey := "my_new_test_key"

Convey("Something was renamed", t, func() {
database := redis.NewTestDatabase(logger)
database.Flush()
defer database.Flush()
err := database.Client().Set(database.Context(), oldKey, "123", 0).Err()
So(err, ShouldBeNil)

err = renameKey(database, oldKey, newKey)
So(err, ShouldBeNil)

res, err := database.Client().Get(database.Context(), newKey).Result()
So(err, ShouldBeNil)
So(res, ShouldResemble, "123")
err = database.Client().Get(database.Context(), oldKey).Err()
So(err, ShouldEqual, rds.Nil)
})

Convey("Nothing was renamed", t, func() {
database := redis.NewTestDatabase(logger)
database.Flush()
defer database.Flush()
err := database.Client().Set(database.Context(), oldKey, "123", 0).Err()
So(err, ShouldBeNil)

err = renameKey(database, "no_exist_key", newKey)
So(err, ShouldBeNil)

err = database.Client().Get(database.Context(), newKey).Err()
So(err, ShouldEqual, rds.Nil)

res, err := database.Client().Get(database.Context(), oldKey).Result()
So(err, ShouldBeNil)
So(res, ShouldResemble, "123")
})
}

func Test_changeKeysPrefix(t *testing.T) {
logger, _ := logging.GetLogger("Test Worker")
oldKey := "my_test_key"
newKey := "my_new_test_key"

Convey("Something was renamed", t, func() {
database := redis.NewTestDatabase(logger)
database.Flush()
defer database.Flush()
err := database.Client().Set(database.Context(), oldKey+"1", "1", 0).Err()
So(err, ShouldBeNil)
err = database.Client().Set(database.Context(), oldKey+"2", "2", 0).Err()
So(err, ShouldBeNil)
err = database.Client().Set(database.Context(), oldKey+"3", "3", 0).Err()
So(err, ShouldBeNil)

err = changeKeysPrefix(database, oldKey, newKey)
So(err, ShouldBeNil)

res, err := database.Client().Get(database.Context(), newKey+"1").Result()
So(err, ShouldBeNil)
So(res, ShouldResemble, "1")
res, err = database.Client().Get(database.Context(), newKey+"2").Result()
So(err, ShouldBeNil)
So(res, ShouldResemble, "2")
res, err = database.Client().Get(database.Context(), newKey+"3").Result()
So(err, ShouldBeNil)
So(res, ShouldResemble, "3")
err = database.Client().Get(database.Context(), oldKey+"1").Err()
So(err, ShouldEqual, rds.Nil)
err = database.Client().Get(database.Context(), oldKey+"2").Err()
So(err, ShouldEqual, rds.Nil)
err = database.Client().Get(database.Context(), oldKey+"3").Err()
So(err, ShouldEqual, rds.Nil)
})

Convey("Nothing was renamed", t, func() {
database := redis.NewTestDatabase(logger)
database.Flush()
defer database.Flush()
err := database.Client().Set(database.Context(), oldKey+"1", "1", 0).Err()
So(err, ShouldBeNil)
err = database.Client().Set(database.Context(), oldKey+"2", "2", 0).Err()
So(err, ShouldBeNil)
err = database.Client().Set(database.Context(), oldKey+"3", "3", 0).Err()
So(err, ShouldBeNil)

err = renameKey(database, "no_exist_key", newKey)
So(err, ShouldBeNil)

err = database.Client().Get(database.Context(), newKey).Err()
So(err, ShouldEqual, rds.Nil)

res, err := database.Client().Get(database.Context(), oldKey+"1").Result()
So(err, ShouldBeNil)
So(res, ShouldResemble, "1")
res, err = database.Client().Get(database.Context(), oldKey+"2").Result()
So(err, ShouldBeNil)
So(res, ShouldResemble, "2")
res, err = database.Client().Get(database.Context(), oldKey+"3").Result()
So(err, ShouldBeNil)
So(res, ShouldResemble, "3")
})
}
14 changes: 9 additions & 5 deletions cmd/cli/from_2.7_to_2.8.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package main

import "github.com/moira-alert/moira"
import (
"fmt"

"github.com/moira-alert/moira"
)

func updateFrom27(logger moira.Logger, dataBase moira.Database) error {
logger.Info().Msg("Update 2.7 -> 2.8 was started")

logger.Info().Msg("Rename keys was started")
if err := updateSubscriptionKeyForAnonymous(logger, dataBase); err != nil {
return err
return fmt.Errorf("failed updateSubscriptionKeyForAnonymous, has error %w", err)
}

if err := updateContactKeyForAnonymous(logger, dataBase); err != nil {
return err
return fmt.Errorf("failed updateContactKeyForAnonymous, has error %w", err)
}

logger.Info().Msg("Update 2.7 -> 2.8 was finished")
Expand Down Expand Up @@ -42,7 +46,7 @@ var (
)

func updateSubscriptionKeyForAnonymous(logger moira.Logger, database moira.Database) error {
err := changeKeysPrefix(database, subscriptionKeyForAnonymousOld, subscriptionKeyForAnonymousNew)
err := renameKey(database, subscriptionKeyForAnonymousOld, subscriptionKeyForAnonymousNew)
if err != nil {
return err
}
Expand All @@ -53,7 +57,7 @@ func updateSubscriptionKeyForAnonymous(logger moira.Logger, database moira.Datab
}

func updateContactKeyForAnonymous(logger moira.Logger, database moira.Database) error {
err := changeKeysPrefix(database, contactKeyForAnonymousOld, contactKeyForAnonymousNew)
err := renameKey(database, contactKeyForAnonymousOld, contactKeyForAnonymousNew)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var (
GoVersion = "unknown"
)

var moiraValidVersions = []string{"2.3", "2.6"}
var moiraValidVersions = []string{"2.3", "2.6", "2.7"}

var (
configFileName = flag.String("config", "/etc/moira/cli.yml", "Path to configuration file")
Expand Down
14 changes: 7 additions & 7 deletions senders/discord/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const (
)

// Structure that represents the Discord configuration in the YAML file
type discord struct {
type config struct {
Token string `mapstructure:"token"`
FrontURI string `mapstructure:"front_uri"`
}
Expand All @@ -35,21 +35,21 @@ type Sender struct {

// Init reads the yaml config
func (sender *Sender) Init(senderSettings interface{}, logger moira.Logger, location *time.Location, dateTimeFormat string) error {
var ds discord
err := mapstructure.Decode(senderSettings, &ds)
var cfg config
err := mapstructure.Decode(senderSettings, &cfg)
if err != nil {
return fmt.Errorf("failed to decode senderSettings to discord config: %w", err)
}
token := ds.Token
if token == "" {

if cfg.Token == "" {
return fmt.Errorf("cannot read the discord token from the config")
}
sender.session, err = discordgo.New("Bot " + token)
sender.session, err = discordgo.New("Bot " + cfg.Token)
if err != nil {
return fmt.Errorf("error creating discord session: %s", err)
}
sender.logger = logger
sender.frontURI = ds.FrontURI
sender.frontURI = cfg.FrontURI
sender.location = location

handleMsg := func(s *discordgo.Session, m *discordgo.MessageCreate) {
Expand Down
30 changes: 15 additions & 15 deletions senders/mail/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,19 @@ import (
"html/template"
"net/smtp"
"path/filepath"
"strconv"
"time"

"github.com/mitchellh/mapstructure"
"github.com/moira-alert/moira"
)

// Structure that represents the Mail configuration in the YAML file
type mail struct {
type config struct {
MailFrom string `mapstructure:"mail_from"`
SMTPHello string `mapstructure:"smtp_hello"`
SMTPHost string `mapstructure:"smtp_host"`
SMTPPort string `mapstructure:"smtp_port"`
InsecureTLS string `mapstructure:"insecure_tls"`
SMTPPort int64 `mapstructure:"smtp_port"`
InsecureTLS bool `mapstructure:"insecure_tls"`
FrontURI string `mapstructure:"front_uri"`
SMTPPass string `mapstructure:"smtp_pass"`
SMTPUser string `mapstructure:"smtp_user"`
Expand Down Expand Up @@ -59,21 +58,22 @@ func (sender *Sender) Init(senderSettings interface{}, logger moira.Logger, loca
}

func (sender *Sender) fillSettings(senderSettings interface{}, logger moira.Logger, location *time.Location, dateTimeFormat string) error {
var m mail
err := mapstructure.Decode(senderSettings, &m)
var cfg config
err := mapstructure.Decode(senderSettings, &cfg)
if err != nil {
return fmt.Errorf("failed to decode senderSettings to mail config: %w", err)
}

sender.logger = logger
sender.From = m.MailFrom
sender.SMTPHello = m.SMTPHello
sender.SMTPHost = m.SMTPHost
sender.SMTPPort, _ = strconv.ParseInt(m.SMTPPort, 10, 64)
sender.InsecureTLS, _ = strconv.ParseBool(m.InsecureTLS)
sender.FrontURI = m.FrontURI
sender.Password = m.SMTPPass
sender.Username = m.SMTPUser
sender.TemplateFile = m.TemplateFile
sender.From = cfg.MailFrom
sender.SMTPHello = cfg.SMTPHello
sender.SMTPHost = cfg.SMTPHost
sender.SMTPPort = cfg.SMTPPort
sender.InsecureTLS = cfg.InsecureTLS
sender.FrontURI = cfg.FrontURI
sender.Password = cfg.SMTPPass
sender.Username = cfg.SMTPUser
sender.TemplateFile = cfg.TemplateFile
sender.location = location
sender.dateTimeFormat = dateTimeFormat
if sender.Username == "" {
Expand Down
Loading
Loading