github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/payload/context/register.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 "gopkg.in/juju/charm.v6-unstable" 10 11 "github.com/juju/juju/payload" 12 ) 13 14 // RegisterCmdName is the name of the payload register command. 15 const RegisterCmdName = "payload-register" 16 17 // NewRegisterCmd returns a new RegisterCmd that wraps the given context. 18 func NewRegisterCmd(ctx HookContext) (*RegisterCmd, error) { 19 compCtx, err := ContextComponent(ctx) 20 if err != nil { 21 // The component wasn't tracked properly. 22 return nil, errors.Trace(err) 23 } 24 return &RegisterCmd{hctx: compCtx}, nil 25 } 26 27 // RegisterCmd is a command that registers a payload with juju. 28 type RegisterCmd struct { 29 cmd.CommandBase 30 31 hctx Component 32 typ string 33 class string 34 id string 35 labels []string 36 } 37 38 // TODO(ericsnow) Change "tags" to "labels" in the help text? 39 40 // Info implements cmd.Command. 41 func (c RegisterCmd) Info() *cmd.Info { 42 return &cmd.Info{ 43 Name: RegisterCmdName, 44 Args: "<type> <class> <id> [tags...]", 45 Purpose: "register a charm payload with juju", 46 Doc: ` 47 "payload-register" is used while a hook is running to let Juju know that a 48 payload has been started. The information used to start the payload must be 49 provided when "register" is run. 50 51 The payload class must correspond to one of the payloads defined in 52 the charm's metadata.yaml. 53 54 `, 55 } 56 } 57 58 // Init implements cmd.Command. 59 func (c *RegisterCmd) Init(args []string) error { 60 if len(args) < 3 { 61 return errors.Errorf("missing required arguments") 62 } 63 c.typ = args[0] 64 c.class = args[1] 65 c.id = args[2] 66 c.labels = args[3:] 67 return nil 68 } 69 70 // Run implements cmd.Command. 71 func (c *RegisterCmd) Run(ctx *cmd.Context) error { 72 if err := c.validate(ctx); err != nil { 73 return errors.Trace(err) 74 } 75 pl := payload.Payload{ 76 PayloadClass: charm.PayloadClass{ 77 Name: c.class, 78 Type: c.typ, 79 }, 80 ID: c.id, 81 Status: payload.StateRunning, 82 Labels: c.labels, 83 Unit: "a-application/0", // TODO(ericsnow) eliminate this! 84 } 85 if err := c.hctx.Track(pl); err != nil { 86 return errors.Trace(err) 87 } 88 89 // We flush to state immedeiately so that status reflects the 90 // payload correctly. 91 if err := c.hctx.Flush(); err != nil { 92 return errors.Trace(err) 93 } 94 95 // TODO(ericsnow) Print out the full ID. 96 97 return nil 98 } 99 100 func (c *RegisterCmd) validate(ctx *cmd.Context) error { 101 meta, err := readMetadata(ctx) 102 if err != nil { 103 return errors.Trace(err) 104 } 105 106 found := false 107 for _, class := range meta.PayloadClasses { 108 if c.class == class.Name { 109 if c.typ != class.Type { 110 return errors.Errorf("incorrect type %q for payload %q, expected %q", c.typ, class.Name, class.Type) 111 } 112 found = true 113 } 114 } 115 if !found { 116 return errors.Errorf("payload %q not found in metadata.yaml", c.class) 117 } 118 return nil 119 }