-
Notifications
You must be signed in to change notification settings - Fork 491
/
reboot.go
82 lines (66 loc) · 2.19 KB
/
reboot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright 2014 Canonical Ltd.
// Copyright 2014 Cloudbase Solutions SRL
// Licensed under the AGPLv3, see LICENCE file for details.
package jujuc
import (
"github.com/juju/cmd/v3"
"github.com/juju/errors"
"github.com/juju/gnuflag"
jujucmd "github.com/juju/juju/cmd"
)
const (
// RebootSkip is a noop.
RebootSkip RebootPriority = iota
// RebootAfterHook means wait for current hook to finish before
// rebooting.
RebootAfterHook
// RebootNow means reboot immediately, killing and requeueing the
// calling hook
RebootNow
)
// JujuRebootCommand implements the juju-reboot command.
type JujuRebootCommand struct {
cmd.CommandBase
ctx Context
Now bool
}
func NewJujuRebootCommand(ctx Context) (cmd.Command, error) {
return &JujuRebootCommand{ctx: ctx}, nil
}
func (c *JujuRebootCommand) Info() *cmd.Info {
doc := `
juju-reboot causes the host machine to reboot, after stopping all containers
hosted on the machine.
An invocation without arguments will allow the current hook to complete, and
will only cause a reboot if the hook completes successfully.
If the --now flag is passed, the current hook will terminate immediately, and
be restarted from scratch after reboot. This allows charm authors to write
hooks that need to reboot more than once in the course of installing software.
The --now flag cannot terminate a debug-hooks session; hooks using --now should
be sure to terminate on unexpected errors, so as to guarantee expected behaviour
in all situations.
juju-reboot is not supported when running actions.
`
return jujucmd.Info(&cmd.Info{
Name: "juju-reboot",
Args: "",
Purpose: "Reboot the host machine",
Doc: doc,
})
}
func (c *JujuRebootCommand) SetFlags(f *gnuflag.FlagSet) {
f.BoolVar(&c.Now, "now", false, "reboot immediately, killing the invoking process")
}
func (c *JujuRebootCommand) Init(args []string) error {
return cmd.CheckEmpty(args)
}
func (c *JujuRebootCommand) Run(ctx *cmd.Context) error {
if _, err := c.ctx.ActionParams(); err == nil {
return errors.New("juju-reboot is not supported when running an action.")
}
rebootPriority := RebootAfterHook
if c.Now {
rebootPriority = RebootNow
}
return c.ctx.RequestReboot(rebootPriority)
}