-
Notifications
You must be signed in to change notification settings - Fork 1
/
ds-amphora-ssh
executable file
·96 lines (77 loc) · 2.76 KB
/
ds-amphora-ssh
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
#!/usr/bin/env python3
import argparse
import os
import subprocess
import time
import openstack
def config_from_env():
config = {}
for k in ('auth_url', 'project_name', 'username',
'password', 'region_name'):
v = os.environ.get('OS_%s' % (k.upper()))
config[k] = v
return config
def amphora_ssh(load_balancer, amphora_index, user, loop, interval,
command, status, verbose):
conn = openstack.connect(**config_from_env())
filter = {}
if status:
filter['status'] = status
if load_balancer:
lb = conn.load_balancer.find_load_balancer(load_balancer)
filter['loadbalancer_id'] = lb['id']
while True:
amphorae = list(conn.load_balancer.amphorae(**filter))
if len(amphorae) > amphora_index:
amphora = amphorae[amphora_index]
if verbose:
print("Using amphora {}".format(amphora.id))
break
if verbose:
print("Didn't find any amphorae, retrying in "
"{} second{}.".format(interval,
's' if interval != 1 else ''))
time.sleep(interval)
compute = conn.compute.get_server(amphora.compute_id)
image = conn.image.get_image(compute.image.id)
if 'ubuntu' in image.name:
user='ubuntu'
address = compute.addresses['lb-mgmt-net'][0]['addr']
cmd = ["ssh"]
if not verbose:
cmd.append("-q")
if not loop:
cmd.append("-t")
cmd.extend(["-i", "/etc/octavia/.ssh/octavia_ssh_key", "-o",
"ConnectTimeout=5", "{}@{}".format(user, address),
"sudo"])
if not command:
cmd.append("-s")
else:
cmd.extend(command)
print("Command is {}".format(cmd))
if not loop:
r = os.execvp(cmd[0], cmd)
else:
while True:
p = subprocess.Popen(cmd)
p.wait()
time.sleep(interval)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-i', dest='interval', type=int, default=1)
parser.add_argument('-s', dest='status', type=str)
parser.add_argument('-u', dest='user', type=str, default='centos')
parser.add_argument('-n', dest='number', type=int, default=0)
parser.add_argument('-l', dest='loop', action='store_true')
parser.add_argument('-v', dest='verbose', action='store_true')
parser.add_argument('--lb', dest='load_balancer', type=str)
parser.add_argument('cmd', nargs='*')
args = parser.parse_args()
amphora_ssh(load_balancer=args.load_balancer,
amphora_index=args.number, user=args.user,
loop=args.loop, interval=args.interval,
status=args.status, command=args.cmd,
verbose=args.verbose)
if __name__ == '__main__':
main()