Skip to content

Commit

Permalink
cli: add integrity-check-period flag in global context
Browse files Browse the repository at this point in the history
It was impossible to provide integrity check period in `tt restart`.
After the patch flag "--integrity-check-period" is locating in the
global cli context.

Part of tarantool/tt-ee#203
  • Loading branch information
themilchenko authored and oleg-jukovec committed Aug 7, 2024
1 parent 8421eff commit d116dfc
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 12 deletions.
1 change: 1 addition & 0 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ After that tt will be able to manage the application using 'replicaset_example'
false, "Skip cli interaction using default behavior")

integrity.RegisterIntegrityCheckFlag(rootCmd.Flags(), &cmdCtx.Cli.IntegrityCheck)
integrity.RegisterIntegrityCheckPeriodFlag(rootCmd.Flags(), &cmdCtx.Cli.IntegrityCheckPeriod)

rootCmd.Flags().SetInterspersed(false)

Expand Down
15 changes: 6 additions & 9 deletions cli/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strconv"
"sync"
"syscall"
"time"

"github.com/spf13/cobra"
"github.com/tarantool/tt/cli/cmd/internal"
Expand All @@ -25,7 +24,7 @@ var (
// In go, we can't just fork the process (reason - goroutines).
// So, for daemonize, we restarts the process with "watchdog" flag.
watchdog bool
// integrityCheckPeriod is a flag enables periodic integrity checks.
// integrityCheckPeriod is a flag that enables periodic integrity checks.
// The default period is 1 day.
integrityCheckPeriod = 24 * 60 * 60
// startInteractive is startInteractive mode flag. If set, the main process does not exit after
Expand Down Expand Up @@ -60,7 +59,7 @@ func NewStartCmd() *cobra.Command {
startCmd.Flags().MarkHidden("watchdog")
startCmd.Flags().BoolVarP(&startInteractive, "interactive", "i", false, "")

integrity.RegisterIntegrityCheckPeriodFlag(startCmd.Flags(), &integrityCheckPeriod)
integrity.RegisterIntegrityCheckPeriodFlag(startCmd.Flags(), &cmdCtx.Cli.IntegrityCheckPeriod)

return startCmd
}
Expand All @@ -75,7 +74,7 @@ func startInstancesUnderWatchdog(cmdCtx *cmdcontext.CmdCtx, instances []running.
startArgs := []string{}
if cmdCtx.Cli.IntegrityCheck != "" {
startArgs = append(startArgs, "--integrity-check-period",
strconv.Itoa(integrityCheckPeriod))
strconv.FormatUint(uint64(cmdCtx.Cli.IntegrityCheckPeriod), 10))
}

for _, instance := range instances {
Expand Down Expand Up @@ -143,13 +142,11 @@ func internalStartModule(cmdCtx *cmdcontext.CmdCtx, args []string) error {
return nil
}

checkPeriod := time.Duration(0)

if cmdCtx.Cli.IntegrityCheck != "" && integrityCheckPeriod > 0 {
checkPeriod = time.Duration(integrityCheckPeriod * int(time.Second))
if cmdCtx.Cli.IntegrityCheck != "" && cmdCtx.Cli.IntegrityCheckPeriod == 0 {
cmdCtx.Cli.IntegrityCheckPeriod = integrityCheckPeriod
}

if err := running.Start(cmdCtx, &runningCtx.Instances[0], checkPeriod); err != nil {
if err := running.Start(cmdCtx, &runningCtx.Instances[0]); err != nil {
return err
}
return nil
Expand Down
2 changes: 2 additions & 0 deletions cli/cmdcontext/cmdcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ type CliCtx struct {
TarantoolCli TarantoolCli
// IntegrityCheck is a public key used for integrity check.
IntegrityCheck string
// IntegrityCheckPeriod is an period during which the integrity check is reproduced.
IntegrityCheckPeriod int
// This flag disables searching of other tt versions to run
// instead of the current one.
IsSelfExec bool
Expand Down
7 changes: 7 additions & 0 deletions cli/configure/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,13 @@ func ValidateCliOpts(cliCtx *cmdcontext.CliCtx) error {
"you can specify only one of -S(--system), -c(--cfg) and 'TT_CLI_CFG' options")
}
}
if len(cliCtx.IntegrityCheck) == 0 && cliCtx.IntegrityCheckPeriod != 0 {
return fmt.Errorf("need to specify public key in --integrity-check to " +
"use --integrity-check-period")
}
if cliCtx.IntegrityCheckPeriod < 0 {
return fmt.Errorf("--integrity-check-period must take non-negative value")
}
return nil
}

Expand Down
7 changes: 4 additions & 3 deletions cli/running/running.go
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ func RunInstance(ctx context.Context, cmdCtx *cmdcontext.CmdCtx, inst InstanceCt
}

// Start an Instance.
func Start(cmdCtx *cmdcontext.CmdCtx, inst *InstanceCtx, integrityCheckPeriod time.Duration) error {
func Start(cmdCtx *cmdcontext.CmdCtx, inst *InstanceCtx) error {
if err := createInstanceDataDirectories(*inst); err != nil {
return fmt.Errorf("failed to create a directory: %s", err)
}
Expand All @@ -771,7 +771,8 @@ func Start(cmdCtx *cmdcontext.CmdCtx, inst *InstanceCtx, integrityCheckPeriod ti
return nil
}
wd := NewWatchdog(inst.Restartable, 5*time.Second, logger,
&provider, preStartAction, cmdCtx.Integrity, integrityCheckPeriod)
&provider, preStartAction, cmdCtx.Integrity,
time.Duration(cmdCtx.Cli.IntegrityCheckPeriod*int(time.Second)))

defer func() {
cleanup(inst)
Expand Down Expand Up @@ -937,6 +938,7 @@ func StartWatchdog(cmdCtx *cmdcontext.CmdCtx, ttExecutable string, instance Inst
if cmdCtx.Cli.IntegrityCheck != "" {
newArgs = append(newArgs, "--integrity-check", cmdCtx.Cli.IntegrityCheck)
}
newArgs = append(newArgs, args...)

if cmdCtx.Cli.IsSystem {
newArgs = append(newArgs, "-S")
Expand All @@ -947,7 +949,6 @@ func StartWatchdog(cmdCtx *cmdcontext.CmdCtx, ttExecutable string, instance Inst
}

newArgs = append(newArgs, "start", "--watchdog", appName)
newArgs = append(newArgs, args...)

f, err := cmdCtx.Integrity.Repository.Read(ttExecutable)
if err != nil {
Expand Down

0 comments on commit d116dfc

Please sign in to comment.