root / snf-cyclades-app / synnefo / logic / management / commands / server-create.py @ 8c911970
History | View | Annotate | Download (6.3 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 optparse import make_option |
35 |
|
36 |
from django.core.management.base import CommandError |
37 |
from snf_django.management.commands import SynnefoCommand |
38 |
from synnefo.management import common, pprint |
39 |
from snf_django.management.utils import parse_bool |
40 |
|
41 |
from synnefo.logic import servers |
42 |
|
43 |
HELP_MSG = """
|
44 |
|
45 |
Create a new VM without authenticating the user or checking the resource
|
46 |
limits of the user. Also the allocator can be bypassed by specifing a
|
47 |
backend-id.
|
48 |
"""
|
49 |
|
50 |
|
51 |
class Command(SynnefoCommand): |
52 |
help = "Create a new VM." + HELP_MSG
|
53 |
|
54 |
option_list = SynnefoCommand.option_list + ( |
55 |
make_option("--backend-id", dest="backend_id", |
56 |
help="Unique identifier of the Ganeti backend."
|
57 |
" Use snf-manage backend-list to find out"
|
58 |
" available backends."),
|
59 |
make_option("--name", dest="name", |
60 |
help="An arbitrary string for naming the server"),
|
61 |
make_option("--user-id", dest="user_id", |
62 |
help="Unique identifier of the owner of the server"),
|
63 |
make_option("--image-id", dest="image_id", |
64 |
help="Unique identifier of the image."
|
65 |
" Use snf-manage image-list to find out"
|
66 |
" available images."),
|
67 |
make_option("--flavor-id", dest="flavor_id", |
68 |
help="Unique identifier of the flavor"
|
69 |
" Use snf-manage flavor-list to find out"
|
70 |
" available flavors."),
|
71 |
make_option("--password", dest="password", |
72 |
help="Password for the new server"),
|
73 |
make_option("--port", dest="connections", action="append", |
74 |
help="--port network:<network_id>(,address=<ip_address>),"
|
75 |
" --port id:<port_id>"
|
76 |
" --port floatingip:<floatingip_id>."),
|
77 |
make_option("--floating-ips", dest="floating_ip_ids", |
78 |
help="Comma separated list of port IDs to connect"),
|
79 |
make_option( |
80 |
'--wait',
|
81 |
dest='wait',
|
82 |
default="False",
|
83 |
choices=["True", "False"], |
84 |
metavar="True|False",
|
85 |
help="Wait for Ganeti job to complete."),
|
86 |
|
87 |
) |
88 |
|
89 |
@common.convert_api_faults
|
90 |
def handle(self, *args, **options): |
91 |
if args:
|
92 |
raise CommandError("Command doesn't accept any arguments") |
93 |
|
94 |
name = options['name']
|
95 |
user_id = options['user_id']
|
96 |
backend_id = options['backend_id']
|
97 |
image_id = options['image_id']
|
98 |
flavor_id = options['flavor_id']
|
99 |
password = options['password']
|
100 |
|
101 |
if not name: |
102 |
raise CommandError("name is mandatory") |
103 |
if not user_id: |
104 |
raise CommandError("user-id is mandatory") |
105 |
if not password: |
106 |
raise CommandError("password is mandatory") |
107 |
if not flavor_id: |
108 |
raise CommandError("flavor-id is mandatory") |
109 |
if not image_id: |
110 |
raise CommandError("image-id is mandatory") |
111 |
|
112 |
flavor = common.get_flavor(flavor_id) |
113 |
image = common.get_image(image_id, user_id) |
114 |
if backend_id:
|
115 |
backend = common.get_backend(backend_id) |
116 |
else:
|
117 |
backend = None
|
118 |
|
119 |
connection_list = parse_connections(options["connections"])
|
120 |
server = servers.create(user_id, name, password, flavor, image, |
121 |
networks=connection_list, |
122 |
use_backend=backend) |
123 |
pprint.pprint_server(server, stdout=self.stdout)
|
124 |
|
125 |
wait = parse_bool(options["wait"])
|
126 |
common.wait_server_task(server, wait, self.stdout)
|
127 |
|
128 |
|
129 |
def parse_connections(con_list): |
130 |
connections = [] |
131 |
if con_list:
|
132 |
for opt in con_list: |
133 |
try:
|
134 |
con_kind = opt.split(":")[0] |
135 |
if con_kind == "network": |
136 |
info = opt.split(",")
|
137 |
network_id = info[0].split(":")[1] |
138 |
try:
|
139 |
address = info[1].split(":")[1] |
140 |
except:
|
141 |
address = None
|
142 |
if address:
|
143 |
val = {"uuid": network_id, "fixed_ip": address} |
144 |
else:
|
145 |
val = {"uuid": network_id}
|
146 |
elif con_kind == "id": |
147 |
port_id = opt.split(":")[1] |
148 |
val = {"port": port_id}
|
149 |
elif con_kind == "floatingip": |
150 |
fip_id = opt.split(":")[1] |
151 |
fip = common.get_floating_ip_by_id(fip_id, for_update=True)
|
152 |
val = {"uuid": fip.network_id, "fixed_ip": fip.address} |
153 |
else:
|
154 |
raise CommandError("Unknown argument for option --port") |
155 |
|
156 |
connections.append(val) |
157 |
except:
|
158 |
raise CommandError("Malformed information for option --port") |
159 |
return connections
|