github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/cmd/juju/constraints.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package main 5 6 import ( 7 "fmt" 8 9 "github.com/juju/names" 10 "launchpad.net/gnuflag" 11 12 "github.com/juju/juju/cmd" 13 "github.com/juju/juju/cmd/envcmd" 14 "github.com/juju/juju/constraints" 15 "github.com/juju/juju/juju" 16 ) 17 18 const getConstraintsDoc = ` 19 get-constraints returns a list of constraints that have been set on 20 the environment using juju set-constraints. You can also view constraints set 21 for a specific service by using juju get-constraints <service>. 22 23 See Also: 24 juju help constraints 25 juju help set-constraints 26 ` 27 28 const setConstraintsDoc = ` 29 set-constraints sets machine constraints on the system, which are used as the 30 default constraints for all new machines provisioned in the environment (unless 31 overridden). You can also set constraints on a specific service by using juju 32 set-constraints <service>. 33 34 Constraints set on a service are combined with environment constraints for 35 commands (such as juju deploy) that provision machines for services. Where 36 environment and service constraints overlap, the service constraints take 37 precedence. 38 39 Examples: 40 41 set-constraints mem=8G (all new machines in the environment must have at least 8GB of RAM) 42 set-constraints --service wordpress mem=4G (all new wordpress machines can ignore the 8G constraint above, and require only 4G) 43 44 See Also: 45 juju help constraints 46 juju help get-constraints 47 juju help deploy 48 juju help add-machine 49 juju help add-unit 50 ` 51 52 // GetConstraintsCommand shows the constraints for a service or environment. 53 type GetConstraintsCommand struct { 54 envcmd.EnvCommandBase 55 ServiceName string 56 out cmd.Output 57 } 58 59 func (c *GetConstraintsCommand) Info() *cmd.Info { 60 return &cmd.Info{ 61 Name: "get-constraints", 62 Args: "[<service>]", 63 Purpose: "view constraints on the environment or a service", 64 Doc: getConstraintsDoc, 65 } 66 } 67 68 func formatConstraints(value interface{}) ([]byte, error) { 69 return []byte(value.(constraints.Value).String()), nil 70 } 71 72 func (c *GetConstraintsCommand) SetFlags(f *gnuflag.FlagSet) { 73 c.out.AddFlags(f, "constraints", map[string]cmd.Formatter{ 74 "constraints": formatConstraints, 75 "yaml": cmd.FormatYaml, 76 "json": cmd.FormatJson, 77 }) 78 } 79 80 func (c *GetConstraintsCommand) Init(args []string) error { 81 if len(args) > 0 { 82 if !names.IsService(args[0]) { 83 return fmt.Errorf("invalid service name %q", args[0]) 84 } 85 c.ServiceName, args = args[0], args[1:] 86 } 87 return cmd.CheckEmpty(args) 88 } 89 90 func (c *GetConstraintsCommand) Run(ctx *cmd.Context) error { 91 apiclient, err := juju.NewAPIClientFromName(c.EnvName) 92 if err != nil { 93 return err 94 } 95 defer apiclient.Close() 96 97 var cons constraints.Value 98 if c.ServiceName == "" { 99 cons, err = apiclient.GetEnvironmentConstraints() 100 } else { 101 cons, err = apiclient.GetServiceConstraints(c.ServiceName) 102 } 103 if err != nil { 104 return err 105 } 106 return c.out.Write(ctx, cons) 107 } 108 109 // SetConstraintsCommand shows the constraints for a service or environment. 110 type SetConstraintsCommand struct { 111 envcmd.EnvCommandBase 112 ServiceName string 113 Constraints constraints.Value 114 } 115 116 func (c *SetConstraintsCommand) Info() *cmd.Info { 117 return &cmd.Info{ 118 Name: "set-constraints", 119 Args: "[key=[value] ...]", 120 Purpose: "set constraints on the environment or a service", 121 Doc: setConstraintsDoc, 122 } 123 } 124 125 func (c *SetConstraintsCommand) SetFlags(f *gnuflag.FlagSet) { 126 f.StringVar(&c.ServiceName, "s", "", "set service constraints") 127 f.StringVar(&c.ServiceName, "service", "", "") 128 } 129 130 func (c *SetConstraintsCommand) Init(args []string) (err error) { 131 if c.ServiceName != "" && !names.IsService(c.ServiceName) { 132 return fmt.Errorf("invalid service name %q", c.ServiceName) 133 } 134 c.Constraints, err = constraints.Parse(args...) 135 return err 136 } 137 138 func (c *SetConstraintsCommand) Run(_ *cmd.Context) (err error) { 139 apiclient, err := juju.NewAPIClientFromName(c.EnvName) 140 if err != nil { 141 return err 142 } 143 defer apiclient.Close() 144 if c.ServiceName == "" { 145 return apiclient.SetEnvironmentConstraints(c.Constraints) 146 } 147 return apiclient.SetServiceConstraints(c.ServiceName, c.Constraints) 148 }