Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (6.6 kB)

1
# Copyright 2011-2014 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 django.utils.encoding import smart_unicode
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 = smart_unicode(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, smart_unicode(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 smart_unicode(name).startswith(settings.BACKEND_PREFIX_ID):
66
        raise Network.InvalidBackendIdError(smart_unicode(name))
67
    ns = smart_unicode(name).replace(settings.BACKEND_PREFIX_ID + 'net-', "", 1)
68
    if not ns.isdigit():
69
        raise Network.InvalidBackendIdError(smart_unicode(name))
70

    
71
    return int(ns)
72

    
73

    
74
def id_to_network_name(id):
75
    return "%snet-%s" % (settings.BACKEND_PREFIX_ID, smart_unicode(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 smart_unicode(name).startswith(settings.BACKEND_PREFIX_ID):
83
        raise ValueError("Invalid NIC name: %s" % name)
84
    ns = smart_unicode(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 get_rsapi_state(vm):
92
    """Returns the API state for a virtual machine
93

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

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

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

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

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

    
133

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

    
144

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

    
153

    
154
OPCODE_TO_ACTION = {
155
    "OP_INSTANCE_CREATE": "BUILD",
156
    "OP_INSTANCE_STARTUP": "START",
157
    "OP_INSTANCE_SHUTDOWN": "STOP",
158
    "OP_INSTANCE_REBOOT": "REBOOT",
159
    "OP_INSTANCE_REMOVE": "DESTROY"}
160

    
161

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

    
184

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

    
193

    
194
def check_name_length(name, max_length, message):
195
    """Check if a string is within acceptable value length"""
196
    name = smart_unicode(name, encoding="utf-8")
197
    if len(name) > max_length:
198
        raise faults.BadRequest(message)