forked from fabric8io/kansible
/
gosupervise.go
331 lines (300 loc) · 7.95 KB
/
gosupervise.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
package main
import (
"errors"
"fmt"
"os"
"strings"
"github.com/codegangsta/cli"
"github.com/fabric8io/gosupervise/ansible"
"github.com/fabric8io/gosupervise/log"
"github.com/fabric8io/gosupervise/k8s"
"github.com/fabric8io/gosupervise/ssh"
"github.com/fabric8io/gosupervise/winrm"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
)
// version is the version of the app.
//
// This value is overwritten by the linker during build. The default version
// here is SemVer 2, but basically indicates that this was a one-off build
// and should not be trusted.
var version = "0.1.0-unstable"
func main() {
app := cli.NewApp()
app.Name = "gosupervise"
app.Usage = `Go Supervise
This command supervises a remote process inside a Pod inside Kubernetes to make
it look and feel like legacy processes running outside of Kubernetes are really
running inside Docker inside Kubernetes.
`
app.Version = version
app.EnableBashCompletion = true
app.After = func(c *cli.Context) error {
if log.ErrorState {
return errors.New("Exiting with errors")
}
return nil
}
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "port",
Value: "22",
Usage: "The port for the remote SSH connection",
},
cli.BoolFlag{
Name: "debug",
Usage: "Enable verbose debugging output",
},
}
app.CommandNotFound = func(c *cli.Context, command string) {
log.Err("No matching command '%s'", command)
cli.ShowAppHelp(c)
log.Die("")
}
app.Commands = []cli.Command{
{
Name: "pod",
Usage: "Runs the supervisor pod for a single host in a set of hosts from an Ansible inventory.",
Description: `This commmand will begin running the supervisor command on one host from the Ansible inventory.`,
ArgsUsage: "[hosts] [command]",
Action: runAnsiblePod,
Flags: []cli.Flag{
cli.StringFlag{
Name: "inventory",
Value: "inventory",
Usage: "The location of your Ansible inventory file",
},
cli.StringFlag{
Name: "rc",
Value: "rc.yml",
Usage: "The YAML file of the ReplicationController for the supervisors",
},
cli.StringFlag{
Name: "password",
Value: "$GOSUPERVISE_PASSWORD",
Usage: "The password used for WinRM connections",
},
cli.BoolFlag{
Name: "winrm",
EnvVar: "GOSUPERVISE_WINRM",
Usage: "Enables the use of WinRM instead of SSH",
},
},
},
{
Name: "rc",
Usage: "Applies ReplicationController for the supervisors for some hosts in an Ansible inventory.",
Description: `This commmand will analyse the hosts in an Ansible inventory and creates or updates the ReplicationController for its supervisors.`,
ArgsUsage: "[hosts] [command]",
Action: applyAnsibleRC,
Flags: []cli.Flag{
cli.StringFlag{
Name: "inventory",
Value: "inventory",
Usage: "The location of your Ansible inventory file",
},
cli.StringFlag{
Name: "rc",
Value: "rc.yml",
Usage: "The YAML file of the ReplicationController for the supervisors",
},
},
},
{
Name: "run",
Usage: "Runs a supervisor command on a given host as a user without using Ansible.",
Description: `This commmand will begin running the supervisor on an avaiable host.`,
ArgsUsage: "[string]",
Action: run,
Flags: []cli.Flag{
cli.StringFlag{
Name: "user",
Value: "$GOSUPERVISE_USER",
Usage: "The user to use on the remote connection",
},
cli.StringFlag{
Name: "privatekey",
Value: "$GOSUPERVISE_PRIVATEKEY",
Usage: "The private key used for SSH",
},
cli.StringFlag{
Name: "host",
Value: "$GOSUPERVISE_HOST",
Usage: "The host for the remote connection",
},
cli.StringFlag{
Name: "command",
Value: "$GOSUPERVISE_COMMAND",
Usage: "The remote command to invoke on the host",
},
cli.StringFlag{
Name: "password",
Usage: "The password if using WinRM to execute the command",
},
cli.BoolFlag{
Name: "winrm",
Usage: "Enables the use of WinRM instead of SSH",
},
},
},
}
app.Before = func(c *cli.Context) error {
log.IsDebugging = c.Bool("debug")
return nil
}
app.RunAndExitOnError()
}
func osExpandAndVerify(c *cli.Context, name string) (string, error) {
flag := c.String(name)
value := os.ExpandEnv(flag)
if len(value) == 0 {
return "", fmt.Errorf("No parameter supplied for: %s", name)
}
log.Debug("flag %s is %s", name, value)
return value, nil
}
func osExpandAndVerifyGlobal(c *cli.Context, name string) (string, error) {
flag := c.GlobalString(name)
value := os.ExpandEnv(flag)
if len(value) == 0 {
return "", fmt.Errorf("No parameter supplied for: %s", name)
}
log.Debug("flag %s is %s", name, value)
return value, nil
}
func fail(err error) {
log.Die("Failed: %s", err)
}
func applyAnsibleRC(c *cli.Context) {
args := c.Args()
if len(args) < 1 {
log.Die("Expected an argument!")
}
hosts := args[0]
f := cmdutil.NewFactory(nil)
if f == nil {
log.Die("Failed to create Kuberentes client factory!")
}
kubeclient, _ := f.Client()
if kubeclient == nil {
log.Die("Failed to create Kuberentes client!")
}
ns, _, _ := f.DefaultNamespace()
if len(ns) == 0 {
ns = "default"
}
rcName, err := osExpandAndVerify(c, "rc")
if err != nil {
fail(err)
}
inventory, err := osExpandAndVerify(c, "inventory")
if err != nil {
fail(err)
}
_, err = ansible.UpdateAnsibleRC(inventory, hosts, kubeclient, ns, rcName)
if err != nil {
fail(err)
}
}
func runAnsiblePod(c *cli.Context) {
args := c.Args()
if len(args) < 2 {
log.Die("Expected at least 2 arguments!")
}
hosts := args[0]
command := strings.Join(args[1:], " ")
log.Info("running command on a host from %s and command `%s`", hosts, command)
f := cmdutil.NewFactory(nil)
if f == nil {
log.Die("Failed to create Kuberentes client factory!")
}
kubeclient, _ := f.Client()
if kubeclient == nil {
log.Die("Failed to create Kuberentes client!")
}
ns, _, _ := f.DefaultNamespace()
if len(ns) == 0 {
ns = "default"
}
rcFile, err := osExpandAndVerify(c, "rc")
if err != nil {
fail(err)
}
port, err := osExpandAndVerifyGlobal(c, "port")
if err != nil {
fail(err)
}
inventory, err := osExpandAndVerify(c, "inventory")
if err != nil {
fail(err)
}
rc, err := k8s.ReadReplicationControllerFromFile(rcFile)
if err != nil {
fail(err)
}
rcName := rc.ObjectMeta.Name
if len(rcName) == 0 {
log.Die("No ReplicationController name in the yaml file %s", rcFile)
}
hostEntry, err := ansible.ChooseHostAndPrivateKey(inventory, hosts, kubeclient, ns, rcName)
if err != nil {
fail(err)
}
host := hostEntry.Host
user := hostEntry.User
useWinRM := c.Bool("winrm") || hostEntry.UseWinRM
if useWinRM {
log.Info("Using WinRM to connect to the hosts %s", hosts)
password, err := osExpandAndVerify(c, "password")
if err != nil {
fail(err)
}
err = winrm.RemoteWinRmCommand(user, password, host, port, command)
} else {
privatekey := hostEntry.PrivateKey
hostPort := host + ":" + port
err = ssh.RemoteSshCommand(user, privatekey, hostPort, command)
}
if err != nil {
log.Err("Failed: %v", err)
}
}
func run(c *cli.Context) {
log.Info("Running GoSupervise!")
port, err := osExpandAndVerifyGlobal(c, "port")
if err != nil {
fail(err)
}
command, err := osExpandAndVerify(c, "command")
if err != nil {
fail(err)
}
host, err := osExpandAndVerify(c, "host")
if err != nil {
fail(err)
}
user, err := osExpandAndVerify(c, "user")
if err != nil {
fail(err)
}
useWinRM := c.Bool("winrm")
if useWinRM {
password, err := osExpandAndVerify(c, "password")
if err != nil {
fail(err)
}
err = winrm.RemoteWinRmCommand(user, password, host, port, command)
} else {
privatekey, err := osExpandAndVerify(c, "privatekey")
if err != nil {
fail(err)
}
hostPort := host + ":" + port
err = ssh.RemoteSshCommand(user, privatekey, hostPort, command)
}
if err != nil {
log.Err("Failed: %v", err)
}
}
type Executor interface {
RemoteSshCommand(user string, privateKey string, hostPort string, cmd string) error
}