1
// Copyright 2013 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
10
"github.com/juju/names"
11
"launchpad.net/gnuflag"
13
"github.com/juju/juju/cmd/envcmd"
14
"github.com/juju/juju/cmd/juju/block"
15
"github.com/juju/juju/constraints"
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>.
25
juju help set-constraints
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>.
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
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)
46
juju help get-constraints
52
// GetConstraintsCommand shows the constraints for a service or environment.
53
type GetConstraintsCommand struct {
59
func (c *GetConstraintsCommand) Info() *cmd.Info {
61
Name: "get-constraints",
63
Purpose: "view constraints on the environment or a service",
64
Doc: getConstraintsDoc,
68
func formatConstraints(value interface{}) ([]byte, error) {
69
return []byte(value.(constraints.Value).String()), nil
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,
80
func (c *GetConstraintsCommand) Init(args []string) error {
82
if !names.IsValidService(args[0]) {
83
return fmt.Errorf("invalid service name %q", args[0])
85
c.ServiceName, args = args[0], args[1:]
87
return cmd.CheckEmpty(args)
90
func (c *GetConstraintsCommand) Run(ctx *cmd.Context) error {
91
apiclient, err := c.NewAPIClient()
95
defer apiclient.Close()
97
var cons constraints.Value
98
if c.ServiceName == "" {
99
cons, err = apiclient.GetEnvironmentConstraints()
101
cons, err = apiclient.GetServiceConstraints(c.ServiceName)
106
return c.out.Write(ctx, cons)
109
// SetConstraintsCommand shows the constraints for a service or environment.
110
type SetConstraintsCommand struct {
111
envcmd.EnvCommandBase
113
Constraints constraints.Value
116
func (c *SetConstraintsCommand) Info() *cmd.Info {
118
Name: "set-constraints",
119
Args: "[key=[value] ...]",
120
Purpose: "set constraints on the environment or a service",
121
Doc: setConstraintsDoc,
125
func (c *SetConstraintsCommand) SetFlags(f *gnuflag.FlagSet) {
126
f.StringVar(&c.ServiceName, "s", "", "set service constraints")
127
f.StringVar(&c.ServiceName, "service", "", "")
130
func (c *SetConstraintsCommand) Init(args []string) (err error) {
131
if c.ServiceName != "" && !names.IsValidService(c.ServiceName) {
132
return fmt.Errorf("invalid service name %q", c.ServiceName)
134
c.Constraints, err = constraints.Parse(args...)
138
func (c *SetConstraintsCommand) Run(_ *cmd.Context) (err error) {
139
apiclient, err := c.NewAPIClient()
143
defer apiclient.Close()
145
if c.ServiceName == "" {
146
err = apiclient.SetEnvironmentConstraints(c.Constraints)
148
err = apiclient.SetServiceConstraints(c.ServiceName, c.Constraints)
150
return block.ProcessBlockedError(err, block.BlockChange)