mirror of
https://github.com/rancher/os.git
synced 2025-07-01 01:01:48 +00:00
cmd/power: Set correct container name and ensure full command executed
This fixes a few issues that are preventing shutdown and friends from behaving correctly: * The command name, which is being used to determine via what command it was being called (ie: shutdown, reboot, or halt) was not being parsed for absolute paths. This was preventing certain logic from being handled (example: enforcing a static time value of "now" for shutdown), but more problematically was the fact that it was being used as the container name being passed to runDocker, the function that launches the independent shutdown container. This was causing the shutdown container to fail as something like "/sbin/shutdown" is not a valid name for a container. The logic to parse out the base command being run is actually present in runDocker, but does not run if a name is supplied to the function. * Further, the command line was not being parsed in the shutdown container if the name supplied to runDocker was non-empty. Rather, the full command to run just became the name of the container. Hence, something like "/sbin/shutdown -h now" became just "shutdown", executing the default power off behaviour for all actions (including reboots). * Further to this, open-vm-tools expects "/sbin/shutdown -h now" to be a valid command to halt the system, which was not being recognized as the only recognized short-form halt flag in shutdown was its capital version (-H). This fixes these three issues by parsing out the base of the called command before sending it to reboot, using all of os.Argv as the command line to run regardless of if a name was set for the container or not, and finally adding the lowercase -h switch to the "shutdown" form of this command ("halt" is still uppercase only). Fixes rancher/os#2121. Fixes rancher/os#2074.
This commit is contained in:
parent
d8bba34a87
commit
2f8eaa3314
@ -36,11 +36,9 @@ func runDocker(name string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
cmd := []string{name}
|
||||
|
||||
cmd := os.Args
|
||||
if name == "" {
|
||||
name = filepath.Base(os.Args[0])
|
||||
cmd = os.Args
|
||||
}
|
||||
|
||||
existing, err := client.ContainerInspect(context.Background(), name)
|
||||
|
@ -84,7 +84,7 @@ func Shutdown() {
|
||||
})
|
||||
} else {
|
||||
app.Flags = append(app.Flags, cli.BoolFlag{
|
||||
Name: "H, halt",
|
||||
Name: "H, h, halt",
|
||||
Usage: "halt the machine",
|
||||
Destination: &haltFlag,
|
||||
})
|
||||
@ -197,7 +197,12 @@ func shutdown(c *cli.Context) error {
|
||||
}
|
||||
|
||||
timeArg := c.Args().Get(0)
|
||||
if c.App.Name == "shutdown" && timeArg != "" {
|
||||
// We may be called via an absolute path, so check that now and make sure we
|
||||
// don't pass the wrong app name down. Aside from the logic in the immediate
|
||||
// context here, the container name is derived from how we were called and
|
||||
// cannot contain slashes.
|
||||
appName := filepath.Base(c.App.Name)
|
||||
if appName == "shutdown" && timeArg != "" {
|
||||
if timeArg != "now" {
|
||||
err := fmt.Errorf("Sorry, can't parse '%s' as time value (only 'now' supported)", timeArg)
|
||||
log.Error(err)
|
||||
@ -206,7 +211,7 @@ func shutdown(c *cli.Context) error {
|
||||
// TODO: if there are more params, LOG them
|
||||
}
|
||||
|
||||
reboot(c.App.Name, forceFlag, powerCmd)
|
||||
reboot(appName, forceFlag, powerCmd)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user