Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / api / management / commands / flavor-create.py @ b1fb3aac

History | View | Annotate | Download (3.7 kB)

1 76f5441f Giorgos Verigakis
# Copyright 2012 GRNET S.A. All rights reserved.
2 76f5441f Giorgos Verigakis
#
3 76f5441f Giorgos Verigakis
# Redistribution and use in source and binary forms, with or
4 76f5441f Giorgos Verigakis
# without modification, are permitted provided that the following
5 76f5441f Giorgos Verigakis
# conditions are met:
6 76f5441f Giorgos Verigakis
#
7 76f5441f Giorgos Verigakis
#   1. Redistributions of source code must retain the above
8 76f5441f Giorgos Verigakis
#      copyright notice, this list of conditions and the following
9 76f5441f Giorgos Verigakis
#      disclaimer.
10 76f5441f Giorgos Verigakis
#
11 76f5441f Giorgos Verigakis
#   2. Redistributions in binary form must reproduce the above
12 76f5441f Giorgos Verigakis
#      copyright notice, this list of conditions and the following
13 76f5441f Giorgos Verigakis
#      disclaimer in the documentation and/or other materials
14 76f5441f Giorgos Verigakis
#      provided with the distribution.
15 76f5441f Giorgos Verigakis
#
16 76f5441f Giorgos Verigakis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 76f5441f Giorgos Verigakis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 76f5441f Giorgos Verigakis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 76f5441f Giorgos Verigakis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 76f5441f Giorgos Verigakis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 76f5441f Giorgos Verigakis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 76f5441f Giorgos Verigakis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 76f5441f Giorgos Verigakis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 76f5441f Giorgos Verigakis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 76f5441f Giorgos Verigakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 76f5441f Giorgos Verigakis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 76f5441f Giorgos Verigakis
# POSSIBILITY OF SUCH DAMAGE.
28 76f5441f Giorgos Verigakis
#
29 76f5441f Giorgos Verigakis
# The views and conclusions contained in the software and
30 76f5441f Giorgos Verigakis
# documentation are those of the authors and should not be
31 76f5441f Giorgos Verigakis
# interpreted as representing official policies, either expressed
32 76f5441f Giorgos Verigakis
# or implied, of GRNET S.A.
33 76f5441f Giorgos Verigakis
34 76f5441f Giorgos Verigakis
from itertools import product
35 cf1984d8 Christos Stavrakakis
from optparse import make_option
36 76f5441f Giorgos Verigakis
37 76f5441f Giorgos Verigakis
from django.core.management.base import BaseCommand, CommandError
38 76f5441f Giorgos Verigakis
39 76f5441f Giorgos Verigakis
from synnefo.db.models import Flavor
40 76f5441f Giorgos Verigakis
41 76f5441f Giorgos Verigakis
42 76f5441f Giorgos Verigakis
class Command(BaseCommand):
43 39a6388d Christos Stavrakakis
    output_transaction = True
44 cf1984d8 Christos Stavrakakis
45 cf1984d8 Christos Stavrakakis
    option_list = BaseCommand.option_list + (
46 cf1984d8 Christos Stavrakakis
        make_option("-n", "--dry-run", dest="dry_run", action="store_true"),
47 cf1984d8 Christos Stavrakakis
    )
48 76f5441f Giorgos Verigakis
    args = "<cpu>[,<cpu>,...] " \
49 76f5441f Giorgos Verigakis
           "<ram>[,<ram>,...] " \
50 76f5441f Giorgos Verigakis
           "<disk>[,<disk>,...] " \
51 76f5441f Giorgos Verigakis
           "<disk template>[,<disk template>,...]"
52 9523af40 Christos Stavrakakis
    help = "Create one or more flavors.\n\nThe flavors that will be created"\
53 9523af40 Christos Stavrakakis
           " are those belonging to the cartesian product of the arguments"
54 9621c777 Christos Stavrakakis
55 76f5441f Giorgos Verigakis
    def handle(self, *args, **options):
56 76f5441f Giorgos Verigakis
        if len(args) != 4:
57 76f5441f Giorgos Verigakis
            raise CommandError("Invalid number of arguments")
58 39a6388d Christos Stavrakakis
59 76f5441f Giorgos Verigakis
        cpus = args[0].split(',')
60 76f5441f Giorgos Verigakis
        rams = args[1].split(',')
61 76f5441f Giorgos Verigakis
        disks = args[2].split(',')
62 76f5441f Giorgos Verigakis
        templates = args[3].split(',')
63 39a6388d Christos Stavrakakis
64 76f5441f Giorgos Verigakis
        flavors = []
65 76f5441f Giorgos Verigakis
        for cpu, ram, disk, template in product(cpus, rams, disks, templates):
66 76f5441f Giorgos Verigakis
            try:
67 76f5441f Giorgos Verigakis
                flavors.append((int(cpu), int(ram), int(disk), template))
68 76f5441f Giorgos Verigakis
            except ValueError:
69 76f5441f Giorgos Verigakis
                raise CommandError("Invalid values")
70 39a6388d Christos Stavrakakis
71 76f5441f Giorgos Verigakis
        for cpu, ram, disk, template in flavors:
72 cf1984d8 Christos Stavrakakis
            if options["dry_run"]:
73 cf1984d8 Christos Stavrakakis
                flavor = Flavor(cpu=cpu, ram=ram, disk=disk,
74 cf1984d8 Christos Stavrakakis
                                disk_template=template)
75 cf1984d8 Christos Stavrakakis
                self.stdout.write("Creating flavor '%s'\n" % (flavor.name,))
76 cf1984d8 Christos Stavrakakis
            else:
77 cf1984d8 Christos Stavrakakis
                flavor, created = \
78 cf1984d8 Christos Stavrakakis
                    Flavor.objects.get_or_create(cpu=cpu, ram=ram, disk=disk,
79 cf1984d8 Christos Stavrakakis
                                                 disk_template=template)
80 cf1984d8 Christos Stavrakakis
                if created:
81 cf1984d8 Christos Stavrakakis
                    self.stdout.write("Created flavor '%s'\n" % (flavor.name,))
82 cf1984d8 Christos Stavrakakis
                else:
83 cf1984d8 Christos Stavrakakis
                    self.stdout.write("Flavor '%s' already exists\n"
84 cf1984d8 Christos Stavrakakis
                                      % flavor.name)
85 cc7312b1 Christos Stavrakakis
                    if flavor.deleted:
86 cc7312b1 Christos Stavrakakis
                        msg = "Flavor '%s' is marked as deleted. Use"\
87 cc7312b1 Christos Stavrakakis
                        " 'snf-manage flavor-modify' to restore this flavor\n"\
88 cc7312b1 Christos Stavrakakis
                        % flavor.name
89 cc7312b1 Christos Stavrakakis
                        self.stdout.write(msg)