Skip to content

Commit

Permalink
cli: skip cluster config for the few commands
Browse files Browse the repository at this point in the history
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
  • Loading branch information
patapenka-alexey committed Dec 23, 2024
1 parent 71fb800 commit 30ff7e3
Show file tree
Hide file tree
Showing 17 changed files with 40 additions and 35 deletions.
2 changes: 1 addition & 1 deletion cli/cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/internal/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/logrotate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions cli/cmd/replicaset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
5 changes: 3 additions & 2 deletions cli/cmd/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package cmd

import (
"fmt"
"os"

"github.com/apex/log"
"github.com/spf13/cobra"
"github.com/tarantool/tt/cli/cmd/internal"
"github.com/tarantool/tt/cli/cmdcontext"
"github.com/tarantool/tt/cli/modules"
"github.com/tarantool/tt/cli/running"
"github.com/tarantool/tt/cli/util"
"os"
)

// NewStopCmd creates stop command.
Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion cli/pack/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
34 changes: 19 additions & 15 deletions cli/running/running.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
}
Expand All @@ -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 := ""
Expand Down Expand Up @@ -463,7 +467,7 @@ func CollectInstances(appName string, applicationsDir string,
}

return collectInstancesFromAppDir(appDir, selectedInstName, integrityCtx,
configRequired)
skipClusterConfig)
}

// cleanup removes runtime artifacts.
Expand Down Expand Up @@ -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 == "." {
Expand All @@ -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)
Expand Down Expand Up @@ -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" &&
Expand All @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions cli/running/running_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 30ff7e3

Please sign in to comment.