-
Notifications
You must be signed in to change notification settings - Fork 0
/
oca_helper.py
116 lines (93 loc) · 2.95 KB
/
oca_helper.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
110
111
112
113
114
115
116
"""
Create a file ~/.surfsara with content:
user=<username>
pass=<password>
Also you need to do some tasks manually since the OCA API is an unmaintained
"enfant terrible":
* login to https://ui.hpccloud.surfsara.nl/
* make sure you are in the user view
* go to 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 click
import oca
import os
import sys
# settings
DEFAULT_TEMPLATE_ID = 7890
DEFAULT_MEMORY = 2048 # GB
DEFAULT_CPU = 4
DEFAULT_VCPU = 4
DEFAULT_NODES_NUM = 4
# 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(config, endpoint):
return oca.Client(config['user'] + ':' + config['pass'], endpoint)
@click.group()
@click.pass_context
def cli(context):
config = read_config()
context.obj = init_client(config, endpoint)
@cli.command()
@click.option('--inactive', default=False, type=bool)
@click.pass_obj
def iplist(client, inactive):
vp = oca.VirtualMachinePool(client)
vp.info()
click.echo("[nodes]")
for vm in vp:
if not inactive and (vm.state != vm.ACTIVE):
continue
ip_list = list(v.ip for v in vm.template.nics)
click.echo(ip_list[0])
@cli.command()
@click.option('--memory', default=DEFAULT_MEMORY, type=int)
@click.option('--cpu', default=DEFAULT_CPU, type=float)
@click.option('--vcpu', default=DEFAULT_VCPU, type=int)
@click.option('--number', help='Number of nodes', default=DEFAULT_NODES_NUM, type=int)
@click.option('--template_id', default=DEFAULT_TEMPLATE_ID, type=int)
@click.pass_obj
def create(client, 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):
name = "workflow-{}".format(i)
click.echo("creating node '{}'".format(name))
template.instantiate(name=name, extra_template=extra_template)
@cli.command()
@click.pass_obj
def destroy(client):
vmp = oca.VirtualMachinePool(client=client)
vmp.info()
for i in vmp:
click.echo("destroying node '{}'".format(i.name))
i.delete()
if __name__ == '__main__':
cli()