root / kamaki / clients / cyclades / __init__.py @ c2b5da2f
History | View | Annotate | Download (9.6 kB)
1 |
# Copyright 2011-2013 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
from sys import stdout |
35 |
from time import sleep |
36 |
|
37 |
from kamaki.clients.cyclades.rest_api import CycladesRestClient |
38 |
from kamaki.clients import ClientError |
39 |
|
40 |
|
41 |
class CycladesClient(CycladesRestClient): |
42 |
"""GRNet Cyclades API client"""
|
43 |
|
44 |
def start_server(self, server_id): |
45 |
"""Submit a startup request
|
46 |
|
47 |
:param server_id: integer (str or int)
|
48 |
"""
|
49 |
req = {'start': {}}
|
50 |
self.servers_post(server_id, 'action', json_data=req, success=202) |
51 |
|
52 |
def shutdown_server(self, server_id): |
53 |
"""Submit a shutdown request
|
54 |
|
55 |
:param server_id: integer (str or int)
|
56 |
"""
|
57 |
req = {'shutdown': {}}
|
58 |
self.servers_post(server_id, 'action', json_data=req, success=202) |
59 |
|
60 |
def get_server_console(self, server_id): |
61 |
"""
|
62 |
:param server_id: integer (str or int)
|
63 |
|
64 |
:returns: (dict) info to set a VNC connection to VM
|
65 |
"""
|
66 |
req = {'console': {'type': 'vnc'}} |
67 |
r = self.servers_post(server_id, 'action', json_data=req, success=200) |
68 |
return r.json['console'] |
69 |
|
70 |
def get_firewall_profile(self, server_id): |
71 |
"""
|
72 |
:param server_id: integer (str or int)
|
73 |
|
74 |
:returns: (str) ENABLED | DISABLED | PROTECTED
|
75 |
|
76 |
:raises ClientError: 520 No Firewall Profile
|
77 |
"""
|
78 |
r = self.get_server_details(server_id)
|
79 |
try:
|
80 |
return r['attachments']['values'][0]['firewallProfile'] |
81 |
except KeyError: |
82 |
raise ClientError(
|
83 |
'No Firewall Profile',
|
84 |
details='Server %s is missing a firewall profile' % server_id)
|
85 |
|
86 |
def set_firewall_profile(self, server_id, profile): |
87 |
"""Set the firewall profile for the public interface of a server
|
88 |
|
89 |
:param server_id: integer (str or int)
|
90 |
|
91 |
:param profile: (str) ENABLED | DISABLED | PROTECTED
|
92 |
"""
|
93 |
req = {'firewallProfile': {'profile': profile}} |
94 |
self.servers_post(server_id, 'action', json_data=req, success=202) |
95 |
|
96 |
def list_servers(self, detail=False, changes_since=None): |
97 |
"""
|
98 |
:param detail: (bool) append full server details to each item if true
|
99 |
|
100 |
:param changes_since: (date)
|
101 |
|
102 |
:returns: list of server ids and names
|
103 |
"""
|
104 |
detail = 'detail' if detail else '' |
105 |
r = self.servers_get(command=detail, changes_since=changes_since)
|
106 |
return r.json['servers']['values'] |
107 |
|
108 |
def list_server_nics(self, server_id): |
109 |
"""
|
110 |
:param server_id: integer (str or int)
|
111 |
|
112 |
:returns: (dict) network interface connections
|
113 |
"""
|
114 |
r = self.servers_get(server_id, 'ips') |
115 |
return r.json['addresses']['values'] |
116 |
|
117 |
def get_server_stats(self, server_id): |
118 |
"""
|
119 |
:param server_id: integer (str or int)
|
120 |
|
121 |
:returns: (dict) auto-generated graphs of statistics (urls)
|
122 |
"""
|
123 |
r = self.servers_get(server_id, 'stats') |
124 |
return r.json['stats'] |
125 |
|
126 |
def list_networks(self, detail=False): |
127 |
"""
|
128 |
:param detail: (bool)
|
129 |
|
130 |
:returns: (list) id,name if not detail else full info per network
|
131 |
"""
|
132 |
detail = 'detail' if detail else '' |
133 |
r = self.networks_get(command=detail)
|
134 |
return r.json['networks']['values'] |
135 |
|
136 |
def list_network_nics(self, network_id): |
137 |
"""
|
138 |
:param network_id: integer (str or int)
|
139 |
|
140 |
:returns: (list)
|
141 |
"""
|
142 |
r = self.networks_get(network_id=network_id)
|
143 |
return r.json['network']['attachments']['values'] |
144 |
|
145 |
def create_network( |
146 |
self, name,
|
147 |
cidr=None, gateway=None, type=None, dhcp=False): |
148 |
"""
|
149 |
:param name: (str)
|
150 |
|
151 |
:param cidr: (str)
|
152 |
|
153 |
:param geteway: (str)
|
154 |
|
155 |
:param type: (str)
|
156 |
|
157 |
:param dhcp: (bool)
|
158 |
|
159 |
:returns: (dict) network detailed info
|
160 |
"""
|
161 |
net = dict(name=name)
|
162 |
if cidr:
|
163 |
net['cidr'] = cidr
|
164 |
if gateway:
|
165 |
net['gateway'] = gateway
|
166 |
if type: |
167 |
net['type'] = type |
168 |
net['dhcp'] = True if dhcp else False |
169 |
req = dict(network=net)
|
170 |
r = self.networks_post(json_data=req, success=202) |
171 |
return r.json['network'] |
172 |
|
173 |
def get_network_details(self, network_id): |
174 |
"""
|
175 |
:param network_id: integer (str or int)
|
176 |
|
177 |
:returns: (dict)
|
178 |
"""
|
179 |
r = self.networks_get(network_id=network_id)
|
180 |
return r.json['network'] |
181 |
|
182 |
def update_network_name(self, network_id, new_name): |
183 |
"""
|
184 |
:param network_id: integer (str or int)
|
185 |
|
186 |
:param new_name: (str)
|
187 |
"""
|
188 |
req = {'network': {'name': new_name}} |
189 |
self.networks_put(network_id=network_id, json_data=req)
|
190 |
|
191 |
def delete_network(self, network_id): |
192 |
"""
|
193 |
:param network_id: integer (str or int)
|
194 |
|
195 |
:raises ClientError: 421 Network in use
|
196 |
"""
|
197 |
try:
|
198 |
self.networks_delete(network_id)
|
199 |
except ClientError as err: |
200 |
if err.status == 421: |
201 |
err.details = [ |
202 |
'Network may be still connected to at least one server']
|
203 |
raise err
|
204 |
|
205 |
def connect_server(self, server_id, network_id): |
206 |
""" Connect a server to a network
|
207 |
|
208 |
:param server_id: integer (str or int)
|
209 |
|
210 |
:param network_id: integer (str or int)
|
211 |
"""
|
212 |
req = {'add': {'serverRef': server_id}} |
213 |
self.networks_post(network_id, 'action', json_data=req) |
214 |
|
215 |
def disconnect_server(self, server_id, nic_id): |
216 |
"""
|
217 |
:param server_id: integer (str or int)
|
218 |
|
219 |
:param nic_id: (str)
|
220 |
|
221 |
:returns: (int) the number of nics disconnected
|
222 |
"""
|
223 |
vm_nets = self.list_server_nics(server_id)
|
224 |
num_of_disconnections = 0
|
225 |
for (nic_id, network_id) in [( |
226 |
net['id'],
|
227 |
net['network_id']) for net in vm_nets if nic_id == net['id']]: |
228 |
req = {'remove': {'attachment': '%s' % nic_id}} |
229 |
self.networks_post(network_id, 'action', json_data=req) |
230 |
num_of_disconnections += 1
|
231 |
return num_of_disconnections
|
232 |
|
233 |
def disconnect_network_nics(self, netid): |
234 |
"""
|
235 |
:param netid: integer (str or int)
|
236 |
"""
|
237 |
for nic in self.list_network_nics(netid): |
238 |
req = dict(remove=dict(attachment=nic)) |
239 |
self.networks_post(netid, 'action', json_data=req) |
240 |
|
241 |
def wait_server( |
242 |
self,
|
243 |
server_id, |
244 |
current_status='BUILD',
|
245 |
delay=0.5,
|
246 |
max_wait=128,
|
247 |
wait_cb=None):
|
248 |
"""Wait for server while its status is current_status
|
249 |
|
250 |
:param server_id: integer (str or int)
|
251 |
|
252 |
:param current_status: (str) BUILD|ACTIVE|STOPPED|DELETED|REBOOT
|
253 |
|
254 |
:param delay: time interval between retries
|
255 |
|
256 |
:param wait_cb: if set a progressbar is used to show progress
|
257 |
|
258 |
:returns: (str) the new mode if succesfull, (bool) False if timed out
|
259 |
"""
|
260 |
r = self.get_server_details(server_id)
|
261 |
if r['status'] != current_status: |
262 |
return r['status'] |
263 |
old_wait = total_wait = 0
|
264 |
|
265 |
if current_status == 'BUILD': |
266 |
max_wait = 100
|
267 |
wait_gen = wait_cb(max_wait) if wait_cb else None |
268 |
elif wait_cb:
|
269 |
wait_gen = wait_cb(1 + max_wait // delay)
|
270 |
wait_gen.next() |
271 |
|
272 |
while r['status'] == current_status and total_wait <= max_wait: |
273 |
if current_status == 'BUILD': |
274 |
total_wait = int(r['progress']) |
275 |
if wait_cb:
|
276 |
for i in range(int(old_wait), int(total_wait)): |
277 |
wait_gen.next() |
278 |
old_wait = total_wait |
279 |
else:
|
280 |
stdout.write('.')
|
281 |
stdout.flush() |
282 |
else:
|
283 |
if wait_cb:
|
284 |
wait_gen.next() |
285 |
else:
|
286 |
stdout.write('.')
|
287 |
stdout.flush() |
288 |
total_wait += delay |
289 |
sleep(delay) |
290 |
r = self.get_server_details(server_id)
|
291 |
|
292 |
if r['status'] != current_status: |
293 |
if wait_cb:
|
294 |
try:
|
295 |
while True: |
296 |
wait_gen.next() |
297 |
except:
|
298 |
pass
|
299 |
return r['status'] |
300 |
return False |