-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.py
110 lines (84 loc) · 2.58 KB
/
init.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
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
"""
Create a file ~/.surfsara with content:
user=<username>
pass=<password>
* login to https://ui.hpccloud.surfsara.nl/
* make sure you are in the user view
* go to Storage -> Apps
* select an "app" (like Ubuntu-16.04.3-Server (2017-12-07)) and press
"openNebula"
* select local_images_ssh datastore and then press "download"
* Go to VM Templates view and notice VM template ID. Use that ID
on the CLI or change in this script.
"""
import oca
import os
import sys
DEFAULT_TEMPLATE_ID = 14190
# hostname, CPUs, MEM
layout = {
'main':
[('main', 4, 16 * 1024)],
'sso':
[('sso', 2, 2 * 1024)],
'keep':
[('keep', 2, 8 * 1024)],
'store': [
('store0', 2, 4 * 1024),
('store1', 2, 4 * 1024),
]
}
# you probably don't want to change these
config_path = os.path.expanduser("~/.surfsara")
endpoint = 'https://api.hpccloud.surfsara.nl/RPC2'
def read_config():
if not os.access(config_path, os.R_OK):
print("can't read {}".format(config_path))
print(__doc__)
sys.exit(1)
config = {}
with open(config_path, 'r') as f:
for l in f:
s = l.strip().split('=')
if len(s) == 2:
config[s[0]] = s[1]
if not ('user' in config and 'pass' in config):
print("can't find user and/or pass in config")
print(__doc__)
sys.exit(1)
return config
def init_client(user, pass_, endpoint):
return oca.Client(f"{user}:{pass_}", endpoint)
def iplist(client):
vp = oca.VirtualMachinePool(client)
vp.info()
for vm in vp:
name = vm.name
ip = list(v.ip for v in vm.template.nics)[0]
yield name, ip
def create(client, name, memory, cpu, vcpu, number, template_id):
extra_template = """
MEMORY = {}
CPU = {}
VCPU = {}
""".format(memory, cpu, vcpu)
tp = oca.VmTemplatePool(client)
tp.info()
template = tp.get_by_id(template_id)
for i in range(number):
print("creating node '{}'".format(name))
template.instantiate(name=name, extra_template=extra_template)
def destroy(client):
vmp = oca.VirtualMachinePool(client=client)
vmp.info()
for i in vmp:
print("destroying node '{}'".format(i.name))
i.delete()
if __name__ == '__main__':
config = read_config()
client = init_client(config['user'], config['pass'], endpoint=endpoint)
#destroy(client)
for group, value in layout.items():
for name, cpus, mem in value:
create(client, name, mem, cpus, cpus, 1, template_id=DEFAULT_TEMPLATE_ID)
print(list(iplist(client)))