github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/payload/context/unregister.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 jujucmd "github.com/juju/juju/cmd" 11 ) 12 13 // UnregisterCmdName is the name of the payload unregister command. 14 const UnregisterCmdName = "payload-unregister" 15 16 // UnregisterCmd implements the untrack command. 17 type UnregisterCmd struct { 18 cmd.CommandBase 19 20 hookContextFunc func() (Component, error) 21 class string 22 id string 23 } 24 25 // NewUnregisterCmd returns a new UnregisterCmd that wraps the given context. 26 func NewUnregisterCmd(ctx HookContext) (*UnregisterCmd, error) { 27 return &UnregisterCmd{hookContextFunc: componentHookContext(ctx)}, nil 28 } 29 30 // Info implements cmd.Command. 31 func (c UnregisterCmd) Info() *cmd.Info { 32 return jujucmd.Info(&cmd.Info{ 33 Name: UnregisterCmdName, 34 Args: "<class> <id>", 35 Purpose: "stop tracking a payload", 36 Doc: ` 37 "payload-unregister" is used while a hook is running to let Juju know 38 that a payload has been manually stopped. The <class> and <id> provided 39 must match a payload that has been previously registered with juju using 40 payload-register. 41 `, 42 }) 43 } 44 45 // Init implements cmd.Command. 46 func (c *UnregisterCmd) Init(args []string) error { 47 if len(args) < 2 { 48 return errors.Errorf("missing required arguments") 49 } 50 51 c.class = args[0] 52 c.id = args[1] 53 54 if err := cmd.CheckEmpty(args[2:]); err != nil { 55 return errors.Trace(err) 56 } 57 return nil 58 } 59 60 // Run runs the unregister command. 61 func (c *UnregisterCmd) Run(ctx *cmd.Context) error { 62 //TODO(wwitzel3) make Unregister accept class and id and 63 // compose the ID in the API layer using BuildID 64 65 logger.Tracef(`Running unregister command with id "%s/%s"`, c.class, c.id) 66 67 hctx, err := c.hookContextFunc() 68 if err != nil { 69 return errors.Trace(err) 70 } 71 // TODO(ericsnow) Verify that Untrack gives a meaningful error when 72 // the ID is not found. 73 if err := hctx.Untrack(c.class, c.id); err != nil { 74 return errors.Trace(err) 75 } 76 77 // TODO(ericsnow) Is the flush really necessary? 78 79 // We flush to state immediately so that status reflects the 80 // payload correctly. 81 if err := hctx.Flush(); err != nil { 82 return errors.Trace(err) 83 } 84 85 return nil 86 }