Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / api / management / commands / server-create.py @ db908bb1

History | View | Annotate | Download (5.4 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 BaseCommand, CommandError
37
from synnefo.management import common, pprint
38
from snf_django.management.utils import parse_bool
39

    
40
from synnefo.logic import servers
41

    
42
HELP_MSG = """
43

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

    
49

    
50
class Command(BaseCommand):
51
    help = "Create a new VM." + HELP_MSG
52

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

    
86
    )
87

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

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

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

    
111
        flavor = common.get_flavor(flavor_id)
112
        image = common.get_image(image_id, user_id)
113
        if backend_id:
114
            backend = common.get_backend(backend_id)
115
        else:
116
            backend = None
117

    
118
        network_ids = parse_list(options["network_ids"])
119
        port_ids = parse_list(options["port_ids"])
120
        floating_ip_ids = parse_list(options["floating_ip_ids"])
121

    
122
        networks = map(lambda x: {"uuid": x}, network_ids)
123
        ports = map(lambda x: {"port": x}, port_ids)
124

    
125
        server = servers.create(user_id, name, password, flavor, image,
126
                                networks=(ports+networks),
127
                                floating_ips=floating_ip_ids,
128
                                use_backend=backend)
129
        pprint.pprint_server(server, stdout=self.stdout)
130

    
131
        wait = parse_bool(options["wait"])
132
        common.wait_server_task(server, wait, self.stdout)
133

    
134

    
135
def parse_list(_list):
136
    if _list is None:
137
        return []
138
    else:
139
        return _list.split(",")