forked from juju/juju
/
constraints.go
195 lines (163 loc) · 5.76 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package application
import (
"fmt"
"io"
"github.com/juju/cmd"
"github.com/juju/errors"
"github.com/juju/gnuflag"
"gopkg.in/juju/names.v2"
"github.com/juju/juju/api/application"
jujucmd "github.com/juju/juju/cmd"
"github.com/juju/juju/cmd/juju/block"
"github.com/juju/juju/cmd/modelcmd"
"github.com/juju/juju/core/constraints"
)
var usageGetConstraintsSummary = `
Displays machine constraints for an application.`[1:]
var usageGetConstraintsDetails = `
Shows machine constraints that have been set for an application with ` + "`juju set-\nconstraints`" + `.
By default, the model is the current model.
Application constraints are combined with model constraints, set with ` +
"`juju \nset-model-constraints`" + `, for commands (such as 'deploy') that provision
machines for applications. Where model and application constraints overlap, the
application constraints take precedence.
Constraints for a specific model can be viewed with ` + "`juju get-model-\nconstraints`" + `.
Examples:
juju get-constraints mysql
juju get-constraints -m mymodel apache2
See also:
set-constraints
get-model-constraints
set-model-constraints`
var usageSetConstraintsSummary = `
Sets machine constraints for an application.`[1:]
// setConstraintsDoc is multi-line since we need to use ` to denote
// commands for ease in markdown.
var usageSetConstraintsDetails = `
Sets constraints for an application, which are used for all new machines
provisioned for that application. They can be viewed with `[1:] + "`juju get-\nconstraints`" + `.
By default, the model is the current model.
Application constraints are combined with model constraints, set with ` +
"`juju \nset-model-constraints`" + `, for commands (such as 'juju deploy') that
provision machines for applications. Where model and application constraints
overlap, the application constraints take precedence.
Constraints for a specific model can be viewed with ` + "`juju get-model-\nconstraints`" + `.
This command requires that the application to have at least one unit. To apply
constraints to
the first unit set them at the model level or pass them as an argument
when deploying.
Examples:
juju set-constraints mysql mem=8G cores=4
juju set-constraints -m mymodel apache2 mem=8G arch=amd64
See also:
get-constraints
get-model-constraints
set-model-constraints`
// NewApplicationGetConstraintsCommand returns a command which gets application constraints.
func NewApplicationGetConstraintsCommand() modelcmd.ModelCommand {
return modelcmd.Wrap(&applicationGetConstraintsCommand{})
}
type applicationConstraintsAPI interface {
Close() error
GetConstraints(...string) ([]constraints.Value, error)
SetConstraints(string, constraints.Value) error
}
type applicationConstraintsCommand struct {
modelcmd.ModelCommandBase
ApplicationName string
out cmd.Output
api applicationConstraintsAPI
}
func (c *applicationConstraintsCommand) getAPI() (applicationConstraintsAPI, error) {
if c.api != nil {
return c.api, nil
}
root, err := c.NewAPIRoot()
if err != nil {
return nil, errors.Trace(err)
}
return application.NewClient(root), nil
}
type applicationGetConstraintsCommand struct {
applicationConstraintsCommand
}
func (c *applicationGetConstraintsCommand) Info() *cmd.Info {
return jujucmd.Info(&cmd.Info{
Name: "get-constraints",
Args: "<application>",
Purpose: usageGetConstraintsSummary,
Doc: usageGetConstraintsDetails,
})
}
func formatConstraints(writer io.Writer, value interface{}) error {
fmt.Fprint(writer, value.(constraints.Value).String())
return nil
}
func (c *applicationGetConstraintsCommand) SetFlags(f *gnuflag.FlagSet) {
c.ModelCommandBase.SetFlags(f)
c.out.AddFlags(f, "constraints", map[string]cmd.Formatter{
"constraints": formatConstraints,
"yaml": cmd.FormatYaml,
"json": cmd.FormatJson,
})
}
func (c *applicationGetConstraintsCommand) Init(args []string) error {
if len(args) == 0 {
return errors.Errorf("no application name specified")
}
if !names.IsValidApplication(args[0]) {
return errors.Errorf("invalid application name %q", args[0])
}
c.ApplicationName, args = args[0], args[1:]
return cmd.CheckEmpty(args)
}
func (c *applicationGetConstraintsCommand) Run(ctx *cmd.Context) error {
apiclient, err := c.getAPI()
if err != nil {
return err
}
defer apiclient.Close()
cons, err := apiclient.GetConstraints(c.ApplicationName)
if err != nil {
return err
}
return c.out.Write(ctx, cons[0])
}
type applicationSetConstraintsCommand struct {
applicationConstraintsCommand
Constraints constraints.Value
}
// NewApplicationSetConstraintsCommand returns a command which sets application constraints.
func NewApplicationSetConstraintsCommand() modelcmd.ModelCommand {
return modelcmd.Wrap(&applicationSetConstraintsCommand{})
}
func (c *applicationSetConstraintsCommand) Info() *cmd.Info {
return jujucmd.Info(&cmd.Info{
Name: "set-constraints",
Args: "<application> <constraint>=<value> ...",
Purpose: usageSetConstraintsSummary,
Doc: usageSetConstraintsDetails,
})
}
func (c *applicationSetConstraintsCommand) Init(args []string) (err error) {
if len(args) == 0 {
return errors.Errorf("no application name specified")
}
if !names.IsValidApplication(args[0]) {
return errors.Errorf("invalid application name %q", args[0])
}
c.ApplicationName, args = args[0], args[1:]
c.Constraints, err = constraints.Parse(args...)
return err
}
func (c *applicationSetConstraintsCommand) Run(_ *cmd.Context) (err error) {
apiclient, err := c.getAPI()
if err != nil {
return err
}
defer apiclient.Close()
err = apiclient.SetConstraints(c.ApplicationName, c.Constraints)
return block.ProcessBlockedError(err, block.BlockChange)
}