-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlite
executable file
·557 lines (456 loc) · 15 KB
/
lite
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
#!/usr/bin/env python
from __future__ import print_function
import sys
import os
import shutil
import subprocess
import platform
import string
import signal
import re
DEFAULT_OPS_FILES = [
"local-dns.yml",
"virtualbox/cpi.yml",
"virtualbox/outbound-network.yml",
"bosh-lite.yml",
"bosh-lite-runc.yml",
"jumpbox-user.yml",
]
USAGE = """\
usage: lite create env-name
lite delete env-name
lite set-routes env-name
lite unset-routes env-name
lite tunnel env-name deployment instance_group tunnel
lite ssh-command env-name deployment instance_group cmd
lite proc-status env-name deployment instance_group
lite go-routines env-name deployment instance_group
set environment variable 'BOSH_CMD' to set bosh-cli path. Defaults to
'/usr/local/bin/bosh'.
"""
BAD_ENV_NAME_CHARS = """ !@#$%^&*()_+-={}|[]\:";'<>?,/~`"""
def bosh_cmd():
return os.environ.get("BOSH_CMD", "/usr/local/bin/bosh")
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
def create_env(env_name, args):
if len(args) != 0:
eprint(USAGE)
sys.exit(1)
eprint("creating env:", env_name)
eprint("on network: 192.168.%d.0/24" % class_c(env_name))
create_deployment_dir(env_name)
write_ops(env_name, DEFAULT_OPS_FILES)
code = subprocess.call([
bosh_cmd(),
"create-env",
"~/workspace/bosh-deployment/bosh.yml",
"--state", dir_name(env_name) + "/state.json",
"--vars-store", dir_name(env_name) + "/creds.yml",
"-v", "director_name='Bosh Lite Director %s'" % env_name,
"-v", "internal_cidr=192.168.%d.0/24" % class_c(env_name),
"-v", "internal_gw=192.168.%d.1" % class_c(env_name),
"-v", "internal_ip=192.168.%d.6" % class_c(env_name),
"-v", "outbound_network_name=NatNetwork",
] + ops_args(DEFAULT_OPS_FILES), stdout=sys.stdout, stderr=sys.stderr)
if code != 0:
eprint("encountered error with creating env")
delete_deployment_dir(env_name)
sys.exit(code)
ca_cert = subprocess.check_output([
bosh_cmd(),
"int",
dir_name(env_name) + "/creds.yml",
"--path", "/director_ssl/ca",
])
password = subprocess.check_output([
bosh_cmd(),
"int",
dir_name(env_name) + "/creds.yml",
"--path", "/admin_password",
])
subprocess.call([
bosh_cmd(),
"-e", "192.168.%d.6" % class_c(env_name),
"--ca-cert", ca_cert,
"--client", "admin",
"--client-secret", password,
"alias-env",
env_name,
], stdout=sys.stdout, stderr=sys.stderr)
insert_secrets(env_name, "admin", password)
subprocess.call([
bosh_cmd(),
"-n",
"-e", env_name,
"upload-stemcell",
"https://bosh.io/d/stemcells/bosh-warden-boshlite-ubuntu-trusty-go_agent",
], stdout=sys.stdout, stderr=sys.stderr)
subprocess.call([
bosh_cmd(),
"-n",
"-e", env_name,
"update-cloud-config",
os.path.expanduser("~/workspace/bosh-deployment/warden/cloud-config.yml"),
], stdout=sys.stdout, stderr=sys.stderr)
set_routes(env_name, [])
def create_deployment_dir(env_name):
if env_exists(env_name):
eprint("env directory already exists:", dir_name(env_name))
sys.exit(1)
os.makedirs(dir_name(env_name))
def env_exists(env_name):
return os.path.isdir(dir_name(env_name))
def delete_deployment_dir(env_name):
shutil.rmtree(dir_name(env_name))
def dir_name(env_name):
return os.path.expanduser("~/deployments/"+env_name)
def write_ops(env_name, ops):
with open(dir_name(env_name) + "/ops", "w") as f:
for op in ops:
f.write(op + "\n")
def ops_args(ops):
args = []
for op in ops:
args.append("-o")
args.append(os.path.expanduser("~/workspace/bosh-deployment/" + op))
return args
def class_c(env_name):
return hash(env_name) % 204 + 50
def insert_secrets(env_name, username, password):
with open(os.path.expanduser("~/.bosh/config")) as f:
data = f.read()
search_line = " alias: %s\n" % env_name
pos = string.find(data, search_line)
if pos == -1:
eprint("unable to add secrets to alias-env.")
return
secrets = """
username: %s
password: %s
""" % (username, password)
with open(os.path.expanduser("~/.bosh/config"), "w") as f:
f.write(data[:pos] + secrets + data[pos:])
def delete_env(env_name, args):
if len(args) != 0:
eprint(USAGE)
sys.exit(1)
eprint("deleting env:", env_name)
ensure_env_exists(env_name)
ops = read_ops(env_name)
code = subprocess.call([
bosh_cmd(),
"delete-env",
"~/workspace/bosh-deployment/bosh.yml",
"--state", dir_name(env_name) + "/state.json",
"--vars-store", dir_name(env_name) + "/creds.yml",
"-v", "director_name='Bosh Lite Director %s'" % env_name,
"-v", "internal_cidr=192.168.%d.0/24" % class_c(env_name),
"-v", "internal_gw=192.168.%d.1" % class_c(env_name),
"-v", "internal_ip=192.168.%d.6" % class_c(env_name),
"-v", "outbound_network_name=NatNetwork",
] + ops_args(ops), stdout=sys.stdout, stderr=sys.stderr)
if code != 0:
eprint("encountered error with deleting env")
sys.exit(code)
eprint("deleting routes")
unset_routes(env_name)
eprint("deleting deployment dir")
delete_deployment_dir(env_name)
log_out(env_name)
def ensure_env_exists(env_name):
if not env_exists(env_name):
eprint("env does not exist")
sys.exit(1)
def read_ops(env_name):
with open(dir_name(env_name) + "/ops") as f:
return [l.strip() for l in f.readlines()]
def log_out(env_name):
eprint("logging out")
code = subprocess.call([
"bosh",
"--environment", env_name,
"log-out"],
stdout=sys.stdout, stderr=sys.stderr)
if code != 0:
eprint("encountered error with logging out")
sys.exit(code)
def unset_routes(env_name):
if platform.system() == "Linux":
subprocess.call([
"sudo",
"route",
"del",
"-net",
"10.244.0.0/16",
"gw",
"192.168.%d.6" % class_c(env_name),
])
elif platform.system() == "Windows":
subprocess.call([
"route",
"del",
"10.244.0.0/16",
"192.168.%d.6" % class_c(env_name),
])
elif platform.system() == "Darwin":
subprocess.call([
"sudo",
"route",
"-n",
"delete",
"10.244.0.0/16",
"192.168.%d.6" % class_c(env_name),
])
else:
eprint("unable to detect platform")
sys.exit(1)
def set_routes(env_name, args):
if len(args) != 0:
eprint(USAGE)
sys.exit(1)
if platform.system() == "Linux":
subprocess.call([
"sudo",
"route",
"add",
"-net",
"10.244.0.0/16",
"gw",
"192.168.%d.6" % class_c(env_name),
])
elif platform.system() == "Windows":
subprocess.call([
"route",
"add",
"10.244.0.0/16",
"192.168.%d.6" % class_c(env_name),
])
elif platform.system() == "Darwin":
subprocess.call([
"sudo",
"route",
"-n",
"add",
"10.244.0.0/16",
"192.168.%d.6" % class_c(env_name),
])
else:
eprint("unable to detect platform")
sys.exit(1)
def tunnel(env_name, args):
if len(args) != 3:
eprint(USAGE)
sys.exit(1)
deployment = args[0]
instance_group = args[1]
tunnel = args[2]
f = open(os.devnull, 'w')
eprint("*******************************")
eprint("****** Use ctrl-d to stop *****")
eprint("*******************************")
p = subprocess.Popen([
bosh_cmd(),
"--environment",
env_name,
"--deployment",
deployment,
"ssh",
instance_group,
"--opts=-L {}".format(tunnel),
],
stdout=f,
).wait()
def run_ssh_command(env_name, deployment, instance_group, cmd):
p = subprocess.Popen([
bosh_cmd(),
"--environment",
env_name,
"--deployment",
deployment,
"ssh",
instance_group,
"--command",
cmd,
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
p.wait()
vms_out = dict()
vms_err = dict()
for line in p.stdout:
m = re.search('(.+)\s+(stdout|stderr)\s+\|\s+(.+)', line)
if m is None or len(m.groups()) < 3:
continue
name = m.groups()[0]
outType = m.groups()[1]
output = m.groups()[2]
vms = vms_out if outType == "stdout" else vms_err
if name not in vms:
vms[name]=[]
vms[name].append(output.strip())
return (vms_out, vms_err)
def ssh_command(env_name, args):
if len(args) != 3:
eprint(USAGE)
sys.exit(1)
deployment = args[0]
instance_group = args[1]
cmd = args[2]
if instance_group == "*":
instance_group = ""
vms_out, vms_err = run_ssh_command(env_name, deployment, instance_group, cmd)
def print_vms(vms, title_color, printer):
for name, outputs in vms.items():
printer("{}{}{}\n".format(title_color, name, bcolors.ENDC))
for output in outputs:
printer("{}\n".format(output))
print_vms(vms_out, bcolors.OKGREEN, print)
print_vms(vms_err, bcolors.FAIL, eprint)
def proc_status(env_name, deployment, instance_group):
class Process:
name = ""
cpu = ""
mem = ""
pid = ""
def parsestatus(status):
procs = []
for procblock in re.split('\nProcess\s+', status):
procname = re.findall("'(.+)'", procblock)
if len(procname) == 0:
continue
proc = Process()
procs.append(proc)
proc.name = procname[0]
for line in procblock.split('\n'):
# TODO: This is a bit kludgy
cpu = re.findall("^\s*cpu percent\s+([0-9]+\.[0-9]+)", line)
mem = re.findall("^\s*memory percent\s+([0-9]+\.[0-9]+)", line)
pid = re.findall("^\s*pid\s+([0-9]+)", line)
if len(cpu) != 0:
proc.cpu = cpu[0]
continue
if len(mem) != 0:
proc.mem = mem[0]
continue
if len(pid) != 0:
proc.pid = pid[0]
continue
return procs
cmd = "sudo /var/vcap/bosh/bin/monit status"
statuses, _ = run_ssh_command(env_name, deployment, instance_group, cmd)
def join(l):
result = ""
for i in l:
result = result+"\n"+i
return result
result = {}
for vm, status in statuses.iteritems():
result[vm] = parsestatus(join(status))
return result
def print_proc_status(env_name, args):
if len(args) != 2:
eprint(USAGE)
sys.exit(1)
deployment = args[0]
instance_group = args[1]
if instance_group == "*":
instance_group = ""
def label(name, value):
return "{}{}{} {}%".format(bcolors.FAIL, name, bcolors.ENDC, value)
for vm, status in proc_status(env_name, deployment, instance_group).iteritems():
print("{}{}{}\n".format(bcolors.OKGREEN, vm, bcolors.ENDC))
for p in status:
name = "{}{}{}".format(bcolors.HEADER, p.name, bcolors.ENDC)
cpu = label("CPU", p.cpu)
mem = label("MEM", p.mem)
print("{} {} {}\n".format(name, cpu, mem))
def listening_ports(env_name, deployment, instance_group, pid):
cmd = "sudo lsof -p {} | grep LISTEN".format(pid)
out, _ = run_ssh_command(env_name, deployment, instance_group, cmd)
results = []
for vm, output in out.iteritems():
for line in output:
ports = re.findall('\:([0-9]+)\s*\(LISTEN\)', line)
if len(ports) != 0:
results.append(ports[0])
continue
return results
def parse_go_routines(env_name, deployment, instance_group, port):
cmd = "curl localhost:{}/debug/pprof/".format(port)
out, _ = run_ssh_command(env_name, deployment, instance_group, cmd)
for vm, lines in out.iteritems():
for line in lines:
goroutines = re.findall('\<tr\>\<td align\=right\>([0-9]+)\<td\>\<a href\=\"goroutine\?debug\=1\"\>goroutine\<\/a\>', line)
if len(goroutines) != 0:
return int(goroutines[0])
return None
def go_routines(env_name, args):
if len(args) != 2:
eprint(USAGE)
sys.exit(1)
deployment = args[0]
instance_group = args[1]
if instance_group == "*":
instance_group = ""
for vm, ps in proc_status(env_name, deployment, instance_group).iteritems():
print("{}{}{}\n".format(bcolors.OKGREEN, vm, bcolors.ENDC))
for p in ps:
ports = listening_ports(env_name, deployment, instance_group, p.pid)
for port in ports:
goroutines = parse_go_routines(env_name, deployment, instance_group, port)
if goroutines is not None:
name = "{}{}{}".format(bcolors.HEADER, p.name, bcolors.ENDC)
print("{} {}\n".format(name, goroutines))
break
def main():
# Validate bosh command is present and executable
if not os.path.isfile(bosh_cmd()) or not os.access(bosh_cmd(), os.X_OK):
eprint("'{}' is not a valid bosh-cli".format(bosh_cmd()))
eprint("please set BOSH_CMD to a valid bosh-cli path")
sys.exit(1)
action, env_name, args = parse_action()
if action == "create":
create_env(env_name, args)
elif action == "delete":
delete_env(env_name, args)
elif action == "set-routes":
set_routes(env_name, args)
elif action == "unset-routes":
unset_routes(env_name)
elif action == "tunnel":
tunnel(env_name, args)
elif action == "ssh-command":
ssh_command(env_name, args)
elif action == "proc-status":
print_proc_status(env_name, args)
elif action == "go-routines":
go_routines(env_name, args)
else:
eprint(USAGE)
sys.exit(1)
def parse_action():
if len(sys.argv) < 3:
eprint(USAGE)
sys.exit(1)
action, env_name = sys.argv[1], sys.argv[2]
if action not in ["create", "delete", "set-routes", "unset-routes", "tunnel", "ssh-command", "proc-status", "go-routines"]:
eprint(USAGE)
sys.exit(1)
for c in BAD_ENV_NAME_CHARS:
if c in env_name:
eprint("invalid character name in env name:", c)
sys.exit(1)
return action, env_name, sys.argv[3:]
if __name__ == "__main__":
main()