Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / logic / utils.py @ d05e5324

History | View | Annotate | Download (7.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 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 + 'vol-', "", 1)
98
    if not ns.isdigit():
99
        raise ValueError("Invalid Disk name: %s" % name)
100

    
101
    return int(ns)
102

    
103

    
104
def id_to_disk_name(id):
105
    return "%svol-%s" % (settings.BACKEND_PREFIX_ID, str(id))
106

    
107

    
108
def get_rsapi_state(vm):
109
    """Returns the API state for a virtual machine
110

111
    The API state for an instance of VirtualMachine is derived as follows:
112

113
    * If the deleted flag has been set, it is "DELETED".
114
    * Otherwise, it is a mapping of the last state reported by Ganeti
115
      (vm.operstate) through the RSAPI_STATE_FROM_OPER_STATE dictionary.
116

117
      The last state reported by Ganeti is set whenever Ganeti reports
118
      successful completion of an operation. If Ganeti says an
119
      OP_INSTANCE_STARTUP operation succeeded, vm.operstate is set to
120
      "STARTED".
121

122
    * To support any transitional states defined by the API (only REBOOT for
123
    the time being) this mapping is amended with information reported by Ganeti
124
    regarding any outstanding operation. If an OP_INSTANCE_STARTUP had
125
    succeeded previously and an OP_INSTANCE_REBOOT has been reported as in
126
    progress, the API state is "REBOOT".
127

128
    """
129
    try:
130
        r = VirtualMachine.RSAPI_STATE_FROM_OPER_STATE[vm.operstate]
131
    except KeyError:
132
        return "UNKNOWN"
133
    # A machine is DELETED if the deleted flag has been set
134
    if vm.deleted:
135
        return "DELETED"
136
    # A machine is in REBOOT if an OP_INSTANCE_REBOOT request is in progress
137
    in_reboot = (r == "ACTIVE") and\
138
                (vm.backendopcode == "OP_INSTANCE_REBOOT") and\
139
                (vm.backendjobstatus in ("queued", "waiting", "running"))
140
    if in_reboot:
141
        return "REBOOT"
142
    in_resize = (r == "STOPPED") and\
143
                (vm.backendopcode == "OP_INSTANCE_MODIFY") and\
144
                (vm.task == "RESIZE") and \
145
                (vm.backendjobstatus in ("queued", "waiting", "running"))
146
    if in_resize:
147
        return "RESIZE"
148
    return r
149

    
150

    
151
TASK_STATE_FROM_ACTION = {
152
    "BUILD": "BUILDING",
153
    "START": "STARTING",
154
    "STOP": "STOPPING",
155
    "REBOOT": "REBOOTING",
156
    "DESTROY": "DESTROYING",
157
    "RESIZE": "RESIZING",
158
    "CONNECT": "CONNECTING",
159
    "DISCONNECT": "DISCONNECTING"}
160

    
161

    
162
def get_task_state(vm):
163
    if vm.task is None:
164
        return ""
165
    try:
166
        return TASK_STATE_FROM_ACTION[vm.task]
167
    except KeyError:
168
        return "UNKNOWN"
169

    
170

    
171
OPCODE_TO_ACTION = {
172
    "OP_INSTANCE_CREATE": "BUILD",
173
    "OP_INSTANCE_STARTUP": "START",
174
    "OP_INSTANCE_SHUTDOWN": "STOP",
175
    "OP_INSTANCE_REBOOT": "REBOOT",
176
    "OP_INSTANCE_REMOVE": "DESTROY"}
177

    
178

    
179
def get_action_from_opcode(opcode, job_fields):
180
    if opcode == "OP_INSTANCE_SET_PARAMS":
181
        nics = job_fields.get("nics")
182
        disks = job_fields.get("disks")
183
        beparams = job_fields.get("beparams")
184
        if nics:
185
            try:
186
                nic_action = nics[0][0]
187
                if nic_action == "add":
188
                    return "CONNECT"
189
                elif nic_action == "remove":
190
                    return "DISCONNECT"
191
                else:
192
                    return None
193
            except:
194
                return None
195
        if disks:
196
            try:
197
                disk_action = disks[0][0]
198
                if disk_action == "add":
199
                    return "ATTACH_VOLUME"
200
                elif disk_action == "remove":
201
                    return "DETACH_VOLUME"
202
                else:
203
                    return None
204
            except:
205
                return None
206
        elif beparams:
207
            return "RESIZE"
208
        else:
209
            return None
210
    else:
211
        return OPCODE_TO_ACTION.get(opcode, None)
212

    
213

    
214
def hide_pass(kw):
215
    if 'osparams' in kw and 'img_passwd' in kw['osparams']:
216
        kw1 = deepcopy(kw)
217
        kw1['osparams']['img_passwd'] = 'x' * 8
218
        return kw1
219
    else:
220
        return kw
221

    
222

    
223
def check_name_length(name, max_length, message):
224
    """Check if a string is within acceptable value length"""
225
    if len(uenc(name)) > max_length:
226
        raise faults.BadRequest(message)