Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / management / common.py @ e57893cd

History | View | Annotate | Download (5.8 kB)

1 11300d7b Giorgos Verigakis
# Copyright 2012 GRNET S.A. All rights reserved.
2 11300d7b Giorgos Verigakis
#
3 11300d7b Giorgos Verigakis
# Redistribution and use in source and binary forms, with or
4 11300d7b Giorgos Verigakis
# without modification, are permitted provided that the following
5 11300d7b Giorgos Verigakis
# conditions are met:
6 11300d7b Giorgos Verigakis
#
7 11300d7b Giorgos Verigakis
#   1. Redistributions of source code must retain the above
8 11300d7b Giorgos Verigakis
#      copyright notice, this list of conditions and the following
9 11300d7b Giorgos Verigakis
#      disclaimer.
10 11300d7b Giorgos Verigakis
#
11 11300d7b Giorgos Verigakis
#   2. Redistributions in binary form must reproduce the above
12 11300d7b Giorgos Verigakis
#      copyright notice, this list of conditions and the following
13 11300d7b Giorgos Verigakis
#      disclaimer in the documentation and/or other materials
14 11300d7b Giorgos Verigakis
#      provided with the distribution.
15 11300d7b Giorgos Verigakis
#
16 11300d7b Giorgos Verigakis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 11300d7b Giorgos Verigakis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 11300d7b Giorgos Verigakis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 11300d7b Giorgos Verigakis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 11300d7b Giorgos Verigakis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 11300d7b Giorgos Verigakis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 11300d7b Giorgos Verigakis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 11300d7b Giorgos Verigakis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 11300d7b Giorgos Verigakis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 11300d7b Giorgos Verigakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 11300d7b Giorgos Verigakis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 11300d7b Giorgos Verigakis
# POSSIBILITY OF SUCH DAMAGE.
28 11300d7b Giorgos Verigakis
#
29 11300d7b Giorgos Verigakis
# The views and conclusions contained in the software and
30 11300d7b Giorgos Verigakis
# documentation are those of the authors and should not be
31 11300d7b Giorgos Verigakis
# interpreted as representing official policies, either expressed
32 11300d7b Giorgos Verigakis
# or implied, of GRNET S.A.
33 11300d7b Giorgos Verigakis
34 53b9ba10 Christos Stavrakakis
from django.core.management import CommandError
35 b84ed662 Christos Stavrakakis
from synnefo.db.models import Backend, VirtualMachine, Network, Flavor
36 bad9404c Christos Stavrakakis
37 bd40abfa Christos Stavrakakis
from snf_django.lib.api import faults
38 bcd80cd9 Christos Stavrakakis
from synnefo.api import util
39 2333a2c4 Christos Stavrakakis
from synnefo.logic.rapi import GanetiApiError, GanetiRapiClient
40 0252fb8b Christos Stavrakakis
from synnefo.logic.utils import (id_from_instance_name,
41 0252fb8b Christos Stavrakakis
                                 id_from_network_name)
42 11300d7b Giorgos Verigakis
43 8814e07c Christos Stavrakakis
import logging
44 8814e07c Christos Stavrakakis
log = logging.getLogger(__name__)
45 8814e07c Christos Stavrakakis
46 11300d7b Giorgos Verigakis
47 f66d8b04 Christos Stavrakakis
def format_vm_state(vm):
48 f66d8b04 Christos Stavrakakis
    if vm.operstate == "BUILD":
49 f66d8b04 Christos Stavrakakis
        return "BUILD(" + str(vm.buildpercentage) + "%)"
50 f66d8b04 Christos Stavrakakis
    else:
51 f66d8b04 Christos Stavrakakis
        return vm.operstate
52 53b9ba10 Christos Stavrakakis
53 53b9ba10 Christos Stavrakakis
54 53b9ba10 Christos Stavrakakis
def validate_network_info(options):
55 53b9ba10 Christos Stavrakakis
    subnet = options['subnet']
56 53b9ba10 Christos Stavrakakis
    gateway = options['gateway']
57 53b9ba10 Christos Stavrakakis
    subnet6 = options['subnet6']
58 53b9ba10 Christos Stavrakakis
    gateway6 = options['gateway6']
59 53b9ba10 Christos Stavrakakis
60 53b9ba10 Christos Stavrakakis
    try:
