Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (4.3 kB)

1 48130e66 Georgios Gousios
# Copyright 2011 GRNET S.A. All rights reserved.
2 8d97deff Vassilios Karakoidas
#
3 48130e66 Georgios Gousios
# Redistribution and use in source and binary forms, with or without
4 48130e66 Georgios Gousios
# modification, are permitted provided that the following conditions
5 48130e66 Georgios Gousios
# are met:
6 48130e66 Georgios Gousios
#
7 48130e66 Georgios Gousios
#   1. Redistributions of source code must retain the above copyright
8 48130e66 Georgios Gousios
#      notice, this list of conditions and the following disclaimer.
9 8d97deff Vassilios Karakoidas
#
10 48130e66 Georgios Gousios
#  2. Redistributions in binary form must reproduce the above copyright
11 48130e66 Georgios Gousios
#     notice, this list of conditions and the following disclaimer in the
12 48130e66 Georgios Gousios
#     documentation and/or other materials provided with the distribution.
13 8d97deff Vassilios Karakoidas
#
14 48130e66 Georgios Gousios
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15 48130e66 Georgios Gousios
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 48130e66 Georgios Gousios
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 48130e66 Georgios Gousios
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18 48130e66 Georgios Gousios
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 48130e66 Georgios Gousios
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 48130e66 Georgios Gousios
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 48130e66 Georgios Gousios
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 48130e66 Georgios Gousios
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 48130e66 Georgios Gousios
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 48130e66 Georgios Gousios
# SUCH DAMAGE.
25 8d97deff Vassilios Karakoidas
#
26 48130e66 Georgios Gousios
# The views and conclusions contained in the software and documentation are
27 48130e66 Georgios Gousios
# those of the authors and should not be interpreted as representing official
28 48130e66 Georgios Gousios
# policies, either expressed or implied, of GRNET S.A.
29 48130e66 Georgios Gousios
30 48130e66 Georgios Gousios
# Utility functions
31 8d97deff Vassilios Karakoidas
32 22ee6892 Christos Stavrakakis
from synnefo.db.models import VirtualMachine, Network
33 6efeecee Faidon Liambotis
from django.conf import settings
34 6de6677b Christos Stavrakakis
from copy import deepcopy
35 8d97deff Vassilios Karakoidas
36 68b952f9 Christos Stavrakakis
37 8d97deff Vassilios Karakoidas
def id_from_instance_name(name):
38 8d97deff Vassilios Karakoidas
    """Returns VirtualMachine's Django id, given a ganeti machine name.
39 8d97deff Vassilios Karakoidas

40 8d97deff Vassilios Karakoidas
    Strips the ganeti prefix atm. Needs a better name!
41 8d97deff Vassilios Karakoidas

42 8d97deff Vassilios Karakoidas
    """
43 4cbef2a0 Christos Stavrakakis
    sname = str(name)
44 4cbef2a0 Christos Stavrakakis
    if not sname.startswith(settings.BACKEND_PREFIX_ID):
45 4cbef2a0 Christos Stavrakakis
        raise VirtualMachine.InvalidBackendIdError(sname)
46 4cbef2a0 Christos Stavrakakis
    ns = sname.replace(settings.BACKEND_PREFIX_ID, "", 1)
47 8d97deff Vassilios Karakoidas
    if not ns.isdigit():
48 4cbef2a0 Christos Stavrakakis
        raise VirtualMachine.InvalidBackendIdError(sname)
49 8d97deff Vassilios Karakoidas
50 8d97deff Vassilios Karakoidas
    return int(ns)
51 8d97deff Vassilios Karakoidas
52 d30f29aa Christos Stavrakakis
53 d30f29aa Christos Stavrakakis
def id_to_instance_name(id):
54 c55b0cdc Christos Stavrakakis
    return "%s%s" % (settings.BACKEND_PREFIX_ID, str(id))
55 d30f29aa Christos Stavrakakis
56 d30f29aa Christos Stavrakakis
57 22ee6892 Christos Stavrakakis
def id_from_network_name(name):
58 22ee6892 Christos Stavrakakis
    """Returns Network's Django id, given a ganeti machine name.
59 22ee6892 Christos Stavrakakis

60 22ee6892 Christos Stavrakakis
    Strips the ganeti prefix atm. Needs a better name!
61 22ee6892 Christos Stavrakakis

62 22ee6892 Christos Stavrakakis
    """
63 22ee6892 Christos Stavrakakis
    if not str(name).startswith(settings.BACKEND_PREFIX_ID):
64 22ee6892 Christos Stavrakakis
        raise Network.InvalidBackendIdError(str(name))
