Revision 1040b85b

b/snf-cyclades-app/synnefo/logic/reconciliation.py
498 498
    names = zip(itertools.repeat('name'), i['nic.names'])
499 499
    macs = zip(itertools.repeat('mac'), i['nic.macs'])
500 500
    networks = zip(itertools.repeat('network'), i['nic.networks.names'])
501
    indexes = zip(itertools.repeat('index'), range(0, len(ips)))
501 502
    # modes = zip(itertools.repeat('mode'), i['nic.modes'])
502 503
    # links = zip(itertools.repeat('link'), i['nic.links'])
503 504
    # nics = zip(ips,macs,modes,networks,links)
504
    nics = zip(ips, names, macs, networks)
505
    nics = zip(ips, names, macs, networks, indexes)
505 506
    nics = map(lambda x: dict(x), nics)
506 507
    #nics = dict(enumerate(nics))
507 508
    tags = i["tags"]
......
522 523
    sizes = zip(itertools.repeat('size'), i['disk.sizes'])
523 524
    names = zip(itertools.repeat('name'), i['disk.names'])
524 525
    uuids = zip(itertools.repeat('uuid'), i['disk.uuids'])
525
    disks = zip(sizes, names, uuids)
526
    indexes = zip(itertools.repeat('index'), range(0, len(sizes)))
527
    disks = zip(sizes, names, uuids, indexes)
526 528
    disks = map(lambda x: dict(x), disks)
527 529
    #disks = dict(enumerate(disks))
528 530
    return disks
b/snf-cyclades-app/synnefo/management/pprint.py
402 402
        ("server_id", volume.machine_id),
403 403
        ("userid", volume.userid),
404 404
        ("username", ucache.get_name(userid) if display_mails else None),
405
        ("index", volume.index),
405 406
        ("name", volume.name),
406 407
        ("state", volume.status),
407 408
        ("deleted", volume.deleted),
b/snf-cyclades-app/synnefo/volume/management/commands/volume-import.py
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 snf_django.management.commands import SynnefoCommand, CommandError
37

  
38
from synnefo.management import common, pprint
39
from synnefo.logic import backend as backend_mod, reconciliation, utils
40
from synnefo.db.models import Volume
41

  
42
HELP_MSG = """Import an existing Ganeti disk to Cyclades
43

  
44
Create a Cyclades Volume from an existing Ganeti disk. This command is useful
45
to handle disks that have been created directly in the Ganeti backend (instead
46
of being created by Cyclades). The command will not create/delete/modify the
47
specified disk in the Ganeti backend. Instead, it will create a Volume in
48
Cyclades DB, and rename the Ganeti disk with the Volume name.
49

  
50
"""
51

  
52

  
53
class Command(SynnefoCommand):
54
    help = HELP_MSG
55

  
56
    option_list = SynnefoCommand.option_list + (
57
        make_option(
58
            "--name",
59
            dest="name",
60
            default=None,
61
            help="Display name of the volume."),
62
        make_option(
63
            "--description",
64
            dest="description",
65
            default=None,
66
            help="Display description of the volume."),
67
        make_option(
68
            "--server",
69
            dest="server_id",
70
            default=None,
71
            help="The ID of the server that the volume is currently attached"),
72
        make_option(
73
            "--disk",
74
            dest="disk_uuid",
75
            default=None,
76
            help="The UUID of the disk to be imported"),
77
    )
78

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

  
84
        display_name = options.get("name", "")
85
        display_description = options.get("description", "")
86

  
87
        server_id = options.get("server_id")
88
        if server_id is None:
89
            raise CommandError("Please specify the server that the disk is"
90
                               " currently attached.")
91

  
92
        disk_uuid = options.get("disk_uuid")
93
        if disk_uuid is None:
94
            raise CommandError("Please specify the UUID of the Ganeti disk")
95

  
96
        vm = common.get_vm(server_id)
97

  
98
        instance_info = backend_mod.get_instance_info(vm)
99
        instance_disks = reconciliation.disks_from_instance(instance_info)
100
        try:
101
            disk = filter(lambda d: d["uuid"] == disk_uuid, instance_disks)[0]
102
        except IndexError:
103
            raise CommandError("Instance '%s' does not have a disk with"
104
                               " UUID '%s" % (vm.id, disk_uuid))
105

  
106
        # Check that the instance disk is not already a Cyclades Volume
107
        try:
108
            disk_id = utils.id_from_disk_name(disk["name"])
109
        except:
110
            pass
111
        else:
112
            raise CommandError("Disk '%s' of instance '%s' is already a"
113
                               " Cyclades Volume. Volume ID: %s"
114
                               % (disk_uuid, vm.id, disk_id))
115

  
116
        size = disk["size"] >> 10  # Convert to GB
117

  
118
        self.stdout.write("Import disk/%s of instance %s, size: %s GB\n"
119
                          % (disk["index"], vm.id, size))
120

  
121
        volume = Volume.objects.create(
122
            userid=vm.userid,
123
            size=size,
124
            machine_id=server_id,
125
            name=display_name,
126
            description=display_description,
127
            index=disk["index"])
128

  
129
        self.stdout.write("Created Volume '%s' in DB\n" % volume.id)
130
        pprint.pprint_volume(volume, stdout=self.stdout)
131
        self.stdout.write("\n")
132

  
133
        client = vm.get_client()
134
        jobId = client.ModifyInstance(
135
            instance=vm.backend_vm_id,
136
            disks=[("modify", disk["index"],
137
                    {"name": volume.backend_volume_uuid})])
138
        (status, error) = backend_mod.wait_for_job(client, jobId)
139
        vm.put_client(client)
140
        if status == "success":
141
            self.stdout.write("Successfully imported disk\n")
142
        else:
143
            self.stdout.write("Failed to imported disk:\n %s\n" % error)

Also available in: Unified diff