Resolve flake8 errors in cobler_system script
Change-Id: Icd1bf34a234410ea0b6365b127b771a3e0ce29ae
This commit is contained in:
parent
8384b8ca4d
commit
32b0e76e21
|
@ -1,58 +1,55 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
# flake8: noqa
|
|
||||||
|
|
||||||
import sys
|
|
||||||
import re
|
|
||||||
import shlex
|
|
||||||
import yaml
|
|
||||||
import argparse
|
import argparse
|
||||||
import logging
|
import logging
|
||||||
|
import re
|
||||||
|
import shlex
|
||||||
import subprocess
|
import subprocess
|
||||||
import StringIO
|
import sys
|
||||||
|
import yaml
|
||||||
|
|
||||||
console = logging.StreamHandler()
|
console = logging.StreamHandler()
|
||||||
# formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
|
|
||||||
formatter = logging.Formatter('%(message)s')
|
formatter = logging.Formatter('%(message)s')
|
||||||
console.setFormatter(formatter)
|
console.setFormatter(formatter)
|
||||||
logger = logging.getLogger()
|
logger = logging.getLogger()
|
||||||
logger.setLevel(logging.DEBUG)
|
logger.setLevel(logging.DEBUG)
|
||||||
logger.addHandler(console)
|
logger.addHandler(console)
|
||||||
|
|
||||||
|
|
||||||
def system_command(command, expected_resultcodes=(0,)):
|
def system_command(command, expected_resultcodes=(0,)):
|
||||||
|
commands = [i.strip() for i in re.split(ur'\|', command)]
|
||||||
commands = [ i.strip() for i in re.split(ur'\|', command)]
|
|
||||||
|
|
||||||
process = []
|
process = []
|
||||||
process.append(subprocess.Popen(shlex.split(commands[0]),
|
process.append(subprocess.Popen(shlex.split(commands[0]),
|
||||||
stdin=None,
|
stdin=None,
|
||||||
stdout=subprocess.PIPE,
|
stdout=subprocess.PIPE,
|
||||||
stderr=subprocess.PIPE))
|
stderr=subprocess.PIPE))
|
||||||
|
|
||||||
for c in commands[1:]:
|
for c in commands[1:]:
|
||||||
process[-1].wait()
|
process[-1].wait()
|
||||||
process.append(subprocess.Popen(shlex.split(c),
|
process.append(subprocess.Popen(shlex.split(c),
|
||||||
stdin=process[-1].stdout,
|
stdin=process[-1].stdout,
|
||||||
stdout=subprocess.PIPE,
|
stdout=subprocess.PIPE,
|
||||||
stderr=subprocess.PIPE))
|
stderr=subprocess.PIPE))
|
||||||
|
|
||||||
process[-1].wait()
|
process[-1].wait()
|
||||||
|
|
||||||
serr = []
|
serr = []
|
||||||
for p in process:
|
for p in process:
|
||||||
serr += [ err.strip() for err in p.stderr.readlines() ]
|
serr += [err.strip() for err in p.stderr.readlines()]
|
||||||
|
|
||||||
returncode = process[-1].returncode
|
returncode = process[-1].returncode
|
||||||
|
|
||||||
if expected_resultcodes and not returncode in expected_resultcodes:
|
if expected_resultcodes and returncode not in expected_resultcodes:
|
||||||
logger.error("""Command: '%s'\nreturned not expected \
|
logger.error(
|
||||||
value: %d\nstdout:\n%s\nstderr:\n%s""" % \
|
"Command: '%s'\nreturned not expected value: %d\n"
|
||||||
(command, returncode,
|
"stdout:\n%s\nstderr:\n%s",
|
||||||
process[-1].stdout.read().rstrip('\n'),
|
(command, returncode, process[-1].stdout.read().rstrip('\n'),
|
||||||
'\n'.join(serr).rstrip('\n')))
|
'\n'.join(serr).rstrip('\n')))
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
else:
|
else:
|
||||||
logger.debug("Command '%s' returned %d" % (command, returncode))
|
logger.debug("Command '%s' returned %d" % (command, returncode))
|
||||||
|
|
||||||
return returncode
|
return returncode
|
||||||
|
|
||||||
|
|
||||||
|
@ -64,34 +61,35 @@ def is_system_exist(system_name):
|
||||||
code = system_command(command, expected_resultcodes=(0, 1))
|
code = system_command(command, expected_resultcodes=(0, 1))
|
||||||
return code == 0
|
return code == 0
|
||||||
|
|
||||||
|
|
||||||
def update_system(system_name, system_dict):
|
def update_system(system_name, system_dict):
|
||||||
addedit = 'add'
|
addedit = 'add'
|
||||||
if is_system_exist(system_name):
|
if is_system_exist(system_name):
|
||||||
addedit = 'edit'
|
addedit = 'edit'
|
||||||
|
|
||||||
command = ["""/usr/bin/cobbler system %s --name='%s' --hostname='%s'""" % \
|
command = ["/usr/bin/cobbler system %s --name='%s' --hostname='%s'" %
|
||||||
(addedit, system_name, system_dict['hostname'])]
|
(addedit, system_name, system_dict['hostname'])]
|
||||||
|
|
||||||
ksmeta = system_dict.get('ksmeta', '')
|
ksmeta = system_dict.get('ksmeta', '')
|
||||||
for opt in system_dict:
|
for opt in system_dict:
|
||||||
if opt in ('interfaces', 'ksmeta', 'interfaces_extra'):
|
if opt in ('interfaces', 'ksmeta', 'interfaces_extra'):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
command.append("""--%s='%s'""" % (opt, system_dict[opt]))
|
command.append("""--%s='%s'""" % (opt, system_dict[opt]))
|
||||||
|
|
||||||
for int_name in system_dict.get('interfaces_extra',{}):
|
for int_name in system_dict.get('interfaces_extra', {}):
|
||||||
int_extra_dict = system_dict['interfaces_extra'][int_name]
|
int_extra_dict = system_dict['interfaces_extra'][int_name]
|
||||||
for int_extra in int_extra_dict:
|
for int_extra in int_extra_dict:
|
||||||
ksmeta = """%s interface_extra_%s_%s=%s""" % \
|
ksmeta = """%s interface_extra_%s_%s=%s""" % \
|
||||||
(ksmeta, int_name, int_extra, int_extra_dict[int_extra])
|
(ksmeta, int_name, int_extra, int_extra_dict[int_extra])
|
||||||
|
|
||||||
command.append("""--ksmeta='%s'""" % ksmeta)
|
command.append("""--ksmeta='%s'""" % ksmeta)
|
||||||
command = " ".join(command)
|
command = " ".join(command)
|
||||||
|
|
||||||
logger.info("Running command: %s" % command)
|
logger.info("Running command: %s" % command)
|
||||||
return system_command(command) == 0
|
return system_command(command) == 0
|
||||||
|
|
||||||
|
|
||||||
def update_system_interfaces(system_name, interfaces_dict):
|
def update_system_interfaces(system_name, interfaces_dict):
|
||||||
addedit = 'add'
|
addedit = 'add'
|
||||||
if is_system_exist(system_name):
|
if is_system_exist(system_name):
|
||||||
|
@ -101,40 +99,40 @@ def update_system_interfaces(system_name, interfaces_dict):
|
||||||
for interface_name in interfaces_dict:
|
for interface_name in interfaces_dict:
|
||||||
logger.info("=== Defining interface ===: %s" % interface_name)
|
logger.info("=== Defining interface ===: %s" % interface_name)
|
||||||
int_opts = interfaces_dict[interface_name]
|
int_opts = interfaces_dict[interface_name]
|
||||||
|
|
||||||
command = ["""/usr/bin/cobbler system %s --name='%s' \
|
command = ["""/usr/bin/cobbler system %s --name='%s' \
|
||||||
--interface='%s'""" % (addedit, system_name, interface_name)]
|
--interface='%s'""" % (addedit, system_name, interface_name)]
|
||||||
|
|
||||||
for opt in int_opts:
|
for opt in int_opts:
|
||||||
logger.debug("Interface option: %s = %s" % (opt, int_opts[opt]))
|
logger.debug("Interface option: %s = %s" % (opt, int_opts[opt]))
|
||||||
command.append("""--%s='%s'""" % (opt, int_opts[opt]))
|
command.append("""--%s='%s'""" % (opt, int_opts[opt]))
|
||||||
|
|
||||||
command = " ".join(command)
|
command = " ".join(command)
|
||||||
|
|
||||||
logger.info("Running command: %s" % command)
|
logger.info("Running command: %s" % command)
|
||||||
code.union(set([system_command(command)]))
|
code.union(set([system_command(command)]))
|
||||||
|
|
||||||
return len(code) == 0
|
return len(code) == 0
|
||||||
|
|
||||||
def main():
|
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("-f", "--file", dest="file",
|
parser.add_argument("-f", "--file", dest="file",
|
||||||
metavar="YAML_FILE", type=str,
|
metavar="YAML_FILE", type=str,
|
||||||
help="nodes yaml file")
|
help="nodes yaml file")
|
||||||
parser.add_argument("-l", "--level", dest="log_level", type=str,
|
parser.add_argument("-l", "--level", dest="log_level", type=str,
|
||||||
help="log level, one of DEBUG, INFO, WARNING, ERROR",
|
help="log level, one of DEBUG, INFO, WARNING, ERROR",
|
||||||
choices=["DEBUG", "INFO", "WARNING", "ERROR"],
|
choices=["DEBUG", "INFO", "WARNING", "ERROR"],
|
||||||
default="INFO", metavar="LEVEL")
|
default="INFO", metavar="LEVEL")
|
||||||
|
|
||||||
params = parser.parse_args()
|
params = parser.parse_args()
|
||||||
|
|
||||||
numeric_level = getattr(logging, params.log_level.upper())
|
numeric_level = getattr(logging, params.log_level.upper())
|
||||||
logger.setLevel(numeric_level)
|
logger.setLevel(numeric_level)
|
||||||
|
|
||||||
if params.file is None:
|
if params.file is None:
|
||||||
parser.error("Yaml file must be defined with -f option.")
|
parser.error("Yaml file must be defined with -f option.")
|
||||||
|
|
||||||
with open(params.file, 'r') as file:
|
with open(params.file, 'r') as file:
|
||||||
nodes = yaml.load(file.read())
|
nodes = yaml.load(file.read())
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue