root / snf-cyclades-app / synnefo / management / common.py @ b48697f0
History | View | Annotate | Download (7.8 kB)
1 |
# Copyright 2012 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 django.core.management import CommandError |
35 |
from synnefo.db.models import (Backend, VirtualMachine, Network, |
36 |
Flavor, IPAddress, Subnet, |
37 |
BridgePoolTable, MacPrefixPoolTable) |
38 |
from functools import wraps |
39 |
|
40 |
from snf_django.lib.api import faults |
41 |
from synnefo.api import util |
42 |
from synnefo.logic import backend as backend_mod |
43 |
from synnefo.logic.rapi import GanetiApiError, GanetiRapiClient |
44 |
from synnefo.logic.utils import (id_from_instance_name, |
45 |
id_from_network_name) |
46 |
|
47 |
import logging |
48 |
log = logging.getLogger(__name__) |
49 |
|
50 |
|
51 |
def format_vm_state(vm): |
52 |
if vm.operstate == "BUILD": |
53 |
return "BUILD(" + str(vm.buildpercentage) + "%)" |
54 |
else:
|
55 |
return vm.operstate
|
56 |
|
57 |
|
58 |
def get_backend(backend_id): |
59 |
try:
|
60 |
backend_id = int(backend_id)
|
61 |
return Backend.objects.get(id=backend_id)
|
62 |
except ValueError: |
63 |
raise CommandError("Invalid Backend ID: %s" % backend_id) |
64 |
except Backend.DoesNotExist:
|
65 |
raise CommandError("Backend with ID %s not found in DB. " |
66 |
" Use snf-manage backend-list to find"
|
67 |
" out available backend IDs." % backend_id)
|
68 |
|
69 |
|
70 |
def get_image(image_id, user_id): |
71 |
if image_id:
|
72 |
try:
|
73 |
return util.get_image_dict(image_id, user_id)
|
74 |
except faults.ItemNotFound:
|
75 |
raise CommandError("Image with ID %s not found." |
76 |
" Use snf-manage image-list to find"
|
77 |
" out available image IDs." % image_id)
|
78 |
else:
|
79 |
raise CommandError("image-id is mandatory") |
80 |
|
81 |
|
82 |
def get_vm(server_id): |
83 |
"""Get a VirtualMachine object by its ID.
|
84 |
|
85 |
@type server_id: int or string
|
86 |
@param server_id: The server's DB id or the Ganeti name
|
87 |
|
88 |
"""
|
89 |
try:
|
90 |
server_id = int(server_id)
|
91 |
except (ValueError, TypeError): |
92 |
try:
|
93 |
server_id = id_from_instance_name(server_id) |
94 |
except VirtualMachine.InvalidBackendIdError:
|
95 |
raise CommandError("Invalid server ID: %s" % server_id) |
96 |
|
97 |
try:
|
98 |
return VirtualMachine.objects.get(id=server_id)
|
99 |
except VirtualMachine.DoesNotExist:
|
100 |
raise CommandError("Server with ID %s not found in DB." |
101 |
" Use snf-manage server-list to find out"
|
102 |
" available server IDs." % server_id)
|
103 |
|
104 |
|
105 |
def get_network(network_id, for_update=True): |
106 |
"""Get a Network object by its ID.
|
107 |
|
108 |
@type network_id: int or string
|
109 |
@param network_id: The networks DB id or the Ganeti name
|
110 |
|
111 |
"""
|
112 |
|
113 |
try:
|
114 |
network_id = int(network_id)
|
115 |
except (ValueError, TypeError): |
116 |
try:
|
117 |
network_id = id_from_network_name(network_id) |
118 |
except Network.InvalidBackendIdError:
|
119 |
raise CommandError("Invalid network ID: %s" % network_id) |
120 |
|
121 |
networks = Network.objects |
122 |
if for_update:
|
123 |
networks = networks.select_for_update() |
124 |
try:
|
125 |
return networks.get(id=network_id)
|
126 |
except Network.DoesNotExist:
|
127 |
raise CommandError("Network with ID %s not found in DB." |
128 |
" Use snf-manage network-list to find out"
|
129 |
" available network IDs." % network_id)
|
130 |
|
131 |
|
132 |
def get_subnet(subnet_id, for_update=True): |
133 |
"""Get a Subnet object by its ID."""
|
134 |
try:
|
135 |
return Subnet.objects.get(id=subnet_id)
|
136 |
except Subnet.DoesNotExist:
|
137 |
raise CommandError("Subnet with ID %s not found in DB." |
138 |
" Use snf-manage subnet-list to find out"
|
139 |
" available subnet IDs" % subnet_id)
|
140 |
|
141 |
|
142 |
def get_flavor(flavor_id): |
143 |
try:
|
144 |
flavor_id = int(flavor_id)
|
145 |
return Flavor.objects.get(id=flavor_id)
|
146 |
except ValueError: |
147 |
raise CommandError("Invalid flavor ID: %s", flavor_id) |
148 |
except Flavor.DoesNotExist:
|
149 |
raise CommandError("Flavor with ID %s not found in DB." |
150 |
" Use snf-manage flavor-list to find out"
|
151 |
" available flavor IDs." % flavor_id)
|
152 |
|
153 |
|
154 |
def get_floating_ip_by_address(address, for_update=False): |
155 |
try:
|
156 |
objects = IPAddress.objects |
157 |
if for_update:
|
158 |
objects = objects.select_for_update() |
159 |
return objects.get(floating_ip=True, address=address, deleted=False) |
160 |
except IPAddress.DoesNotExist:
|
161 |
raise CommandError("Floating IP does not exist.") |
162 |
|
163 |
|
164 |
def get_floating_ip_by_id(floating_ip_id, for_update=False): |
165 |
try:
|
166 |
objects = IPAddress.objects |
167 |
if for_update:
|
168 |
objects = objects.select_for_update() |
169 |
return objects.get(floating_ip=True, id=floating_ip_id, deleted=False) |
170 |
except IPAddress.DoesNotExist:
|
171 |
raise CommandError("Floating IP does not exist.") |
172 |
|
173 |
|
174 |
def check_backend_credentials(clustername, port, username, password): |
175 |
try:
|
176 |
client = GanetiRapiClient(clustername, port, username, password) |
177 |
# This command will raise an exception if there is no
|
178 |
# write-access
|
179 |
client.ModifyCluster() |
180 |
except GanetiApiError as e: |
181 |
raise CommandError(e)
|
182 |
|
183 |
info = client.GetInfo() |
184 |
info_name = info['name']
|
185 |
if info_name != clustername:
|
186 |
raise CommandError("Invalid clustername value. Please use the" |
187 |
" Ganeti Cluster name: %s" % info_name)
|
188 |
|
189 |
|
190 |
def convert_api_faults(func): |
191 |
@wraps(func)
|
192 |
def wrapper(*args, **kwargs): |
193 |
try:
|
194 |
return func(*args, **kwargs)
|
195 |
except faults.Fault as e: |
196 |
raise CommandError(e.message)
|
197 |
return wrapper
|
198 |
|
199 |
|
200 |
class Omit(object): |
201 |
pass
|
202 |
|
203 |
|
204 |
def wait_server_task(server, wait, stdout): |
205 |
jobID = server.task_job_id |
206 |
if wait:
|
207 |
msg = "Issued job '%s'. Waiting to complete...\n"
|
208 |
stdout.write(msg % jobID) |
209 |
client = server.get_client() |
210 |
wait_ganeti_job(client, jobID, stdout) |
211 |
else:
|
212 |
msg = "Issued job '%s'.\n"
|
213 |
stdout.write(msg % jobID) |
214 |
|
215 |
|
216 |
def wait_ganeti_job(client, jobID, stdout): |
217 |
status, error = backend_mod.wait_for_job(client, jobID) |
218 |
if status == "success": |
219 |
stdout.write("Job finished successfully.\n")
|
220 |
else:
|
221 |
raise CommandError("Job failed! Error: %s\n" % error) |
222 |
|
223 |
|
224 |
def pool_table_from_type(type_): |
225 |
if type_ == "mac-prefix": |
226 |
return MacPrefixPoolTable
|
227 |
elif type_ == "bridge": |
228 |
return BridgePoolTable
|
229 |
# elif type == "ip":
|
230 |
# return IPPoolTable
|
231 |
else:
|
232 |
raise ValueError("Invalid pool type") |