/
op_callbacks.go
177 lines (158 loc) · 5.41 KB
/
op_callbacks.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
// Copyright 2012-2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package uniter
import (
"fmt"
"github.com/juju/charm/v12/hooks"
"github.com/juju/errors"
"github.com/juju/names/v5"
"github.com/juju/juju/core/model"
"github.com/juju/juju/core/status"
"github.com/juju/juju/rpc/params"
"github.com/juju/juju/worker/uniter/charm"
"github.com/juju/juju/worker/uniter/hook"
"github.com/juju/juju/worker/uniter/remotestate"
"github.com/juju/juju/worker/uniter/runner/context"
)
// operationCallbacks implements operation.Callbacks, and exists entirely to
// keep those methods off the Uniter itself.
type operationCallbacks struct {
u *Uniter
}
// PrepareHook is part of the operation.Callbacks interface.
func (opc *operationCallbacks) PrepareHook(hi hook.Info) (string, error) {
name := string(hi.Kind)
switch {
case hi.Kind.IsWorkload():
name = fmt.Sprintf("%s-%s", hi.WorkloadName, hi.Kind)
case hi.Kind.IsRelation():
var err error
name, err = opc.u.relationStateTracker.PrepareHook(hi)
if err != nil {
return "", err
}
case hi.Kind.IsStorage():
if err := opc.u.storage.ValidateHook(hi); err != nil {
return "", err
}
storageName, err := names.StorageName(hi.StorageId)
if err != nil {
return "", err
}
name = fmt.Sprintf("%s-%s", storageName, hi.Kind)
// TODO(axw) if the agent is not installed yet,
// set the status to "preparing storage".
case hi.Kind.IsSecret():
err := opc.u.secretsTracker.PrepareHook(hi)
if err != nil {
return "", err
}
case hi.Kind == hooks.ConfigChanged:
// TODO(axw)
//opc.u.f.DiscardConfigEvent()
case hi.Kind == hooks.LeaderSettingsChanged:
// TODO(axw)
//opc.u.f.DiscardLeaderSettingsEvent()
}
return name, nil
}
// CommitHook is part of the operation.Callbacks interface.
func (opc *operationCallbacks) CommitHook(hi hook.Info) error {
switch {
case hi.Kind == hooks.Start:
opc.u.Probe.SetHasStarted(true)
case hi.Kind == hooks.Stop:
opc.u.Probe.SetHasStarted(false)
case hi.Kind.IsWorkload():
case hi.Kind.IsRelation():
return opc.u.relationStateTracker.CommitHook(hi)
case hi.Kind.IsStorage():
return opc.u.storage.CommitHook(hi)
case hi.Kind.IsSecret():
return opc.u.secretsTracker.CommitHook(hi)
}
return nil
}
func notifyHook(hook string, ctx context.Context, method func(string)) {
if r, err := ctx.HookRelation(); err == nil {
remote, _ := ctx.RemoteUnitName()
if remote == "" {
remote, _ = ctx.RemoteApplicationName()
}
if remote != "" {
remote = " " + remote
}
hook = hook + remote + " " + r.FakeId()
}
method(hook)
}
// NotifyHookCompleted is part of the operation.Callbacks interface.
func (opc *operationCallbacks) NotifyHookCompleted(hook string, ctx context.Context) {
if opc.u.observer != nil {
notifyHook(hook, ctx, opc.u.observer.HookCompleted)
}
}
// NotifyHookFailed is part of the operation.Callbacks interface.
func (opc *operationCallbacks) NotifyHookFailed(hook string, ctx context.Context) {
if opc.u.observer != nil {
notifyHook(hook, ctx, opc.u.observer.HookFailed)
}
}
// FailAction is part of the operation.Callbacks interface.
func (opc *operationCallbacks) FailAction(actionId, message string) error {
if !names.IsValidAction(actionId) {
return errors.Errorf("invalid action id %q", actionId)
}
tag := names.NewActionTag(actionId)
err := opc.u.st.ActionFinish(tag, params.ActionFailed, nil, message)
if params.IsCodeNotFoundOrCodeUnauthorized(err) || params.IsCodeAlreadyExists(err) {
err = nil
}
return err
}
func (opc *operationCallbacks) ActionStatus(actionId string) (string, error) {
if !names.IsValidAction(actionId) {
return "", errors.NotValidf("invalid action id %q", actionId)
}
tag := names.NewActionTag(actionId)
return opc.u.st.ActionStatus(tag)
}
// GetArchiveInfo is part of the operation.Callbacks interface.
func (opc *operationCallbacks) GetArchiveInfo(url string) (charm.BundleInfo, error) {
ch, err := opc.u.st.Charm(url)
if err != nil {
return nil, errors.Trace(err)
}
return ch, nil
}
// SetCurrentCharm is part of the operation.Callbacks interface.
func (opc *operationCallbacks) SetCurrentCharm(charmURL string) error {
return opc.u.unit.SetCharmURL(charmURL)
}
// SetExecutingStatus is part of the operation.Callbacks interface.
func (opc *operationCallbacks) SetExecutingStatus(message string) error {
return setAgentStatus(opc.u, status.Executing, message, nil)
}
// SetUpgradeSeriesStatus is part of the operation.Callbacks interface.
func (opc *operationCallbacks) SetUpgradeSeriesStatus(upgradeSeriesStatus model.UpgradeSeriesStatus, reason string) error {
return setUpgradeSeriesStatus(opc.u, upgradeSeriesStatus, reason)
}
// RemoteInit is part of the operation.Callbacks interface.
func (opc *operationCallbacks) RemoteInit(runningStatus remotestate.ContainerRunningStatus, abort <-chan struct{}) error {
if opc.u.modelType != model.CAAS || opc.u.sidecar {
// Non CAAS model or sidecar CAAS model do not have remote init process.
return nil
}
if opc.u.remoteInitFunc == nil {
return nil
}
return opc.u.remoteInitFunc(runningStatus, abort)
}
// SetSecretRotated is part of the operation.Callbacks interface.
func (opc *operationCallbacks) SetSecretRotated(uri string, oldRevision int) error {
return opc.u.secretsClient.SecretRotated(uri, oldRevision)
}
// SecretsRemoved is part of the operation.Callbacks interface.
func (opc *operationCallbacks) SecretsRemoved(uris []string) error {
return opc.u.secretsTracker.SecretsRemoved(uris)
}