/
runcommands.go
98 lines (83 loc) · 2.46 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
87
88
89
90
91
92
93
94
95
96
97
98
// 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/remotestate"
"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
logger Logger
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,
// TODO(jam): 2019-10-24 include RemoteAppName
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) {
rc.logger.Tracef("run commands: %s", rc)
if err := rc.callbacks.SetExecutingStatus("running commands"); err != nil {
return nil, errors.Trace(err)
}
response, err := rc.runner.RunCommands(rc.args.Commands, rc.args.RunLocation)
switch err {
case context.ErrRequeueAndReboot:
rc.logger.Warningf("cannot requeue external commands")
fallthrough
case context.ErrReboot:
rc.sendResponse(response, nil)
err = ErrNeedsReboot
default:
errorHandled := rc.sendResponse(response, err)
if errorHandled {
return nil, nil
}
}
return nil, err
}
// Commit does nothing.
// Commit is part of the Operation interface.
func (rc *runCommands) Commit(state State) (*State, error) {
return nil, nil
}
// RemoteStateChanged is called when the remote state changed during execution
// of the operation.
func (rc *runCommands) RemoteStateChanged(snapshot remotestate.Snapshot) {
}