Completed
Pull Request — master (#415)
by Anthony
02:06
created

CreateVMMcp2Action   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %
Metric Value
dl 0
loc 43
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
B run() 0 41 5
1
from libcloud.compute.base import NodeAuthPassword, NodeState
2
from libcloud.common.dimensiondata import DimensionDataServerCpuSpecification
3
from lib import actions
4
from time import sleep
5
6
__all__ = [
7
    'CreateVMMcp2Action',
8
]
9
10
11
class CreateVMMcp2Action(actions.BaseAction):
12
13
    def run(self, region, location, network_domain_id,
14
            name,
15
            vlan_id, image_name,
16
            description, is_started, password,
17
            memory_gb, cpu_count, cpu_speed, cores_per_socket):
18
        driver = self._get_compute_driver(region)
19
        root_pw = NodeAuthPassword(password)
20
        location = driver.ex_get_location_by_id(location)
21
22
        images = driver.list_images(location=location)
23
24
        image = list(filter(lambda x: x.name == image_name,
25
                            images))[0]
26
        network_domain = driver.ex_get_network_domain(network_domain_id)
27
        vlan = driver.ex_get_vlan(vlan_id)
28
        cpu = None
29
        if cpu_count is not None:
30
            cpu = DimensionDataServerCpuSpecification(
31
                cpu_count=cpu_count,
32
                cores_per_socket=cores_per_socket,
33
                performance=cpu_speed
34
            )
35
36
        node = driver.create_node(name=name, image=image,
37
                                  auth=root_pw,
38
                                  ex_description=description,
39
                                  ex_network_domain=network_domain,
40
                                  ex_vlan=vlan,
41
                                  ex_cpu_specification=cpu,
42
                                  ex_memory_gb=memory_gb,
43
                                  ex_is_started=is_started)
44
        timeout = 1200  # 20 minutes
45
        poll_interval = 5
46
        cnt = 0
47
        while cnt < timeout / poll_interval:
48
            result = driver.ex_get_node_by_id(node.id)
49
            if result.state is NodeState.RUNNING:
50
                return self.resultsets.formatter(result)
51
            sleep(poll_interval)
52
            cnt += 1
53
        raise Exception("Timed out creating server")
54