root / snf-cyclades-app / synnefo / logic / utils.py @ c583d487
History | View | Annotate | Download (6.9 kB)
1 |
# Copyright 2011 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or without
|
4 |
# modification, are permitted provided that the following conditions
|
5 |
# are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above copyright
|
8 |
# notice, this list of conditions and the following disclaimer.
|
9 |
#
|
10 |
# 2. Redistributions in binary form must reproduce the above copyright
|
11 |
# notice, this list of conditions and the following disclaimer in the
|
12 |
# documentation and/or other materials provided with the distribution.
|
13 |
#
|
14 |
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
15 |
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
16 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
17 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
18 |
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
19 |
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
20 |
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
21 |
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
22 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
23 |
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
24 |
# SUCH DAMAGE.
|
25 |
#
|
26 |
# The views and conclusions contained in the software and documentation are
|
27 |
# those of the authors and should not be interpreted as representing official
|
28 |
# policies, either expressed or implied, of GRNET S.A.
|
29 |
|
30 |
# Utility functions
|
31 |
|
32 |
from synnefo.db.models import VirtualMachine, Network |
33 |
from snf_django.lib.api import faults |
34 |
from django.conf import settings |
35 |
from copy import deepcopy |
36 |
from synnefo.util.text import uenc |
37 |
|
38 |
|
39 |
def id_from_instance_name(name): |
40 |
"""Returns VirtualMachine's Django id, given a ganeti machine name.
|
41 |
|
42 |
Strips the ganeti prefix atm. Needs a better name!
|
43 |
|
44 |
"""
|
45 |
sname = str(name)
|
46 |
if not sname.startswith(settings.BACKEND_PREFIX_ID): |
47 |
raise VirtualMachine.InvalidBackendIdError(sname)
|
48 |
ns = sname.replace(settings.BACKEND_PREFIX_ID, "", 1) |
49 |
if not ns.isdigit(): |
50 |
raise VirtualMachine.InvalidBackendIdError(sname)
|
51 |
|
52 |
return int(ns) |
53 |
|
54 |
|
55 |
def id_to_instance_name(id): |
56 |
return "%s%s" % (settings.BACKEND_PREFIX_ID, str(id)) |
57 |
|
58 |
|
59 |
def id_from_network_name(name): |
60 |
"""Returns Network's Django id, given a ganeti network name.
|
61 |
|
62 |
Strips the ganeti prefix atm. Needs a better name!
|
63 |
|
64 |
"""
|
65 |
if not str(name).startswith(settings.BACKEND_PREFIX_ID): |
66 |
raise Network.InvalidBackendIdError(str(name)) |
67 |
ns = str(name).replace(settings.BACKEND_PREFIX_ID + 'net-', "", 1) |
68 |
if not ns.isdigit(): |
69 |
raise Network.InvalidBackendIdError(str(name)) |
70 |
|
71 |
return int(ns) |
72 |
|
73 |
|
74 |
def id_to_network_name(id): |
75 |
return "%snet-%s" % (settings.BACKEND_PREFIX_ID, str(id)) |
76 |
|
77 |
|
78 |
def id_from_nic_name(name): |
79 |
"""Returns NIC's Django id, given a Ganeti's NIC name.
|
80 |
|
81 |
"""
|
82 |
if not str(name).startswith(settings.BACKEND_PREFIX_ID): |
83 |
raise ValueError("Invalid NIC name: %s" % name) |
84 |
ns = str(name).replace(settings.BACKEND_PREFIX_ID + 'nic-', "", 1) |
85 |
if not ns.isdigit(): |
86 |
raise ValueError("Invalid NIC name: %s" % name) |
87 |
|
88 |
return int(ns) |
89 |
|
90 |
|
91 |
def id_from_disk_name(name): |
92 |
"""Returns Disk Django id, given a Ganeti's Disk name.
|
93 |
|
94 |
"""
|
95 |
if not str(name).startswith(settings.BACKEND_PREFIX_ID): |
96 |
raise ValueError("Invalid Disk name: %s" % name) |
97 |
ns = str(name).replace(settings.BACKEND_PREFIX_ID + 'volume-', "", 1) |
98 |
if not ns.isdigit(): |
99 |
raise ValueError("Invalid Disk name: %s" % name) |
100 |
|
101 |
return int(ns) |
102 |
|
103 |
|
104 |
def get_rsapi_state(vm): |
105 |
"""Returns the API state for a virtual machine
|
106 |
|
107 |
The API state for an instance of VirtualMachine is derived as follows:
|
108 |
|
109 |
* If the deleted flag has been set, it is "DELETED".
|
110 |
* Otherwise, it is a mapping of the last state reported by Ganeti
|
111 |
(vm.operstate) through the RSAPI_STATE_FROM_OPER_STATE dictionary.
|
112 |
|
113 |
The last state reported by Ganeti is set whenever Ganeti reports
|
114 |
successful completion of an operation. If Ganeti says an
|
115 |
OP_INSTANCE_STARTUP operation succeeded, vm.operstate is set to
|
116 |
"STARTED".
|
117 |
|
118 |
* To support any transitional states defined by the API (only REBOOT for
|
119 |
the time being) this mapping is amended with information reported by Ganeti
|
120 |
regarding any outstanding operation. If an OP_INSTANCE_STARTUP had
|
121 |
succeeded previously and an OP_INSTANCE_REBOOT has been reported as in
|
122 |
progress, the API state is "REBOOT".
|
123 |
|
124 |
"""
|
125 |
try:
|
126 |
r = VirtualMachine.RSAPI_STATE_FROM_OPER_STATE[vm.operstate] |
127 |
except KeyError: |
128 |
return "UNKNOWN" |
129 |
# A machine is DELETED if the deleted flag has been set
|
130 |
if vm.deleted:
|
131 |
return "DELETED" |
132 |
# A machine is in REBOOT if an OP_INSTANCE_REBOOT request is in progress
|
133 |
in_reboot = (r == "ACTIVE") and\ |
134 |
(vm.backendopcode == "OP_INSTANCE_REBOOT") and\ |
135 |
(vm.backendjobstatus in ("queued", "waiting", "running")) |
136 |
if in_reboot:
|
137 |
return "REBOOT" |
138 |
in_resize = (r == "STOPPED") and\ |
139 |
(vm.backendopcode == "OP_INSTANCE_MODIFY") and\ |
140 |
(vm.task == "RESIZE") and \ |
141 |
(vm.backendjobstatus in ("queued", "waiting", "running")) |
142 |
if in_resize:
|
143 |
return "RESIZE" |
144 |
return r
|
145 |
|
146 |
|
147 |
TASK_STATE_FROM_ACTION = { |
148 |
"BUILD": "BUILDING", |
149 |
"START": "STARTING", |
150 |
"STOP": "STOPPING", |
151 |
"REBOOT": "REBOOTING", |
152 |
"DESTROY": "DESTROYING", |
153 |
"RESIZE": "RESIZING", |
154 |
"CONNECT": "CONNECTING", |
155 |
"DISCONNECT": "DISCONNECTING"} |
156 |
|
157 |
|
158 |
def get_task_state(vm): |
159 |
if vm.task is None: |
160 |
return "" |
161 |
try:
|
162 |
return TASK_STATE_FROM_ACTION[vm.task]
|
163 |
except KeyError: |
164 |
return "UNKNOWN" |
165 |
|
166 |
|
167 |
OPCODE_TO_ACTION = { |
168 |
"OP_INSTANCE_CREATE": "BUILD", |
169 |
"OP_INSTANCE_STARTUP": "START", |
170 |
"OP_INSTANCE_SHUTDOWN": "STOP", |
171 |
"OP_INSTANCE_REBOOT": "REBOOT", |
172 |
"OP_INSTANCE_REMOVE": "DESTROY"} |
173 |
|
174 |
|
175 |
def get_action_from_opcode(opcode, job_fields): |
176 |
if opcode == "OP_INSTANCE_SET_PARAMS": |
177 |
nics = job_fields.get("nics")
|
178 |
beparams = job_fields.get("beparams")
|
179 |
if nics:
|
180 |
try:
|
181 |
nic_action = nics[0][0] |
182 |
if nic_action == "add": |
183 |
return "CONNECT" |
184 |
elif nic_action == "remove": |
185 |
return "DISCONNECT" |
186 |
else:
|
187 |
return None |
188 |
except:
|
189 |
return None |
190 |
elif beparams:
|
191 |
return "RESIZE" |
192 |
else:
|
193 |
return None |
194 |
else:
|
195 |
return OPCODE_TO_ACTION.get(opcode, None) |
196 |
|
197 |
|
198 |
def hide_pass(kw): |
199 |
if 'osparams' in kw and 'img_passwd' in kw['osparams']: |
200 |
kw1 = deepcopy(kw) |
201 |
kw1['osparams']['img_passwd'] = 'x' * 8 |
202 |
return kw1
|
203 |
else:
|
204 |
return kw
|
205 |
|
206 |
|
207 |
def check_name_length(name, max_length, message): |
208 |
"""Check if a string is within acceptable value length"""
|
209 |
if len(uenc(name)) > max_length: |
210 |
raise faults.BadRequest(message)
|