Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (6.9 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
import json
35
from optparse import make_option
36

    
37
from django.db import transaction
38
from django.core.management.base import BaseCommand, CommandError
39
from synnefo.management import common
40

    
41
from synnefo.db.models import VirtualMachine
42
from synnefo.logic.backend import create_instance
43
from synnefo.logic.backend_allocator import BackendAllocator
44
from synnefo.api import util
45
from synnefo.api.servers import server_created
46
from synnefo import quotas
47

    
48
HELP_MSG = """
49

50
Create a new VM without authenticating the user or checking the resource
51
limits of the user. Also the allocator can be bypassed by specifing a
52
backend-id.
53
"""
54

    
55

    
56
class Command(BaseCommand):
57
    help = "Create a new VM." + HELP_MSG
58

    
59
    option_list = BaseCommand.option_list + (
60
        make_option("--backend-id", dest="backend_id",
61
                    help="Unique identifier of the Ganeti backend."
62
                         " Use snf-manage backend-list to find out"
63
                         " available backends."),
64
        make_option("--name", dest="name",
65
                    help="An arbitrary string for naming the server"),
66
        make_option("--user-id", dest="user_id",
67
                    help="Unique identifier of the owner of the server"),
68
        make_option("--image-id", dest="image_id",
69
                    help="Unique identifier of the image."
70
                         " Use snf-manage image-list to find out"
71
                         " available images."),
72
        make_option("--flavor-id", dest="flavor_id",
73
                    help="Unique identifier of the flavor"
74
                         " Use snf-manage flavor-list to find out"
75
                         " available flavors."),
76
        make_option("--password", dest="password",
77
                    help="Password for the new server")
78
    )
79

    
80
    @transaction.commit_manually
81
    def handle(self, *args, **options):
82
        if args:
83
            raise CommandError("Command doesn't accept any arguments")
84

    
85
        name = options['name']
86
        user_id = options['user_id']
87
        backend_id = options['backend_id']
88
        image_id = options['image_id']
89
        flavor_id = options['flavor_id']
90
        password = options['password']
91

    
92
        if not name:
93
            raise CommandError("name is mandatory")
94
        if not user_id:
95
            raise CommandError("user-id is mandatory")
96
        if not password:
97
            raise CommandError("password is mandatory")
98
        if not flavor_id:
99
            raise CommandError("flavor-id is mandatory")
100

    
101
        # Get Flavor
102
        if flavor_id:
103
            flavor = common.get_flavor(flavor_id)
104

    
105
        if image_id:
106
            img = common.get_image(image_id, user_id)
107

    
108
            properties = img.get('properties', {})
109
            image = {}
110
            image['backend_id'] = img['location']
111
            image['format'] = img['disk_format']
112
            image['metadata'] = dict((key.upper(), val)
113
                                     for key, val in properties.items())
114
        else:
115
            raise CommandError("image-id is mandatory")
116

    
117
        # Fix flavor for archipelago
118
        disk_template, provider = util.get_flavor_provider(flavor)
119
        if provider:
120
            flavor.disk_template = disk_template
121
            flavor.disk_provider = provider
122
            flavor.disk_origin = None
123
            if provider == 'vlmc':
124
                flavor.disk_origin = image['checksum']
125
                image['backend_id'] = 'null'
126
        else:
127
            flavor.disk_provider = None
128

    
129
        try:
130
            # Get Backend
131
            if backend_id:
132
                backend = common.get_backend(backend_id)
133
            else:
134
                ballocator = BackendAllocator()
135
                backend = ballocator.allocate(user_id, flavor)
136
                if not backend:
137
                    raise CommandError("Can not allocate VM")
138

    
139
            # Get Public address
140
            (network, address) = util.allocate_public_address(backend)
141
            if address is None:
142
                raise CommandError("Can not allocate a public address."
143
                                   " No available public network.")
144
            nic = {'ip': address, 'network': network.backend_id}
145

    
146
            # Create the VM in DB
147
            vm = VirtualMachine.objects.create(name=name,
148
                                               backend=backend,
149
                                               userid=user_id,
150
                                               imageid=image_id,
151
                                               flavor=flavor)
152
            # dispatch server created signal
153
            server_created.send(sender=vm, created_vm_params={
154
                'img_id': image['backend_id'],
155
                'img_passwd': password,
156
                'img_format': str(image['format']),
157
                'img_personality': '[]',
158
                'img_properties': json.dumps(image['metadata']),
159
            })
160

    
161
            quotas.issue_and_accept_commission(vm)
162
        except:
163
            transaction.rollback()
164
            raise
165
        else:
166
            transaction.commit()
167

    
168
        try:
169
            # Create the instance in Backend
170
            jobID = create_instance(vm, nic, flavor, image)
171

    
172
            vm.backendjobid = jobID
173
            vm.save()
174
            self.stdout.write("Creating VM %s with IP %s in Backend %s."
175
                              " JobID: %s\n" % (vm, address, backend, jobID))
176
        except:
177
            transaction.rollback()
178
            raise
179
        else:
180
            transaction.commit()