github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/uniter/runner/jujuc/reboot.go (about)

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