Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logrotate: don't exit at non-running instance #1006

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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); 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
7 changes: 3 additions & 4 deletions cli/cmd/logrotate.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,15 @@ 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
}

for _, run := range runningCtx.Instances {
res, err := running.Logrotate(&run)
err := running.Logrotate(&run)
if err != nil {
return err
log.Infof(err.Error())
}
log.Info(res)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions cli/cmd/replicaset.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,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 @@ -466,7 +466,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
81 changes: 42 additions & 39 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,39 @@ 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{}
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)
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 +395,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 +417,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 +434,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 +462,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 +625,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 +634,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 +676,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 +703,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 Expand Up @@ -856,24 +856,27 @@ func Status(run *InstanceCtx) process_utils.ProcessState {
}

// Logrotate rotates logs of a started tarantool instance.
func Logrotate(run *InstanceCtx) (string, error) {
func Logrotate(run *InstanceCtx) error {
fullInstanceName := GetAppInstanceName(*run)

pid, err := process_utils.GetPIDFromFile(run.PIDFile)
if err != nil {
return "", errors.New(instStateStopped.String())
return fmt.Errorf("%s: the instance is not running, it must be started", fullInstanceName)
}

alive, err := process_utils.IsProcessAlive(pid)
if !alive {
return "", errors.New(instStateDead.String())
return errors.New(instStateDead.String())
}

if err := syscall.Kill(pid, syscall.Signal(syscall.SIGHUP)); err != nil {
return "", fmt.Errorf(`can't rotate logs: "%v"`, err)
return fmt.Errorf(`can't rotate logs: "%v"`, err)
}

// Rotates logs [instance name pid]
fullInstanceName := GetAppInstanceName(*run)
return fmt.Sprintf("%s: logs has been rotated. PID: %v.", fullInstanceName, pid), nil
log.Infof("%s (PID = %v): logs has been rotated.", fullInstanceName, pid)

return nil
}

// Check returns the result of checking the syntax of the application file.
Expand Down
Loading