/
nova.py
772 lines (567 loc) · 18.2 KB
/
nova.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
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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
# -*- coding: utf-8 -*-
'''
Module for handling OpenStack Nova calls
:depends: - novaclient Python module
:configuration: This module is not usable until the user, password, tenant, and
auth URL are specified either in a pillar or in the minion's config file.
For example::
keystone.user: admin
keystone.password: verybadpass
keystone.tenant: admin
keystone.auth_url: 'http://127.0.0.1:5000/v2.0/'
# Optional
keystone.region_name: 'regionOne'
If configuration for multiple OpenStack accounts is required, they can be
set up as different configuration profiles:
For example::
openstack1:
keystone.user: admin
keystone.password: verybadpass
keystone.tenant: admin
keystone.auth_url: 'http://127.0.0.1:5000/v2.0/'
openstack2:
keystone.user: admin
keystone.password: verybadpass
keystone.tenant: admin
keystone.auth_url: 'http://127.0.0.2:5000/v2.0/'
With this configuration in place, any of the nova functions can make use of
a configuration profile by declaring it explicitly.
For example::
salt '*' nova.flavor_list profile=openstack1
'''
from __future__ import absolute_import
# Import python libs
import logging
# Import salt libs
try:
import salt.utils.openstack.nova as suon
HAS_NOVA = True
except NameError as exc:
HAS_NOVA = False
# Get logging started
log = logging.getLogger(__name__)
# Function alias to not shadow built-ins
__func_alias__ = {
'list_': 'list'
}
def __virtual__():
'''
Only load this module if nova
is installed on this minion.
'''
return HAS_NOVA
__opts__ = {}
def _auth(profile=None):
'''
Set up nova credentials
'''
if profile:
credentials = __salt__['config.option'](profile)
user = credentials['keystone.user']
password = credentials['keystone.password']
tenant = credentials['keystone.tenant']
auth_url = credentials['keystone.auth_url']
region_name = credentials.get('keystone.region_name', None)
api_key = credentials.get('keystone.api_key', None)
os_auth_system = credentials.get('keystone.os_auth_system', None)
else:
user = __salt__['config.option']('keystone.user')
password = __salt__['config.option']('keystone.password')
tenant = __salt__['config.option']('keystone.tenant')
auth_url = __salt__['config.option']('keystone.auth_url')
region_name = __salt__['config.option']('keystone.region_name')
api_key = __salt__['config.option']('keystone.api_key')
os_auth_system = __salt__['config.option']('keystone.os_auth_system')
kwargs = {
'username': user,
'password': password,
'api_key': api_key,
'project_id': tenant,
'auth_url': auth_url,
'region_name': region_name,
'os_auth_plugin': os_auth_system
}
return suon.SaltNova(**kwargs)
def boot(name, flavor_id=0, image_id=0, profile=None, timeout=300):
'''
Boot (create) a new instance
name
Name of the new instance (must be first)
flavor_id
Unique integer ID for the flavor
image_id
Unique integer ID for the image
timeout
How long to wait, after creating the instance, for the provider to
return information about it (default 300 seconds).
.. versionadded:: 2014.1.0
CLI Example:
.. code-block:: bash
salt '*' nova.boot myinstance flavor_id=4596 image_id=2
The flavor_id and image_id are obtained from nova.flavor_list and
nova.image_list
.. code-block:: bash
salt '*' nova.flavor_list
salt '*' nova.image_list
'''
conn = _auth(profile)
return conn.boot(name, flavor_id, image_id, timeout)
def volume_list(search_opts=None, profile=None):
'''
List storage volumes
search_opts
Dictionary of search options
profile
Profile to use
CLI Example:
.. code-block:: bash
salt '*' nova.volume_list \
search_opts='{"display_name": "myblock"}' \
profile=openstack
'''
conn = _auth(profile)
return conn.volume_list(search_opts=search_opts)
def volume_show(name, profile=None):
'''
Create a block storage volume
name
Name of the volume
profile
Profile to use
CLI Example:
.. code-block:: bash
salt '*' nova.volume_show myblock profile=openstack
'''
conn = _auth(profile)
return conn.volume_show(name)
def volume_create(name, size=100, snapshot=None, voltype=None,
profile=None):
'''
Create a block storage volume
name
Name of the new volume (must be first)
size
Volume size
snapshot
Block storage snapshot id
voltype
Type of storage
profile
Profile to build on
CLI Example:
.. code-block:: bash
salt '*' nova.volume_create myblock size=300 profile=openstack
'''
conn = _auth(profile)
return conn.volume_create(
name,
size,
snapshot,
voltype
)
def volume_delete(name, profile=None):
'''
Destroy the volume
name
Name of the volume
profile
Profile to build on
CLI Example:
.. code-block:: bash
salt '*' nova.volume_delete myblock profile=openstack
'''
conn = _auth(profile)
return conn.volume_delete(name)
def volume_detach(name,
profile=None,
timeout=300):
'''
Attach a block storage volume
name
Name of the new volume to attach
server_name
Name of the server to detach from
profile
Profile to build on
CLI Example:
.. code-block:: bash
salt '*' nova.volume_detach myblock profile=openstack
'''
conn = _auth(profile)
return conn.volume_detach(
name,
timeout
)
def volume_attach(name,
server_name,
device='/dev/xvdb',
profile=None,
timeout=300):
'''
Attach a block storage volume
name
Name of the new volume to attach
server_name
Name of the server to attach to
device
Name of the device on the server
profile
Profile to build on
CLI Example:
.. code-block:: bash
salt '*' nova.volume_attach myblock slice.example.com profile=openstack
salt '*' nova.volume_attach myblock server.example.com \
device='/dev/xvdb' profile=openstack
'''
conn = _auth(profile)
return conn.volume_attach(
name,
server_name,
device,
timeout
)
def suspend(instance_id, profile=None):
'''
Suspend an instance
instance_id
ID of the instance to be suspended
CLI Example:
.. code-block:: bash
salt '*' nova.suspend 1138
'''
conn = _auth(profile)
return conn.suspend(instance_id)
def resume(instance_id, profile=None):
'''
Resume an instance
instance_id
ID of the instance to be resumed
CLI Example:
.. code-block:: bash
salt '*' nova.resume 1138
'''
conn = _auth(profile)
return conn.resume(instance_id)
def lock(instance_id, profile=None):
'''
Lock an instance
instance_id
ID of the instance to be locked
CLI Example:
.. code-block:: bash
salt '*' nova.lock 1138
'''
conn = _auth(profile)
return conn.lock(instance_id)
def delete(instance_id, profile=None):
'''
Delete an instance
instance_id
ID of the instance to be deleted
CLI Example:
.. code-block:: bash
salt '*' nova.delete 1138
'''
conn = _auth(profile)
return conn.delete(instance_id)
def flavor_list(profile=None):
'''
Return a list of available flavors (nova flavor-list)
CLI Example:
.. code-block:: bash
salt '*' nova.flavor_list
'''
conn = _auth(profile)
return conn.flavor_list()
def flavor_create(name, # pylint: disable=C0103
flavor_id=0, # pylint: disable=C0103
ram=0,
disk=0,
vcpus=1,
profile=None):
'''
Add a flavor to nova (nova flavor-create). The following parameters are
required:
name
Name of the new flavor (must be first)
flavor_id
Unique integer ID for the new flavor
ram
Memory size in MB
disk
Disk size in GB
vcpus
Number of vcpus
CLI Example:
.. code-block:: bash
salt '*' nova.flavor_create myflavor flavor_id=6 \
ram=4096 disk=10 vcpus=1
'''
conn = _auth(profile)
return conn.flavor_create(
name,
flavor_id,
ram,
disk,
vcpus
)
def flavor_delete(flavor_id, profile=None): # pylint: disable=C0103
'''
Delete a flavor from nova by id (nova flavor-delete)
CLI Example:
.. code-block:: bash
salt '*' nova.flavor_delete 7
'''
conn = _auth(profile)
return conn.flavor_delete(flavor_id)
def keypair_list(profile=None):
'''
Return a list of available keypairs (nova keypair-list)
CLI Example:
.. code-block:: bash
salt '*' nova.keypair_list
'''
conn = _auth(profile)
return conn.keypair_list()
def keypair_add(name, pubfile=None, pubkey=None, profile=None):
'''
Add a keypair to nova (nova keypair-add)
CLI Examples:
.. code-block:: bash
salt '*' nova.keypair_add mykey pubfile='/home/myuser/.ssh/id_rsa.pub'
salt '*' nova.keypair_add mykey pubkey='ssh-rsa <key> myuser@mybox'
'''
conn = _auth(profile)
return conn.keypair_add(
name,
pubfile,
pubkey
)
def keypair_delete(name, profile=None):
'''
Add a keypair to nova (nova keypair-delete)
CLI Example:
.. code-block:: bash
salt '*' nova.keypair_delete mykey'
'''
conn = _auth(profile)
return conn.keypair_delete(name)
def image_list(name=None, profile=None):
'''
Return a list of available images (nova images-list + nova image-show)
If a name is provided, only that image will be displayed.
CLI Examples:
.. code-block:: bash
salt '*' nova.image_list
salt '*' nova.image_list myimage
'''
conn = _auth(profile)
return conn.image_list(name)
def image_meta_set(image_id=None,
name=None,
profile=None,
**kwargs): # pylint: disable=C0103
'''
Sets a key=value pair in the metadata for an image (nova image-meta set)
CLI Examples:
.. code-block:: bash
salt '*' nova.image_meta_set 6f52b2ff-0b31-4d84-8fd1-af45b84824f6 \
cheese=gruyere
salt '*' nova.image_meta_set name=myimage salad=pasta beans=baked
'''
conn = _auth(profile)
return conn.image_meta_set(
image_id,
name,
**kwargs
)
def image_meta_delete(image_id=None, # pylint: disable=C0103
name=None,
keys=None,
profile=None):
'''
Delete a key=value pair from the metadata for an image
(nova image-meta set)
CLI Examples:
.. code-block:: bash
salt '*' nova.image_meta_delete \
6f52b2ff-0b31-4d84-8fd1-af45b84824f6 keys=cheese
salt '*' nova.image_meta_delete name=myimage keys=salad,beans
'''
conn = _auth(profile)
return conn.image_meta_delete(
image_id,
name,
keys
)
def list_(profile=None):
'''
To maintain the feel of the nova command line, this function simply calls
the server_list function.
'''
return server_list(profile=profile)
def server_list(profile=None):
'''
Return list of active servers
CLI Example:
.. code-block:: bash
salt '*' nova.show
'''
conn = _auth(profile)
return conn.server_list()
def show(server_id, profile=None):
'''
To maintain the feel of the nova command line, this function simply calls
the server_show function.
CLI Example:
.. code-block:: bash
salt '*' nova.show
'''
return server_show(server_id, profile)
def server_list_detailed(profile=None):
'''
Return detailed list of active servers
CLI Example:
.. code-block:: bash
salt '*' nova.server_list_detailed
'''
conn = _auth(profile)
return conn.server_list_detailed()
def server_show(server_id, profile=None):
'''
Return detailed information for an active server
CLI Example:
.. code-block:: bash
salt '*' nova.server_show <server_id>
'''
conn = _auth(profile)
return conn.server_show(server_id)
def secgroup_create(name, description, profile=None):
'''
Add a secgroup to nova (nova secgroup-create)
CLI Example:
.. code-block:: bash
salt '*' nova.secgroup_create mygroup 'This is my security group'
'''
conn = _auth(profile)
return conn.secgroup_create(name, description)
def secgroup_delete(name, profile=None):
'''
Delete a secgroup to nova (nova secgroup-delete)
CLI Example:
.. code-block:: bash
salt '*' nova.secgroup_delete mygroup
'''
conn = _auth(profile)
return conn.secgroup_delete(name)
def secgroup_list(profile=None):
'''
Return a list of available security groups (nova items-list)
CLI Example:
.. code-block:: bash
salt '*' nova.secgroup_list
'''
conn = _auth(profile)
return conn.secgroup_list()
def server_by_name(name, profile=None):
'''
Return information about a server
name
Server Name
CLI Example:
.. code-block:: bash
salt '*' nova.server_by_name myserver profile=openstack
'''
conn = _auth(profile)
return conn.server_by_name(name)
#The following is a list of functions that need to be incorporated in the
#nova module. This list should be updated as functions are added.
#
#absolute-limits Print a list of absolute limits for a user
#actions Retrieve server actions.
#add-fixed-ip Add new IP address to network.
#add-floating-ip Add a floating IP address to a server.
#aggregate-add-host Add the host to the specified aggregate.
#aggregate-create Create a new aggregate with the specified details.
#aggregate-delete Delete the aggregate by its id.
#aggregate-details Show details of the specified aggregate.
#aggregate-list Print a list of all aggregates.
#aggregate-remove-host
# Remove the specified host from the specified aggregate.
#aggregate-set-metadata
# Update the metadata associated with the aggregate.
#aggregate-update Update the aggregate's name and optionally
# availability zone.
#cloudpipe-create Create a cloudpipe instance for the given project
#cloudpipe-list Print a list of all cloudpipe instances.
#console-log Get console log output of a server.
#credentials Show user credentials returned from auth
#describe-resource Show details about a resource
#diagnostics Retrieve server diagnostics.
#dns-create Create a DNS entry for domain, name and ip.
#dns-create-private-domain
# Create the specified DNS domain.
#dns-create-public-domain
# Create the specified DNS domain.
#dns-delete Delete the specified DNS entry.
#dns-delete-domain Delete the specified DNS domain.
#dns-domains Print a list of available dns domains.
#dns-list List current DNS entries for domain and ip or domain
# and name.
#endpoints Discover endpoints that get returned from the
# authenticate services
#floating-ip-create Allocate a floating IP for the current tenant.
#floating-ip-delete De-allocate a floating IP.
#floating-ip-list List floating ips for this tenant.
#floating-ip-pool-list
# List all floating ip pools.
#get-vnc-console Get a vnc console to a server.
#host-action Perform a power action on a host.
#host-update Update host settings.
#image-create Create a new image by taking a snapshot of a running
# server.
#image-delete Delete an image.
#live-migration Migrates a running instance to a new machine.
#meta Set or Delete metadata on a server.
#migrate Migrate a server.
#pause Pause a server.
#rate-limits Print a list of rate limits for a user
#reboot Reboot a server.
#rebuild Shutdown, re-image, and re-boot a server.
#remove-fixed-ip Remove an IP address from a server.
#remove-floating-ip Remove a floating IP address from a server.
#rename Rename a server.
#rescue Rescue a server.
#resize Resize a server.
#resize-confirm Confirm a previous resize.
#resize-revert Revert a previous resize (and return to the previous
# VM).
#root-password Change the root password for a server.
#secgroup-add-group-rule
# Add a source group rule to a security group.
#secgroup-add-rule Add a rule to a security group.
#secgroup-delete-group-rule
# Delete a source group rule from a security group.
#secgroup-delete-rule
# Delete a rule from a security group.
#secgroup-list-rules
# List rules for a security group.
#ssh SSH into a server.
#unlock Unlock a server.
#unpause Unpause a server.
#unrescue Unrescue a server.
#usage-list List usage data for all tenants
#volume-list List all the volumes.
#volume-snapshot-create
# Add a new snapshot.
#volume-snapshot-delete
# Remove a snapshot.
#volume-snapshot-list
# List all the snapshots.
#volume-snapshot-show
# Show details about a snapshot.
#volume-type-create Create a new volume type.
#volume-type-delete Delete a specific flavor
#volume-type-list Print a list of available 'volume types'.
#x509-create-cert Create x509 cert for a user in tenant
#x509-get-root-cert Fetches the x509 root cert.