Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (3.6 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 234f8b07 Vangelis Koukis
from synnefo.db.models import VirtualMachine
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 8d97deff Vassilios Karakoidas
    if not str(name).startswith(settings.BACKEND_PREFIX_ID):
43 8d97deff Vassilios Karakoidas
        raise VirtualMachine.InvalidBackendIdError(str(name))
44 f16c6944 Kostas Papadimitriou
    ns = str(name).replace(settings.BACKEND_PREFIX_ID, "", 1)
45 8d97deff Vassilios Karakoidas
    if not ns.isdigit():
46 8d97deff Vassilios Karakoidas
        raise VirtualMachine.InvalidBackendIdError(str(name))
47 8d97deff Vassilios Karakoidas
48 8d97deff Vassilios Karakoidas
    return int(ns)
49 8d97deff Vassilios Karakoidas
50 8d97deff Vassilios Karakoidas
def get_rsapi_state(vm):
51 fc30c430 Vangelis Koukis
    """Returns the API state for a virtual machine
52 f16c6944 Kostas Papadimitriou

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

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

59 fc30c430 Vangelis Koukis
      The last state reported by Ganeti is set whenever Ganeti reports
60 fc30c430 Vangelis Koukis
      successful completion of an operation. If Ganeti says an OP_INSTANCE_STARTUP
61 fc30c430 Vangelis Koukis
      operation succeeded, vm.operstate is set to "STARTED".
62 fc30c430 Vangelis Koukis

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

69 fc30c430 Vangelis Koukis
    """
70 8d97deff Vassilios Karakoidas
    try:
71 dfd19c2d Vassilios Karakoidas
        r = VirtualMachine.RSAPI_STATE_FROM_OPER_STATE[vm.operstate]
72 8d97deff Vassilios Karakoidas
    except KeyError:
73 8d97deff Vassilios Karakoidas
        return "UNKNOWN"
74 685b219e Vangelis Koukis
    # A machine is DELETED if the deleted flag has been set
75 685b219e Vangelis Koukis
    if vm.deleted:
76 685b219e Vangelis Koukis
        return "DELETED"
77 8d97deff Vassilios Karakoidas
    # A machine is in REBOOT if an OP_INSTANCE_REBOOT request is in progress
78 dfd19c2d Vassilios Karakoidas
    if r == 'ACTIVE' and vm.backendopcode == 'OP_INSTANCE_REBOOT' and \
79 fb381fbe Markos Gogoulos
        vm.backendjobstatus in ('queued', 'waiting', 'running'):
80 8d97deff Vassilios Karakoidas
        return "REBOOT"
81 8d97deff Vassilios Karakoidas
    return r
82 8d97deff Vassilios Karakoidas
83 dfd19c2d Vassilios Karakoidas
def update_state(vm, new_operstate):
84 dfd19c2d Vassilios Karakoidas
    """Wrapper around updates of the VirtualMachine.operstate field"""
85 8d97deff Vassilios Karakoidas
86 dfd19c2d Vassilios Karakoidas
    vm.operstate = new_operstate