This repository has been archived by the owner on Sep 2, 2021. It is now read-only.
/
plugin.go
336 lines (308 loc) · 8.87 KB
/
plugin.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package plugin
/*
* This is used by plugins to define their base App.
*/
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"time"
"github.com/digitalrebar/logger"
"github.com/digitalrebar/provision/api"
"github.com/digitalrebar/provision/models"
"github.com/digitalrebar/provision/plugin/mux"
"github.com/spf13/cobra"
)
// PluginStop defines the Stop routine used to inform a plugin
// that it should stop.
type PluginStop interface {
Stop(logger.Logger)
}
// PluginConfig defines the Config routine used to configure a
// specific instance of a plugin.
type PluginConfig interface {
Config(logger.Logger, *api.Client, map[string]interface{}) *models.Error
}
// PluginPublisher defines the Publish routine used to send events
// to a plugin.
type PluginPublisher interface {
Publish(logger.Logger, *models.Event) *models.Error
}
// PluginActor defines the Action routine used to invoke actions
// by the plugin.
type PluginActor interface {
Action(logger.Logger, *models.Action) (interface{}, *models.Error)
}
// PluginValidator defines the Validate routine used to ensure that
// the environment is valid around the define timeframe.
type PluginValidator interface {
Validate(logger.Logger, *api.Client) (interface{}, *models.Error)
}
// PluginUnpacker defines the Unpack routine used to unpack embedded
// assets to the specified path.
type PluginUnpacker interface {
Unpack(logger.Logger, string) error
}
var (
thelog logger.Logger
// App is the global cobra command structure.
App = &cobra.Command{
Use: "replaceme",
Short: "Replace ME!",
}
debug = false
client *http.Client
session *api.Client
)
// Publish allows the plugin to generate events back to DRP.
func Publish(t, a, k string, o interface{}) {
if client == nil {
return
}
e := &models.Event{Time: time.Now(), Type: t, Action: a, Key: k, Object: o}
_, err := mux.Post(client, "/publish", e)
if err != nil {
thelog.Errorf("Failed to publish event! %v %v", e, err)
}
}
// Leaving allows the plugin to inform DRP that it is about to exit.
func Leaving(e *models.Error) {
if client == nil {
return
}
_, err := mux.Post(client, "/leaving", e)
if err != nil {
thelog.Errorf("Failed to send leaving event! %v %v", e, err)
}
}
// InitApp initializes the plugin system and makes the base actions
// available in cobra CLI.
func InitApp(use, short, version string, def *models.PluginProvider, pc PluginConfig) {
App.Use = use
App.Short = short
localLogger := log.New(ioutil.Discard, App.Use, log.LstdFlags|log.Lmicroseconds|log.LUTC)
thelog = logger.New(localLogger).SetDefaultLevel(logger.Debug).SetPublisher(logToDRP).Log(App.Use)
App.PersistentFlags().BoolVarP(&debug,
"debug", "d", false,
"Whether the CLI should run in debug mode")
App.AddCommand(&cobra.Command{
Use: "version",
Short: "Digital Rebar Provision CLI Command Version",
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Printf("Version: %v\n", version)
return nil
},
})
App.AddCommand(&cobra.Command{
Use: "autocomplete <filename>",
Short: "Digital Rebar Provision CLI Command Bash AutoCompletion File",
Long: "Generate a bash autocomplete file as <filename>.\nPlace the generated file in /etc/bash_completion.d or /usr/local/etc/bash_completion.d.",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("%v requires 1 argument", cmd.UseLine())
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
App.GenBashCompletionFile(args[0])
return nil
},
})
App.AddCommand(&cobra.Command{
SilenceUsage: true,
Use: "define",
Short: "Digital Rebar Provision CLI Command Define",
Args: func(c *cobra.Command, args []string) error {
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
var theDef interface{}
if pv, ok := pc.(PluginValidator); ok {
session, err2 := buildSession()
if err2 != nil {
return err2
}
ndef, err := pv.Validate(thelog, session)
if err != nil {
return err
}
theDef = ndef
} else {
theDef = def
}
buf, err := json.MarshalIndent(theDef, "", " ")
if err == nil {
fmt.Println(string(buf))
return nil
}
return err
},
})
App.AddCommand(&cobra.Command{
SilenceUsage: true,
Use: "listen <socket path to plugin> <socket path from plugin>",
Short: "Digital Rebar Provision CLI Command Listen",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 2 {
fmt.Printf("Failed\n")
return fmt.Errorf("%v requires 2 argument", cmd.UseLine())
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return run(args[0], args[1], pc)
},
})
App.AddCommand(&cobra.Command{
SilenceUsage: true,
Use: "unpack [loc]",
Short: "Unpack embedded static content to [loc]",
Args: func(c *cobra.Command, args []string) error {
if args[0] == `` {
return fmt.Errorf("Not a valid location: `%s`", args[0])
}
return nil
},
RunE: func(c *cobra.Command, args []string) error {
if pu, ok := pc.(PluginUnpacker); ok {
if err := os.MkdirAll(args[0], 0755); err != nil {
return err
}
return pu.Unpack(thelog, args[0])
}
return nil
},
})
}
// run implements the listen part of the CLI.
func run(toPath, fromPath string, pc PluginConfig) error {
// Get HTTP2 client on our socket.
client = &http.Client{
Transport: &http.Transport{
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
return net.Dial("unix", fromPath)
},
},
}
pmux := mux.New(thelog)
pmux.Handle("/api-plugin/v3/config",
func(w http.ResponseWriter, r *http.Request) { configHandler(w, r, pc) })
if ps, ok := pc.(PluginStop); ok {
pmux.Handle("/api-plugin/v3/stop",
func(w http.ResponseWriter, r *http.Request) { stopHandler(w, r, ps) })
} else {
pmux.Handle("/api-plugin/v3/stop",
func(w http.ResponseWriter, r *http.Request) { stopHandler(w, r, nil) })
}
// Optional Pieces
if pp, ok := pc.(PluginPublisher); ok {
pmux.Handle("/api-plugin/v3/publish",
func(w http.ResponseWriter, r *http.Request) { publishHandler(w, r, pp) })
}
if pa, ok := pc.(PluginActor); ok {
pmux.Handle("/api-plugin/v3/action",
func(w http.ResponseWriter, r *http.Request) { actionHandler(w, r, pa) })
}
os.Remove(toPath)
sock, err := net.Listen("unix", toPath)
if err != nil {
return err
}
defer sock.Close()
go func() {
fmt.Printf("READY!\n")
}()
return http.Serve(sock, pmux)
}
func logToDRP(l *logger.Line) {
if client == nil {
fmt.Fprintf(os.Stderr, "local log: %v\n", l)
return
}
_, err := mux.Post(client, "/log", l)
if err != nil {
thelog.NoRepublish().Errorf("Failed to log line! %v %v", l, err)
}
}
func stopHandler(w http.ResponseWriter, r *http.Request, ps PluginStop) {
l := w.(logger.Logger)
if ps != nil {
ps.Stop(l)
}
resp := models.Error{Code: http.StatusOK}
mux.JsonResponse(w, resp.Code, resp)
l.Infof("STOPPING\n")
os.Exit(0)
}
func buildSession() (*api.Client, error) {
defaultEndpoint := "https://127.0.0.1:8092"
if ep := os.Getenv("RS_ENDPOINT"); ep != "" {
defaultEndpoint = ep
}
defaultToken := ""
if tk := os.Getenv("RS_TOKEN"); tk != "" {
defaultToken = tk
}
var session *api.Client
var err2 error
if defaultToken != "" {
session, err2 = api.TokenSession(defaultEndpoint, defaultToken)
} else {
err2 = fmt.Errorf("Must have a token specified")
}
return session, err2
}
func configHandler(w http.ResponseWriter, r *http.Request, pc PluginConfig) {
var params map[string]interface{}
if !mux.AssureDecode(w, r, ¶ms) {
return
}
l := w.(logger.Logger)
l.Infof("Setting API session\n")
session, err2 := buildSession()
if err2 != nil {
err := &models.Error{Code: 400, Model: "plugin", Key: "incrementer", Type: "plugin", Messages: []string{}}
err.AddError(err2)
mux.JsonResponse(w, err.Code, err)
return
}
l.Infof("Received Config request: %v\n", params)
resp := models.Error{Code: http.StatusOK}
if err := pc.Config(l, session, params); err != nil {
resp.Code = err.Code
b, _ := json.Marshal(err)
resp.Messages = append(resp.Messages, string(b))
}
mux.JsonResponse(w, resp.Code, resp)
}
func actionHandler(w http.ResponseWriter, r *http.Request, pa PluginActor) {
var actionInfo models.Action
if !mux.AssureDecode(w, r, &actionInfo) {
return
}
l := w.(logger.Logger)
if ret, err := pa.Action(l, &actionInfo); err != nil {
mux.JsonResponse(w, err.Code, err)
} else {
mux.JsonResponse(w, http.StatusOK, ret)
}
}
func publishHandler(w http.ResponseWriter, r *http.Request, pp PluginPublisher) {
var event models.Event
if !mux.AssureDecode(w, r, &event) {
return
}
l := w.(logger.Logger)
resp := models.Error{Code: http.StatusOK}
if err := pp.Publish(l.NoRepublish(), &event); err != nil {
resp.Code = err.Code
b, _ := json.Marshal(err)
resp.Messages = append(resp.Messages, string(b))
}
mux.JsonResponse(w, resp.Code, resp)
}