-
Notifications
You must be signed in to change notification settings - Fork 34
/
create_vm_cluster.py
70 lines (65 loc) · 2.44 KB
/
create_vm_cluster.py
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
#!/usr/bin/env python
from lndynamic import LNDynamic
import natsort
import time
# find lunanode credentials
with open(r"/home/hme/.lunanode/commands.txt") as hpass:
lines = hpass.readlines()
api = LNDynamic(lines[0].rstrip('\n'), lines[1].rstrip('\n'))
def create_master_cluster(name):
api.request("vm", "create",
{'hostname': name, 'plan_id': 4, 'region': 'roubaix', 'image_id': 148540, 'storage': 70})
cluster_name = 'k8s-white'
number_of_vm = input("Nbr_of_cluster ? ")
for i in range(1, int(number_of_vm) + 1):
create_master_cluster(cluster_name + "-master")
for j in range(1, 3):
vm_name = cluster_name + "-node-" + str(j)
api.request("vm", "create",
{'hostname': vm_name, 'plan_id': 4, 'region': 'roubaix', 'image_id': 148540, 'storage': 70})
# sleep while lunanode setting up public ip addresses for each VMs
time.sleep(240)
results = api.request('vm', 'list')
f = open(r"/home/hme/inventory-" + cluster_name, "w+")
hfile = open(r"/home/hme/user_list-" + cluster_name, "w+")
val = results.get("vms")
user_dic = {}
for i in range(0, len(val)):
flag = 0
for key, value in val[i].items():
if key == 'name':
if cluster_name not in value:
break
print('name=', value)
user = value
if key == 'primaryip':
ip = value
print('ip=', value)
if key == 'plan_id':
print('plan_id=', value)
if key == 'vm_id':
print('vm_id=', value)
vm_info = api.request('vm', 'info', {'vm_id': value})
st = vm_info.get('info')
try:
print(st['login_details'])
user_login = st['login_details']
a = user_login.split()
print(str(ip), str(a[1]), str(a[3]))
gt = str(a[1])[:-1]
line = "{} ansible_ssh_user={} ansible_ssh_pass={} ansible_ssh_extra_args='-o StrictHostKeyChecking=no'\n".format(
str(ip), str(gt), str(a[3]))
f.write(line)
user_dic[str(user)] = str(ip)
except KeyError as error:
pass
f.close()
# print (user_dic)
list_user = user_dic.keys();
natural = natsort.natsorted(list_user)
# print natural
for vts in range(0, len(natural)):
myip = user_dic[natural[vts]]
user_line = "{} \t {} \t ubuntu \t lawn-vex \n".format(natural[vts], myip)
hfile.write(user_line)
hfile.close()