From 30ff7e3c285509ee8769105f99fb79bb8b1f65b6 Mon Sep 17 00:00:00 2001 From: Alexey Potapenko Date: Mon, 23 Dec 2024 10:35:39 +0300 Subject: [PATCH] cli: skip cluster config for the few commands From now on only the `check` and `start` commands do not skip errors during cluster config generation from local, etcs/tcs and env sources. Closes #1038 --- cli/cmd/build.go | 2 +- cli/cmd/check.go | 2 +- cli/cmd/clean.go | 2 +- cli/cmd/cluster.go | 2 +- cli/cmd/connect.go | 2 +- cli/cmd/internal/completion.go | 2 +- cli/cmd/kill.go | 2 +- cli/cmd/log.go | 2 +- cli/cmd/logrotate.go | 2 +- cli/cmd/play.go | 2 +- cli/cmd/replicaset.go | 4 ++-- cli/cmd/start.go | 2 +- cli/cmd/status.go | 2 +- cli/cmd/stop.go | 5 +++-- cli/pack/opts.go | 2 +- cli/running/running.go | 34 +++++++++++++++++++--------------- cli/running/running_test.go | 6 +++--- 17 files changed, 40 insertions(+), 35 deletions(-) diff --git a/cli/cmd/build.go b/cli/cmd/build.go index efcf195c3..08152a06c 100644 --- a/cli/cmd/build.go +++ b/cli/cmd/build.go @@ -30,7 +30,7 @@ func NewBuildCmd() *cobra.Command { args []string, toComplete string) ([]string, cobra.ShellCompDirective) { var runningCtx running.RunningCtx - if err := running.FillCtx(cliOpts, &cmdCtx, &runningCtx, nil, false); err != nil { + if err := running.FillCtx(cliOpts, &cmdCtx, &runningCtx, nil, true); err != nil { return nil, cobra.ShellCompDirectiveNoFileComp } return running.ExtractAppNames(runningCtx.Instances), diff --git a/cli/cmd/check.go b/cli/cmd/check.go index b5aed35b5..cbdcb264f 100644 --- a/cli/cmd/check.go +++ b/cli/cmd/check.go @@ -32,7 +32,7 @@ func internalCheckModule(cmdCtx *cmdcontext.CmdCtx, args []string) error { } var runningCtx running.RunningCtx - if err := running.FillCtx(cliOpts, cmdCtx, &runningCtx, args, true); err != nil { + if err := running.FillCtx(cliOpts, cmdCtx, &runningCtx, args, false); err != nil { return err } diff --git a/cli/cmd/clean.go b/cli/cmd/clean.go index db69c186c..f86db09dd 100644 --- a/cli/cmd/clean.go +++ b/cli/cmd/clean.go @@ -104,7 +104,7 @@ func internalCleanModule(cmdCtx *cmdcontext.CmdCtx, args []string) error { } var runningCtx running.RunningCtx - if err := running.FillCtx(cliOpts, cmdCtx, &runningCtx, args, false); err != nil { + if err := running.FillCtx(cliOpts, cmdCtx, &runningCtx, args, true); err != nil { return err } diff --git a/cli/cmd/cluster.go b/cli/cmd/cluster.go index d317c655b..36b99a79b 100644 --- a/cli/cmd/cluster.go +++ b/cli/cmd/cluster.go @@ -673,7 +673,7 @@ func parseAppStr(cmdCtx *cmdcontext.CmdCtx, appStr string) (string, string, stri // Fill context for the entire application. // publish app:inst can work even if the `inst` instance doesn't exist right now. var runningCtx running.RunningCtx - err := running.FillCtx(cliOpts, cmdCtx, &runningCtx, []string{appName}, false) + err := running.FillCtx(cliOpts, cmdCtx, &runningCtx, []string{appName}, true) if err != nil { return "", "", "", err } diff --git a/cli/cmd/connect.go b/cli/cmd/connect.go index b5d25124e..0386183cd 100644 --- a/cli/cmd/connect.go +++ b/cli/cmd/connect.go @@ -140,7 +140,7 @@ func resolveConnectOpts(cmdCtx *cmdcontext.CmdCtx, cliOpts *config.CliOpts, newArgs = args[1:] // FillCtx returns error if no instances found. var runningCtx running.RunningCtx - if fillErr := running.FillCtx(cliOpts, cmdCtx, &runningCtx, args, false); fillErr == nil { + if fillErr := running.FillCtx(cliOpts, cmdCtx, &runningCtx, args, true); fillErr == nil { if len(runningCtx.Instances) > 1 { err = fmt.Errorf("specify instance name") return diff --git a/cli/cmd/internal/completion.go b/cli/cmd/internal/completion.go index 17bfd15f5..d23fb6e25 100644 --- a/cli/cmd/internal/completion.go +++ b/cli/cmd/internal/completion.go @@ -26,7 +26,7 @@ func ValidArgsFunction( directive = cobra.ShellCompDirectiveNoFileComp var runningCtx running.RunningCtx - if err := running.FillCtx(cliOpts, cmdCtx, &runningCtx, nil, false); err != nil { + if err := running.FillCtx(cliOpts, cmdCtx, &runningCtx, nil, true); err != nil { return } diff --git a/cli/cmd/kill.go b/cli/cmd/kill.go index fbee974c3..7c4d2d77a 100644 --- a/cli/cmd/kill.go +++ b/cli/cmd/kill.go @@ -78,7 +78,7 @@ func internalKillModule(cmdCtx *cmdcontext.CmdCtx, args []string) error { if confirm { var runningCtx running.RunningCtx - if err = running.FillCtx(cliOpts, cmdCtx, &runningCtx, args, false); err != nil { + if err = running.FillCtx(cliOpts, cmdCtx, &runningCtx, args, true); err != nil { return err } diff --git a/cli/cmd/log.go b/cli/cmd/log.go index bbff37ccf..07715266e 100644 --- a/cli/cmd/log.go +++ b/cli/cmd/log.go @@ -133,7 +133,7 @@ func internalLogModule(cmdCtx *cmdcontext.CmdCtx, args []string) error { var err error var runningCtx running.RunningCtx - if err = running.FillCtx(cliOpts, cmdCtx, &runningCtx, args, false); err != nil { + if err = running.FillCtx(cliOpts, cmdCtx, &runningCtx, args, true); err != nil { return err } diff --git a/cli/cmd/logrotate.go b/cli/cmd/logrotate.go index 00b70f484..ceae3eb73 100644 --- a/cli/cmd/logrotate.go +++ b/cli/cmd/logrotate.go @@ -42,7 +42,7 @@ func internalLogrotateModule(cmdCtx *cmdcontext.CmdCtx, args []string) error { } var runningCtx running.RunningCtx - if err := running.FillCtx(cliOpts, cmdCtx, &runningCtx, args, false); err != nil { + if err := running.FillCtx(cliOpts, cmdCtx, &runningCtx, args, true); err != nil { return err } diff --git a/cli/cmd/play.go b/cli/cmd/play.go index 3fdf7e082..fa6964b4d 100644 --- a/cli/cmd/play.go +++ b/cli/cmd/play.go @@ -82,7 +82,7 @@ func internalPlayModule(cmdCtx *cmdcontext.CmdCtx, args []string) error { // FillCtx returns error if no instances found. var runningCtx running.RunningCtx - if err := running.FillCtx(cliOpts, cmdCtx, &runningCtx, []string{args[0]}, false); err == nil { + if err := running.FillCtx(cliOpts, cmdCtx, &runningCtx, []string{args[0]}, true); err == nil { if len(runningCtx.Instances) > 1 { return util.InternalError( "Internal error: specify instance name", diff --git a/cli/cmd/replicaset.go b/cli/cmd/replicaset.go index 42ecaa971..5868dc535 100644 --- a/cli/cmd/replicaset.go +++ b/cli/cmd/replicaset.go @@ -497,7 +497,7 @@ func replicasetFillCtx(cmdCtx *cmdcontext.CmdCtx, ctx *replicasetCtx, args []str } var connOpts connector.ConnectOpts - if err := running.FillCtx(cliOpts, cmdCtx, &ctx.RunningCtx, args, false); err == nil { + if err := running.FillCtx(cliOpts, cmdCtx, &ctx.RunningCtx, args, true); err == nil { ctx.IsApplication = true if len(ctx.RunningCtx.Instances) == 1 { if connectCtx.Username != "" || connectCtx.Password != "" { @@ -518,7 +518,7 @@ func replicasetFillCtx(cmdCtx *cmdcontext.CmdCtx, ctx *replicasetCtx, args []str } // Re-fill context for an application. ctx.InstName = instName - err := running.FillCtx(cliOpts, cmdCtx, &ctx.RunningCtx, []string{appName}, false) + err := running.FillCtx(cliOpts, cmdCtx, &ctx.RunningCtx, []string{appName}, true) if err != nil { // Should not happen. return err diff --git a/cli/cmd/start.go b/cli/cmd/start.go index 9ef374529..702d38784 100644 --- a/cli/cmd/start.go +++ b/cli/cmd/start.go @@ -127,7 +127,7 @@ func internalStartModule(cmdCtx *cmdcontext.CmdCtx, args []string) error { } var runningCtx running.RunningCtx - if err := running.FillCtx(cliOpts, cmdCtx, &runningCtx, args, true); err != nil { + if err := running.FillCtx(cliOpts, cmdCtx, &runningCtx, args, false); err != nil { return err } diff --git a/cli/cmd/status.go b/cli/cmd/status.go index afa332a06..6fa187d78 100644 --- a/cli/cmd/status.go +++ b/cli/cmd/status.go @@ -62,7 +62,7 @@ func internalStatusModule(cmdCtx *cmdcontext.CmdCtx, args []string) error { } var runningCtx running.RunningCtx - if err := running.FillCtx(cliOpts, cmdCtx, &runningCtx, args, false); err != nil { + if err := running.FillCtx(cliOpts, cmdCtx, &runningCtx, args, true); err != nil { return err } diff --git a/cli/cmd/stop.go b/cli/cmd/stop.go index 65a6a48b8..782c0707c 100644 --- a/cli/cmd/stop.go +++ b/cli/cmd/stop.go @@ -2,6 +2,8 @@ package cmd import ( "fmt" + "os" + "github.com/apex/log" "github.com/spf13/cobra" "github.com/tarantool/tt/cli/cmd/internal" @@ -9,7 +11,6 @@ import ( "github.com/tarantool/tt/cli/modules" "github.com/tarantool/tt/cli/running" "github.com/tarantool/tt/cli/util" - "os" ) // NewStopCmd creates stop command. @@ -79,7 +80,7 @@ func internalStopModule(cmdCtx *cmdcontext.CmdCtx, args []string) error { var runningCtx running.RunningCtx var err error - if err = running.FillCtx(cliOpts, cmdCtx, &runningCtx, args, false); err != nil { + if err = running.FillCtx(cliOpts, cmdCtx, &runningCtx, args, true); err != nil { return err } diff --git a/cli/pack/opts.go b/cli/pack/opts.go index 8212e7a69..bbfad2bf0 100644 --- a/cli/pack/opts.go +++ b/cli/pack/opts.go @@ -49,7 +49,7 @@ func initAppsInfo(cliOpts *config.CliOpts, cmdCtx *cmdcontext.CmdCtx, packCtx *P } packCtx.AppList = appList packCtx.AppsInfo, err = running.CollectInstancesForApps(packCtx.AppList, cliOpts, - cmdCtx.Cli.ConfigDir, cmdCtx.Integrity, false) + cmdCtx.Cli.ConfigDir, cmdCtx.Integrity, true) if err != nil { return fmt.Errorf("failed to collect applications info: %s", err) } diff --git a/cli/running/running.go b/cli/running/running.go index 9348ab78b..6f8fd4232 100644 --- a/cli/running/running.go +++ b/cli/running/running.go @@ -348,7 +348,7 @@ func loadInstanceConfig(configPath, instName string, // collectInstancesFromAppDir collects instances information from application directory. func collectInstancesFromAppDir(appDir string, selectedInstName string, - integrityCtx integrity.IntegrityCtx, configRequired bool) ( + integrityCtx integrity.IntegrityCtx, skipClusterConfig bool) ( []InstanceCtx, error, ) { @@ -376,7 +376,8 @@ func collectInstancesFromAppDir(appDir string, selectedInstName string, InstName: filepath.Base(appDir), AppDir: appDir, SingleApp: true}}, nil - } else if configRequired { + } + if !skipClusterConfig { return nil, fmt.Errorf("require files are missing in application directory %q: "+ "there must be instances config or the default instance script (%q)", appDir, "init.lua") @@ -409,16 +410,19 @@ func collectInstancesFromAppDir(appDir string, selectedInstName string, } log.Debugf("Instance %q", instance.InstName) - if instance.Configuration, err = loadInstanceConfig(instance.ClusterConfigPath, - instance.InstName, integrityCtx); err != nil && configRequired { - return instances, fmt.Errorf("error loading instance %q configuration from "+ - "config %q: %w", instance.InstName, instance.ClusterConfigPath, err) + if !skipClusterConfig { + instance.Configuration, err = loadInstanceConfig(instance.ClusterConfigPath, + instance.InstName, integrityCtx) + if err != nil { + return instances, fmt.Errorf("error loading instance %q configuration from "+ + "config %q: %w", instance.InstName, instance.ClusterConfigPath, err) + } } instance.SingleApp = false - if instance.InstanceScript, err = findInstanceScriptInAppDir(appDir, instance.InstName, - appDirFiles.clusterCfgPath, appDirFiles.defaultLuaPath); err != nil && - configRequired { + instance.InstanceScript, err = findInstanceScriptInAppDir(appDir, instance.InstName, + appDirFiles.clusterCfgPath, appDirFiles.defaultLuaPath) + if err != nil && !skipClusterConfig { return instances, fmt.Errorf("cannot find instance script for %q in config %q: %w ", instance.InstName, appDirFiles.clusterCfgPath, err) } @@ -434,7 +438,7 @@ func collectInstancesFromAppDir(appDir string, selectedInstName string, // CollectInstances searches all instances available in application. func CollectInstances(appName string, applicationsDir string, - integrityCtx integrity.IntegrityCtx, configRequired bool) ([]InstanceCtx, error) { + integrityCtx integrity.IntegrityCtx, skipClusterConfig bool) ([]InstanceCtx, error) { // The user can select a specific instance from the application. // Example: `tt status application:server`. selectedInstName := "" @@ -463,7 +467,7 @@ func CollectInstances(appName string, applicationsDir string, } return collectInstancesFromAppDir(appDir, selectedInstName, integrityCtx, - configRequired) + skipClusterConfig) } // cleanup removes runtime artifacts. @@ -625,7 +629,7 @@ func GetClusterConfigPath(cliOpts *config.CliOpts, // CollectInstancesForApps collects instances information per application. func CollectInstancesForApps(appList []string, cliOpts *config.CliOpts, - ttConfigDir string, integrityCtx integrity.IntegrityCtx, configRequired bool) ( + ttConfigDir string, integrityCtx integrity.IntegrityCtx, skipClusterConfig bool) ( map[string][]InstanceCtx, error) { instEnabledPath := cliOpts.Env.InstancesEnabled if cliOpts.Env.InstancesEnabled == "." { @@ -635,7 +639,7 @@ func CollectInstancesForApps(appList []string, cliOpts *config.CliOpts, for _, appName := range appList { appName = strings.TrimSuffix(appName, ".lua") collectedInstances, err := CollectInstances(appName, instEnabledPath, integrityCtx, - configRequired) + skipClusterConfig) if err != nil { return apps, fmt.Errorf("can't collect instance information for %s: %w", appName, err) @@ -676,7 +680,7 @@ func createInstanceDataDirectories(instance InstanceCtx) error { // FillCtx fills the RunningCtx context. func FillCtx(cliOpts *config.CliOpts, cmdCtx *cmdcontext.CmdCtx, - runningCtx *RunningCtx, args []string, configRequired bool) error { + runningCtx *RunningCtx, args []string, skipClusterConfig bool) error { var err error if len(args) > 1 && cmdCtx.CommandName != "run" && cmdCtx.CommandName != "connect" && @@ -703,7 +707,7 @@ func FillCtx(cliOpts *config.CliOpts, cmdCtx *cmdcontext.CmdCtx, } instances, err := CollectInstancesForApps(appList, cliOpts, - cmdCtx.Cli.ConfigDir, cmdCtx.Integrity, configRequired) + cmdCtx.Cli.ConfigDir, cmdCtx.Integrity, skipClusterConfig) if err != nil { return err } diff --git a/cli/running/running_test.go b/cli/running/running_test.go index aca2f4097..a3265b5a9 100644 --- a/cli/running/running_test.go +++ b/cli/running/running_test.go @@ -189,7 +189,7 @@ func Test_collectInstancesForApps(t *testing.T) { instances, err := CollectInstancesForApps(apps, cliOpts, "/etc/tarantool/", integrity.IntegrityCtx{ Repository: &mockRepository{}, - }, true) + }, false) require.NoError(t, err) require.Contains(t, instances, appName) @@ -316,7 +316,7 @@ func Test_collectInstancesForSingleInstApp(t *testing.T) { instances, err := CollectInstancesForApps(apps, cliOpts, "/etc/tarantool/", integrity.IntegrityCtx{ Repository: &mockRepository{}, - }, true) + }, false) require.NoError(t, err) require.Equal(t, 1, len(instances)) require.Contains(t, instances, appName) @@ -347,7 +347,7 @@ func Test_collectInstancesSingleInstanceTntCtlLayout(t *testing.T) { instances, err := CollectInstancesForApps(apps, cliOpts, cfgDir, integrity.IntegrityCtx{ Repository: &mockRepository{}, - }, true) + }, false) require.NoError(t, err) require.Len(t, instances, 1) require.Contains(t, instances, appName)