github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/payload/context/status-set.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package context 5 6 import ( 7 "github.com/juju/cmd" 8 "github.com/juju/errors" 9 10 "github.com/juju/juju/payload" 11 ) 12 13 // StatusSetCmdName is the name of the payload status-set command. 14 const StatusSetCmdName = "payload-status-set" 15 16 // NewStatusSetCmd returns a new StatusSetCmd that wraps the given context. 17 func NewStatusSetCmd(ctx HookContext) (*StatusSetCmd, error) { 18 compCtx, err := ContextComponent(ctx) 19 if err != nil { 20 // The component wasn't tracked properly. 21 return nil, errors.Trace(err) 22 } 23 return &StatusSetCmd{hctx: compCtx}, nil 24 } 25 26 // StatusSetCmd is a command that registers a payload with juju. 27 type StatusSetCmd struct { 28 cmd.CommandBase 29 30 hctx Component 31 class string 32 id string 33 status string 34 } 35 36 // Info implements cmd.Command. 37 func (c StatusSetCmd) Info() *cmd.Info { 38 return &cmd.Info{ 39 Name: StatusSetCmdName, 40 Args: "<class> <id> <status>", 41 Purpose: "update the status of a payload", 42 Doc: ` 43 "payload-status-set" is used to update the current status of a registered payload. 44 The <class> and <id> provided must match a payload that has been previously 45 registered with juju using payload-register. The <status> must be one of the 46 follow: starting, started, stopping, stopped 47 `, 48 } 49 } 50 51 // Init implements cmd.Command. 52 func (c *StatusSetCmd) Init(args []string) error { 53 if len(args) < 3 { 54 return errors.Errorf("missing required arguments") 55 } 56 c.class = args[0] 57 c.id = args[1] 58 c.status = args[2] 59 return cmd.CheckEmpty(args[3:]) 60 } 61 62 // Run implements cmd.Command. 63 func (c *StatusSetCmd) Run(ctx *cmd.Context) error { 64 if err := c.validate(ctx); err != nil { 65 return errors.Trace(err) 66 } 67 68 if err := c.hctx.SetStatus(c.class, c.id, c.status); err != nil { 69 return errors.Trace(err) 70 } 71 72 // TODO(ericsnow) Is the flush really necessary? 73 74 // We flush to state immedeiately so that status reflects the 75 // payload correctly. 76 if err := c.hctx.Flush(); err != nil { 77 return errors.Trace(err) 78 } 79 80 return nil 81 } 82 83 func (c *StatusSetCmd) validate(ctx *cmd.Context) error { 84 return payload.ValidateState(c.status) 85 }