Commit 972d4e9c authored by Christos Stavrakakis's avatar Christos Stavrakakis
Browse files

Fix pep8 warnings

parent d9b58b74
......@@ -170,12 +170,12 @@ def process_ganeti_nics(ganeti_nics):
firewall_profile = settings.DEFAULT_FIREWALL_PROFILE
nic = {
'index': i,
'network': net,
'mac': mac,
'ipv4': ipv4,
'ipv6': ipv6,
'firewall_profile': firewall_profile}
'index': i,
'network': net,
'mac': mac,
'ipv4': ipv4,
'ipv6': ipv6,
'firewall_profile': firewall_profile}
new_nics.append(nic)
return new_nics
......@@ -186,11 +186,11 @@ def nics_changed(old_nics, new_nics):
if len(old_nics) != len(new_nics):
return True
for old_nic, new_nic in zip(old_nics, new_nics):
if not (old_nic.ipv4 == new_nic['ipv4'] and\
old_nic.ipv6 == new_nic['ipv6'] and\
old_nic.mac == new_nic['mac'] and\
old_nic.firewall_profile == new_nic['firewall_profile'] and\
old_nic.index == new_nic['index'] and\
if not (old_nic.ipv4 == new_nic['ipv4'] and
old_nic.ipv6 == new_nic['ipv6'] and
old_nic.mac == new_nic['mac'] and
old_nic.firewall_profile == new_nic['firewall_profile'] and
old_nic.index == new_nic['index'] and
old_nic.network == new_nic['network']):
return True
return False
......@@ -243,7 +243,7 @@ def update_network_state(serials, network):
old_state = network.state
backend_states = [s.operstate for s in
network.backend_networks.filter(backend__offline=False)]
network.backend_networks.filter(backend__offline=False)]
if not backend_states:
network.state = 'PENDING'
network.save()
......@@ -332,7 +332,7 @@ def process_create_progress(vm, etime, progress):
def create_instance_diagnostic(vm, message, source, level="DEBUG", etime=None,
details=None):
details=None):
"""
Create virtual machine instance diagnostic entry.
......@@ -344,7 +344,9 @@ def create_instance_diagnostic(vm, message, source, level="DEBUG", etime=None,
:param details: Additional details or debug information.
"""
VirtualMachineDiagnostic.objects.create_for_vm(vm, level, source=source,
source_date=etime, message=message, details=details)
source_date=etime,
message=message,
details=details)
def create_instance(vm, public_nic, flavor, image, password=None):
......@@ -389,9 +391,9 @@ def create_instance(vm, public_nic, flavor, image, password=None):
kw['dry_run'] = settings.TEST
kw['beparams'] = {
'auto_balance': True,
'vcpus': flavor.cpu,
'memory': flavor.ram}
'auto_balance': True,
'vcpus': flavor.cpu,
'memory': flavor.ram}
kw['osparams'] = {
'config_url': vm.config_url,
......@@ -497,7 +499,7 @@ def _create_network(network, backend):
bn = BackendNetwork.objects.get(network=network, backend=backend)
mac_prefix = bn.mac_prefix
except BackendNetwork.DoesNotExist:
raise Exception("BackendNetwork for network '%s' in backend '%s'"\
raise Exception("BackendNetwork for network '%s' in backend '%s'"
" does not exist" % (network.id, backend.id))
with pooled_rapi_client(backend) as client:
......@@ -605,8 +607,9 @@ def set_firewall_profile(vm, profile):
# XXX NOP ModifyInstance call to force process_net_status to run
# on the dispatcher
os_name = settings.GANETI_CREATEINSTANCE_KWARGS['os']
client.ModifyInstance(vm.backend_vm_id,
os_name=settings.GANETI_CREATEINSTANCE_KWARGS['os'])
os_name=os_name)
def get_ganeti_instances(backend=None, bulk=False):
......@@ -661,8 +664,8 @@ def get_physical_resources(backend):
for n in nodes:
# Filter out drained, offline and not vm_capable nodes since they will
# not take part in the vm allocation process
if n['vm_capable'] and not n['drained'] and not n['offline']\
and n['cnodes']:
can_host_vms = n['vm_capable'] and not (n['drained'] or n['offline'])
if can_host_vms and n['cnodes']:
for a in attr:
res[a] += int(n[a])
return res
......@@ -737,7 +740,7 @@ def wait_for_job(client, jobid):
status = result['job_info'][0]
while status not in ['success', 'error', 'cancel']:
result = client.WaitForJobChange(jobid, ['status', 'opresult'],
[result], None)
[result], None)
status = result['job_info'][0]
if status == 'success':
......
......@@ -41,6 +41,7 @@ from synnefo.lib.utils import merge_time
log = logging.getLogger(__name__)
def handle_message_delivery(func):
""" Generic decorator for handling messages.
......@@ -145,7 +146,8 @@ def if_update_required(func):
if db_time and event_time <= db_time:
format_ = "%d/%m/%y %H:%M:%S:%f"
log.debug("Ignoring message %s.\nevent_timestamp: %s db_timestamp: %s",
log.debug("Ignoring message %s.\nevent_timestamp: %s"
" db_timestamp: %s",
msg,
event_time.strftime(format_),
db_time.strftime(format_))
......@@ -246,10 +248,10 @@ def update_build_progress(vm, msg, event_time):
if msg['type'] == 'image-helper':
# for helper task events join subtype to diagnostic source and
# set task name as diagnostic message
if msg.get('subtype', None) and msg.get('subtype') in ['task-start',
'task-end']:
message = msg.get('task', message)
source = "%s-%s" % (source, msg.get('subtype'))
if msg.get('subtype', None):
if msg.get('subtype') in ['task-start', 'task-end']:
message = msg.get('task', message)
source = "%s-%s" % (source, msg.get('subtype'))
if msg.get('subtype', None) == 'warning':
level = 'WARNING'
......@@ -271,7 +273,7 @@ def update_build_progress(vm, msg, event_time):
# create the diagnostic entry
backend.create_instance_diagnostic(vm, message, source, level, event_time,
details=details)
details=details)
log.debug("Done processing ganeti-create-progress msg for vm %s.",
msg['instance'])
......
......@@ -276,7 +276,8 @@ def daemon_mode(opts):
def setup_logging(opts):
import logging
formatter = logging.Formatter("%(asctime)s %(name)s %(module)s [%(levelname)s] %(message)s")
formatter = logging.Formatter("%(asctime)s %(name)s %(module)s"
" [%(levelname)s] %(message)s")
if opts.debug:
log_handler = logging.StreamHandler()
log_handler.setFormatter(formatter)
......
......@@ -52,13 +52,15 @@ class Command(BaseCommand):
make_option('--port', dest='port', default=5080),
make_option('--user', dest='username'),
make_option('--pass', dest='password'),
make_option('--no-check', action='store_false',
make_option(
'--no-check', action='store_false',
dest='check', default=True,
help="Do not perform credentials check and resources update"),
make_option('--no-init', action='store_false',
make_option(
'--no-init', action='store_false',
dest='init', default=True,
help="Do not perform initialization of the Backend Model")
)
)
@transaction.commit_on_success
def handle(self, **options):
......@@ -119,15 +121,15 @@ class Command(BaseCommand):
net.create_backend_network(backend)
result = create_network_synced(net, backend)
if result[0] != "success":
self.stdout.write('\nError Creating Network %s: %s\n' %\
self.stdout.write('\nError Creating Network %s: %s\n' %
(net.backend_id, result[1]))
else:
self.stdout.write('Successfully created Network: %s\n' %
net.backend_id)
net.backend_id)
result = connect_network_synced(network=net, backend=backend)
if result[0] != "success":
self.stdout.write('\nError Connecting Network %s: %s\n' %\
self.stdout.write('\nError Connecting Network %s: %s\n' %
(net.backend_id, result[1]))
else:
self.stdout.write('Successfully connected Network: %s\n' %
net.backend_id)
net.backend_id)
......@@ -43,11 +43,11 @@ class Command(BaseCommand):
option_list = BaseCommand.option_list + (
make_option('-c',
action='store_true',
dest='csv',
default=False,
help="Use pipes to separate values"),
)
action='store_true',
dest='csv',
default=False,
help="Use pipes to separate values"),
)
def handle(self, *args, **options):
if args:
......
......@@ -44,30 +44,30 @@ class Command(BaseCommand):
option_list = BaseCommand.option_list + (
make_option('--clustername',
dest='clustername',
help="Set backend's clustername"),
dest='clustername',
help="Set backend's clustername"),
make_option('--port',
dest='port',
help="Set backend's port"),
dest='port',
help="Set backend's port"),
make_option('--username',
dest='username',
help="Set backend'username"),
dest='username',
help="Set backend'username"),
make_option('--password',
dest='password',
help="Set backend's password"),
dest='password',
help="Set backend's password"),
make_option('--drained',
dest='drained',
choices=["True", "False"],
metavar="True|False",
help="Set the backend as drained to exclude from allocation "\
"operations"),
dest='drained',
choices=["True", "False"],
metavar="True|False",
help="Set the backend as drained to exclude from"
" allocation operations"),
make_option('--offline',
dest='offline',
choices=["True", "False"],
metavar="True|False",
help="Set the backend as offline to not communicate in order "\
"to avoid delays"),
)
dest='offline',
choices=["True", "False"],
metavar="True|False",
help="Set the backend as offline to not communicate in"
" order to avoid delays"),
)
def handle(self, *args, **options):
if len(args) != 1:
......
......@@ -46,12 +46,13 @@ class Command(BaseCommand):
# an SQL transaction
def handle(self, *args, **options):
write = self.stdout.write
if len(args) < 1:
raise CommandError("Please provide a backend ID")
backend = get_backend(args[0])
self.stdout.write('Trying to remove backend: %s\n' % backend.clustername)
write('Trying to remove backend: %s\n' % backend.clustername)
vms_in_backend = VirtualMachine.objects.filter(backend=backend,
deleted=False)
......@@ -59,14 +60,15 @@ class Command(BaseCommand):
if vms_in_backend:
raise CommandError('Backend hosts non-deleted vms. Can not delete')
networks = BackendNetwork.objects.filter(backend=backend, deleted=False)
networks = BackendNetwork.objects.filter(backend=backend,
deleted=False)
networks = [net.network.backend_id for net in networks]
backend.delete()
self.stdout.write('Successfully removed backend.\n')
write('Successfully removed backend.\n')
if networks:
self.stdout.write('Left the following orphans networks in Ganeti:\n')
self.stdout.write(' ' + '\n * '.join(networks) + '\n')
self.stdout.write('Manually remove them.\n')
write('Left the following orphans networks in Ganeti:\n')
write(' ' + '\n * '.join(networks) + '\n')
write('Manually remove them.\n')
......@@ -47,15 +47,15 @@ class Command(BaseCommand):
# an SQL transaction
option_list = BaseCommand.option_list + (
make_option('--backend-id', dest='backend_id',
help="Update statistics of only this backend"),
help="Update statistics of only this backend"),
make_option('--older-than', dest='older_than', metavar="MINUTES",
help="Update only backends that have not been updated for\
MINUTES. Set to 0 to force update."),
help="Update only backends that have not been updated for\
MINUTES. Set to 0 to force update."),
make_option('--include-drained', dest='drained',
default=False,
action='store_true',
help="Also update statistics of drained backends")
)
)
def handle(self, **options):
......
......@@ -46,54 +46,55 @@ class Command(BaseCommand):
help = "Inspect a network on DB and Ganeti."
def handle(self, *args, **options):
write = self.stdout.write
if len(args) != 1:
raise CommandError("Please provide a network ID.")
net = get_network(args[0])
sep = '-' * 80 + '\n'
labels = ('name', 'backend-name', 'state', 'owner', 'subnet', 'gateway',
'mac_prefix', 'link', 'public', 'dhcp', 'flavor', 'deleted',
'action', 'pool')
labels = ('name', 'backend-name', 'state', 'owner', 'subnet',
'gateway', 'mac_prefix', 'link', 'public', 'dhcp', 'flavor',
'deleted', 'action', 'pool')
fields = (net.name, net.backend_id, net.state, str(net.userid),
str(net.subnet), str(net.gateway), str(net.mac_prefix),
str(net.link), str(net.public), str(net.dhcp),
str(net.flavor), str(net.deleted), str(net.action),
str(splitPoolMap(net.get_pool().to_map(), 64)))
self.stdout.write(sep)
self.stdout.write('State of Network in DB\n')
self.stdout.write(sep)
write(sep)
write('State of Network in DB\n')
write(sep)
for l, f in zip(labels, fields):
self.stdout.write(l.ljust(20) + ': ' + f.ljust(20) + '\n')
write(l.ljust(20) + ': ' + f.ljust(20) + '\n')
labels = ('Backend', 'State', 'Deleted', 'JobID', 'OpCode',
'JobStatus')
for back_net in BackendNetwork.objects.filter(network=net):
self.stdout.write('\n')
write('\n')
fields = (back_net.backend.clustername, back_net.operstate,
str(back_net.deleted), str(back_net.backendjobid),
str(back_net.backendopcode),
str(back_net.backendjobstatus))
str(back_net.deleted), str(back_net.backendjobid),
str(back_net.backendopcode),
str(back_net.backendjobstatus))
for l, f in zip(labels, fields):
self.stdout.write(l.ljust(20) + ': ' + f.ljust(20) + '\n')
self.stdout.write('\n')
write(l.ljust(20) + ': ' + f.ljust(20) + '\n')
write('\n')
self.stdout.write(sep)
self.stdout.write('State of Network in Ganeti\n')
self.stdout.write(sep)
write(sep)
write('State of Network in Ganeti\n')
write(sep)
for backend in Backend.objects.exclude(offline=True):
with pooled_rapi_client(backend) as client:
try:
g_net = client.GetNetwork(net.backend_id)
self.stdout.write("Backend: %s\n" % backend.clustername)
write("Backend: %s\n" % backend.clustername)
print json.dumps(g_net, indent=2)
self.stdout.write(sep)
write(sep)
except GanetiApiError as e:
if e.code == 404:
self.stdout.write('Network does not exist in backend %s\n' %
backend.clustername)
write('Network does not exist in backend %s\n' %
backend.clustername)
else:
raise e
......
......@@ -38,6 +38,7 @@ from util import pool_table_from_type
POOL_CHOICES = ['bridge', 'mac-prefix']
class Command(BaseCommand):
help = "List available pools"
output_transaction = True
......
......@@ -39,6 +39,7 @@ from synnefo.db.pools import bitarray_to_map
POOL_CHOICES = ['bridge', 'mac-prefix']
class Command(BaseCommand):
args = "<pool ID>"
help = "Show a pool"
......@@ -81,7 +82,8 @@ class Command(BaseCommand):
step = (type_ == 'bridge') and 64 or 80
print_map('Pool', pool.to_map(), step, self.stdout)
print_map('Reserved', bitarray_to_map(pool.reserved[:pool_row.size]), step, self.stdout)
print_map('Reserved', bitarray_to_map(pool.reserved[:pool_row.size]),
step, self.stdout)
def print_map(name, pool_map, step, out):
......
......@@ -46,7 +46,7 @@ class Command(BaseCommand):
default=True, help="Do not requeue the messages"),
make_option('-i', '--interactive', action='store_true', default=False,
dest='interactive', help="Interactive mode")
)
)
def handle(self, *args, **options):
if len(args) != 1:
......
......@@ -43,13 +43,13 @@ class Command(BaseCommand):
help = "Resend messages from dead letter queues to original exchange"""
option_list = BaseCommand.option_list + (
make_option('--keep-zombies',
action='store_true',
dest='keep_zombies',
default=False,
help="Do not remove messages that died more than one times"
),
)
make_option(
'--keep-zombies',
action='store_true',
dest='keep_zombies',
default=False,
help="Do not remove messages that died more than one times"),
)
def handle(self, *args, **options):
verbose = (options["verbosity"] == "2")
......@@ -97,7 +97,8 @@ class Command(BaseCommand):
headers = message['headers']
death = headers['x-death'][0]
except KeyError:
log.warning("Received message without death section %s. Removing..",
log.warning("Received message without death section %s."
"Removing..",
message)
self.client.basic_nack(message)
......
......@@ -42,7 +42,6 @@ from optparse import make_option
from synnefo.settings import PUBLIC_USE_POOL
from django.core.management.base import BaseCommand
from django.db import transaction
from synnefo.db.models import Backend, Network, BackendNetwork
from synnefo.db.pools import IPPool
......@@ -76,7 +75,7 @@ Network reconciliation can detect and fix the following cases:
make_option('--conflicting-ips', action='store_true',
dest='conflicting_ips', default=False,
help='Detect conflicting ips')
)
)
def handle(self, **options):
global fix, write
......@@ -192,9 +191,9 @@ def reconcile_stale_network(backend_network):
if fix:
etime = datetime.datetime.now()
backend_mod.process_network_status(backend_network, etime, 0,
"OP_NETWORK_REMOVE",
"success",
"Reconciliation simulated event")
"OP_NETWORK_REMOVE",
"success",
"Reconciliation simulated event")
write("F: Reconciled event: OP_NETWORK_REMOVE\n")
......@@ -223,9 +222,9 @@ def reconcile_unsynced_network(network, backend, backend_network):
write("F: Issuing OP_NETWORK_CONNECT\n")
etime = datetime.datetime.now()
backend_mod.process_network_status(backend_network, etime, 0,
"OP_NETWORK_CONNECT",
"success",
"Reconciliation simulated eventd")
"OP_NETWORK_CONNECT",
"success",
"Reconciliation simulated eventd")
def reconcile_ip_pools(network, available_maps, reserved_maps):
......@@ -257,7 +256,7 @@ def detect_conflicting_ips(network):
for i in distinct_ips:
ips.remove(i)
for i in ips:
machines = [utils.id_to_instance_name(x[1]) \
machines = [utils.id_to_instance_name(x[1])
for x in machine_ips if x[0] == i]
write('D: Conflicting IP:%s Machines: %s\n' %
(i, ', '.join(machines)))
......
......@@ -34,7 +34,6 @@ from synnefo.db.models import (Network, BackendNetwork,
from synnefo.db.pools import EmptyPool
class Command(BaseCommand):
help = 'Check consistency of unique resources.'
......@@ -58,17 +57,16 @@ class Command(BaseCommand):
bridges = []
for i in xrange(0, bridge_pool.size()):
if not bridge_pool.is_available(i, index=True) and \
not bridge_pool.is_reserved(i, index=True):
bridges.append(bridge_pool.index_to_value(i))
not bridge_pool.is_reserved(i, index=True):
bridges.append(bridge_pool.index_to_value(i))
write("Used bridges from Pool: %d\n" % len(bridges))
network_bridges = Network.objects.filter(flavor='PHYSICAL_VLAN',
deleted=False)\
deleted=False)\
.values_list('link', flat=True)
write("Used bridges from Networks: %d\n" %
len(network_bridges))
write("Used bridges from Networks: %d\n" % len(network_bridges))
set_network_bridges = set(network_bridges)
if len(network_bridges) > len(set_network_bridges):
......@@ -80,8 +78,7 @@ class Command(BaseCommand):
write("Duplicated bridge: %s. " % bridge)
write("Used by the following Networks:\n")
nets = Network.objects.filter(deleted=False, link=bridge)
write(" " + "\n ".join([str(net.id) for net in nets])\
+ "\n")
write(" " + "\n ".join([str(net.id) for net in nets]) + "\n")
def detect_mac_prefixes(self):
write = self.stdout.write
......@@ -107,9 +104,9 @@ class Command(BaseCommand):
network_mac_prefixes = \
Network.objects.filter(deleted=False, flavor='MAC_FILTERED')\
.values_list('mac_prefix', flat=True)
.values_list('mac_prefix', flat=True)
write("Used MAC prefixes from Networks: %d\n" %
len(network_mac_prefixes))
len(network_mac_prefixes))