1
|
|
|
from libcloud.compute.base import NodeSize |
2
|
|
|
from libcloud.compute.base import NodeImage |
3
|
|
|
from libcloud.compute.base import NodeLocation |
4
|
|
|
|
5
|
|
|
from lib.actions import BaseAction |
6
|
|
|
|
7
|
|
|
__all__ = [ |
8
|
|
|
'CreateVMAction' |
9
|
|
|
] |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class CreateVMAction(BaseAction): |
13
|
|
|
api_type = 'compute' |
14
|
|
|
|
15
|
|
|
def run(self, credentials, name, size_id=None, image_id=None, size_name=None, image_name=None, |
16
|
|
|
location_id=None): |
17
|
|
|
driver = self._get_driver_for_credentials(credentials=credentials) |
18
|
|
|
location = NodeLocation(id=location_id, name=None, |
19
|
|
|
country=None, driver=driver) |
20
|
|
|
|
21
|
|
|
if (not size_id and not size_name) or (size_id and size_name): |
22
|
|
|
raise ValueError('Either "size_id" or "size_name" needs to be provided') |
23
|
|
|
|
24
|
|
|
if (not image_id and not image_name) or (image_id and image_name): |
25
|
|
|
raise ValueError('Either "image_id" or "image_name" needs to be provided') |
26
|
|
|
|
27
|
|
|
if size_id is not None: |
28
|
|
|
size = NodeSize(id=size_id, name=None, |
29
|
|
|
ram=None, disk=None, bandwidth=None, |
30
|
|
|
price=None, driver=driver) |
31
|
|
|
elif size_name is not None: |
32
|
|
|
size = [s for s in driver.list_sizes() if size_name in s.name][0] |
33
|
|
|
|
34
|
|
|
if image_id is not None: |
35
|
|
|
image = NodeImage(id=image_id, name=None, |
36
|
|
|
driver=driver) |
37
|
|
|
elif image_name is not None: |
38
|
|
|
image = [i for i in driver.list_images() if |
39
|
|
|
image_name in i['extra'].get('displaytext', image.name)][0] |
40
|
|
|
|
41
|
|
|
kwargs = {'name': name, 'size': size, 'image': image} |
42
|
|
|
|
43
|
|
|
if location_id: |
44
|
|
|
kwargs['location'] = location |
45
|
|
|
|
46
|
|
|
self.logger.info('Creating node...') |
47
|
|
|
|
48
|
|
|
node = driver.create_node(**kwargs) |
49
|
|
|
|
50
|
|
|
self.logger.info('Node successfully created: %s' % (node)) |
51
|
|
|
return node |
52
|
|
|
|