forked from Marie-Donnie/juice
-
Notifications
You must be signed in to change notification settings - Fork 1
/
juice.py
executable file
·344 lines (282 loc) · 9.86 KB
/
juice.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
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
#!/usr/bin/env python
"""Tool to test the performances of MariaDB, Galera and CockroachDB with
OpenStack on Grid'5000
Usage:
juice [-h | --help] [-v | --version] <command> [<args>...]
Options:
-h --help Show this help
-v --version Show version number
Commands:
deploy Claim resources from g5k and configure them
openstack Add OpenStack Keystone to the deployment
rally Benchmark the Openstack
stress Launch sysbench tests (after a deployment)
emulate Emulate network using tc
backup Backup the environment
destroy Destroy all the running dockers (not the resources)
info Show information of the actual deployment
help Show this help
Run 'juice COMMAND --help' for more information on a command
"""
import os
import logging
import sys
import pprint
import yaml
import json
import operator
import pickle
from docopt import docopt
from enoslib.api import (generate_inventory, emulate_network,
validate_network)
from enoslib.task import enostask, _save_env
from utils import (JUICE_PATH, ANSIBLE_PATH, SYMLINK_NAME, doc,
doc_lookup, run_ansible, g5k_deploy)
logging.basicConfig(level=logging.DEBUG)
tc = {
"enable": True,
"default_delay": "0ms",
"default_rate": "10gbit",
"constraints": [{
"src": "database",
"dst": "database",
"delay": "0ms",
"rate": "10gbit",
"loss": "0",
"network": "database_network",
}],
"groups": ['database'],
}
######################################################################
## SCAFFOLDING
######################################################################
@doc()
@enostask()
def deploy(conf, provider='g5k', force_deployment=False, xp_name=None,
tags=['provide', 'inventory', 'scaffold'], env=None,
**kwargs):
"""
usage: juice deploy [--conf CONFIG_PATH] [--provider PROVIDER]
[--force-deployment]
[--xp-name NAME] [--tags TAGS...]
Claim resources from PROVIDER and configure them.
Options:
--conf CONFIG_PATH Path to the configuration file describing the
deployment [default: ./conf.yaml]
--provider PROVIDER Provider to target [default: g5k]
--force-deployment Force provider to redo the deployment
--xp-name NAME NAME of the folder generated by juice for this
new deployment.
--tags TAGS Only run tasks relative to the specific tags
[default: provide inventory scaffold]
"""
# Read the configuration
config = {}
if isinstance(conf, str):
# Get the config object from a yaml file
with open(conf) as f:
config = yaml.load(f)
elif isinstance(conf, dict):
# Get the config object from a dict
config = conf
else:
# Data format error
raise Exception(
'conf is type {!r} while it should be a yaml file or a dict'.format(type(conf)))
env['db'] = config.get('database', 'cockroachdb')
env['monitoring'] = config.get('monitoring', True)
env['config'] = config
# Claim resources on Grid'5000
if 'provide' in tags:
if provider == 'g5k' and 'g5k' in config:
env['provider'] = 'g5k'
updated_env = g5k_deploy(config['g5k'], env=xp_name,
force_deploy=force_deployment)
env.update(updated_env)
else:
raise Exception(
'The provider {!r} is not supported or it lacks a configuration'.format(provider))
# Generate the Ansible inventory file
if 'inventory' in tags:
env['inventory'] = os.path.join(env['resultdir'], 'hosts')
generate_inventory(env['roles'] , env['networks'],
env['inventory'], check_networks=True)
_save_env(env)
# Deploy the resources, requires both g5k and inventory executions
if 'scaffold' in tags:
run_ansible('scaffolding.yml', extra_vars={
'registry': config['registry'],
'db': env['db'],
'monitoring': env['monitoring'],
'enos_action': 'deploy'
})
@doc()
@enostask()
def backup(backup_dir='current/backup', env=None, **kwargs):
"""
usage: juice backup [--backup-dir DIRECTORY]
Backup the experiment
--backup-dir DIRECTORY Backup directory [default: current/backup]
"""
backup_dir = os.path.abspath(backup_dir)
os.path.isdir(backup_dir) or os.mkdir(backup_dir)
extra_vars = {
"enos_action": "backup",
"db": env['db'],
"backup_dir": backup_dir,
"monitoring": env['monitoring'],
"rally_nodes": env.get('rally_nodes', [])
}
run_ansible('scaffolding.yml', extra_vars=extra_vars)
run_ansible('openstack.yml', extra_vars=extra_vars)
run_ansible('rally.yml', extra_vars=extra_vars)
@doc()
@enostask()
def destroy(env=None, hard=False, **kwargs):
"""
usage: juice destroy
Destroy all the running dockers (not destroying the resources), requires g5k
and inventory executions
"""
extra_vars = {}
# Call destroy on each component
extra_vars.update({
'monitoring': env.get('monitoring', True),
"db": env.get('db', 'cockroachdb'),
"rally_nodes": env.get('rally_nodes', []),
"enos_action": "destroy"
})
run_ansible('scaffolding.yml', extra_vars=extra_vars)
run_ansible('openstack.yml', extra_vars=extra_vars)
run_ansible('rally.yml', extra_vars=extra_vars)
######################################################################
## Scaffolding ++
######################################################################
@doc()
@enostask()
def openstack(env=None, **kwargs):
"""
usage: juice openstack
Launch OpenStack.
"""
# Generate inventory
extra_vars = {
"db": env['db'],
}
# use deploy of each role
extra_vars.update({"enos_action": "deploy"})
run_ansible('openstack.yml', extra_vars=extra_vars)
######################################################################
## Stress
######################################################################
@doc()
@enostask()
def stress(env=None, **kwargs):
"""
usage: juice stress
Launch sysbench tests.
"""
# Generate inventory
extra_vars = {
"registry": env["config"]["registry"],
"db": env.get('db', 'cockroachdb'),
"enos_action": "stress"
}
# use deploy of each role
run_ansible('stress.yml', extra_vars=extra_vars)
@doc()
@enostask()
def rally(files, directory, high, env=None, **kwargs):
"""
usage: juice rally [--files FILE... | --directory DIRECTORY] [--high]
Benchmark the Openstack
--files FILE Files to use for rally scenarios (name must be a path
from rally scenarios folder).
--directory DIRECTORY Directory that contains rally scenarios. [default:
keystone]
--high Use high mode or not
"""
logging.info("Launching rally using scenarios: %s" % (', '.join(files)))
logging.info("Launching rally using all scenarios in %s directory.",
directory)
database_nodes = [host.address for role, hosts in env['roles'].items()
if role.startswith('database')
for host in hosts]
# In high mode: runs rally in all database nodes, in light mode:
# runs rally on one database node. In light mode, we pick the
# second database node (ie, `database_node[1]`) to not run rally
# on the same node than the one that contains mariadb.
rally_nodes = database_nodes if high else database_nodes[1]
env['rally_nodes'] = rally_nodes
extra_vars = {
"rally_nodes": rally_nodes
}
if files:
extra_vars.update({"rally_files": files})
else:
extra_vars.update({"rally_directory": directory})
# use deploy of each role
extra_vars.update({"enos_action": "deploy"})
run_ansible('rally.yml', extra_vars=extra_vars)
######################################################################
## Other
######################################################################
@doc(tc)
@enostask()
def emulate(tc=tc, env=None, **kwargs):
"""
usage: juice emulate
Emulate network using: {0}
"""
inventory = env["inventory"]
roles = env["roles"]
logging.info("Emulates using constraints: %s" % tc)
emulate_network(roles, inventory, tc)
env["latency"] = tc['constraints'][0]['delay']
@doc()
@enostask()
def validate(env=None, **kwargs):
"""
usage: juice validate
Validate network. Doesn't work for now since there is no flent installed
"""
inventory = env["inventory"]
roles = env["roles"]
validate_network(roles, inventory)
@doc(SYMLINK_NAME)
@enostask()
def info(env, out, **kwargs):
"""
usage: enos info [-e ENV|--env=ENV] [--out=FORMAT]
Show information of the `ENV` deployment.
Options:
-e ENV --env=ENV Path to the environment directory. You should use
this option when you want to link a
specific experiment [default: {0}].
--out FORMAT Output the result in either json, pickle or
yaml format.
"""
if not out:
pprint.pprint(env)
elif out == 'json':
print(json.dumps(env, default=operator.attrgetter('__dict__')))
elif out == 'pickle':
print(pickle.dumps(env))
elif out == 'yaml':
print(yaml.dump(env))
else:
print("--out doesn't suppport %s output format" % out)
print(info.__doc__)
@doc()
def help(**kwargs):
"""
usage: juice help
Show the help message
"""
print(__doc__)
if __name__ == '__main__':
args = docopt(__doc__,
version='juice version 1.0.0',
options_first=True)
argv = [args['<command>']] + args['<args>']
doc_lookup(args['<command>'], argv)