Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (4.2 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 8d97deff Vassilios Karakoidas
34 6efeecee Faidon Liambotis
from django.conf import settings
35 8d97deff Vassilios Karakoidas
36 8d97deff Vassilios Karakoidas
def id_from_instance_name(name):
37 8d97deff Vassilios Karakoidas
    """Returns VirtualMachine's Django id, given a ganeti machine name.
38 8d97deff Vassilios Karakoidas

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

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

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

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

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

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

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

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

94 fc30c430 Vangelis Koukis
    """
95 8d97deff Vassilios Karakoidas
    try:
96 dfd19c2d Vassilios Karakoidas
        r = VirtualMachine.RSAPI_STATE_FROM_OPER_STATE[vm.operstate]
97 8d97deff Vassilios Karakoidas
    except KeyError:
98 8d97deff Vassilios Karakoidas
        return "UNKNOWN"
99 685b219e Vangelis Koukis
    # A machine is DELETED if the deleted flag has been set
100 685b219e Vangelis Koukis
    if vm.deleted:
101 685b219e Vangelis Koukis
        return "DELETED"
102 8d97deff Vassilios Karakoidas
    # A machine is in REBOOT if an OP_INSTANCE_REBOOT request is in progress
103 dfd19c2d Vassilios Karakoidas
    if r == 'ACTIVE' and vm.backendopcode == 'OP_INSTANCE_REBOOT' and \
104 fb381fbe Markos Gogoulos
        vm.backendjobstatus in ('queued', 'waiting', 'running'):
105 8d97deff Vassilios Karakoidas
        return "REBOOT"
106 8d97deff Vassilios Karakoidas
    return r
107 8d97deff Vassilios Karakoidas
108 dfd19c2d Vassilios Karakoidas
def update_state(vm, new_operstate):
109 dfd19c2d Vassilios Karakoidas
    """Wrapper around updates of the VirtualMachine.operstate field"""
110 8d97deff Vassilios Karakoidas
111 dfd19c2d Vassilios Karakoidas
    vm.operstate = new_operstate