Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / logic / management / commands / server-create.py @ b6426ead

History | View | Annotate | Download (6.4 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 optparse import make_option
35

    
36
from django.core.management.base import CommandError
37

    
38
from synnefo.management import common, pprint
39
from snf_django.management.utils import parse_bool
40
from snf_django.management.commands import SynnefoCommand
41

    
42
from synnefo.logic import servers
43

    
44
HELP_MSG = """
45

46
Create a new VM without authenticating the user or checking the resource
47
limits of the user. Also the allocator can be bypassed by specifing a
48
backend-id.
49
"""
50

    
51

    
52
class Command(SynnefoCommand):
53
    help = "Create a new VM." + HELP_MSG
54

    
55
    option_list = SynnefoCommand.option_list + (
56
        make_option("--backend-id", dest="backend_id",
57
                    help="Unique identifier of the Ganeti backend."
58
                         " Use snf-manage backend-list to find out"
59
                         " available backends."),
60
        make_option("--name", dest="name",
61
                    help="An arbitrary string for naming the server"),
62
        make_option("--user-id", dest="user_id",
63
                    help="Unique identifier of the owner of the server"),
64
        make_option("--image-id", dest="image_id",
65
                    help="Unique identifier of the image."
66
                         " Use snf-manage image-list to find out"
67
                         " available images."),
68
        make_option("--flavor-id", dest="flavor_id",
69
                    help="Unique identifier of the flavor"
70
                         " Use snf-manage flavor-list to find out"
71
                         " available flavors."),
72
        make_option("--password", dest="password",
73
                    help="Password for the new server"),
74
        make_option("--port", dest="connections", action="append",
75
                    help="--port network:<network_id>(,address=<ip_address>),"
76
                         " --port id:<port_id>"
77
                         " --port floatingip:<floatingip_id>."),
78
        make_option("--floating-ips", dest="floating_ip_ids",
79
                    help="Comma separated list of port IDs to connect"),
80
        make_option(
81
            '--wait',
82
            dest='wait',
83
            default="False",
84
            choices=["True", "False"],
85
            metavar="True|False",
86
            help="Wait for Ganeti job to complete."),
87

    
88
    )
89

    
90
    @common.convert_api_faults
91
    def handle(self, *args, **options):
92
        if args:
93
            raise CommandError("Command doesn't accept any arguments")
94

    
95
        name = options['name']
96
        user_id = options['user_id']
97
        backend_id = options['backend_id']
98
        image_id = options['image_id']
99
        flavor_id = options['flavor_id']
100
        password = options['password']
101

    
102
        if not name:
103
            raise CommandError("name is mandatory")
104
        if not user_id:
105
            raise CommandError("user-id is mandatory")
106
        if not password:
107
            raise CommandError("password is mandatory")
108
        if not flavor_id:
109
            raise CommandError("flavor-id is mandatory")
110
        if not image_id:
111
            raise CommandError("image-id is mandatory")
112

    
113
        flavor = common.get_resource("flavor", flavor_id)
114
        image = common.get_image(image_id, user_id)
115
        if backend_id:
116
            backend = common.get_resource("backend", backend_id)
117
        else:
118
            backend = None
119

    
120
        connection_list = parse_connections(options["connections"])
121
        server = servers.create(user_id, name, password, flavor, image,
122
                                networks=connection_list,
123
                                use_backend=backend)
124
        pprint.pprint_server(server, stdout=self.stdout)
125

    
126
        wait = parse_bool(options["wait"])
127
        common.wait_server_task(server, wait, self.stdout)
128

    
129

    
130
def parse_connections(con_list):
131
    connections = []
132
    if con_list:
133
        for opt in con_list:
134
            try:
135
                con_kind = opt.split(":")[0]
136
                if con_kind == "network":
137
                    info = opt.split(",")
138
                    network_id = info[0].split(":")[1]
139
                    try:
140
                        address = info[1].split(":")[1]
141
                    except:
142
                        address = None
143
                    if address:
144
                        val = {"uuid": network_id, "fixed_ip": address}
145
                    else:
146
                        val = {"uuid": network_id}
147
                elif con_kind == "id":
148
                    port_id = opt.split(":")[1]
149
                    val = {"port": port_id}
150
                elif con_kind == "floatingip":
151
                    fip_id = opt.split(":")[1]
152
                    fip = common.get_resource("floating-ip", fip_id,
153
                                              for_update=True)
154
                    val = {"uuid": fip.network_id, "fixed_ip": fip.address}
155
                else:
156
                    raise CommandError("Unknown argument for option --port")
157

    
158
                connections.append(val)
159
            except:
160
                raise CommandError("Malformed information for option --port")
161
    return connections