root / snf-cyclades-app / synnefo / management / common.py @ a6e6fe48
History | View | Annotate | Download (6.1 kB)
1 |
# Copyright 2012-2014 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, |
36 |
Flavor, IPAddress, Subnet, |
37 |
BridgePoolTable, MacPrefixPoolTable, |
38 |
NetworkInterface) |
39 |
from functools import wraps |
40 |
|
41 |
from django.conf import settings |
42 |
from snf_django.lib.api import faults |
43 |
from synnefo.api import util |
44 |
from synnefo.logic import backend as backend_mod |
45 |
from synnefo.logic.rapi import GanetiApiError, GanetiRapiClient |
46 |
from synnefo.logic.utils import (id_from_instance_name, |
47 |
id_from_network_name, |
48 |
id_from_nic_name) |
49 |
from django.core.exceptions import ObjectDoesNotExist |
50 |
import logging |
51 |
log = logging.getLogger(__name__) |
52 |
|
53 |
|
54 |
def format_vm_state(vm): |
55 |
if vm.operstate == "BUILD": |
56 |
return "BUILD(" + str(vm.buildpercentage) + "%)" |
57 |
else:
|
58 |
return vm.operstate
|
59 |
|
60 |
RESOURCE_MAP = { |
61 |
"backend": Backend.objects,
|
62 |
"flavor": Flavor.objects,
|
63 |
"server": VirtualMachine.objects,
|
64 |
"network": Network.objects,
|
65 |
"subnet": Subnet.objects,
|
66 |
"port": NetworkInterface.objects,
|
67 |
"floating-ip": IPAddress.objects.filter(floating_ip=True)} |
68 |
|
69 |
|
70 |
def get_resource(name, value, for_update=False): |
71 |
"""Get object from DB based by it's ID
|
72 |
|
73 |
Helper function for getting an object from DB by it's DB and raising
|
74 |
appropriate command line errors if the object does not exist or the
|
75 |
ID is invalid.
|
76 |
|
77 |
"""
|
78 |
objects = RESOURCE_MAP[name] |
79 |
if name == "floating-ip": |
80 |
capital_name = "Floating IP"
|
81 |
else:
|
82 |
capital_name = name.capitalize() |
83 |
|
84 |
if name in ["server", "network", "port"]: |
85 |
try:
|
86 |
if value.startswith(settings.BACKEND_PREFIX_ID):
|
87 |
if name == "server": |
88 |
value = id_from_instance_name(value) |
89 |
elif name == "network": |
90 |
value = id_from_network_name(value) |
91 |
elif name == "port": |
92 |
value = id_from_nic_name(value) |
93 |
except ValueError: |
94 |
raise CommandError("Invalid {} ID: {}".format(capital_name, value)) |
95 |
|
96 |
if for_update:
|
97 |
objects = objects.select_for_update() |
98 |
try:
|
99 |
return objects.get(id=value)
|
100 |
except ObjectDoesNotExist:
|
101 |
msg = ("{0} with ID {1} does not exist. Use {2}-list to find out"
|
102 |
" available {2} IDs.")
|
103 |
raise CommandError(msg.format(capital_name, value, name))
|
104 |
except (ValueError, TypeError): |
105 |
raise CommandError("Invalid {} ID: {}".format(capital_name, value)) |
106 |
|
107 |
|
108 |
def get_image(image_id, user_id): |
109 |
if image_id:
|
110 |
try:
|
111 |
return util.get_image_dict(image_id, user_id)
|
112 |
except faults.ItemNotFound:
|
113 |
raise CommandError("Image with ID %s not found." |
114 |
" Use snf-manage image-list to find"
|
115 |
" out available image IDs." % image_id)
|
116 |
else:
|
117 |
raise CommandError("image-id is mandatory") |
118 |
|
119 |
|
120 |
def check_backend_credentials(clustername, port, username, password): |
121 |
try:
|
122 |
client = GanetiRapiClient(clustername, port, username, password) |
123 |
# This command will raise an exception if there is no
|
124 |
# write-access
|
125 |
client.ModifyCluster() |
126 |
except GanetiApiError as e: |
127 |
raise CommandError(e)
|
128 |
|
129 |
info = client.GetInfo() |
130 |
info_name = info['name']
|
131 |
if info_name != clustername:
|
132 |
raise CommandError("Invalid clustername value. Please use the" |
133 |
" Ganeti Cluster name: %s" % info_name)
|
134 |
|
135 |
|
136 |
def convert_api_faults(func): |
137 |
@wraps(func)
|
138 |
def wrapper(*args, **kwargs): |
139 |
try:
|
140 |
return func(*args, **kwargs)
|
141 |
except faults.Fault as e: |
142 |
raise CommandError(e.message)
|
143 |
return wrapper
|
144 |
|
145 |
|
146 |
class Omit(object): |
147 |
pass
|
148 |
|
149 |
|
150 |
def wait_server_task(server, wait, stdout): |
151 |
jobID = server.task_job_id |
152 |
if jobID is None: |
153 |
return
|
154 |
if wait:
|
155 |
msg = "Issued job '%s'. Waiting to complete...\n"
|
156 |
stdout.write(msg % jobID) |
157 |
client = server.get_client() |
158 |
wait_ganeti_job(client, jobID, stdout) |
159 |
else:
|
160 |
msg = "Issued job '%s'.\n"
|
161 |
stdout.write(msg % jobID) |
162 |
|
163 |
|
164 |
def wait_ganeti_job(client, jobID, stdout): |
165 |
status, error = backend_mod.wait_for_job(client, jobID) |
166 |
if status == "success": |
167 |
stdout.write("Job finished successfully.\n")
|
168 |
else:
|
169 |
raise CommandError("Job failed! Error: %s\n" % error) |
170 |
|
171 |
|
172 |
def pool_table_from_type(type_): |
173 |
if type_ == "mac-prefix": |
174 |
return MacPrefixPoolTable
|
175 |
elif type_ == "bridge": |
176 |
return BridgePoolTable
|
177 |
# elif type == "ip":
|
178 |
# return IPPoolTable
|
179 |
else:
|
180 |
raise ValueError("Invalid pool type") |