Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (6.4 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 c75ab92e Christos Stavrakakis
from functools import wraps
37 bad9404c Christos Stavrakakis
38 bd40abfa Christos Stavrakakis
from snf_django.lib.api import faults
39 bcd80cd9 Christos Stavrakakis
from synnefo.api import util
40 35f2fc07 Christos Stavrakakis
from synnefo.logic import backend as backend_mod
41 2333a2c4 Christos Stavrakakis
from synnefo.logic.rapi import GanetiApiError, GanetiRapiClient
42 0252fb8b Christos Stavrakakis
from synnefo.logic.utils import (id_from_instance_name,
43 0252fb8b Christos Stavrakakis
                                 id_from_network_name)
44 11300d7b Giorgos Verigakis
45 8814e07c Christos Stavrakakis
import logging
46 8814e07c Christos Stavrakakis
log = logging.getLogger(__name__)
47 8814e07c Christos Stavrakakis
48 11300d7b Giorgos Verigakis
49 f66d8b04 Christos Stavrakakis
def format_vm_state(vm):
50 f66d8b04 Christos Stavrakakis
    if vm.operstate == "BUILD":
51 f66d8b04 Christos Stavrakakis
        return "BUILD(" + str(vm.buildpercentage) + "%)"
52 f66d8b04 Christos Stavrakakis
    else:
53 f66d8b04 Christos Stavrakakis
        return vm.operstate
54 53b9ba10 Christos Stavrakakis
55 53b9ba10 Christos Stavrakakis
56 b84ed662 Christos Stavrakakis
def get_backend(backend_id):
57 b84ed662 Christos Stavrakakis
    try:
58 b84ed662 Christos Stavrakakis
        backend_id = int(backend_id)
59 b84ed662 Christos Stavrakakis
        return Backend.objects.get(id=backend_id)
60 b84ed662 Christos Stavrakakis
    except ValueError:
61 b84ed662 Christos Stavrakakis
        raise CommandError("Invalid Backend ID: %s" % backend_id)
62 b84ed662 Christos Stavrakakis
    except Backend.DoesNotExist:
63 b84ed662 Christos Stavrakakis
        raise CommandError("Backend with ID %s not found in DB. "
64 b84ed662 Christos Stavrakakis
                           " Use snf-manage backend-list to find"
65 b84ed662 Christos Stavrakakis
                           " out available backend IDs." % backend_id)
66 b84ed662 Christos Stavrakakis
67 b84ed662 Christos Stavrakakis
68 b84ed662 Christos Stavrakakis
def get_image(image_id, user_id):
69 b84ed662 Christos Stavrakakis
    if image_id:
70 b84ed662 Christos Stavrakakis
        try:
71 bcd80cd9 Christos Stavrakakis
            return util.get_image_dict(image_id, user_id)
72 bd40abfa Christos Stavrakakis
        except faults.ItemNotFound:
73 b84ed662 Christos Stavrakakis
            raise CommandError("Image with ID %s not found."
74 b84ed662 Christos Stavrakakis
                               " Use snf-manage image-list to find"
75 b84ed662 Christos Stavrakakis
                               " out available image IDs." % image_id)
76 b84ed662 Christos Stavrakakis
    else:
77 b84ed662 Christos Stavrakakis
        raise CommandError("image-id is mandatory")
78 b84ed662 Christos Stavrakakis
79 b84ed662 Christos Stavrakakis
80 b84ed662 Christos Stavrakakis
def get_vm(server_id):
81 0252fb8b Christos Stavrakakis
    """Get a VirtualMachine object by its ID.
82 0252fb8b Christos Stavrakakis

83 0252fb8b Christos Stavrakakis
    @type server_id: int or string
84 0252fb8b Christos Stavrakakis
    @param server_id: The server's DB id or the Ganeti name
85 0252fb8b Christos Stavrakakis

86 0252fb8b Christos Stavrakakis
    """
87 b84ed662 Christos Stavrakakis
    try:
88 b84ed662 Christos Stavrakakis
        server_id = int(server_id)
89 0252fb8b Christos Stavrakakis
    except (ValueError, TypeError):
90 0252fb8b Christos Stavrakakis
        try:
91 0252fb8b Christos Stavrakakis
            server_id = id_from_instance_name(server_id)
92 0252fb8b Christos Stavrakakis
        except VirtualMachine.InvalidBackendIdError:
93 0252fb8b Christos Stavrakakis
            raise CommandError("Invalid server ID: %s" % server_id)
94 0252fb8b Christos Stavrakakis
95 0252fb8b Christos Stavrakakis
    try:
96 b84ed662 Christos Stavrakakis
        return VirtualMachine.objects.get(id=server_id)
97 b84ed662 Christos Stavrakakis
    except VirtualMachine.DoesNotExist:
98 b84ed662 Christos Stavrakakis
        raise CommandError("Server with ID %s not found in DB."
99 b84ed662 Christos Stavrakakis
                           " Use snf-manage server-list to find out"
100 b84ed662 Christos Stavrakakis
                           " available server IDs." % server_id)
101 b84ed662 Christos Stavrakakis
102 b84ed662 Christos Stavrakakis
103 e57893cd Christos Stavrakakis
def get_network(network_id, for_update=True):
104 0252fb8b Christos Stavrakakis
    """Get a Network object by its ID.
105 0252fb8b Christos Stavrakakis

106 0252fb8b Christos Stavrakakis
    @type network_id: int or string
107 0252fb8b Christos Stavrakakis
    @param network_id: The networks DB id or the Ganeti name
108 0252fb8b Christos Stavrakakis

109 0252fb8b Christos Stavrakakis
    """
110 0252fb8b Christos Stavrakakis
111 b84ed662 Christos Stavrakakis
    try:
112 b84ed662 Christos Stavrakakis
        network_id = int(network_id)
113 0252fb8b Christos Stavrakakis
    except (ValueError, TypeError):
114 0252fb8b Christos Stavrakakis
        try:
115 0252fb8b Christos Stavrakakis
            network_id = id_from_network_name(network_id)
116 0252fb8b Christos Stavrakakis
        except Network.InvalidBackendIdError:
117 0252fb8b Christos Stavrakakis
            raise CommandError("Invalid network ID: %s" % network_id)
118 0252fb8b Christos Stavrakakis
119 e57893cd Christos Stavrakakis
    networks = Network.objects
120 e57893cd Christos Stavrakakis
    if for_update:
121 e57893cd Christos Stavrakakis
        networks = networks.select_for_update()
122 0252fb8b Christos Stavrakakis
    try:
123 e57893cd Christos Stavrakakis
        return networks.get(id=network_id)
124 b84ed662 Christos Stavrakakis
    except Network.DoesNotExist:
125 b84ed662 Christos Stavrakakis
        raise CommandError("Network with ID %s not found in DB."
126 b84ed662 Christos Stavrakakis
                           " Use snf-manage network-list to find out"
127 b84ed662 Christos Stavrakakis
                           " available network IDs." % network_id)
128 b84ed662 Christos Stavrakakis
129 b84ed662 Christos Stavrakakis
130 b84ed662 Christos Stavrakakis
def get_flavor(flavor_id):
131 b84ed662 Christos Stavrakakis
    try:
132 b84ed662 Christos Stavrakakis
        flavor_id = int(flavor_id)
133 b84ed662 Christos Stavrakakis
        return Flavor.objects.get(id=flavor_id)
134 b84ed662 Christos Stavrakakis
    except ValueError:
135 b84ed662 Christos Stavrakakis
        raise CommandError("Invalid flavor ID: %s", flavor_id)
136 b84ed662 Christos Stavrakakis
    except Flavor.DoesNotExist:
137 b84ed662 Christos Stavrakakis
        raise CommandError("Flavor with ID %s not found in DB."
138 b84ed662 Christos Stavrakakis
                           " Use snf-manage flavor-list to find out"
139 b84ed662 Christos Stavrakakis
                           " available flavor IDs." % flavor_id)
140 bad9404c Christos Stavrakakis
141 bad9404c Christos Stavrakakis
142 2333a2c4 Christos Stavrakakis
def check_backend_credentials(clustername, port, username, password):
143 2333a2c4 Christos Stavrakakis
    try:
144 2333a2c4 Christos Stavrakakis
        client = GanetiRapiClient(clustername, port, username, password)
145 2333a2c4 Christos Stavrakakis
        # This command will raise an exception if there is no
146 2333a2c4 Christos Stavrakakis
        # write-access
147 2333a2c4 Christos Stavrakakis
        client.ModifyCluster()
148 2333a2c4 Christos Stavrakakis
    except GanetiApiError as e:
149 2333a2c4 Christos Stavrakakis
        raise CommandError(e)
150 2333a2c4 Christos Stavrakakis
151 2333a2c4 Christos Stavrakakis
    info = client.GetInfo()
152 2333a2c4 Christos Stavrakakis
    info_name = info['name']
153 2333a2c4 Christos Stavrakakis
    if info_name != clustername:
154 2333a2c4 Christos Stavrakakis
        raise CommandError("Invalid clustername value. Please use the"
155 2333a2c4 Christos Stavrakakis
                           " Ganeti Cluster name: %s" % info_name)
156 7a0aa449 Christos Stavrakakis
157 7a0aa449 Christos Stavrakakis
158 c75ab92e Christos Stavrakakis
def convert_api_faults(func):
159 c75ab92e Christos Stavrakakis
    @wraps(func)
160 c75ab92e Christos Stavrakakis
    def wrapper(*args, **kwargs):
161 c75ab92e Christos Stavrakakis
        try:
162 c75ab92e Christos Stavrakakis
            return func(*args, **kwargs)
163 c75ab92e Christos Stavrakakis
        except faults.Fault as e:
164 c75ab92e Christos Stavrakakis
            raise CommandError(e.message)
165 c75ab92e Christos Stavrakakis
    return wrapper
166 c75ab92e Christos Stavrakakis
167 c75ab92e Christos Stavrakakis
168 4500650c Stratos Psomadakis
class Omit(object):
169 4500650c Stratos Psomadakis
    pass
170 35f2fc07 Christos Stavrakakis
171 35f2fc07 Christos Stavrakakis
172 35f2fc07 Christos Stavrakakis
def wait_server_task(server, wait, stdout):
173 35f2fc07 Christos Stavrakakis
    jobID = server.task_job_id
174 35f2fc07 Christos Stavrakakis
    if wait:
175 35f2fc07 Christos Stavrakakis
        msg = "Issued job '%s'. Waiting to complete...\n"
176 35f2fc07 Christos Stavrakakis
        stdout.write(msg % jobID)
177 35f2fc07 Christos Stavrakakis
        client = server.get_client()
178 35f2fc07 Christos Stavrakakis
        wait_ganeti_job(client, jobID, stdout)
179 35f2fc07 Christos Stavrakakis
    else:
180 35f2fc07 Christos Stavrakakis
        msg = "Issued job '%s'.\n"
181 35f2fc07 Christos Stavrakakis
        stdout.write(msg % jobID)
182 35f2fc07 Christos Stavrakakis
183 35f2fc07 Christos Stavrakakis
184 35f2fc07 Christos Stavrakakis
def wait_ganeti_job(client, jobID, stdout):
185 35f2fc07 Christos Stavrakakis
    status, error = backend_mod.wait_for_job(client, jobID)
186 35f2fc07 Christos Stavrakakis
    if status == "success":
187 35f2fc07 Christos Stavrakakis
        stdout.write("Job finished successfully.\n")
188 35f2fc07 Christos Stavrakakis
    else:
189 35f2fc07 Christos Stavrakakis
        raise CommandError("Job failed! Error: %s\n" % error)