Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (6.4 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

    
37

    
38
def id_from_instance_name(name):
39
    """Returns VirtualMachine's Django id, given a ganeti machine name.
40

41
    Strips the ganeti prefix atm. Needs a better name!
42

43
    """
44
    sname = str(name)
45
    if not sname.startswith(settings.BACKEND_PREFIX_ID):
46
        raise VirtualMachine.InvalidBackendIdError(sname)
47
    ns = sname.replace(settings.BACKEND_PREFIX_ID, "", 1)
48
    if not ns.isdigit():
49
        raise VirtualMachine.InvalidBackendIdError(sname)
50

    
51
    return int(ns)
52

    
53

    
54
def id_to_instance_name(id):
55
    return "%s%s" % (settings.BACKEND_PREFIX_ID, str(id))
56

    
57

    
58
def id_from_network_name(name):
59
    """Returns Network's Django id, given a ganeti network name.
60

61
    Strips the ganeti prefix atm. Needs a better name!
62

63
    """
64
    if not str(name).startswith(settings.BACKEND_PREFIX_ID):
65
        raise Network.InvalidBackendIdError(str(name))
66
    ns = str(name).replace(settings.BACKEND_PREFIX_ID + 'net-', "", 1)
67
    if not ns.isdigit():
68
        raise Network.InvalidBackendIdError(str(name))
69

    
70
    return int(ns)
71

    
72

    
73
def id_to_network_name(id):
74
    return "%snet-%s" % (settings.BACKEND_PREFIX_ID, str(id))
75

    
76

    
77
def id_from_nic_name(name):
78
    """Returns NIC's Django id, given a Ganeti's NIC name.
79

80
    """
81
    if not str(name).startswith(settings.BACKEND_PREFIX_ID):
82
        raise ValueError("Invalid NIC name: %s" % name)
83
    ns = str(name).replace(settings.BACKEND_PREFIX_ID + 'nic-', "", 1)
84
    if not ns.isdigit():
85
        raise ValueError("Invalid NIC name: %s" % name)
86

    
87
    return int(ns)
88

    
89

    
90
def get_rsapi_state(vm):
91
    """Returns the API state for a virtual machine
92

93
    The API state for an instance of VirtualMachine is derived as follows:
94

95
    * If the deleted flag has been set, it is "DELETED".
96
    * Otherwise, it is a mapping of the last state reported by Ganeti
97
      (vm.operstate) through the RSAPI_STATE_FROM_OPER_STATE dictionary.
98

99
      The last state reported by Ganeti is set whenever Ganeti reports
100
      successful completion of an operation. If Ganeti says an
101
      OP_INSTANCE_STARTUP operation succeeded, vm.operstate is set to
102
      "STARTED".
103

104
    * To support any transitional states defined by the API (only REBOOT for
105
    the time being) this mapping is amended with information reported by Ganeti
106
    regarding any outstanding operation. If an OP_INSTANCE_STARTUP had
107
    succeeded previously and an OP_INSTANCE_REBOOT has been reported as in
108
    progress, the API state is "REBOOT".
109

110
    """
111
    try:
112
        r = VirtualMachine.RSAPI_STATE_FROM_OPER_STATE[vm.operstate]
113
    except KeyError:
114
        return "UNKNOWN"
115
    # A machine is DELETED if the deleted flag has been set
116
    if vm.deleted:
117
        return "DELETED"
118
    # A machine is in REBOOT if an OP_INSTANCE_REBOOT request is in progress
119
    in_reboot = (r == "ACTIVE") and\
120
                (vm.backendopcode == "OP_INSTANCE_REBOOT") and\
121
                (vm.backendjobstatus in ("queued", "waiting", "running"))
122
    if in_reboot:
123
        return "REBOOT"
124
    in_resize = (r == "STOPPED") and\
125
                (vm.backendopcode == "OP_INSTANCE_MODIFY") and\
126
                (vm.task == "RESIZE") and \
127
                (vm.backendjobstatus in ("queued", "waiting", "running"))
128
    if in_resize:
129
        return "RESIZE"
130
    return r
131

    
132

    
133
TASK_STATE_FROM_ACTION = {
134
    "BUILD": "BULDING",
135
    "START": "STARTING",
136
    "STOP": "STOPPING",
137
    "REBOOT": "REBOOTING",
138
    "DESTROY": "DESTROYING",
139
    "RESIZE": "RESIZING",
140
    "CONNECT": "CONNECTING",
141
    "DISCONNECT": "DISCONNECTING"}
142

    
143

    
144
def get_task_state(vm):
145
    if vm.task is None:
146
        return ""
147
    try:
148
        return TASK_STATE_FROM_ACTION[vm.task]
149
    except KeyError:
150
        return "UNKNOWN"
151

    
152

    
153
OPCODE_TO_ACTION = {
154
    "OP_INSTANCE_CREATE": "BUILD",
155
    "OP_INSTANCE_START": "START",
156
    "OP_INSTANCE_STOP": "STOP",
157
    "OP_INSTANCE_REBOOT": "REBOOT",
158
    "OP_INSTANCE_REMOVE": "DESTROY"}
159

    
160

    
161
def get_action_from_opcode(opcode, job_fields):
162
    if opcode == "OP_INSTANCE_SET_PARAMS":
163
        nics = job_fields.get("nics")
164
        beparams = job_fields.get("beparams")
165
        if nics:
166
            try:
167
                nic_action = nics[0][0]
168
                if nic_action == "add":
169
                    return "CONNECT"
170
                elif nic_action == "remove":
171
                    return "DISCONNECT"
172
                else:
173
                    return None
174
            except:
175
                return None
176
        elif beparams:
177
            return "RESIZE"
178
        else:
179
            return None
180
    else:
181
        return OPCODE_TO_ACTION.get(opcode, None)
182

    
183

    
184
def hide_pass(kw):
185
    if 'osparams' in kw and 'img_passwd' in kw['osparams']:
186
        kw1 = deepcopy(kw)
187
        kw1['osparams']['img_passwd'] = 'x' * 8
188
        return kw1
189
    else:
190
        return kw
191

    
192

    
193
def check_name_length(name, max_length, message):
194
    """Check if a string is within acceptable value length"""
195
    if len(str(name)) > max_length:
196
        raise faults.BadRequest(message)