Statistics
| Branch: | Revision:

root / snfOCCI / compute.py @ b8e8a8f9

History | View | Annotate | Download (4.1 kB)

1
from snfOCCI.config import SERVER_CONFIG
2

    
3
from occi.backend import ActionBackend, KindBackend
4
from occi.extensions.infrastructure import START, STOP, SUSPEND, RESTART
5
from occi.exceptions import HTTPError
6

    
7

    
8
#Compute Backend for snf-occi-server
9

    
10
class MyBackend(KindBackend, ActionBackend):
11

    
12
    # Updating and Replacing compute instances not supported by Cyclades
13

    
14
    def update(self, old, new, extras):
15
        raise HTTPError(501, "Update is currently no applicable")
16

    
17
    def replace(self, old, new, extras):
18
        raise HTTPError(501, "Replace is currently no applicable")
19

    
20

    
21
class ComputeBackend(MyBackend):
22
    '''
23
    Backend for Cyclades/Openstack compute instances
24
    '''
25

    
26
    def create(self, entity, extras):
27

    
28
        #Creating new compute instance
29
        
30
        try:
31

    
32
            snf = extras['snf']
33

    
34
            for mixin in entity.mixins:
35
                if mixin.related[0].term == 'os_tpl':
36
                    image_id = mixin.attributes['occi.core.id']
37
                if mixin.related[0].term == 'resource_tpl':
38
                    flavor = mixin
39
                    flavor_id = mixin.attributes['occi.core.id']
40

    
41
            vm_name = entity.attributes['occi.core.title']
42
            info = snf.create_server(vm_name, flavor_id, image_id)
43

    
44
            entity.actions = [START]
45
            entity.attributes['occi.compute.state'] = 'inactive'
46
            entity.attributes['occi.core.id'] = str(info['id'])
47
            entity.attributes['occi.compute.architecture'] = SERVER_CONFIG['compute_arch']
48
            entity.attributes['occi.compute.cores'] = flavor.attributes['occi.compute.cores']
49
            entity.attributes['occi.compute.memory'] = flavor.attributes['occi.compute.memory']
50
            entity.attributes['occi.compute.hostname'] = SERVER_CONFIG['hostname'] % {'id':info['id']}
51

    
52
        except (UnboundLocalError, KeyError) as e:
53
            raise HTTPError(406, 'Missing details about compute instance')
54
            
55

    
56
    def retrieve(self, entity, extras):
57
        
58
        #Triggering cyclades to retrieve up to date information
59

    
60
        snf = extras['snf']
61

    
62
        vm_id = int(entity.attributes['occi.core.id'])
63
        vm_info = snf.get_server_details(vm_id)
64
        vm_state = vm_info['status']
65
        
66
        status_dict = {'ACTIVE' : 'active',
67
                       'STOPPED' : 'inactive',
68
                       'ERROR' : 'inactive',
69
                       'BUILD' : 'inactive',
70
                       'DELETED' : 'inactive',
71
                       }
72
        
73
        entity.attributes['occi.compute.state'] = status_dict[vm_state]
74
                
75
        if vm_state == 'ERROR':
76
            raise HTTPError(500, 'ERROR building the compute instance')
77

    
78
        else:
79
            if entity.attributes['occi.compute.state'] == 'inactive':
80
                entity.actions = [START]
81
            if entity.attributes['occi.compute.state'] == 'active': 
82
                entity.actions = [STOP, SUSPEND, RESTART]
83

    
84

    
85
    def delete(self, entity, extras):
86

    
87
        #Deleting compute instance
88

    
89
        snf = extras['snf']
90
        vm_id = int(entity.attributes['occi.core.id'])
91
        snf.delete_server(vm_id)
92

    
93

    
94
    def action(self, entity, action, extras):
95

    
96
        #Triggering action to compute instances
97

    
98
        client = extras['client']
99
        snf = extras['snf']
100

    
101
        vm_id = int(entity.attributes['occi.core.id'])
102
        vm_info = snf.get_server_details(vm_id)
103
        vm_state = vm_info['status']
104

    
105

    
106
        if vm_state == 'ERROR':
107
            raise HTTPError(500, 'ERROR building the compute instance')
108

    
109
        else:
110
            if action not in entity.actions:
111
                raise AttributeError("This action is currently no applicable.")
112
            
113
            elif action == START:
114
                print "Starting VM"
115
                client.start_server(vm_id)
116
                
117
            elif action == STOP:
118
                print "Stopping VM"
119
                client.shutdown_server(vm_id)
120
    
121
            elif action == RESTART:
122
                print "Restarting VM"
123
                snf.reboot_server(vm_id)
124

    
125
            elif action == SUSPEND:
126
                raise HTTPError(501, "This actions is currently no applicable")