root / snf-cyclades-app / synnefo / management / common.py @ bcd80cd9
History | View | Annotate | Download (5.7 kB)
1 |
# Copyright 2012 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
from django.core.management import CommandError |
35 |
from synnefo.db.models import Backend, VirtualMachine, Network, Flavor |
36 |
|
37 |
from snf_django.lib.api import faults |
38 |
from synnefo.api import util |
39 |
from synnefo.logic.rapi import GanetiApiError, GanetiRapiClient |
40 |
from synnefo.logic.utils import (id_from_instance_name, |
41 |
id_from_network_name) |
42 |
|
43 |
import logging |
44 |
log = logging.getLogger(__name__) |
45 |
|
46 |
|
47 |
def format_vm_state(vm): |
48 |
if vm.operstate == "BUILD": |
49 |
return "BUILD(" + str(vm.buildpercentage) + "%)" |
50 |
else:
|
51 |
return vm.operstate
|
52 |
|
53 |
|
54 |
def validate_network_info(options): |
55 |
subnet = options['subnet']
|
56 |
gateway = options['gateway']
|
57 |
subnet6 = options['subnet6']
|
58 |
gateway6 = options['gateway6']
|
59 |
|
60 |
try:
|
61 |
util.validate_network_params(subnet, gateway) |
62 |
except (faults.BadRequest, faults.OverLimit) as e: |
63 |
raise CommandError(e)
|
64 |
|
65 |
return subnet, gateway, subnet6, gateway6
|
66 |
|
67 |
|
68 |
def get_backend(backend_id): |
69 |
try:
|
70 |
backend_id = int(backend_id)
|
71 |
return Backend.objects.get(id=backend_id)
|
72 |
except ValueError: |
73 |
raise CommandError("Invalid Backend ID: %s" % backend_id) |
74 |
except Backend.DoesNotExist:
|
75 |
raise CommandError("Backend with ID %s not found in DB. " |
76 |
" Use snf-manage backend-list to find"
|
77 |
" out available backend IDs." % backend_id)
|
78 |
|
79 |
|
80 |
def get_image(image_id, user_id): |
81 |
if image_id:
|
82 |
try:
|
83 |
return util.get_image_dict(image_id, user_id)
|
84 |
except faults.ItemNotFound:
|
85 |
raise CommandError("Image with ID %s not found." |
86 |
" Use snf-manage image-list to find"
|
87 |
" out available image IDs." % image_id)
|
88 |
else:
|
89 |
raise CommandError("image-id is mandatory") |
90 |
|
91 |
|
92 |
def get_vm(server_id): |
93 |
"""Get a VirtualMachine object by its ID.
|
94 |
|
95 |
@type server_id: int or string
|
96 |
@param server_id: The server's DB id or the Ganeti name
|
97 |
|
98 |
"""
|
99 |
try:
|
100 |
server_id = int(server_id)
|
101 |
except (ValueError, TypeError): |
102 |
try:
|
103 |
server_id = id_from_instance_name(server_id) |
104 |
except VirtualMachine.InvalidBackendIdError:
|
105 |
raise CommandError("Invalid server ID: %s" % server_id) |
106 |
|
107 |
try:
|
108 |
return VirtualMachine.objects.get(id=server_id)
|
109 |
except VirtualMachine.DoesNotExist:
|
110 |
raise CommandError("Server with ID %s not found in DB." |
111 |
" Use snf-manage server-list to find out"
|
112 |
" available server IDs." % server_id)
|
113 |
|
114 |
|
115 |
def get_network(network_id): |
116 |
"""Get a Network object by its ID.
|
117 |
|
118 |
@type network_id: int or string
|
119 |
@param network_id: The networks DB id or the Ganeti name
|
120 |
|
121 |
"""
|
122 |
|
123 |
try:
|
124 |
network_id = int(network_id)
|
125 |
except (ValueError, TypeError): |
126 |
try:
|
127 |
network_id = id_from_network_name(network_id) |
128 |
except Network.InvalidBackendIdError:
|
129 |
raise CommandError("Invalid network ID: %s" % network_id) |
130 |
|
131 |
try:
|
132 |
return Network.objects.get(id=network_id)
|
133 |
except Network.DoesNotExist:
|
134 |
raise CommandError("Network with ID %s not found in DB." |
135 |
" Use snf-manage network-list to find out"
|
136 |
" available network IDs." % network_id)
|
137 |
|
138 |
|
139 |
def get_flavor(flavor_id): |
140 |
try:
|
141 |
flavor_id = int(flavor_id)
|
142 |
return Flavor.objects.get(id=flavor_id)
|
143 |
except ValueError: |
144 |
raise CommandError("Invalid flavor ID: %s", flavor_id) |
145 |
except Flavor.DoesNotExist:
|
146 |
raise CommandError("Flavor with ID %s not found in DB." |
147 |
" Use snf-manage flavor-list to find out"
|
148 |
" available flavor IDs." % flavor_id)
|
149 |
|
150 |
|
151 |
def check_backend_credentials(clustername, port, username, password): |
152 |
try:
|
153 |
client = GanetiRapiClient(clustername, port, username, password) |
154 |
# This command will raise an exception if there is no
|
155 |
# write-access
|
156 |
client.ModifyCluster() |
157 |
except GanetiApiError as e: |
158 |
raise CommandError(e)
|
159 |
|
160 |
info = client.GetInfo() |
161 |
info_name = info['name']
|
162 |
if info_name != clustername:
|
163 |
raise CommandError("Invalid clustername value. Please use the" |
164 |
" Ganeti Cluster name: %s" % info_name)
|
165 |
|
166 |
|
167 |
class Omit(object): |
168 |
pass
|