Revision b4eaff05

b/snf-cyclades-app/synnefo/logic/management/commands/backend_add.py
1
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or without
4
# modification, are permitted provided that the following conditions
5
# are met:
6
#
7
#   1. Redistributions of source code must retain the above copyright
8
#      notice, this list of conditions and the following disclaimer.
9
#
10
#  2. Redistributions in binary form must reproduce the above copyright
11
#     notice, this list of conditions and the following disclaimer in the
12
#     documentation and/or other materials provided with the distribution.
13
#
14
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
# SUCH DAMAGE.
25
#
26
# The views and conclusions contained in the software and documentation are
27
# those of the authors and should not be interpreted as representing official
28
# policies, either expressed or implied, of GRNET S.A.
29
#
30

  
31
from optparse import make_option
32
from django.core.management.base import BaseCommand, CommandError
33

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

  
42

  
43
class Command(BaseCommand):
44
    can_import_settings = True
45

  
46
    help = 'Create a new backend.'
47
    output_transaction = True  # The management command runs inside
48
                               # an SQL transaction
49
    option_list = BaseCommand.option_list + (
50
        make_option('--clustername', dest='clustername'),
51
        make_option('--port', dest='port', default=5080),
52
        make_option('--user', dest='username'),
53
        make_option('--pass', dest='password'),
54
        make_option('--drained', action='store_true',
55
            dest='drained', default=False,
56
            help="Set as drained to exclude from allocations"),
57
        make_option('--no-check', action='store_false',
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")
63
        )
64

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

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

  
75
        # Ensure correctness of credentials
76
        if options['check']:
77
            self.stdout.write('Checking connectivity and credentials.\n')
78
            try:
79
                client = create_client(clustername, port, username, password)
80
                # This command will raise an exception if there is no
81
                # write-access
82
                client.ModifyCluster()
83
            except GanetiApiError as e:
84
                self.stdout.write('Check failed:\n%s\n' % e)
85
                return
86
            else:
87
                self.stdout.write('Check passed.\n')
88

  
89
        # Create the new backend in database
90
        try:
91
            backend = Backend.objects.create(clustername=clustername,
92
                                             port=port,
93
                                             username=username,
94
                                             password=password,
95
                                             drained=drained)
96
        except IntegrityError as e:
97
            raise CommandError("Cannot create backend: %s\n" % e)
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])
/dev/null
1
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or without
4
# modification, are permitted provided that the following conditions
5
# are met:
6
#
7
#   1. Redistributions of source code must retain the above copyright
8
#      notice, this list of conditions and the following disclaimer.
9
#
10
#  2. Redistributions in binary form must reproduce the above copyright
11
#     notice, this list of conditions and the following disclaimer in the
12
#     documentation and/or other materials provided with the distribution.
13
#
14
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
# SUCH DAMAGE.
25
#
26
# The views and conclusions contained in the software and documentation are
27
# those of the authors and should not be interpreted as representing official
28
# policies, either expressed or implied, of GRNET S.A.
29
#
30

  
31
from optparse import make_option
32
from django.core.management.base import BaseCommand, CommandError
33

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

  
42

  
43
class Command(BaseCommand):
44
    can_import_settings = True
45

  
46
    help = 'Create a new backend.'
47
    output_transaction = True  # The management command runs inside
48
                               # an SQL transaction
49
    option_list = BaseCommand.option_list + (
50
        make_option('--clustername', dest='clustername'),
51
        make_option('--port', dest='port', default=5080),
52
        make_option('--user', dest='username'),
53
        make_option('--pass', dest='password'),
54
        make_option('--drained', action='store_true',
55
            dest='drained', default=False,
56
            help="Set as drained to exclude from allocations"),
57
        make_option('--no-check', action='store_false',
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")
63
        )
64

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

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

  
75
        # Ensure correctness of credentials
76
        if options['check']:
77
            self.stdout.write('Checking connectivity and credentials.\n')
78
            try:
79
                client = create_client(clustername, port, username, password)
80
                # This command will raise an exception if there is no
81
                # write-access
82
                client.ModifyCluster()
83
            except GanetiApiError as e:
84
                self.stdout.write('Check failed:\n%s\n' % e)
85
                return
86
            else:
87
                self.stdout.write('Check passed.\n')
88

  
89
        # Create the new backend in database
90
        try:
91
            backend = Backend.objects.create(clustername=clustername,
92
                                             port=port,
93
                                             username=username,
94
                                             password=password,
95
                                             drained=drained)
96
        except IntegrityError as e:
97
            raise CommandError("Cannot create backend: %s\n" % e)
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