Statistics
| Branch: | Revision:

root / snfOCCI / compute.py @ 4ab8bfab

History | View | Annotate | Download (4.6 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
           
51
            # entity.attributes['occi.compute.hostname'] = SERVER_CONFIG['hostname'] % {'id':info['id']}
52
            info['adminPass']= ""
53
            print info
54
            networkIDs = info['addresses'].keys()
55
                #resource.attributes['occi.compute.hostname'] = SERVER_CONFIG['hostname'] % {'id':int(key)}
56
            if len(networkIDs)>0:    
57
                entity.attributes['occi.compute.hostname'] =  str(info['addresses'][networkIDs[0]][0]['addr'])
58
            else:
59
                entity.attributes['occi.compute.hostname'] = ""
60
               
61
        except (UnboundLocalError, KeyError) as e:
62
            raise HTTPError(406, 'Missing details about compute instance')
63
            
64

    
65
    def retrieve(self, entity, extras):
66
        
67
        #Triggering cyclades to retrieve up to date information
68

    
69
        snf = extras['snf']
70

    
71
        vm_id = int(entity.attributes['occi.core.id'])
72
        vm_info = snf.get_server_details(vm_id)
73
        vm_state = vm_info['status']
74
        
75
        status_dict = {'ACTIVE' : 'active',
76
                       'STOPPED' : 'inactive',
77
                       'ERROR' : 'inactive',
78
                       'BUILD' : 'inactive',
79
                       'DELETED' : 'inactive',
80
                       }
81
        
82
        entity.attributes['occi.compute.state'] = status_dict[vm_state]
83
                
84
        if vm_state == 'ERROR':
85
            raise HTTPError(500, 'ERROR building the compute instance')
86

    
87
        else:
88
            if entity.attributes['occi.compute.state'] == 'inactive':
89
                entity.actions = [START]
90
            if entity.attributes['occi.compute.state'] == 'active': 
91
                entity.actions = [STOP, SUSPEND, RESTART]
92

    
93

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

    
96
        #Deleting compute instance
97

    
98
        snf = extras['snf']
99
        vm_id = int(entity.attributes['occi.core.id'])
100
        snf.delete_server(vm_id)
101

    
102

    
103
    def action(self, entity, action, extras):
104

    
105
        #Triggering action to compute instances
106

    
107
        client = extras['client']
108
        snf = extras['snf']
109

    
110
        vm_id = int(entity.attributes['occi.core.id'])
111
        vm_info = snf.get_server_details(vm_id)
112
        vm_state = vm_info['status']
113

    
114

    
115
        if vm_state == 'ERROR':
116
            raise HTTPError(500, 'ERROR building the compute instance')
117

    
118
        else:
119
            if action not in entity.actions:
120
                raise AttributeError("This action is currently no applicable.")
121
            
122
            elif action == START:
123
                print "Starting VM"
124
                client.start_server(vm_id)
125
                
126
            elif action == STOP:
127
                print "Stopping VM"
128
                client.shutdown_server(vm_id)
129
    
130
            elif action == RESTART:
131
                print "Restarting VM"
132
                snf.reboot_server(vm_id)
133

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