forked from juju/juju
/
run.go
151 lines (126 loc) · 4.03 KB
/
run.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
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package action
import (
"strings"
"time"
"github.com/juju/collections/set"
"github.com/juju/errors"
"gopkg.in/juju/names.v2"
"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/core/actions"
"github.com/juju/juju/state"
)
// getAllUnitNames returns a sequence of valid Unit objects from state. If any
// of the application names or unit names are not found, an error is returned.
func getAllUnitNames(st *state.State, units, services []string) (result []names.Tag, err error) {
var leaders map[string]string
getLeader := func(appName string) (string, error) {
if leaders == nil {
var err error
leaders, err = st.ApplicationLeaders()
if err != nil {
return "", err
}
}
if leader, ok := leaders[appName]; ok {
return leader, nil
}
return "", errors.Errorf("could not determine leader for %q", appName)
}
// Replace units matching $app/leader with the appropriate unit for
// the leader.
unitsSet := set.NewStrings()
for _, unit := range units {
if !strings.HasSuffix(unit, "leader") {
unitsSet.Add(unit)
continue
}
app := strings.Split(unit, "/")[0]
leaderUnit, err := getLeader(app)
if err != nil {
return nil, common.ServerError(err)
}
unitsSet.Add(leaderUnit)
}
for _, name := range services {
service, err := st.Application(name)
if err != nil {
return nil, err
}
units, err := service.AllUnits()
if err != nil {
return nil, err
}
for _, unit := range units {
unitsSet.Add(unit.Name())
}
}
for _, unitName := range unitsSet.SortedValues() {
if !names.IsValidUnit(unitName) {
return nil, errors.Errorf("invalid unit name %q", unitName)
}
result = append(result, names.NewUnitTag(unitName))
}
return result, nil
}
// Run the commands specified on the machines identified through the
// list of machines, units and services.
func (a *ActionAPI) Run(run params.RunParams) (results params.ActionResults, err error) {
if err := a.checkCanAdmin(); err != nil {
return results, err
}
if err := a.check.ChangeAllowed(); err != nil {
return results, errors.Trace(err)
}
units, err := getAllUnitNames(a.state, run.Units, run.Applications)
if err != nil {
return results, errors.Trace(err)
}
machines := make([]names.Tag, len(run.Machines))
for i, machineId := range run.Machines {
if !names.IsValidMachine(machineId) {
return results, errors.Errorf("invalid machine id %q", machineId)
}
machines[i] = names.NewMachineTag(machineId)
}
actionParams := a.createActionsParams(append(units, machines...), run.Commands, run.Timeout)
return queueActions(a, actionParams)
}
// RunOnAllMachines attempts to run the specified command on all the machines.
func (a *ActionAPI) RunOnAllMachines(run params.RunParams) (results params.ActionResults, err error) {
if err := a.checkCanAdmin(); err != nil {
return results, err
}
if err := a.check.ChangeAllowed(); err != nil {
return results, errors.Trace(err)
}
machines, err := a.state.AllMachines()
if err != nil {
return results, err
}
machineTags := make([]names.Tag, len(machines))
for i, machine := range machines {
machineTags[i] = machine.Tag()
}
actionParams := a.createActionsParams(machineTags, run.Commands, run.Timeout)
return queueActions(a, actionParams)
}
func (a *ActionAPI) createActionsParams(actionReceiverTags []names.Tag, quotedCommands string, timeout time.Duration) params.Actions {
apiActionParams := params.Actions{Actions: []params.Action{}}
actionParams := map[string]interface{}{}
actionParams["command"] = quotedCommands
actionParams["timeout"] = timeout.Nanoseconds()
for _, tag := range actionReceiverTags {
apiActionParams.Actions = append(apiActionParams.Actions, params.Action{
Receiver: tag.String(),
Name: actions.JujuRunActionName,
Parameters: actionParams,
})
}
return apiActionParams
}
var queueActions = func(a *ActionAPI, args params.Actions) (results params.ActionResults, err error) {
return a.Enqueue(args)
}