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