Skip to content

Commit

Permalink
reduce requirements for stop/kill/clean/logrotate
Browse files Browse the repository at this point in the history
For multi-instance applications no need to have instance scripts.
For tarantool3-based applications no need to have cluster config.

Closes #880
  • Loading branch information
elhimov committed Oct 14, 2024
1 parent 22a8acc commit 0785834
Show file tree
Hide file tree
Showing 18 changed files with 65 additions and 63 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, []string{}); err != nil {
if err := running.FillCtx(cliOpts, &cmdCtx, &runningCtx, nil, false); 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); 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/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); 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/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})
err := running.FillCtx(cliOpts, cmdCtx, &runningCtx, []string{appName}, false)
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 @@ -134,7 +134,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); fillErr == nil {
if fillErr := running.FillCtx(cliOpts, cmdCtx, &runningCtx, args, false); 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, []string{}); err != nil {
if err := running.FillCtx(cliOpts, cmdCtx, &runningCtx, nil, false); 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); 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/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); 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/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); err != nil {
if err := running.FillCtx(cliOpts, cmdCtx, &runningCtx, args, false); err != nil {
return err
}

Expand Down
4 changes: 2 additions & 2 deletions cli/cmd/replicaset.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ func replicasetFillCtx(cmdCtx *cmdcontext.CmdCtx, ctx *replicasetCtx, args []str
}
var connOpts connector.ConnectOpts

if err := running.FillCtx(cliOpts, cmdCtx, &ctx.RunningCtx, args); err == nil {
if err := running.FillCtx(cliOpts, cmdCtx, &ctx.RunningCtx, args, false); err == nil {
ctx.IsApplication = true
if len(ctx.RunningCtx.Instances) == 1 {
if connectCtx.Username != "" || connectCtx.Password != "" {
Expand All @@ -435,7 +435,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})
err := running.FillCtx(cliOpts, cmdCtx, &ctx.RunningCtx, []string{appName}, false)
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); 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/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); 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/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func internalStopModule(cmdCtx *cmdcontext.CmdCtx, args []string) error {

var runningCtx running.RunningCtx
var err error
if err = running.FillCtx(cliOpts, cmdCtx, &runningCtx, args); err != nil {
if err = running.FillCtx(cliOpts, cmdCtx, &runningCtx, args, false); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion cli/instances/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func ListInstances(cmdCtx *cmdcontext.CmdCtx, cliOpts *config.CliOpts) error {
fmt.Printf("instances enabled directory: %s\n", cliOpts.Env.InstancesEnabled)

applications, err := running.CollectInstancesForApps(appList, cliOpts, cmdCtx.Cli.ConfigDir,
cmdCtx.Integrity)
cmdCtx.Integrity, false)
if 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)
cmdCtx.Cli.ConfigDir, cmdCtx.Integrity, true)
if err != nil {
return fmt.Errorf("failed to collect applications info: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cli/replicaset/rebootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func cleanDataFiles(instCtx running.InstanceCtx) error {
// and starting it again.
func Rebootstrap(cmdCtx cmdcontext.CmdCtx, cliOpts config.CliOpts, rbCtx RebootstrapCtx) error {
apps, err := running.CollectInstancesForApps([]string{rbCtx.AppName}, &cliOpts,
cmdCtx.Cli.ConfigDir, cmdCtx.Integrity)
cmdCtx.Cli.ConfigDir, cmdCtx.Integrity, true)
if err != nil {
return fmt.Errorf("cannot collect application instances info: %s", err)
}
Expand Down
70 changes: 36 additions & 34 deletions cli/running/running.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (provider *providerImpl) updateCtx() error {
}

var runningCtx RunningCtx
if err = FillCtx(cliOpts, provider.cmdCtx, &runningCtx, args); err != nil {
if err = FillCtx(cliOpts, provider.cmdCtx, &runningCtx, args, false); err != nil {
return err
}
provider.instanceCtx = &runningCtx.Instances[0]
Expand Down Expand Up @@ -274,20 +274,6 @@ func collectAppDirFiles(appDir string) (appDirCtx appDirCtx, err error) {
return
}

if appDirCtx.instCfgPath == "" {
if appDirCtx.clusterCfgPath != "" {
// Cluster config will work only if instances.yml exists nearby.
err = fmt.Errorf(
"cluster config %q is found, but instances config (instances.yml) is missing",
appDirCtx.clusterCfgPath)
} else {
if appDirCtx.defaultLuaPath == "" {
err = fmt.Errorf("require files are missing in application directory %q: "+
"there must be instances config or the default instance script (%q)",
appDir, "init.lua")
}
}
}
return
}

Expand Down Expand Up @@ -362,29 +348,41 @@ func loadInstanceConfig(configPath, instName string,

// collectInstancesFromAppDir collects instances information from application directory.
func collectInstancesFromAppDir(appDir string, selectedInstName string,
integrityCtx integrity.IntegrityCtx) (
integrityCtx integrity.IntegrityCtx, instancesScriptsRequired bool) (
[]InstanceCtx,
error,
) {
log.Debugf("Collecting instances from application directory %q", appDir)
instances := []InstanceCtx{}
// log.Debugf("Collecting instances from application directory %q", appDir)
if !util.IsDir(appDir) {
return instances, fmt.Errorf("%q doesn't exist or not a directory", appDir)
return nil, fmt.Errorf("%q doesn't exist or not a directory", appDir)
}

appDirFiles, err := collectAppDirFiles(appDir)
// log.Errorf("appDirFiles: %v", appDirFiles)
// log.Error("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")
if err != nil {
return instances, err
return nil, err
}

if appDirFiles.instCfgPath == "" {
if appDirFiles.defaultLuaPath != "" {
return []InstanceCtx{{
InstanceScript: appDirFiles.defaultLuaPath,
AppName: filepath.Base(appDir),
InstName: filepath.Base(appDir),
AppDir: appDir,
SingleApp: true}}, nil
if appDirFiles.clusterCfgPath != "" {
// Cluster config will work only if instances.yml exists nearby.
return nil, fmt.Errorf(
"cluster config %q is found, but instances config (instances.yml) is missing",
appDirFiles.clusterCfgPath)
} else {
if appDirFiles.defaultLuaPath != "" {
return []InstanceCtx{{
InstanceScript: appDirFiles.defaultLuaPath,
AppName: filepath.Base(appDir),
InstName: filepath.Base(appDir),
AppDir: appDir,
SingleApp: true}}, nil
} else if instancesScriptsRequired {
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 All @@ -399,6 +397,7 @@ func collectInstancesFromAppDir(appDir string, selectedInstName string,
return nil, err
}
log.Debug("Processing application instances file")
instances := []InstanceCtx{}
for inst := range instParams {
instance := InstanceCtx{AppDir: appDir, ClusterConfigPath: appDirFiles.clusterCfgPath}
instance.InstName = getInstanceName(inst, instance.ClusterConfigPath != "")
Expand All @@ -420,7 +419,8 @@ func collectInstancesFromAppDir(appDir string, selectedInstName string,

instance.SingleApp = false
if instance.InstanceScript, err = findInstanceScriptInAppDir(appDir, instance.InstName,
appDirFiles.clusterCfgPath, appDirFiles.defaultLuaPath); err != nil {
appDirFiles.clusterCfgPath, appDirFiles.defaultLuaPath); err != nil &&
instancesScriptsRequired {
return instances, fmt.Errorf("cannot find instance script for %q in config %q: %w ",
instance.InstName, appDirFiles.clusterCfgPath, err)
}
Expand All @@ -436,7 +436,7 @@ func collectInstancesFromAppDir(appDir string, selectedInstName string,

// CollectInstances searches all instances available in application.
func CollectInstances(appName string, applicationsDir string,
integrityCtx integrity.IntegrityCtx) ([]InstanceCtx, error) {
integrityCtx integrity.IntegrityCtx, instancesScriptsRequired bool) ([]InstanceCtx, error) {
// The user can select a specific instance from the application.
// Example: `tt status application:server`.
selectedInstName := ""
Expand Down Expand Up @@ -464,7 +464,8 @@ func CollectInstances(appName string, applicationsDir string,
appDir = applicationsDir
}

return collectInstancesFromAppDir(appDir, selectedInstName, integrityCtx)
return collectInstancesFromAppDir(appDir, selectedInstName, integrityCtx,
instancesScriptsRequired)
}

// cleanup removes runtime artifacts.
Expand Down Expand Up @@ -626,7 +627,7 @@ func GetClusterConfigPath(cliOpts *config.CliOpts,

// CollectInstancesForApps collects instances information per application.
func CollectInstancesForApps(appList []string, cliOpts *config.CliOpts,
ttConfigDir string, integrityCtx integrity.IntegrityCtx) (
ttConfigDir string, integrityCtx integrity.IntegrityCtx, instancesScriptsRequired bool) (
map[string][]InstanceCtx, error) {
instEnabledPath := cliOpts.Env.InstancesEnabled
if cliOpts.Env.InstancesEnabled == "." {
Expand All @@ -635,7 +636,8 @@ func CollectInstancesForApps(appList []string, cliOpts *config.CliOpts,
apps := make(map[string][]InstanceCtx)
for _, appName := range appList {
appName = strings.TrimSuffix(appName, ".lua")
collectedInstances, err := CollectInstances(appName, instEnabledPath, integrityCtx)
collectedInstances, err := CollectInstances(appName, instEnabledPath, integrityCtx,
instancesScriptsRequired)
if err != nil {
return apps, fmt.Errorf("can't collect instance information for %s: %w",
appName, err)
Expand Down Expand Up @@ -676,7 +678,7 @@ func createInstanceDataDirectories(instance InstanceCtx) error {

// FillCtx fills the RunningCtx context.
func FillCtx(cliOpts *config.CliOpts, cmdCtx *cmdcontext.CmdCtx,
runningCtx *RunningCtx, args []string) error {
runningCtx *RunningCtx, args []string, instancesScriptsRequired bool) error {
var err error

if len(args) > 1 && cmdCtx.CommandName != "run" && cmdCtx.CommandName != "connect" &&
Expand All @@ -703,7 +705,7 @@ func FillCtx(cliOpts *config.CliOpts, cmdCtx *cmdcontext.CmdCtx,
}

instances, err := CollectInstancesForApps(appList, cliOpts,
cmdCtx.Cli.ConfigDir, cmdCtx.Integrity)
cmdCtx.Cli.ConfigDir, cmdCtx.Integrity, instancesScriptsRequired)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 0785834

Please sign in to comment.