This repository has been archived by the owner on Jan 9, 2020. It is now read-only.
forked from juju/juju
/
op_callbacks.go
125 lines (111 loc) · 3.58 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
// Copyright 2012-2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package uniter
import (
"fmt"
"github.com/juju/errors"
"github.com/juju/names"
corecharm "gopkg.in/juju/charm.v6-unstable"
"gopkg.in/juju/charm.v6-unstable/hooks"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/worker/uniter/charm"
"github.com/juju/juju/worker/uniter/hook"
"github.com/juju/juju/worker/uniter/runner"
)
// 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.IsRelation():
var err error
name, err = opc.u.relations.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 == hooks.ConfigChanged:
// TODO(axw)
//opc.u.f.DiscardConfigEvent()
case hi.Kind == hook.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.IsRelation():
return opc.u.relations.CommitHook(hi)
case hi.Kind.IsStorage():
return opc.u.storage.CommitHook(hi)
case hi.Kind == hooks.ConfigChanged:
opc.u.ranConfigChanged = true
}
return nil
}
func notifyHook(hook string, ctx runner.Context, method func(string)) {
if r, err := ctx.HookRelation(); err == nil {
remote, _ := ctx.RemoteUnitName()
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 runner.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 runner.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) {
err = nil
}
return err
}
// GetArchiveInfo is part of the operation.Callbacks interface.
func (opc *operationCallbacks) GetArchiveInfo(charmURL *corecharm.URL) (charm.BundleInfo, error) {
ch, err := opc.u.st.Charm(charmURL)
if err != nil {
return nil, errors.Trace(err)
}
return ch, nil
}
// SetCurrentCharm is part of the operation.Callbacks interface.
func (opc *operationCallbacks) SetCurrentCharm(charmURL *corecharm.URL) 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, params.StatusExecuting, message, nil)
}