61 bcd80cd9 Christos Stavrakakis
        util.validate_network_params(subnet, gateway)
62 bd40abfa Christos Stavrakakis
    except (faults.BadRequest, faults.OverLimit) as e:
63 9ae613af Christos Stavrakakis
        raise CommandError(e)
64 53b9ba10 Christos Stavrakakis
65 53b9ba10 Christos Stavrakakis
    return subnet, gateway, subnet6, gateway6
66 b84ed662 Christos Stavrakakis
67 b84ed662 Christos Stavrakakis
68 b84ed662 Christos Stavrakakis
def get_backend(backend_id):
69 b84ed662 Christos Stavrakakis
    try:
70 b84ed662 Christos Stavrakakis
        backend_id = int(backend_id)
71 b84ed662 Christos Stavrakakis
        return Backend.objects.get(id=backend_id)
72 b84ed662 Christos Stavrakakis
    except ValueError:
73 b84ed662 Christos Stavrakakis
        raise CommandError("Invalid Backend ID: %s" % backend_id)
74 b84ed662 Christos Stavrakakis
    except Backend.DoesNotExist:
75 b84ed662 Christos Stavrakakis
        raise CommandError("Backend with ID %s not found in DB. "
76 b84ed662 Christos Stavrakakis
                           " Use snf-manage backend-list to find"
77 b84ed662 Christos Stavrakakis
                           " out available backend IDs." % backend_id)
78 b84ed662 Christos Stavrakakis
79 b84ed662 Christos Stavrakakis
80 b84ed662 Christos Stavrakakis
def get_image(image_id, user_id):
81 b84ed662 Christos Stavrakakis
    if image_id:
82 b84ed662 Christos Stavrakakis
        try:
83 bcd80cd9 Christos Stavrakakis
            return util.get_image_dict(image_id, user_id)
84 bd40abfa Christos Stavrakakis
        except faults.ItemNotFound:
85 b84ed662 Christos Stavrakakis
            raise CommandError("Image with ID %s not found."
86 b84ed662 Christos Stavrakakis
                               " Use snf-manage image-list to find"
87 b84ed662 Christos Stavrakakis
                               " out available image IDs." % image_id)
88 b84ed662 Christos Stavrakakis
    else:
89 b84ed662 Christos Stavrakakis
        raise CommandError("image-id is mandatory")
90 b84ed662 Christos Stavrakakis
91 b84ed662 Christos Stavrakakis
92 b84ed662 Christos Stavrakakis
def get_vm(server_id):
93 0252fb8b Christos Stavrakakis
    """Get a VirtualMachine object by its ID.
94 0252fb8b Christos Stavrakakis

95 0252fb8b Christos Stavrakakis
    @type server_id: int or string
96 0252fb8b Christos Stavrakakis
    @param server_id: The server's DB id or the Ganeti name
97 0252fb8b Christos Stavrakakis

98 0252fb8b Christos Stavrakakis
    """
99 b84ed662 Christos Stavrakakis
    try:
100 b84ed662 Christos Stavrakakis
        server_id = int(server_id)
101 0252fb8b Christos Stavrakakis
    except (ValueError, TypeError):
102 0252fb8b Christos Stavrakakis
        try:
103 0252fb8b Christos Stavrakakis
            server_id = id_from_instance_name(server_id)
104 0252fb8b Christos Stavrakakis
        except VirtualMachine.InvalidBackendIdError:
105 0252fb8b Christos Stavrakakis
            raise CommandError("Invalid server ID: %s" % server_id)
106 0252fb8b Christos Stavrakakis
107 0252fb8b Christos Stavrakakis
    try:
108 b84ed662 Christos Stavrakakis
        return VirtualMachine.objects.get(id=server_id)
109 b84ed662 Christos Stavrakakis
    except VirtualMachine.DoesNotExist:
110 b84ed662 Christos Stavrakakis
        raise CommandError("Server with ID %s not found in DB."
111 b84ed662 Christos Stavrakakis
                           " Use snf-manage server-list to find out"
112 b84ed662 Christos Stavrakakis
                           " available server IDs." % server_id)
