-
Notifications
You must be signed in to change notification settings - Fork 0
/
payload.go
142 lines (120 loc) · 3.7 KB
/
payload.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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package all
import (
"github.com/juju/cmd"
"github.com/juju/errors"
"github.com/juju/juju/api/base"
"github.com/juju/juju/cmd/juju/commands"
"github.com/juju/juju/cmd/modelcmd"
"github.com/juju/juju/payload"
"github.com/juju/juju/payload/api/client"
internalclient "github.com/juju/juju/payload/api/private/client"
"github.com/juju/juju/payload/context"
"github.com/juju/juju/payload/status"
unitercontext "github.com/juju/juju/worker/uniter/runner/context"
"github.com/juju/juju/worker/uniter/runner/jujuc"
)
type payloads struct{}
func (c payloads) registerForServer() error {
c.registerHookContext()
return nil
}
func (c payloads) registerForClient() error {
c.registerPublicCommands()
// needed for hook-tool
c.registerHookContextCommands()
return nil
}
type facadeCaller struct {
base.FacadeCaller
closeFunc func() error
}
func (c facadeCaller) Close() error {
return c.closeFunc()
}
func (payloads) newListAPIClient(cmd *status.ListCommand) (status.ListAPI, error) {
apiCaller, err := cmd.NewAPIRoot()
if err != nil {
return nil, errors.Trace(err)
}
caller := base.NewFacadeCallerForVersion(apiCaller, "Payloads", 1)
listAPI := client.NewPublicClient(&facadeCaller{
FacadeCaller: caller,
closeFunc: apiCaller.Close,
})
return listAPI, nil
}
func (c payloads) registerPublicCommands() {
if !markRegistered(payload.ComponentName, "public-commands") {
return
}
commands.RegisterEnvCommand(func() modelcmd.ModelCommand {
return status.NewListCommand(c.newListAPIClient)
})
}
func (c payloads) registerHookContext() {
if !markRegistered(payload.ComponentName, "hook-context") {
return
}
unitercontext.RegisterComponentFunc(payload.ComponentName,
func(config unitercontext.ComponentConfig) (jujuc.ContextComponent, error) {
hctxClient := c.newUnitFacadeClient(config.APICaller)
// TODO(ericsnow) Pass the unit's tag through to the component?
component, err := context.NewContextAPI(hctxClient, config.DataDir)
if err != nil {
return nil, errors.Trace(err)
}
return component, nil
},
)
c.registerHookContextCommands()
}
type payloadsHookContext struct {
jujuc.Context
}
// Component implements context.HookContext.
func (c payloadsHookContext) Component(name string) (context.Component, error) {
found, err := c.Context.Component(name)
if err != nil {
return nil, errors.Trace(err)
}
compCtx, ok := found.(context.Component)
if !ok && found != nil {
return nil, errors.Errorf("wrong component context type registered: %T", found)
}
return compCtx, nil
}
func (payloads) newUnitFacadeClient(caller base.APICaller) context.APIClient {
facadeCaller := base.NewFacadeCallerForVersion(caller, "PayloadsHookContext", 1)
return internalclient.NewUnitFacadeClient(facadeCaller)
}
func (payloads) registerHookContextCommands() {
if !markRegistered(payload.ComponentName, "hook-context-commands") {
return
}
jujuc.RegisterCommand(context.RegisterCmdName, func(ctx jujuc.Context) (cmd.Command, error) {
compCtx := payloadsHookContext{ctx}
cmd, err := context.NewRegisterCmd(compCtx)
if err != nil {
return nil, errors.Trace(err)
}
return cmd, nil
})
jujuc.RegisterCommand(context.StatusSetCmdName, func(ctx jujuc.Context) (cmd.Command, error) {
compCtx := payloadsHookContext{ctx}
cmd, err := context.NewStatusSetCmd(compCtx)
if err != nil {
return nil, errors.Trace(err)
}
return cmd, nil
})
jujuc.RegisterCommand(context.UnregisterCmdName, func(ctx jujuc.Context) (cmd.Command, error) {
compCtx := payloadsHookContext{ctx}
cmd, err := context.NewUnregisterCmd(compCtx)
if err != nil {
return nil, errors.Trace(err)
}
return cmd, nil
})
}