Revision 12cb6933 snf-cyclades-app/synnefo/logic/management/commands/backend-add.py
b/snf-cyclades-app/synnefo/logic/management/commands/backend-add.py | ||
---|---|---|
1 |
# Copyright 2011-2012 GRNET S.A. All rights reserved.
|
|
1 |
# Copyright 2011-2013 GRNET S.A. All rights reserved.
|
|
2 | 2 |
# |
3 | 3 |
# Redistribution and use in source and binary forms, with or without |
4 | 4 |
# modification, are permitted provided that the following conditions |
... | ... | |
27 | 27 |
# those of the authors and should not be interpreted as representing official |
28 | 28 |
# policies, either expressed or implied, of GRNET S.A. |
29 | 29 |
# |
30 |
|
|
30 |
import sys |
|
31 | 31 |
from optparse import make_option |
32 | 32 |
from django.core.management.base import BaseCommand, CommandError |
33 | 33 |
|
... | ... | |
54 | 54 |
make_option('--user', dest='username'), |
55 | 55 |
make_option('--pass', dest='password'), |
56 | 56 |
make_option( |
57 |
'--no-check', action='store_false', |
|
58 |
dest='check', default=True, |
|
57 |
'--no-check', |
|
58 |
action='store_false', |
|
59 |
dest='check', |
|
60 |
default=True, |
|
59 | 61 |
help="Do not perform credentials check and resources update"), |
60 |
make_option('--hypervisor', |
|
62 |
make_option( |
|
63 |
'--hypervisor', |
|
61 | 64 |
dest='hypervisor', |
62 | 65 |
default=None, |
63 | 66 |
choices=HYPERVISORS, |
... | ... | |
77 | 80 |
port = options['port'] |
78 | 81 |
username = options['username'] |
79 | 82 |
password = options['password'] |
80 |
hypervisor = options["hypervisor"] |
|
81 | 83 |
|
82 | 84 |
if not (clustername and username and password): |
83 | 85 |
raise CommandError("Clustername, user and pass must be supplied") |
... | ... | |
86 | 88 |
if options['check']: |
87 | 89 |
check_backend_credentials(clustername, port, username, password) |
88 | 90 |
|
91 |
create_backend(clustername, port, username, password, |
|
92 |
hypervisor=options["hypervisor"], |
|
93 |
initialize=options["init"]) |
|
94 |
|
|
95 |
|
|
96 |
def create_backend(clustername, port, username, password, hypervisor=None, |
|
97 |
initialize=True, stream=sys.stdout): |
|
89 | 98 |
kw = {"clustername": clustername, |
90 | 99 |
"port": port, |
91 | 100 |
"username": username, |
... | ... | |
94 | 103 |
|
95 | 104 |
if hypervisor: |
96 | 105 |
kw["hypervisor"] = hypervisor |
106 |
|
|
97 | 107 |
# Create the new backend in database |
98 | 108 |
try: |
99 | 109 |
backend = Backend.objects.create(**kw) |
100 | 110 |
except IntegrityError as e: |
101 | 111 |
raise CommandError("Cannot create backend: %s\n" % e) |
102 | 112 |
|
103 |
self.stdout.write('\nSuccessfully created backend with id %d\n' % |
|
104 |
backend.id) |
|
113 |
stream.write("Successfully created backend with id %d\n" % backend.id) |
|
105 | 114 |
|
106 |
if not options['check']:
|
|
115 |
if not initialize:
|
|
107 | 116 |
return |
108 | 117 |
|
109 |
self.stdout.write('\rRetrieving backend resources:\n')
|
|
118 |
stream.write("Retrieving backend resources:\n")
|
|
110 | 119 |
resources = get_physical_resources(backend) |
111 | 120 |
attr = ['mfree', 'mtotal', 'dfree', 'dtotal', 'pinst_cnt', 'ctotal'] |
112 | 121 |
|
113 | 122 |
table = [[str(resources[x]) for x in attr]] |
114 |
pprint_table(self.stdout, table, attr)
|
|
123 |
pprint_table(stream, table, attr)
|
|
115 | 124 |
|
116 | 125 |
update_resources(backend, resources) |
117 | 126 |
|
118 |
if not options['init']: |
|
119 |
return |
|
120 |
|
|
121 | 127 |
networks = Network.objects.filter(deleted=False, floating_ip_pool=True) |
122 | 128 |
if not networks: |
123 | 129 |
return |
124 | 130 |
|
125 |
self.stdout.write('\nCreating the follow networks:\n')
|
|
131 |
stream.write("Creating the follow networks:\n")
|
|
126 | 132 |
headers = ('Name', 'Subnet', 'Gateway', 'Mac Prefix', 'Public') |
127 | 133 |
table = [] |
128 | 134 |
|
129 | 135 |
for net in networks: |
130 | 136 |
table.append((net.backend_id, str(net.subnet), str(net.gateway), |
131 | 137 |
str(net.mac_prefix), str(net.public))) |
132 |
pprint_table(self.stdout, table, headers)
|
|
138 |
pprint_table(stream, table, headers)
|
|
133 | 139 |
|
134 | 140 |
for net in networks: |
135 | 141 |
net.create_backend_network(backend) |
136 | 142 |
result = create_network_synced(net, backend) |
137 | 143 |
if result[0] != "success": |
138 |
self.stdout.write('\nError Creating Network %s: %s\n' %
|
|
139 |
(net.backend_id, result[1]))
|
|
144 |
stream.write('\nError Creating Network %s: %s\n' %
|
|
145 |
(net.backend_id, result[1])) |
|
140 | 146 |
else: |
141 |
self.stdout.write('Successfully created Network: %s\n' %
|
|
142 |
net.backend_id)
|
|
147 |
stream.write('Successfully created Network: %s\n' %
|
|
148 |
net.backend_id) |
|
143 | 149 |
result = connect_network_synced(network=net, backend=backend) |
144 | 150 |
if result[0] != "success": |
145 |
self.stdout.write('\nError Connecting Network %s: %s\n' %
|
|
146 |
(net.backend_id, result[1]))
|
|
151 |
stream.write('\nError Connecting Network %s: %s\n' %
|
|
152 |
(net.backend_id, result[1])) |
|
147 | 153 |
else: |
148 |
self.stdout.write('Successfully connected Network: %s\n' % |
|
149 |
net.backend_id) |
|
154 |
stream.write('Successfully connected Network: %s\n' % |
|
155 |
net.backend_id) |
Also available in: Unified diff