root / snf-cyclades-app / synnefo / logic / utils.py @ 5480daec
History | View | Annotate | Download (4.3 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 django.conf import settings |
34 |
from copy import deepcopy |
35 |
|
36 |
def id_from_instance_name(name): |
37 |
"""Returns VirtualMachine's Django id, given a ganeti machine name.
|
38 |
|
39 |
Strips the ganeti prefix atm. Needs a better name!
|
40 |
|
41 |
"""
|
42 |
sname = str(name)
|
43 |
if not sname.startswith(settings.BACKEND_PREFIX_ID): |
44 |
raise VirtualMachine.InvalidBackendIdError(sname)
|
45 |
ns = sname.replace(settings.BACKEND_PREFIX_ID, "", 1) |
46 |
if not ns.isdigit(): |
47 |
raise VirtualMachine.InvalidBackendIdError(sname)
|
48 |
|
49 |
return int(ns) |
50 |
|
51 |
|
52 |
def id_to_instance_name(id): |
53 |
return "%s%s" % (settings.BACKEND_PREFIX_ID, str(id)) |
54 |
|
55 |
|
56 |
def id_from_network_name(name): |
57 |
"""Returns Network's Django id, given a ganeti machine name.
|
58 |
|
59 |
Strips the ganeti prefix atm. Needs a better name!
|
60 |
|
61 |
"""
|
62 |
if not str(name).startswith(settings.BACKEND_PREFIX_ID): |
63 |
raise Network.InvalidBackendIdError(str(name)) |
64 |
ns = str(name).replace(settings.BACKEND_PREFIX_ID + 'net-', "", 1) |
65 |
if not ns.isdigit(): |
66 |
raise Network.InvalidBackendIdError(str(name)) |
67 |
|
68 |
return int(ns) |
69 |
|
70 |
|
71 |
def id_to_network_name(id): |
72 |
return "%snet-%s" % (settings.BACKEND_PREFIX_ID, str(id)) |
73 |
|
74 |
|
75 |
def get_rsapi_state(vm): |
76 |
"""Returns the API state for a virtual machine
|
77 |
|
78 |
The API state for an instance of VirtualMachine is derived as follows:
|
79 |
|
80 |
* If the deleted flag has been set, it is "DELETED".
|
81 |
* Otherwise, it is a mapping of the last state reported by Ganeti
|
82 |
(vm.operstate) through the RSAPI_STATE_FROM_OPER_STATE dictionary.
|
83 |
|
84 |
The last state reported by Ganeti is set whenever Ganeti reports
|
85 |
successful completion of an operation. If Ganeti says an OP_INSTANCE_STARTUP
|
86 |
operation succeeded, vm.operstate is set to "STARTED".
|
87 |
|
88 |
* To support any transitional states defined by the API (only REBOOT for the time
|
89 |
being) this mapping is amended with information reported by Ganeti regarding
|
90 |
any outstanding operation. If an OP_INSTANCE_STARTUP had succeeded previously
|
91 |
and an OP_INSTANCE_REBOOT has been reported as in progress, the API state is
|
92 |
"REBOOT".
|
93 |
|
94 |
"""
|
95 |
try:
|
96 |
r = VirtualMachine.RSAPI_STATE_FROM_OPER_STATE[vm.operstate] |
97 |
except KeyError: |
98 |
return "UNKNOWN" |
99 |
# A machine is DELETED if the deleted flag has been set
|
100 |
if vm.deleted:
|
101 |
return "DELETED" |
102 |
# A machine is in REBOOT if an OP_INSTANCE_REBOOT request is in progress
|
103 |
if r == 'ACTIVE' and vm.backendopcode == 'OP_INSTANCE_REBOOT' and \ |
104 |
vm.backendjobstatus in ('queued', 'waiting', 'running'): |
105 |
return "REBOOT" |
106 |
return r
|
107 |
|
108 |
|
109 |
def hide_pass(kw): |
110 |
if 'osparams' in kw and 'img_passwd' in kw['osparams']: |
111 |
kw1 = deepcopy(kw) |
112 |
kw1['osparams']['img_passwd'] = 'x' * 8 |
113 |
return kw1
|
114 |
else:
|
115 |
return kw
|