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