Skip to content

Commit

Permalink
replicaset downgrade: make version a positional argument
Browse files Browse the repository at this point in the history
Closes #1073

@TarantoolBot
title: replicaset downgrade: make version a positional argument
  • Loading branch information
elhimov committed Dec 28, 2024
1 parent 4974301 commit 23668d2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- `tt coredump pack`: if `-e` option is omitted first search tarantool
executable in tt environment then in `PATH` instead of using the hardcoded
path `/usr/bin/tarantool`.
- `tt replicaset downgrade`: make version a positional argument rather than
using the mandatory option `-v` (`--version`).

### Fixed

Expand Down
42 changes: 20 additions & 22 deletions cli/cmd/replicaset.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"errors"
"fmt"
"os"
"regexp"
"strings"

Expand Down Expand Up @@ -51,7 +50,6 @@ var (

chosenReplicasetAliases []string
lsnTimeout int
downgradeVersion string

replicasetUriHelp = " The URI can be specified in the following formats:\n" +
" * [tcp://][username:password@][host:port]\n" +
Expand Down Expand Up @@ -90,33 +88,33 @@ func newUpgradeCmd() *cobra.Command {

// newDowngradeCmd creates a "replicaset downgrade" command.
func newDowngradeCmd() *cobra.Command {
validateVersion := func(i int) cobra.PositionalArgs {
return func(cmd *cobra.Command, args []string) error {
var versionPattern = regexp.MustCompile(`^\d+\.\d+\.\d+$`)
if args[i] == "" {
return errors.New("need to specify the version to downgrade to")
} else if !versionPattern.MatchString(args[i]) {
return errors.New("version must be in the format " +
"'x.x.x', where x is a number")
}
return nil
}
}

cmd := &cobra.Command{
Use: "downgrade (<APP_NAME> | <URI>) [flags]\n\n" +
Use: "downgrade (<APP_NAME> | <URI>) VERSION [flags]\n\n" +
replicasetUriHelp,
DisableFlagsInUseLine: true,
Short: "Downgrade tarantool cluster",
Long: "Downgrade tarantool cluster.\n\n" +
libconnect.EnvCredentialsHelp + "\n\n",
Run: func(cmd *cobra.Command, args []string) {
var versionPattern = regexp.MustCompile(`^\d+\.\d+\.\d+$`)
if downgradeVersion == "" {
err := errors.New("need to specify the version to downgrade " +
"use --version (-v) option")
util.HandleCmdErr(cmd, err)
os.Exit(1)
} else if !versionPattern.MatchString(downgradeVersion) {
err := errors.New("--version (-v) must be in the format " +
"'x.x.x', where x is a number")
util.HandleCmdErr(cmd, err)
os.Exit(1)
}

cmdCtx.CommandName = cmd.Name()
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo,
internalReplicasetDowngradeModule, args)
util.HandleCmdErr(cmd, err)
},
Args: cobra.ExactArgs(1),
Args: cobra.MatchAll(cobra.ExactArgs(2), validateVersion(1)),
}

cmd.Flags().StringArrayVarP(&chosenReplicasetAliases, "replicaset", "r",
Expand All @@ -125,9 +123,6 @@ func newDowngradeCmd() *cobra.Command {
cmd.Flags().IntVarP(&lsnTimeout, "timeout", "t", 5,
"timeout for waiting the LSN synchronization (in seconds)")

cmd.Flags().StringVarP(&downgradeVersion, "version", "v", "",
"version to downgrade the schema to")

addOrchestratorFlags(cmd)
addTarantoolConnectFlags(cmd)
return cmd
Expand Down Expand Up @@ -608,8 +603,11 @@ func internalReplicasetUpgradeModule(cmdCtx *cmdcontext.CmdCtx, args []string) e

// internalReplicasetDowngradeModule is a "upgrade" command for the replicaset module.
func internalReplicasetDowngradeModule(cmdCtx *cmdcontext.CmdCtx, args []string) error {
target := args[0]
downgradeVersion := args[1]

var ctx replicasetCtx
if err := replicasetFillCtx(cmdCtx, &ctx, args[0], false); err != nil {
if err := replicasetFillCtx(cmdCtx, &ctx, target, false); err != nil {
return err
}
if ctx.IsInstanceConnect {
Expand All @@ -625,7 +623,7 @@ func internalReplicasetDowngradeModule(cmdCtx *cmdcontext.CmdCtx, args []string)
SslCiphers: replicasetSslCiphers,
}
var connOpts connector.ConnectOpts
connOpts, _ = resolveConnectOpts(cmdCtx, cliOpts, &connectCtx, args[0])
connOpts, _ = resolveConnectOpts(cmdCtx, cliOpts, &connectCtx, target)

return replicasetcmd.Downgrade(replicasetcmd.DiscoveryCtx{
IsApplication: ctx.IsApplication,
Expand Down
14 changes: 7 additions & 7 deletions test/integration/replicaset/test_replicaset_downgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def test_downgrade_multi_master(tt_cmd, tmpdir_with_cfg):
)
assert file != ""

downgrade_cmd = [tt_cmd, "replicaset", "downgrade", app_name, "-v=3.0.0"]
downgrade_cmd = [tt_cmd, "replicaset", "downgrade", app_name, "3.0.0"]

rc, out = run_command_and_get_output(downgrade_cmd, cwd=tmpdir)
assert rc == 1
Expand Down Expand Up @@ -97,7 +97,7 @@ def test_downgrade_t2_app_dummy_replicaset(tt_cmd):
assert file != ""

downgrade_cmd = [
tt_cmd, "replicaset", "downgrade", app_name, "--custom", "-v=2.8.2"
tt_cmd, "replicaset", "downgrade", app_name, "2.8.2", "--custom"
]
rc, out = run_command_and_get_output(downgrade_cmd, cwd=test_app_path)
assert rc == 0
Expand Down Expand Up @@ -139,7 +139,7 @@ def test_cluster_replicasets(tt_cmd, tmp_path):
cmd_master
)

downgrade_cmd = [tt_cmd, "replicaset", "downgrade", app_name, "-t=15", "-v=2.11.1"]
downgrade_cmd = [tt_cmd, "replicaset", "downgrade", app_name, "2.11.1", "-t=15"]
rc, out = run_command_and_get_output(downgrade_cmd, cwd=tmp_path)

assert rc == 0
Expand Down Expand Up @@ -171,13 +171,13 @@ def test_downgrade_invalid_version(tt_cmd, tmp_path):
app.build()
app.start()

downgrade_cmd = [tt_cmd, "replicaset", "downgrade", app_name, "-t=15", "-v=1.1.1"]
downgrade_cmd = [tt_cmd, "replicaset", "downgrade", app_name, "1.1.1", "-t=15"]
rc, out = run_command_and_get_output(downgrade_cmd, cwd=tmp_path)

assert rc == 1
assert "Version '1.1.1' is not allowed." in out

downgrade_cmd = [tt_cmd, "replicaset", "downgrade", app_name, "-t=15", "-v=3.0"]
downgrade_cmd = [tt_cmd, "replicaset", "downgrade", app_name, "3.0", "-t=15"]
rc, out = run_command_and_get_output(downgrade_cmd, cwd=tmp_path)

assert "--version (-v) must be in the format 'x.x.x', where x is a number" in out
Expand Down Expand Up @@ -215,7 +215,7 @@ def test_downgrade_remote_replicasets(tt_cmd, tmpdir_with_cfg):
)

uri = "tcp://client:[email protected]:3301"
upgrade_cmd = [tt_cmd, "replicaset", "downgrade", uri, "-t=15", "-v=2.11.1"]
upgrade_cmd = [tt_cmd, "replicaset", "downgrade", uri, "2.11.1", "-t=15"]
rc, out = run_command_and_get_output(upgrade_cmd, cwd=tmpdir)
assert rc == 0
assert "ok" in out
Expand Down Expand Up @@ -267,7 +267,7 @@ def have_buckets_created():
assert wait_event(10, have_buckets_created)

app_dir = cartridge_app.workdir
upgrade_cmd = [tt_cmd, "replicaset", "downgrade", cartridge_name, "-t=15", "-v=2.10.0"]
upgrade_cmd = [tt_cmd, "replicaset", "downgrade", cartridge_name, "2.10.0", "-t=15"]
rc, out = run_command_and_get_output(upgrade_cmd, cwd=app_dir)
assert rc == 0
assert "ok" in out

0 comments on commit 23668d2

Please sign in to comment.