Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / volume / management / commands / volume-create.py @ ac6a221f

History | View | Annotate | Download (4.4 kB)

1
# Copyright 2013 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

    
38
from snf_django.management.utils import parse_bool
39
from synnefo.management import common, pprint
40
from synnefo.volume import volumes
41

    
42
HELP_MSG = """Create a new volume."""
43

    
44

    
45
class Command(BaseCommand):
46
    help = HELP_MSG
47

    
48
    option_list = BaseCommand.option_list + (
49
        make_option(
50
            "--name",
51
            dest="name",
52
            default=None,
53
            help="Display name of the volume."),
54
        make_option(
55
            "--description",
56
            dest="description",
57
            default=None,
58
            help="Display description of the volume."),
59
        make_option(
60
            "--owner",
61
            dest="user_id",
62
            default=None,
63
            help="UUID of the owner of the volume."),
64
        make_option(
65
            "-s", "--size",
66
            dest="size",
67
            default=None,
68
            help="Size of the new volume in GB"),
69
        make_option(
70
            "--server",
71
            dest="server_id",
72
            default=None,
73
            help="The ID of the server that the volume will be connected to."),
74
        make_option(
75
            "--wait",
76
            dest="wait",
77
            default="True",
78
            choices=["True", "False"],
79
            metavar="True|False",
80
            help="Wait for Ganeti jobs to complete."),
81
    )
82

    
83
    @common.convert_api_faults
84
    def handle(self, *args, **options):
85
        if args:
86
            raise CommandError("Command doesn't accept any arguments")
87

    
88
        size = options.get("size")
89
        user_id = options.get("user_id")
90
        server_id = options.get("server_id")
91
        wait = parse_bool(options["wait"])
92

    
93
        display_name = options.get("name", "")
94
        display_description = options.get("description", "")
95

    
96
        if size is None:
97
            raise CommandError("Please specify the size of the volume")
98

    
99
        if server_id is None:
100
            raise CommandError("Please specify the server to attach the"
101
                               " volume.")
102

    
103
        vm = common.get_vm(server_id)
104

    
105
        if user_id is None:
106
            user_id = vm.userid
107

    
108
        source_image_id = source_volume_id = source_snapshot_id = None
109
        volume = volumes.create(user_id, size, server_id,
110
                                name=display_name,
111
                                description=display_description,
112
                                source_image_id=source_image_id,
113
                                source_snapshot_id=source_snapshot_id,
114
                                source_volume_id=source_volume_id,
115
                                metadata={})
116

    
117
        self.stdout.write("Created volume '%s' in DB:\n" % volume.id)
118

    
119
        pprint.pprint_volume(volume, stdout=self.stdout)
120
        self.stdout.write("\n")
121
        if volume.machine is not None:
122
            volume.machine.task_job_id = volume.backendjobid
123
            common.wait_server_task(volume.machine, wait, stdout=self.stdout)