113 b84ed662 Christos Stavrakakis
114 b84ed662 Christos Stavrakakis
115 e57893cd Christos Stavrakakis
def get_network(network_id, for_update=True):
116 0252fb8b Christos Stavrakakis
    """Get a Network object by its ID.
117 0252fb8b Christos Stavrakakis

118 0252fb8b Christos Stavrakakis
    @type network_id: int or string
119 0252fb8b Christos Stavrakakis
    @param network_id: The networks DB id or the Ganeti name
120 0252fb8b Christos Stavrakakis

121 0252fb8b Christos Stavrakakis
    """
122 0252fb8b Christos Stavrakakis
123 b84ed662 Christos Stavrakakis
    try:
124 b84ed662 Christos Stavrakakis
        network_id = int(network_id)
125 0252fb8b Christos Stavrakakis
    except (ValueError, TypeError):
126 0252fb8b Christos Stavrakakis
        try:
127 0252fb8b Christos Stavrakakis
            network_id = id_from_network_name(network_id)
128 0252fb8b Christos Stavrakakis
        except Network.InvalidBackendIdError:
129 0252fb8b Christos Stavrakakis
            raise CommandError("Invalid network ID: %s" % network_id)
130 0252fb8b Christos Stavrakakis
131 e57893cd Christos Stavrakakis
    networks = Network.objects
132 e57893cd Christos Stavrakakis
    if for_update:
133 e57893cd Christos Stavrakakis
        networks = networks.select_for_update()
134 0252fb8b Christos Stavrakakis
    try:
135 e57893cd Christos Stavrakakis
        return networks.get(id=network_id)
136 b84ed662 Christos Stavrakakis
    except Network.DoesNotExist:
137 b84ed662 Christos Stavrakakis
        raise CommandError("Network with ID %s not found in DB."
138 b84ed662 Christos Stavrakakis
                           " Use snf-manage network-list to find out"
139 b84ed662 Christos Stavrakakis
                           " available network IDs." % network_id)
140 b84ed662 Christos Stavrakakis
141 b84ed662 Christos Stavrakakis
142 b84ed662 Christos Stavrakakis
def get_flavor(flavor_id):
143 b84ed662 Christos Stavrakakis
    try:
144 b84ed662 Christos Stavrakakis
        flavor_id = int(flavor_id)
145 b84ed662 Christos Stavrakakis
        return Flavor.objects.get(id=flavor_id)
146 b84ed662 Christos Stavrakakis
    except ValueError:
147 b84ed662 Christos Stavrakakis
        raise CommandError("Invalid flavor ID: %s", flavor_id)
148 b84ed662 Christos Stavrakakis
    except Flavor.DoesNotExist:
149 b84ed662 Christos Stavrakakis
        raise CommandError("Flavor with ID %s not found in DB."
150 b84ed662 Christos Stavrakakis
                           " Use snf-manage flavor-list to find out"
151 b84ed662 Christos Stavrakakis
                           " available flavor IDs." % flavor_id)
152 bad9404c Christos Stavrakakis
153 bad9404c Christos Stavrakakis
154 2333a2c4 Christos Stavrakakis
def check_backend_credentials(clustername, port, username, password):
155 2333a2c4 Christos Stavrakakis
    try:
156 2333a2c4 Christos Stavrakakis
        client = GanetiRapiClient(clustername, port, username, password)
157 2333a2c4 Christos Stavrakakis
        # This command will raise an exception if there is no
158 2333a2c4 Christos Stavrakakis
        # write-access
159 2333a2c4 Christos Stavrakakis
        client.ModifyCluster()
160 2333a2c4 Christos Stavrakakis
    except GanetiApiError as e:
161 2333a2c4 Christos Stavrakakis
        raise CommandError(e)
162 2333a2c4 Christos Stavrakakis
163 2333a2c4 Christos Stavrakakis
    info = client.GetInfo()
164 2333a2c4 Christos Stavrakakis
    info_name = info['name']
165 2333a2c4 Christos Stavrakakis
    if info_name != clustername:
166 2333a2c4 Christos Stavrakakis
        raise CommandError("Invalid clustername value. Please use the"
167 2333a2c4 Christos Stavrakakis
                           " Ganeti Cluster name: %s" % info_name)
168 7a0aa449 Christos Stavrakakis
169 7a0aa449 Christos Stavrakakis
170 4500650c Stratos Psomadakis
class Omit(object):
171 4500650c Stratos Psomadakis
    pass