Revision 592e8117 snf-occi-server.py
b/snf-occi-server.py | ||
---|---|---|
38 | 38 |
|
39 | 39 |
class ComputeBackend(MyBackend): |
40 | 40 |
''' |
41 |
A Backend for compute instances.
|
|
41 |
Backend for Cyclades/Openstack compute instances
|
|
42 | 42 |
''' |
43 | 43 |
|
44 | 44 |
def create(self, entity, extras): |
45 |
entity.attributes['occi.compute.state'] = 'active' |
|
46 |
entity.actions = [STOP, SUSPEND, RESTART] |
|
47 |
|
|
45 |
|
|
48 | 46 |
for mixin in entity.mixins: |
49 |
print mixin.term |
|
50 |
print mixin.attributes |
|
51 |
if mixin.related.term == 'os_tpl': |
|
47 |
if mixin.related[0].term == 'os_tpl': |
|
48 |
image = mixin |
|
52 | 49 |
image_id = mixin.attributes['occi.core.id'] |
53 |
if mixin.related.term == 'resource_tpl': |
|
50 |
if mixin.related[0].term == 'resource_tpl': |
|
51 |
flavor = mixin |
|
54 | 52 |
flavor_id = mixin.attributes['occi.core.id'] |
53 |
|
|
54 |
entity.attributes['occi.compute.state'] = 'active' |
|
55 |
entity.actions = [STOP, SUSPEND, RESTART] |
|
56 |
|
|
57 |
#Registry identifier is the uuid key occi.handler assigns |
|
58 |
#attribute 'occi.core.id' will be the snf-server id |
|
55 | 59 |
|
56 | 60 |
snf = ComputeClient(Config()) |
57 | 61 |
vm_name = entity.attributes['occi.compute.hostname'] |
58 |
snf.create_server(vm_name, flavor_id, image_id) |
|
59 |
|
|
60 |
print('Creating the virtual machine with id: ' + entity.identifier)
|
|
61 |
|
|
62 |
info = snf.create_server(vm_name, flavor_id, image_id)
|
|
63 |
entity.attributes['occi.core.id'] = str(info['id']) |
|
64 |
entity.attributes['occi.compute.cores'] = flavor.attributes['occi.compute.cores']
|
|
65 |
entity.attributes['occi.compute.memory'] = flavor.attributes['occi.compute.memory'] |
|
62 | 66 |
|
63 | 67 |
def retrieve(self, entity, extras): |
68 |
|
|
64 | 69 |
# triggering cyclades to retrieve up to date information |
65 | 70 |
|
71 |
snf = ComputeClient(Config()) |
|
72 |
|
|
73 |
vm_id = int(entity.attributes['occi.core.id']) |
|
74 |
vm_info = snf.get_server_details(vm_id) |
|
75 |
vm_state = vm_info['status'] |
|
76 |
|
|
77 |
status_dict = {'ACTIVE' : 'active', |
|
78 |
'STOPPED' : 'inactive', |
|
79 |
'ERROR' : 'inactive', |
|
80 |
'BUILD' : 'inactive', |
|
81 |
'DELETED' : 'inactive', |
|
82 |
} |
|
83 |
|
|
84 |
entity.attributes['occi.compute.state'] = status_dict[vm_state] |
|
85 |
|
|
66 | 86 |
if entity.attributes['occi.compute.state'] == 'inactive': |
67 | 87 |
entity.actions = [START] |
68 | 88 |
if entity.attributes['occi.compute.state'] == 'active': |
... | ... | |
70 | 90 |
if entity.attributes['occi.compute.state'] == 'suspended': |
71 | 91 |
entity.actions = [START] |
72 | 92 |
|
93 |
|
|
94 |
|
|
95 |
|
|
73 | 96 |
def delete(self, entity, extras): |
74 |
# call the management framework to delete this compute instance... |
|
75 |
print('Removing representation of virtual machine with id: ' |
|
76 |
+ entity.identifier) |
|
97 |
# delete vm with vm_id = entity.attributes['occi.core.id'] |
|
98 |
snf = ComputeClient(Config()) |
|
99 |
vm_id = int(entity.attributes['occi.core.id']) |
|
100 |
snf.delete_server(vm_id) |
|
101 |
|
|
77 | 102 |
|
78 | 103 |
def action(self, entity, action, extras): |
79 | 104 |
if action not in entity.actions: |
... | ... | |
108 | 133 |
def __call__(self, environ, response): |
109 | 134 |
sec_obj = {'username': 'password'} |
110 | 135 |
|
111 |
|
|
136 |
|
|
112 | 137 |
#Refresh registry entries with current Cyclades state |
113 | 138 |
snf = ComputeClient(Config()) |
114 | 139 |
|
140 |
''' |
|
115 | 141 |
images = snf.list_images() |
116 | 142 |
for image in images: |
117 | 143 |
IMAGE_ATTRIBUTES = {'occi.core.id': str(image['id'])} |
118 |
IMAGE = Mixin("http://schemas.ogf.org/occi/infrastructure#", str(image['name']), OS_TEMPLATE, attributes = IMAGE_ATTRIBUTES)
|
|
144 |
IMAGE = Mixin("http://schemas.ogf.org/occi/infrastructure#", str(image['name']), [OS_TEMPLATE], attributes = IMAGE_ATTRIBUTES)
|
|
119 | 145 |
self.register_backend(IMAGE, MixinBackend()) |
120 | 146 |
|
121 | 147 |
flavors = snf.list_flavors() |
... | ... | |
125 | 151 |
'occi.compute.memory': snf.get_flavor_details(flavor['id'])['ram'], |
126 | 152 |
'occi.storage.size': snf.get_flavor_details(flavor['id'])['disk'], |
127 | 153 |
} |
128 |
FLAVOR = Mixin("http://schemas.ogf.org/occi/infrastructure#", str(flavor['name']), RESOURCE_TEMPLATE, attributes = FLAVOR_ATTRIBUTES)
|
|
154 |
FLAVOR = Mixin("http://schemas.ogf.org/occi/infrastructure#", str(flavor['name']), [RESOURCE_TEMPLATE], attributes = FLAVOR_ATTRIBUTES)
|
|
129 | 155 |
self.register_backend(FLAVOR, MixinBackend()) |
130 |
|
|
156 |
''' |
|
157 |
|
|
131 | 158 |
#TODO show only current VM instances |
132 |
|
|
133 | 159 |
|
134 | 160 |
return self._call_occi(environ, response, security=sec_obj, foo=None) |
135 | 161 |
|
... | ... | |
147 | 173 |
APP.register_backend(RESOURCE_TEMPLATE, MixinBackend()) |
148 | 174 |
APP.register_backend(OS_TEMPLATE, MixinBackend()) |
149 | 175 |
|
176 |
snf = ComputeClient(Config()) |
|
177 |
|
|
178 |
images = snf.list_images() |
|
179 |
for image in images: |
|
180 |
IMAGE_ATTRIBUTES = {'occi.core.id': str(image['id'])} |
|
181 |
IMAGE = Mixin("http://schemas.ogf.org/occi/infrastructure#", str(image['name']), [OS_TEMPLATE], attributes = IMAGE_ATTRIBUTES) |
|
182 |
APP.register_backend(IMAGE, MixinBackend()) |
|
183 |
|
|
184 |
flavors = snf.list_flavors() |
|
185 |
for flavor in flavors: |
|
186 |
FLAVOR_ATTRIBUTES = {'occi.core.id': flavor['id'], |
|
187 |
'occi.compute.cores': snf.get_flavor_details(flavor['id'])['cpu'], |
|
188 |
'occi.compute.memory': snf.get_flavor_details(flavor['id'])['ram'], |
|
189 |
'occi.storage.size': snf.get_flavor_details(flavor['id'])['disk'], |
|
190 |
} |
|
191 |
FLAVOR = Mixin("http://schemas.ogf.org/occi/infrastructure#", str(flavor['name']), [RESOURCE_TEMPLATE], attributes = FLAVOR_ATTRIBUTES) |
|
192 |
APP.register_backend(FLAVOR, MixinBackend()) |
|
193 |
|
|
150 | 194 |
|
151 | 195 |
VALIDATOR_APP = validator(APP) |
152 | 196 |
HTTPD = make_server('', 8888, VALIDATOR_APP) |
Also available in: Unified diff