forked from juju/juju
/
runcommands.go
86 lines (72 loc) · 2.06 KB
/
runcommands.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
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package operation
import (
"fmt"
"github.com/juju/errors"
"github.com/juju/juju/worker/uniter/runner"
"github.com/juju/juju/worker/uniter/runner/context"
)
type runCommands struct {
args CommandArgs
sendResponse CommandResponseFunc
callbacks Callbacks
runnerFactory runner.Factory
runner runner.Runner
RequiresMachineLock
}
// String is part of the Operation interface.
func (rc *runCommands) String() string {
suffix := ""
if rc.args.RelationId != -1 {
infix := ""
if rc.args.RemoteUnitName != "" {
infix = "; " + rc.args.RemoteUnitName
}
suffix = fmt.Sprintf(" (%d%s)", rc.args.RelationId, infix)
}
return "run commands" + suffix
}
// Prepare ensures the commands can be run. It never returns a state change.
// Prepare is part of the Operation interface.
func (rc *runCommands) Prepare(state State) (*State, error) {
rnr, err := rc.runnerFactory.NewCommandRunner(context.CommandInfo{
RelationId: rc.args.RelationId,
RemoteUnitName: rc.args.RemoteUnitName,
ForceRemoteUnit: rc.args.ForceRemoteUnit,
})
if err != nil {
return nil, err
}
err = rnr.Context().Prepare()
if err != nil {
return nil, errors.Trace(err)
}
rc.runner = rnr
return nil, nil
}
// Execute runs the commands and dispatches their results. It never returns a
// state change.
// Execute is part of the Operation interface.
func (rc *runCommands) Execute(state State) (*State, error) {
if err := rc.callbacks.SetExecutingStatus("running commands"); err != nil {
return nil, errors.Trace(err)
}
response, err := rc.runner.RunCommands(rc.args.Commands)
switch err {
case context.ErrRequeueAndReboot:
logger.Warningf("cannot requeue external commands")
fallthrough
case context.ErrReboot:
rc.sendResponse(response, nil)
err = ErrNeedsReboot
default:
rc.sendResponse(response, err)
}
return nil, err
}
// Commit does nothing.
// Commit is part of the Operation interface.
func (rc *runCommands) Commit(state State) (*State, error) {
return nil, nil
}