65 44e2c577 Christos Stavrakakis
    ns = str(name).replace(settings.BACKEND_PREFIX_ID + 'net-', "", 1)
66 22ee6892 Christos Stavrakakis
    if not ns.isdigit():
67 22ee6892 Christos Stavrakakis
        raise Network.InvalidBackendIdError(str(name))
68 22ee6892 Christos Stavrakakis
69 22ee6892 Christos Stavrakakis
    return int(ns)
70 22ee6892 Christos Stavrakakis
71 22ee6892 Christos Stavrakakis
72 d30f29aa Christos Stavrakakis
def id_to_network_name(id):
73 0b827aa4 Christos Stavrakakis
    return "%snet-%s" % (settings.BACKEND_PREFIX_ID, str(id))
74 d30f29aa Christos Stavrakakis
75 d30f29aa Christos Stavrakakis
76 8d97deff Vassilios Karakoidas
def get_rsapi_state(vm):
77 fc30c430 Vangelis Koukis
    """Returns the API state for a virtual machine
78 f16c6944 Kostas Papadimitriou

79 fc30c430 Vangelis Koukis
    The API state for an instance of VirtualMachine is derived as follows:
80 fc30c430 Vangelis Koukis

81 fc30c430 Vangelis Koukis
    * If the deleted flag has been set, it is "DELETED".
82 fc30c430 Vangelis Koukis
    * Otherwise, it is a mapping of the last state reported by Ganeti
83 fc30c430 Vangelis Koukis
      (vm.operstate) through the RSAPI_STATE_FROM_OPER_STATE dictionary.
84 f16c6944 Kostas Papadimitriou

85 fc30c430 Vangelis Koukis
      The last state reported by Ganeti is set whenever Ganeti reports
86 68b952f9 Christos Stavrakakis
      successful completion of an operation. If Ganeti says an
87 68b952f9 Christos Stavrakakis
      OP_INSTANCE_STARTUP operation succeeded, vm.operstate is set to
88 68b952f9 Christos Stavrakakis
      "STARTED".
89 68b952f9 Christos Stavrakakis

90 68b952f9 Christos Stavrakakis
    * To support any transitional states defined by the API (only REBOOT for
91 68b952f9 Christos Stavrakakis
    the time being) this mapping is amended with information reported by Ganeti
92 68b952f9 Christos Stavrakakis
    regarding any outstanding operation. If an OP_INSTANCE_STARTUP had
93 68b952f9 Christos Stavrakakis
    succeeded previously and an OP_INSTANCE_REBOOT has been reported as in
94 68b952f9 Christos Stavrakakis
    progress, the API state is "REBOOT".
95 fc30c430 Vangelis Koukis

96 fc30c430 Vangelis Koukis
    """
97 8d97deff Vassilios Karakoidas
    try:
98 dfd19c2d Vassilios Karakoidas
        r = VirtualMachine.RSAPI_STATE_FROM_OPER_STATE[vm.operstate]
99 8d97deff Vassilios Karakoidas
    except KeyError:
100 8d97deff Vassilios Karakoidas
        return "UNKNOWN"
101 685b219e Vangelis Koukis
    # A machine is DELETED if the deleted flag has been set
102 685b219e Vangelis Koukis
    if vm.deleted:
103 685b219e Vangelis Koukis
        return "DELETED"
104 8d97deff Vassilios Karakoidas
    # A machine is in REBOOT if an OP_INSTANCE_REBOOT request is in progress
105 dfd19c2d Vassilios Karakoidas
    if r == 'ACTIVE' and vm.backendopcode == 'OP_INSTANCE_REBOOT' and \
106 fb381fbe Markos Gogoulos
        vm.backendjobstatus in ('queued', 'waiting', 'running'):
107 8d97deff Vassilios Karakoidas
        return "REBOOT"
108 8d97deff Vassilios Karakoidas
    return r
109 8d97deff Vassilios Karakoidas
110 6afeb85d Christos Stavrakakis
111 6afeb85d Christos Stavrakakis
def hide_pass(kw):
112 6afeb85d Christos Stavrakakis
    if 'osparams' in kw and 'img_passwd' in kw['osparams']:
113 6de6677b Christos Stavrakakis
        kw1 = deepcopy(kw)
114 6afeb85d Christos Stavrakakis
        kw1['osparams']['img_passwd'] = 'x' * 8
115 6afeb85d Christos Stavrakakis
        return kw1
116 6afeb85d Christos Stavrakakis
    else:
117 6afeb85d Christos Stavrakakis
        return kw