Revision b3d28af2

b/snf-cyclades-app/synnefo/logic/backend.py
606 606
    for i in instances:
607 607
        mem += i['oper_ram']
608 608
    return mem
609

  
610
##
611
## Synchronized operations for reconciliation
612
##
613

  
614

  
615
def create_network_synced(network, backend):
616
    result = _create_network_synced(network, backend)
617
    if result[0] != 'success':
618
        return result
619
    result = connect_network_synced(network, backend)
620
    return result
621

  
622

  
623
def _create_network_synced(network, backend):
624
    client = backend.client
625
    job = client.CreateNetwork(network.backend_id, network.subnet)
626
    return wait_for_job(client, job)
627

  
628

  
629
def connect_network_synced(network, backend):
630
    mode = network.public and 'routed' or 'bridged'
631
    client = backend.client
632

  
633
    for group in client.GetGroups():
634
        job = client.ConnectNetwork(network.backend_id, group, mode,
635
                                    network.link)
636
        result = wait_for_job(client, job)
637
        if result[0] != 'success':
638
            return result
639

  
640
    return result
641

  
642

  
643
def wait_for_job(client, jobid):
644
    result = client.WaitForJobChange(jobid, ['status', 'opresult'], None, None)
645
    status = result['job_info'][0]
646
    while status not in ['success', 'error', 'cancel']:
647
        result = client.WaitForJobChange(jobid, ['status', 'opresult'],
648
                                        [result], None)
649
        status = result['job_info'][0]
650

  
651
    if status == 'success':
652
        return (status, None)
653
    else:
654
        error = result['job_info'][1]
655
        return (status, error)
b/snf-cyclades-app/synnefo/logic/management/commands/backend_create.py
31 31
from optparse import make_option
32 32
from django.core.management.base import BaseCommand, CommandError
33 33

  
34
from synnefo.db.models import Backend
34
from synnefo.db.models import Backend, Network
35 35
from django.db.utils import IntegrityError
36
from synnefo.logic.backend import get_physical_resources, \
37
                                  update_resources, \
38
                                  create_client
36
from synnefo.logic.backend import (get_physical_resources,
37
                                   update_resources,
38
                                   create_client,
39
                                   create_network_synced)
39 40
from synnefo.util.rapi import GanetiApiError
40 41

  
41 42

  
......
48 49
    option_list = BaseCommand.option_list + (
49 50
        make_option('--clustername', dest='clustername'),
50 51
        make_option('--port', dest='port', default=5080),
51
        make_option('--username', dest='username'),
52
        make_option('--password', dest='password'),
52
        make_option('--user', dest='username'),
53
        make_option('--pass', dest='password'),
53 54
        make_option('--drained', action='store_true',
54
                    dest='drained',default=False,
55
                    help="Set as drained to exclude from allocations"),
55
            dest='drained', default=False,
56
            help="Set as drained to exclude from allocations"),
56 57
        make_option('--no-check', action='store_false',
57
                    dest='check', default=True,
58
                    help="Do not perform credentials check and resources update")
58
            dest='check', default=True,
59
            help="Do not perform credentials check and resources update"),
60
        make_option('--no-init', action='store_false',
61
            dest='init', default=True,
62
            help="Do not perform initialization of the Backend Model")
59 63
        )
60 64

  
61 65
    def handle(self, **options):
62 66
        clustername = options['clustername']
63
        port= options['port']
64
        username = options['username']
65
        password = options['password']
67
        port = options['port']
68
        username = options['user']
69
        password = options['pass']
66 70
        drained = options['drained']
67 71

  
68 72
        if not (clustername and username and password):
69
            raise CommandError("Clustername, username and password must be supplied")
73
            raise CommandError("Clustername, user and pass must be supplied")
70 74

  
71 75
        # Ensure correctness of credentials
72 76
        if options['check']:
73 77
            self.stdout.write('Checking connectivity and credentials.\n')
74 78
            try:
75 79
                client = create_client(clustername, port, username, password)
76
                # This command will raise an exception if there is no write-access
80
                # This command will raise an exception if there is no
81
                # write-access
77 82
                client.ModifyCluster()
78 83
            except GanetiApiError as e:
79
                self.stdout.write('Check failed:\n%s\n' %e)
84
                self.stdout.write('Check failed:\n%s\n' % e)
80 85
                return
81 86
            else:
82 87
                self.stdout.write('Check passed.\n')
......
89 94
                                             password=password,
90 95
                                             drained=drained)
91 96
        except IntegrityError as e:
92
            self.stdout.write("Cannot create backend: %s\n" % e)
93
        else:
94
            if options['check']:
95
                self.stdout.write('\nRetriving backend resources:\n')
96
                resources = get_physical_resources(backend)
97
                attr = ['mfree', 'mtotal', 'dfree', 'dtotal', 'pinst_cnt', 'ctotal']
98
                for a in attr:
99
                    self.stdout.write(a + ' : ' + str(resources[a])+'\n')
100
                update_resources(backend, resources)
101

  
102
            self.stdout.write('\nSuccessfully created backend with id %d\n' %
103
                              backend.id)
97
            raise CommandError("Cannot create backend: %s\n" % e)
104 98

  
99
        self.stdout.write('\nSuccessfully created backend with id %d\n' %
100
                          backend.id)
101

  
102
        if not options['check']:
103
            return
104

  
105
        self.stdout.write('\rRetrieving backend resources:\n')
106
        resources = get_physical_resources(backend)
107
        attr = ['mfree', 'mtotal', 'dfree', 'dtotal', 'pinst_cnt', 'ctotal']
108
        self.stdout.write('----------------------------\n')
109
        for a in attr:
110
            self.stdout.write(a.ljust(12) + ' : ' + str(resources[a]) + '\n')
111
        update_resources(backend, resources)
112

  
113
        if not options['init']:
114
            return
115

  
116
        networks = Network.objects.filter(deleted=False)
117

  
118
        self.stdout.write('\nCreating the follow networks:\n')
119
        fields = ('Name', 'Subnet', 'Gateway', 'Mac Prefix', 'Public')
120
        columns = (20, 16, 16, 16, 10)
121
        line = ' '.join(f.ljust(c) for f, c in zip(fields, columns))
122
        sep = '-' * len(line)
123
        self.stdout.write(sep + '\n')
124
        self.stdout.write(line + '\n')
125
        self.stdout.write(sep + '\n')
126

  
127
        for net in networks:
128
            fields = (net.backend_id, str(net.subnet), str(net.gateway),
129
                      str(net.mac_prefix), str(net.public))
130
            line = ' '.join(f.ljust(c) for f, c in zip(fields, columns))
131
            self.stdout.write(line + '\n')
132
        self.stdout.write(sep + '\n\n')
133

  
134
        for net in networks:
135
            result = create_network_synced(net, backend)
136
            if result[0] != "success":
137
                self.stdout.write('\nError Creating Network %s: %s\n' %\
138
                                  (net.backend_id, result[1])

Also available in: Unified diff