This repository has been archived by the owner on Sep 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
endpoint-exec.go
188 lines (159 loc) · 4.78 KB
/
endpoint-exec.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
// RackN Provision Plugins
// Copyright 2019, RackN
// License: RackN Limited Use
package main
//go:generate sh -c "cd content ; drpcli contents bundle ../content.go"
//go:generate sh -c "cd content ; drpcli contents bundle ../content.yaml"
//go:generate sh -c "drpcli contents document content.yaml > endpoint-exec.rst"
//go:generate rm content.yaml
import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"
"github.com/digitalrebar/logger"
"github.com/digitalrebar/provision-plugins/v4"
"github.com/digitalrebar/provision-plugins/v4/utils"
"github.com/digitalrebar/provision/v4/api"
"github.com/digitalrebar/provision/v4/models"
"github.com/digitalrebar/provision/v4/plugin"
)
// ==== PRIMARY ENTRY POINT FOR PLUGIN ====
// def information is passed to DRP during plugin registration
var (
version = v4.RSVersion
def = models.PluginProvider{
Name: "endpoint-exec",
Version: version,
PluginVersion: 4,
AutoStart: true,
AvailableActions: []models.AvailableAction{
{
Command: "endpointExecDo",
Model: "machines",
RequiredParams: []string{
"endpoint-exec/action",
},
OptionalParams: []string{
"endpoint-exec/parameters",
},
},
},
RequiredParams: []string{
"endpoint-exec/actions",
},
Content: contentYamlString,
}
)
type action struct {
Path string
BaseArgs string
}
// Plugin is the overall data holder for the plugin
// If you defined extra operational values or params, they are typically included here
type Plugin struct {
drpClient *api.Client
name string
actions map[string]*action
}
// Config handles the configuration call from the DRP Endpoint
func (p *Plugin) Config(l logger.Logger, session *api.Client, config map[string]interface{}) *models.Error {
p.drpClient = session
name, vverr := utils.ValidateStringValue("Name", config["Name"])
if vverr != nil {
name = "unknown"
}
p.name = name
utils.SetErrorName(p.name)
err := &models.Error{Type: "plugin", Model: "plugins", Key: name}
actions := map[string]*action{}
if rerr := models.Remarshal(config["endpoint-exec/actions"], &actions); rerr != nil {
err.AddError(rerr)
}
p.actions = actions
if err.HasError() != nil {
return err
}
return nil
}
// ActionReturn the answer to the commands
type ActionReturn struct {
ReturnCode int
StandardErr string
StandardOut string
}
func (p *Plugin) postTrigger(l logger.Logger, ma *models.Action) (answer interface{}, err *models.Error) {
// validate action and do
var action string
action, err = utils.ValidateStringValue("endpoint-exec/action", ma.Params["endpoint-exec/action"])
if err != nil {
return
}
localParams := ""
localParams, err = utils.ValidateStringValue("endpoint-exec/parameters", ma.Params["endpoint-exec/parameters"])
if err != nil {
return
}
machine := &models.Machine{}
machine.Fill()
if rerr := models.Remarshal(ma.Model, &machine); rerr != nil {
err = utils.ConvertError(400, rerr)
return
}
act, ok := p.actions[action]
if !ok {
l.Infof("endpoint-exec action unknown: %s", action)
answer = &ActionReturn{
ReturnCode: 1,
StandardErr: fmt.Sprintf("endpoint-exec attempted, but skipped because action unknown: %s", action),
StandardOut: "",
}
return
}
params := []string{}
if act.BaseArgs != "" {
params = append(params, strings.Split(act.BaseArgs, " ")...)
}
if localParams != "" {
params = append(params, strings.Split(localParams, " ")...)
}
cmd := exec.Command(act.Path, params...)
// Set RS_UUID and a TOKEN (this is a pretty powerful token) to machine the machine caller
env := os.Environ()
env = append(env, fmt.Sprintf("RS_UUID=%s", machine.UUID()))
env = append(env, fmt.Sprintf("RS_TOKEN=%s", p.drpClient.Token()))
cmd.Env = env
var outb, errb bytes.Buffer
cmd.Stdout = &outb
cmd.Stderr = &errb
cerr := cmd.Run()
se := errb.String()
if cerr != nil && se == "" {
se = fmt.Sprintf("Exec failed: %v", cerr)
}
answer = &ActionReturn{ReturnCode: cmd.ProcessState.ExitCode(), StandardErr: se, StandardOut: outb.String()}
return
}
// Action handles the action call from the DRP Endpoint
// using ma.Command, all registered actions should be handled
// reminder when validating params:
// DRP will pass in required machine params if they exist in hierarchy
func (p *Plugin) Action(l logger.Logger, ma *models.Action) (answer interface{}, err *models.Error) {
switch ma.Command {
case "endpointExecDo":
answer, err = p.postTrigger(l, ma)
default:
err = utils.MakeError(404, fmt.Sprintf("Unknown command: %s", ma.Command))
}
return
}
// main is the entry point for the plugin code
// the InitApp routine should reflect the name and purpose of the plugin
func main() {
plugin.InitApp("endpoint-exec", "Provides way to run scripts on the Endpoint", version, &def, &Plugin{})
err := plugin.App.Execute()
if err != nil {
os.Exit(1)
}
}