-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsynnefo_compute
executable file
·621 lines (487 loc) · 20.4 KB
/
synnefo_compute
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
#!/usr/bin/python
#coding: utf-8 -*-
# Copyright 2014 K.Kagkelidis <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from base64 import b64encode
import time
try:
from kamaki.clients import ClientError
from kamaki.clients.astakos import AstakosClient
from kamaki.clients.cyclades import CycladesClient
from kamaki.clients.network import NetworkClient
from kamaki.cli import logging
except ImportError:
print "Kamaki Client has to be installed to use this module"
DOCUMENTATION = '''
module: synnefo
version_added: "0.2"
short_description: Create VMs in synnefo-based clouds
description:
- Create/Start/Stop and get state of Virtual Machines in infrastructures based on synnefo platform
options:
url:
description:
- Authentication url of specific synnefo infrastructure
default: 'https://accounts.okeanos.grnet.gr/identity/v2.0'
token:
description:
- Security token to authenticate specific user
name:
description:
- Virtual Machine's name
id:
description:
- Virtual Machine's id number
image:
description:
- Virtual Machine's image name
image_id:
description:
- Virtual Machine's image id number
flavor:
description:
- Virtual Machine's flavor name
flavor_id:
description:
- Virtual Machine's flavor id number
vcpus:
description:
- Number of Virtual Cores in the desired flavor
ram:
description:
- Amount of ram (MB/GB) in the desired flavor
disk:
description:
- Amount of hard drive (GB) space in the desired flavor
dtype:
description:
- Type of disk template to be used in the desired flavor
choices: ['drbd','ext_vlmc']
state:
description:
- Indicate desired state of the vm
choices: ['present','absent','active','stopped']
default: present
requirements: ["kamakiclient"]
'''
EXAMPLES = '''
# Create a new VM
synnefo_compute:
name=my_brand_new_vm
state=present
wait=yes
flavor_id=4
image='Fedora'
auth_url='https://accounts.myexamplecloud.local/identity/v2.0'
token='myApIaCcEssToKen'
# Shutdown a VM
synnefo_compute:
name=my_running_vm
state=stopped
wait=yes
auth_url='https://accounts.myexamplecloud.local/identity/v2.0'
token='myApIaCcEssToKen'
# Start a VM
synnefo_compute:
name=my_stopped_vm
state=active
wait=yes
auth_url='https://accounts.myexamplecloud.local/identity/v2.0'
token='myApIaCcEssToKen'
#Delete a VM
synnefo_compute:
name=my_loved_vm
state=absent
wait=yes
auth_url='https://accounts.myexamplecloud.local/identity/v2.0'
token='myApIaCcEssToKen'
# Create a VM and provide specs
synnefo_compute:
name=vm_with_specs
state=present
image='Ubuntu Server'
vcpus=1
ram=1024
disk=20
dtype=drdb
wait=yes
auth_url='https://accounts.myexamplecloud.local/identity/v2.0'
token='myApIaCcEssToKen'
'''
def snf_list_public_networks(module,network):
try:
result = []
net_list = network.list_networks()
for item in net_list:
if item['SNF:floating_ip_pool'] and item['router:external'] and ['public']:
result.append(item['id'])
return result
except ClientError as e:
module.fail_json(msg="Network: listing of available networks/subnets failed", msg_details=e.message)
def snf_add_public_network(module,network):
# if the public_network arg is set as 'any'
if module.params['public_network'] == 'any':
#look first for available floating ips
floating_ip = snf_available_floating_ip(module,network,None)
#if found available return it
if floating_ip:
return floating_ip
# list public networks
pub_net_list = snf_list_public_networks(module,network)
if not pub_net_list:
module.fail_json(msg="Zero public networks found!")
for item in pub_net_list:
#iterate and try to create floating ip
floating_ip = snf_create_floating_ip(module,network,item)
if floating_ip:
return floating_ip
module.fail_json(msg="Could not create new floating ip on any available public networks")
else:
# Read public network id argument...
public_net_id = module.params['public_network']
# ...if not a number then must be cidr of subnet so get the according network_id
if public_net_id.isdigit() == False:
public_net_id = snf_network_by_cidr(module,network,public_net_id)
# look for available floating ips in the specified network
floating_ip = snf_available_floating_ip(module,network,public_net_id)
#if found available return it
if floating_ip:
return floating_ip
#...else try to create it
floating_ip = snf_create_floating_ip(module,network,public_net_id)
if floating_ip:
return floating_ip
module.fail_json(msg="Could not create a new floating ip in the specified network")
def snf_network_by_cidr(module,network,cidr):
try:
subnet_list = network.list_subnets()
for item_subnet in subnet_list:
if item_subnet['cidr'] == cidr:
return item_subnet['network_id']
module.fail_json(msg="Network containing subnet with cidr: %s not found!" % cidr)
except ClientError as e:
module.fail_json(msg="Network: listing of available networks/subnets failed", msg_details=e.message)
def snf_available_floating_ip(module,network,network_id):
try:
floating_list = network.list_floatingips()
if network_id:
for item_floating in floating_list:
if item_floating['floating_network_id'] == network_id:
if item_floating['instance_id'] == None:
return item_floating
else:
for item_floating in floating_list:
if item_floating['instance_id'] == None:
return item_floating
return None
except ClientError as e:
module.fail_json(msg="Network: listing floating ips failed",msg_details=e.message)
def snf_create_floating_ip(module,network,network_id):
try:
fl_ip = network.create_floatingip(network_id)
return fl_ip
except ClientError as e:
return None
def snf_attach_floating_ip(floating_ip):
net = {'uuid':floating_ip['floating_network_id'],'floating_ip_address':floating_ip['floating_ip_address']}
net_list = []
net_list.append(net)
return net_list
def snf_wait_pending(module,cyclades,vm_id):
try:
pending = cyclades.get_server_details(vm_id)['SNF:task_state']
while (pending != ''):
time.sleep(3)
pending = cyclades.get_server_details(vm_id)['SNF:task_state']
except ClientError as e:
module.fail_json(msg="Cyclades: wait for pending task failed",msg_details=e.message)
def snf_get_image_id(module,cyclades,img_name):
# Obtain list of images
exact = []
similar = []
try:
img_list = cyclades.list_images()
for img_item in img_list:
if (img_item['name'] == img_name):
exact.append(img_item)
if (img_name.lower() in img_item['name'].lower()):
similar.append(img_item)
if ( len(exact) == 1 ):
return exact[0]['id']
elif ( len(exact) > 1 ):
module.fail_json(msg="Found Multiple images with the name: %s" % img_name, exact_matches=exact,similar_matches=similar)
else:
module.fail_json(msg="Found none image with the name: %s" % img_name,similar_matches=similar)
return 0
except ClientError as e:
module.fail_json(msg="Cyclades: Image Listing Error", msg_details=e.message)
# Okeanos default flavors naming convention
def snf_ram_conform(ram):
# Check if user specifed ram using single digit
if (ram==1): ram = 1024
elif (ram==2): ram = 2048
elif (ram==4): ram = 4092
elif (ram==6): ram = 6144
elif (ram==8): ram = 8192
return ram
def snf_spec_flavor_id(module,cyclades):
try:
f_list = cyclades.list_flavors(detail=True)
f_vcpus = module.params['vcpus']
f_ram = module.params['ram']
f_disk = module.params['disk']
f_dtype = module.params['dtype']
f_ram = snf_ram_conform(f_ram)
for f_item in f_list:
if (f_item['SNF:disk_template'] == f_dtype):
if (f_item['vcpus'] == f_vcpus):
if (f_item['ram'] == f_ram):
if (f_item['disk'] == f_disk):
return f_item['id']
module.fail_json(msg="No such flavour found!",vcpus=f_vcpus,ram=f_ram,disk=f_disk,dtype=f_dtype)
except ClientError as e:
module.fail_json(msg="Cyclades: Flavor listing failed!",msg_details = e.message)
# Get flavor id from machine parameters (okeanos default flavors)
def snf_get_flavor_id(module,cyclades,fname):
try:
flist = cyclades.list_flavors()
for fitem in flist:
if (fitem['name'] == fname):
return fitem['id']
module.fail_json(msg = "flavor not found!")
except ClientError as e:
module.fail_json(msg = "Cyclades: Listing Flavors failed!", msg_details = e.message)
def snf_progress_placeholder(state,progress):
return (None,None)
def snf_get_state(module,cyclades,vm_id):
try:
res = cyclades.get_server_details(vm_id)
except ClientError as e :
module.fail_json(msg = "Cyclades: VM state inquiry failed!", msg_details = e.message)
return res
def snf_stop_vm(module,cyclades,vm_id):
try:
res = cyclades.shutdown_server(vm_id)
if (module.params['wait'] == False):
module.exit_json(changed=True,vm_name=module.params['name'],vm_id=vm_id,msg="vm is stopping...")
else:
#cyclades.wait_for(vm_id,'STOPPED',snf_progress_placeholder,1,1200)
snf_wait_pending(module,cyclades,vm_id)
module.exit_json(changed=True,vm_name=module.params['name'],vm_id=vm_id,msg="vm is now stopped!")
except ClientError as e :
module.fail_json(msg = "Cyclades: VM stop action failed!", msg_details = e.message)
def snf_start_vm(module,cyclades,vm_id):
try:
res = cyclades.start_server(vm_id)
if (module.params['wait'] == False):
module.exit_json(changed=True,vm_name=module.params['name'],vm_id=vm_id,msg="vm is booting...")
else:
#cyclades.wait_for(vm_id,'ACTIVE',snf_progress_placeholder,1,1200)
snf_wait_pending(module,cyclades,vm_id)
module.exit_json(changed=True,vm_name=module.params['name'],vm_id=vm_id,msg="vm is now active!")
except ClientError as e :
module.fail_json(msg = "Cyclades: VM start action failed!" , msg_details = e.message)
def snf_search_vm(module,cyclades):
try:
vm_list = cyclades.list_servers()
if (module.params['id']):
query_id = module.params['id']
for vm_item in vm_list:
if (vm_item['id'] == query_id):
return vm_item['id']
else:
query_name = module.params['name']
for vm_item in vm_list:
if (vm_item['name'] == query_name):
return vm_item['id']
except ClientError as e :
module.fail_json(msg = "Cyclades: VM list action failed", msg_details = e.message)
return 0
def snf_create_vm(module,cyclades,network):
vm_name = module.params['name']
if module.params['image_id']:
vm_image = module.params['image_id']
elif module.params['image']:
vm_image = snf_get_image_id(module,cyclades,module.params['image'])
else:
module.fail_json(msg= "Arguments: image or image_id required!")
if module.params['flavor_id']:
vm_flavor = module.params['flavor_id']
elif module.params['flavor']:
vm_flavor = snf_get_flavor_id(module,cyclades)
elif module.params['vcpus']:
vm_flavor = snf_spec_flavor_id(module,cyclades)
else:
module.fail_json(msg= "Arguments: flavor or flavor_id or ( core,ram,hdd and dtype) required!")
vm_keys = module.params['sshkey']
pub_net_arg = module.params['public_network']
# if ssh keys are imported
# based on specific example from excellent synnefo kamaki api documentation
personality = []
if vm_keys:
with open(vm_keys) as f:
personality.append(dict(
contents=b64encode(f.read()),
path='/root/.ssh/authorized_keys',
owner='root', group='root', mode=0600))
personality.append(dict(
contents=b64encode('StrictHostKeyChecking no'),
path='/root/.ssh/config',
owner='root', group='root', mode=0600))
try:
if vm_keys:
if pub_net_arg:
public_network = snf_attach_floating_ip(snf_add_public_network(module,network))
vm_born = cyclades.create_server(vm_name,vm_flavor,vm_image,None,personality,public_network)
else:
vm_born = cyclades.create_server(vm_name,vm_flavor,vm_image,None,personality)
else:
if pub_net_arg:
public_network = snf_attach_floating_ip(snf_add_public_network(module,network))
vm_born = cyclades.create_server(vm_name,vm_flavor,vm_image,None,None,public_network)
else:
vm_born = cyclades.create_server(vm_name,vm_flavor,vm_image)
if (module.params['wait'] == True):
#cyclades.wait_for(vm_born['id'],'ACTIVE',snf_progress_placeholder,1,1200)
snf_wait_pending(module,cyclades,vm_born['id'])
module.exit_json(changed = True, id = vm_born['id'], admin_pass = vm_born['adminPass'], msg="vm is now ready!")
module.exit_json(changed = True, id = vm_born['id'], admin_pass = vm_born['adminPass'], msg="vm is getting ready...")
except ClientError as e:
module.fail_json(msg = "Cyclades: VM creation failed", details=e.message)
def snf_delete_vm(module,cyclades,vm_id):
try:
cyclades.delete_server(vm_id)
if ( module.params['wait'] == True ):
#cyclades.wait_for(vm_id,'DELETED',snf_progress_placeholder,1,1200)
snf_wait_pending(module,cyclades,vm_id)
module.exit_json(changed = True, msg="VM:%s is now deleted!" % vm_id)
module.exit_json(changed = True, msg="deleting vm with id:%s ..." % vm_id)
except ClientError as e:
module.fail_json(msg="Cyclades: Error deleting VM", msg_details = e.message)
def main():
# According to ansible module development documentation...
module = AnsibleModule(
argument_spec = dict(
auth_url = dict(default='https://accounts.okeanos.grnet.gr/identity/v2.0'),
token = dict(required=True),
name = dict(),
id = dict(type='int'),
image = dict(),
flavor = dict(),
image_id = dict(),
flavor_id = dict(),
public_network = dict(),
sshkey = dict(),
wait = dict(type='bool',choices=BOOLEANS,default='yes'),
vcpus = dict(type='int'),
ram = dict(type='int'),
disk = dict(type='int'),
dtype = dict(choices=['drbd','ext_vlmc']),
state = dict(default='present', choices=['absent', 'present','stopped','active'])
),
required_together = (
['vcpus','ram','disk','dtype'],
),
mutually_exclusive = (
['id','name'],
['image','image_id'],
['flavor','flavor_id'],
['flavor','vcpus'],
['flavor','ram'],
['flavor','disk'],
['flavor','dtype'],
['flavor_id','vcpus'],
['flavor_id','ram'],
['flavor_id','disk'],
['flavor_id','dtype'],
),
required_one_of = (
['id','name'],
)
)
auth_url = module.params['auth_url']
auth_token = module.params['token']
# Disable DEBUG logging, important for ansible to parse raw output properly
logging.disable('DEBUG')
# Initialize Astakos Client & Authenticate User
try:
astakos = AstakosClient(auth_url,auth_token)
except ClientError:
module.fail_json(msg = "Astakos Client initialization failed")
# Retrieve Cyclades Endpoint
try:
cyclades_endpoint = astakos.get_service_endpoints('compute')
cyclades_url = cyclades_endpoint['publicURL']
except ClientError:
module.fail_json(msg = "Cyclades api endpoint retrieval failed")
# Initialize Cyclades Client
try:
cyclades = CycladesClient(cyclades_url,auth_token)
except ClientError:
module.fail_json(msg = "Cyclades Client initialization failed")
# Retrieve Network Service Endpoint
try:
network_endpoint = astakos.get_service_endpoints('network')
network_url = network_endpoint['publicURL']
except ClientError:
module.fail_json(msg = "Network api endpoint retrieval failed")
# Initialize Network Client
try:
network = NetworkClient(network_url,auth_token)
except ClientError:
module.fail_json(msg= "Network Client initialization failed")
# Try to Create The Virtual Machine
if (module.params['state'] == 'present'):
vm_id = snf_search_vm(module,cyclades)
if (vm_id):
check_state = snf_get_state(module,cyclades,vm_id)
module.exit_json(changed=False,vm_name=module.params['name'],vm_id=vm_id,status=check_state["status"],msg="vm already present!")
else:
if (module.params['name']):
snf_create_vm(module, cyclades, network)
else:
module.fail_json(msg="VM with id: %s not found! if you wan't to create a new VM you must specify a name instead" % module.params['id'])
elif (module.params['state'] == 'stopped'):
vm_id = snf_search_vm(module,cyclades)
if (vm_id):
check_state = snf_get_state(module,cyclades,vm_id)
if (check_state['status'] == 'ACTIVE'):
snf_stop_vm(module,cyclades,vm_id)
elif (check_state['status'] == 'STOPPED'):
module.exit_json(changed=False,vm_name=module.params['name'],vm_id=vm_id,status=check_state["status"],msg="vm already stopped!")
elif (check_state['status'] == 'BUILD'):
module.fail_json(changed=False,vm_name=module.params['name'],vm_id=vm_id,status=check_state["status"],msg="vm in building process!")
else:
module.fail_json(msg="%s not found" % module.params['name'])
elif (module.params['state'] == 'active'):
vm_id = snf_search_vm(module,cyclades)
if (vm_id):
check_state = snf_get_state(module,cyclades,vm_id)
if (check_state['status'] == 'STOPPED'):
snf_start_vm(module,cyclades,vm_id)
elif (check_state['status'] == 'ACTIVE'):
module.exit_json(changed=False,vm_name=module.params['name'],vm_id=vm_id,status=check_state["status"],msg="vm already active!")
elif (check_state['status'] == 'BUILD'):
module.fail_json(changed=False,vm_name=module.params['name'],vm_id=vm_id,status=check_state["status"],msg="vm in building process!")
else:
module.fail_json(msg="%s not found" % module.params['name'])
elif (module.params['state'] == 'absent'):
vm_id = snf_search_vm(module,cyclades)
if (vm_id):
snf_delete_vm(module,cyclades,vm_id)
else:
module.exit_json(msg="VM with id: %s arleady absent!" % module.params['id'])
module.exit_json(change = False, msg="Nothing to do")
from ansible.module_utils.basic import *
main()