Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (6.8 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

    
47
HELP_MSG = """
48

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

    
54

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

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

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

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

    
91
        if not name:
92
            raise CommandError("name is mandatory")
93
        if not user_id:
94
            raise CommandError("user-id is mandatory")
95
        if not password:
96
            raise CommandError("password is mandatory")
97

    
98
        # Get Flavor
99
        if flavor_id:
100
            flavor = common.get_flavor(flavor_id)
101

    
102
        if image_id:
103
            img = common.get_image(image_id, user_id)
104

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

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

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

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

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

    
163
        try:
164
            # Create the instance in Backend
165
            jobID = create_instance(vm, nic, flavor, image, password)
166

    
167
            vm.backendjobid = jobID
168
            vm.save()
169
            self.stdout.write("Creating VM %s with IP %s in Backend %s."
170
                              " JobID: %s\n" % (vm, address, backend, jobID))
171
        except:
172
            transaction.rollback()
173
            raise
174
        else:
175
            transaction.commit()