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

Add tentez switch #41

Merged
merged 3 commits into from
May 25, 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
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ aws_listener_rules:

```console
# rollback
# if you want to skip the pause step, add `--no-pause` flag.
$ tentez -f ./examples/example.yaml rollback
1. pause
2. switch old:new = 100:0
Expand All @@ -135,6 +136,34 @@ Switched at 2022-03-12 12:05:30
Apply complete!
```

```console
# switch weights
# this command overrides steps of the config file.
# if you want to skip the pause step, add `--no-pause` flag.
$ tentez -f ./examples/example.yaml switch --weights 70,30
1. pause
2. switch old:new = 70:30
1. tentez-web
2. tentez-api
3. tentez-foo

1 / 2 steps
Pause
enter "yes", continue steps.
If you'd like to interrupt steps, enter "quit".
> yes
continue step

2 / 2 steps
Switch old:new = 70:30
1. tentez-web switched!
2. tentez-api switched!
3. tentez-foo switched!
Switched at 2023-05-25 13:18:25

Apply complete!
```

```console
# show version
$ tentez version
Expand Down
1 change: 0 additions & 1 deletion internal/cli/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ var applyCmd = &cobra.Command{
# show plan and apply
$ tentez -f ./examples/example.yaml apply`,
RunE: func(cmd *cobra.Command, args []string) error {
filename := cmd.Flag("filename").Value.String()
t, err := tentez.NewFromYaml(filename)
if err != nil {
return err
Expand Down
1 change: 0 additions & 1 deletion internal/cli/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ var getCmd = &cobra.Command{
# Show current state of targets
$ tentez -f ./examples/example.yaml get`,
RunE: func(cmd *cobra.Command, args []string) error {
filename := cmd.Flag("filename").Value.String()
t, err := tentez.NewFromYaml(filename)
if err != nil {
return err
Expand Down
1 change: 0 additions & 1 deletion internal/cli/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ var planCmd = &cobra.Command{
# show plan
$ tentez -f ./examples/example.yaml plan`,
RunE: func(cmd *cobra.Command, args []string) error {
filename := cmd.Flag("filename").Value.String()
t, err := tentez.NewFromYaml(filename)
if err != nil {
return err
Expand Down
6 changes: 4 additions & 2 deletions internal/cli/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@ var rollbackCmd = &cobra.Command{
# rollback
$ tentez -f ./examples/example.yaml rollback`,
RunE: func(cmd *cobra.Command, args []string) error {
filename := cmd.Flag("filename").Value.String()
t, err := tentez.NewFromYaml(filename)

if err != nil {
return err
}

return t.Rollback(true)
return t.Rollback(!noPause)
},
}

func init() {
rollbackCmd.Flags().StringVarP(&filename, "filename", "f", "", "config file for tentez")
rollbackCmd.Flags().BoolVar(&noPause, "no-pause", false, "skip pause")

if err := rollbackCmd.MarkFlagRequired("filename"); err != nil {
panic(err)
}
Expand Down
1 change: 1 addition & 0 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
)

var filename = ""
var noPause = false

var rootCmd = &cobra.Command{
Use: "tentez",
Expand Down
42 changes: 42 additions & 0 deletions internal/cli/switch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cli

import (
"fmt"

"github.com/FeLvi-zzz/tentez"
"github.com/spf13/cobra"
)

var switchCmd = &cobra.Command{
Use: "switch",
Short: "Switch targets weights",
Long: `
# show plan and switch
$ tentez -f ./examples/example.yaml switch --weight 30,70`,
RunE: func(cmd *cobra.Command, args []string) error {
t, err := tentez.NewFromYaml(filename)
if err != nil {
return err
}

if len(weights) != 2 {
return fmt.Errorf("the length of weights must be 2, got %v, len: %d", weights, len(weights))
}

return t.Switch(weights, !noPause)
},
}

var weights []int

func init() {
switchCmd.Flags().StringVarP(&filename, "filename", "f", "", "config file for tentez")
switchCmd.Flags().IntSliceVarP(&weights, "weights", "w", []int{}, "weights for switch, 'old,new'")
switchCmd.Flags().BoolVar(&noPause, "no-pause", false, "skip pause")

if err := switchCmd.MarkFlagRequired("filename"); err != nil {
panic(err)
}

rootCmd.AddCommand(switchCmd)
}
24 changes: 24 additions & 0 deletions tentez.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Tentez interface {
Apply(isForce bool) error
Get() (map[TargetType]TargetsData, error)
Rollback(hasPause bool) error
Switch(weights []int, hasPause bool) error
}

type tentez struct {
Expand Down Expand Up @@ -129,3 +130,26 @@ func (t tentez) Rollback(hasPause bool) (err error) {
}
return t.Apply(true)
}

func (t tentez) Switch(weights []int, hasPause bool) (err error) {
t.Steps = []Step{}

if hasPause {
t.Steps = append(t.Steps, Step{
Type: StepTypePause,
})
}

t.Steps = append(t.Steps, Step{
Type: StepTypeSwitch,
Weight: Weight{
Old: int32(weights[0]),
New: int32(weights[1]),
},
})

if err = t.Plan(); err != nil {
return err
}
return t.Apply(false)
}