-
Notifications
You must be signed in to change notification settings - Fork 0
/
plcli.go
276 lines (263 loc) · 8.32 KB
/
plcli.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
package main
import (
"log"
"os"
"strings"
"github.com/axelniklasson/plcli/lib"
"github.com/axelniklasson/plcli/lib/commands"
"github.com/axelniklasson/plcli/lib/pl"
"github.com/axelniklasson/plcli/lib/util"
"github.com/urfave/cli"
)
func main() {
app := cli.NewApp()
app.Name = "plcli"
app.Usage = "CLI for PlanetLab"
app.Version = "1.1"
app.Authors = []cli.Author{{Name: "Axel Niklasson", Email: "axel.r.niklasson@gmail.com"}}
conf := util.GetConf()
options := &util.Options{}
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "slice",
Value: conf.Slice,
Usage: "name of slice to use when connecting to PlanetLab",
Destination: &options.Slice,
},
&cli.IntFlag{
Name: "workers",
Value: lib.WorkerPoolSize,
Usage: "number of workers to use",
Destination: &lib.WorkerPoolSize,
},
&cli.StringFlag{
Name: "nodes-file",
Usage: "file containing node hostnames and ids of the form \"ID,HOSTNAME\" on each line",
Destination: &options.NodesFile,
},
&cli.BoolFlag{
Name: "sudo",
Usage: "if set, everything will be run as sudo on nodes",
Destination: &options.Sudo,
},
}
app.Commands = []cli.Command{
{
Name: "init",
Aliases: []string{"i"},
Usage: "Init plcli",
UsageText: "plcli init",
Action: func(c *cli.Context) error {
return commands.Init()
},
},
{
Name: "connect",
Aliases: []string{"c"},
Usage: "Connect to a PlanetLab node over ssh",
UsageText: "plcli connect [node]",
Action: func(c *cli.Context) error {
node := c.Args().Get(0)
return commands.ConnectOverSSH(options.Slice, node)
},
},
{
Name: "execute",
Aliases: []string{"e"},
Usage: "Execute a command on a PlanetLab node",
UsageText: "plcli execute [command] [HOSTNAME|all|HOSTNAME1,HOSTNAME2..]",
Action: func(c *cli.Context) error {
cmd := c.Args().Get(0)
hostnamesString := c.Args().Get(1)
var hostnames []string
if len(hostnamesString) == 0 {
log.Fatal("No hostnames found. Run as execute [command] [HOSTNAME|all|HOSTNAME1,HOSTNAME2..]")
} else if hostnamesString == "all" {
log.Printf("Finding all nodes attached to slice %s", options.Slice)
nodes, _ := pl.GetNodesForSlice(options.Slice)
for _, n := range nodes {
hostnames = append(hostnames, n.HostName)
}
} else {
hostnames = strings.Split(hostnamesString, ",")
}
return commands.ExecCmdOnNodes(hostnames, cmd, options)
},
},
{
Name: "transfer",
Aliases: []string{"t"},
Usage: "Transfer a file/directory to a PlanetLab node",
UsageText: "plcli transfer [node] [path_to_source_file] [path_to_target]",
Action: func(c *cli.Context) error {
node := c.Args().Get(0)
src := c.Args().Get(1)
target := c.Args().Get(2)
return commands.Transfer(options.Slice, node, src, target)
},
},
{
Name: "slice-details",
Usage: "Lists details for the current slice",
UsageText: "plcli slice-details",
Action: func(c *cli.Context) error {
return commands.GetDetailsForSlice(options.Slice)
},
},
{
Name: "list-nodes",
Usage: "Lists all nodes attached to the current slice",
UsageText: "plcli list-nodes",
Action: func(c *cli.Context) error {
return commands.GetNodesForSlice(options.Slice)
},
},
{
Name: "health-check",
Usage: "Performs a health check of all nodes attached to the slice and outputs healthy nodes",
UsageText: "plcli [--remove-faulty] health-check",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "remove-faulty",
Usage: "remove faulty nodes from slice during healthcheck",
Destination: &options.RemoveFaulty,
},
&cli.BoolFlag{
Name: "attach-to-slice",
Usage: "attach all healthy nodes to slice",
Destination: &options.AttachToSlice,
},
},
Action: func(c *cli.Context) error {
commands.HealthCheck(options.Slice, options.RemoveFaulty)
return nil
},
},
{
Name: "discover-healthy",
Usage: "Performs a health check of all nodes in the system and outputs hostnames and ids to an output file",
UsageText: "plcli [--attach-to-slice] discover-healthy",
Action: func(c *cli.Context) error {
return commands.DiscoverHealthyNodes(options.Slice, options.AttachToSlice)
},
},
{
Name: "deploy",
Usage: "Deploys an application on PlanetLab nodes",
UsageText: "plcli deploy GIT_URL",
Flags: []cli.Flag{
&cli.IntFlag{
Name: "node-count",
Usage: "number of nodes to deploy to",
Destination: &options.NodeCount,
},
&cli.BoolFlag{
Name: "skip-healthcheck",
Usage: "skip health check when deploying",
Destination: &options.SkipHealthCheck,
},
&cli.IntFlag{
Name: "scale",
Value: 1,
Usage: "number of instances of app to launch on each node",
Destination: &options.Scale,
},
&cli.StringFlag{
Name: "git-branch",
Value: "master",
Usage: "what branch to use in deployment",
Destination: &options.GitBranch,
},
&cli.StringFlag{
Name: "app-path",
Value: "~/app",
Usage: "where the app should be stored on a node during deployment",
Destination: &options.AppPath,
},
&cli.StringFlag{
Name: "prometheus-sd-path",
Usage: "if present, plcli will generate sd.json for prometheus and write to supplied path",
Destination: &options.PrometheusSDPath,
},
&cli.BoolFlag{
Name: "node-exporter",
Usage: "if set, node-exporter will be installed and launched on port 2100",
Destination: &options.NodeExporter,
},
&cli.BoolFlag{
Name: "shuffle-nodes",
Usage: "if set, nodes form PL api will be shuffled prior to deployment",
Destination: &options.ShuffleNodes,
},
&cli.BoolFlag{
Name: "skip-write-hosts-file",
Usage: "if set, no file called hosts_deployment.txt will be written to current directory",
Destination: &options.SkipWriteHostsFile,
},
&cli.StringFlag{
Name: "blacklist",
Usage: "HOST1,HOST2,... string of hostnames to blacklist in the deployment",
Destination: &options.BlacklistedHostnames,
},
&cli.StringFlag{
Name: "env",
Usage: "VAR1=VAL1,VAR2=VAL2,... string of env vars to use in deployment",
Destination: &options.EnvVars,
},
},
Action: func(c *cli.Context) error {
gitURL := c.Args().Get(0)
return commands.Deploy(gitURL, options)
},
},
{
Name: "provision",
Usage: "Provisions node(s) using a provided script",
UsageText: "plcli provision PATH_TO_SCRIPT HOSTNAME|all|HOSTNAME1,HOSTNAME1",
Action: func(c *cli.Context) error {
if len(c.Args()) != 2 {
log.Fatal("Run as provision PATH_TO_SCRIPT HOSTNAME|HOSTNAME1,HOSTNAME1")
}
provisionScriptPath := c.Args().Get(0)
hostnamesString := c.Args().Get(1)
var hostnames []string
if len(hostnamesString) == 0 {
log.Fatal("No hostnames found. Run as provision PATH_TO_SCRIPT HOSTNAME|all|HOSTNAME1,HOSTNAME2..")
} else if hostnamesString == "all" {
log.Printf("Finding all nodes attached to slice %s", options.Slice)
nodes, _ := pl.GetNodesForSlice(options.Slice)
for _, n := range nodes {
hostnames = append(hostnames, n.HostName)
}
} else {
hostnames = strings.Split(hostnamesString, ",")
}
return commands.Provision(provisionScriptPath, hostnames, options)
},
},
{
Name: "cleanup",
Usage: "Performs node cleanup on the given nodes",
UsageText: "plcli cleanup HOSTNAME|HOSTNAME1,HOSTNAME2..",
Action: func(c *cli.Context) error {
hostnamesString := c.Args().Get(0)
var hostnames []string
if len(hostnamesString) == 0 {
log.Fatal("No hostnames found. Run as cleanup all|HOSTNAME|HOSTNAME1,HOSTNAME2..")
} else if hostnamesString == "all" {
nodes, _ := pl.GetNodesForSlice(options.Slice)
for _, n := range nodes {
hostnames = append(hostnames, n.HostName)
}
} else {
hostnames = strings.Split(hostnamesString, ",")
}
return commands.Cleanup(options.Slice, hostnames)
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}