Commit 32bd7c06 authored by Marco Mariani's avatar Marco Mariani

bring out argparsing and logger setup from mammoth function

parent 521e7c91
for slapgrid:
refactor loggers -- maybe reduce their number?
pay attention to mandatory parameters and related dependencies
* enable --debug by default
* multiline help from docstring
instance
software
report
issues:
slapformat --log_file vs cliff --log-file
...@@ -85,95 +85,97 @@ class _formatXMLError(Exception): ...@@ -85,95 +85,97 @@ class _formatXMLError(Exception):
pass pass
def parseArgumentTupleAndReturnSlapgridObject(*argument_tuple): def parse_arguments_merge_config(*argument_tuple):
"""Parses arguments either from command line, from method parameters or from """Parse arguments and return options dictionary
config file. Then returns a new instance of slapgrid.Slapgrid with those merged with the config file."""
parameters. Also returns the options dict and unused variable list, and
configures logger. ap = argparse.ArgumentParser()
""" ap.add_argument('--instance-root',
parser = argparse.ArgumentParser() help='The instance root directory location.')
parser.add_argument("--instance-root", ap.add_argument('--software-root',
help="The instance root directory location.") help='The software_root directory location.')
parser.add_argument("--software-root", ap.add_argument('--master-url',
help="The software_root directory location.") help='The master server URL. Mandatory.')
parser.add_argument("--master-url", ap.add_argument('--computer-id',
help="The master server URL. Mandatory.") help='The computer id defined in the server.')
parser.add_argument("--computer-id", ap.add_argument('--supervisord-socket',
help="The computer id defined in the server.") help='The socket supervisor will use.')
parser.add_argument("--supervisord-socket", ap.add_argument('--supervisord-configuration-path',
help="The socket supervisor will use.") help='The location where supervisord configuration will be stored.')
parser.add_argument("--supervisord-configuration-path", ap.add_argument('--buildout', default=None,
help="The location where supervisord configuration will be stored.") help='Location of buildout binary.')
parser.add_argument("--buildout", default=None, ap.add_argument('--pidfile',
help="Location of buildout binary.") help='The location where pidfile will be created.')
parser.add_argument("--pidfile", ap.add_argument('--logfile',
help="The location where pidfile will be created.") help='The location where slapgrid logfile will be created.')
parser.add_argument("--logfile", ap.add_argument('--key_file',
help="The location where slapgrid logfile will be created.") help='SSL Authorisation key file.')
parser.add_argument("--key_file", help="SSL Authorisation key file.") ap.add_argument('--cert_file',
parser.add_argument("--cert_file", help='SSL Authorisation certificate file.')
help="SSL Authorisation certificate file.") ap.add_argument('--signature_private_key_file',
parser.add_argument("--signature_private_key_file", help='Signature private key file.')
help="Signature private key file.") ap.add_argument('--master_ca_file',
parser.add_argument("--master_ca_file", help='Root certificate of SlapOS master key.')
help="Root certificate of SlapOS master key.") ap.add_argument('--certificate_repository_path',
parser.add_argument("--certificate_repository_path", help='Path to directory where downloaded certificates would be stored.')
help="Path to directory where downloaded certificates would be stored.") ap.add_argument('-v', '--verbose', action='store_true',
parser.add_argument("-v", "--verbose", action="store_true", help='Be verbose.')
help="Be verbose.") ap.add_argument('--maximum-periodicity', type=int, default=None,
parser.add_argument("--maximum-periodicity", type=int, default=None, help='Periodicity at which buildout should be run in instance.')
help="Periodicity at which buildout should be run in instance.") ap.add_argument('--promise-timeout', type=int, default=3,
parser.add_argument("--promise-timeout", type=int, default=3, help='Promise timeout in seconds.')
help="Promise timeout in seconds.") ap.add_argument('--now', action='store_true',
parser.add_argument("--now", action="store_true", help='Launch slapgrid without delay. Default behavior.')
help="Launch slapgrid without delay. Default behavior.") ap.add_argument('--all', action='store_true',
parser.add_argument("--all", action="store_true", help='Launch slapgrid to process all Softare Releases '
help="Launch slapgrid to process all Softare Releases " 'and/or Computer Partitions.')
"and/or Computer Partitions.") ap.add_argument('--only-sr',
parser.add_argument("--only-sr", help='Force the update of a single software release (use url hash), '
help="Force the update of a single software release (use url hash), " 'even if is already installed. This option will make all others '
"even if is already installed. This option will make all others " 'sofware releases be ignored.')
"sofware releases be ignored.") ap.add_argument("--only-cp",
parser.add_argument("--only-cp", help='Update a single or a list of computer partitions '
help="Update a single or a list of computer partitions " '(ie.:slappartX, slappartY), '
"(ie.:slappartX, slappartY)," 'this option will make all others computer partitions be ignored.')
"this option will make all others computer partitions be ignored.")
ap.add_argument('configuration_file', type=argparse.FileType(),
parser.add_argument("configuration_file", nargs=1, type=argparse.FileType(), help='SlapOS configuration file.')
help="SlapOS configuration file.")
# Deprecated options # Deprecated options
parser.add_argument("-c", "--console", action="store_true", ap.add_argument('-c', '--console', action='store_true',
help="Deprecated, doesn't do anything.") help="Deprecated, doesn't do anything.")
parser.add_argument("--develop", action="store_true", ap.add_argument('--develop', action='store_true',
help="Deprecated, same as --all.") help='Deprecated, same as --all.')
parser.add_argument("--only_sr", ap.add_argument('--only_sr',
help="Deprecated, same as --only-sr.") help='Deprecated, same as --only-sr.')
parser.add_argument("--only_cp", ap.add_argument('--only_cp',
help="Deprecated, same as --only-cp.") help='Deprecated, same as --only-cp.')
parser.add_argument("--maximal_delay", ap.add_argument('--maximal_delay',
help="Deprecated. Will only work from configuration file in the future.") help='Deprecated. Will only work from configuration file in the future.')
# Parses arguments # Parses arguments
if not argument_tuple: if not argument_tuple:
# No arguments given to entry point : we parse sys.argv. # No arguments given to entry point : we parse sys.argv.
argument_option_instance = parser.parse_args() args = ap.parse_args()
else: else:
argument_option_instance = parser.parse_args(list(argument_tuple)) args = ap.parse_args(list(argument_tuple))
# Parses arguments from config file, if needed, then merge previous arguments # Parses arguments from config file, if needed, then merge previous arguments
options = {} options = {}
configuration_file = argument_option_instance.configuration_file[0]
# Loads config (if config specified) # Loads config (if config specified)
slapgrid_configuration = ConfigParser.SafeConfigParser() config = ConfigParser.SafeConfigParser()
slapgrid_configuration.readfp(configuration_file) config.readfp(args.configuration_file)
# Merges the two dictionnaries # Merges the two dictionnaries
options = dict(slapgrid_configuration.items("slapos")) options = dict(config.items('slapos'))
if slapgrid_configuration.has_section("networkcache"): if config.has_section('networkcache'):
options.update(dict(slapgrid_configuration.items("networkcache"))) options.update(dict(config.items('networkcache')))
for argument_key, argument_value in vars(argument_option_instance for key, value in vars(args).iteritems():
).iteritems(): if value is not None:
if argument_value is not None: options[key] = value
options.update({argument_key: argument_value})
return options
def setup_logger(options):
# Configures logger. # Configures logger.
if options['verbose']: if options['verbose']:
level = logging.DEBUG level = logging.DEBUG
...@@ -189,10 +191,21 @@ def parseArgumentTupleAndReturnSlapgridObject(*argument_tuple): ...@@ -189,10 +191,21 @@ def parseArgumentTupleAndReturnSlapgridObject(*argument_tuple):
'%(asctime)s %(name)-18s: %(levelname)-8s %(message)s')) '%(asctime)s %(name)-18s: %(levelname)-8s %(message)s'))
logging.getLogger('').addHandler(console) logging.getLogger('').addHandler(console)
missing_mandatory_parameter_list = [] # XXX return and use logger object
def parseArgumentTupleAndReturnSlapgridObject(*argument_tuple):
"""Returns a new instance of slapgrid.Slapgrid created with argument+config parameters.
Also returns the options dict and unused variable list, and configures logger.
"""
options = parse_arguments_merge_config(*argument_tuple)
setup_logger(options)
missing = []
for mandatory_parameter in MANDATORY_PARAMETER_LIST: for mandatory_parameter in MANDATORY_PARAMETER_LIST:
if not mandatory_parameter in options: if not mandatory_parameter in options:
missing_mandatory_parameter_list.append(mandatory_parameter) missing.append(mandatory_parameter)
if options.get('all'): if options.get('all'):
options['develop'] = True options['develop'] = True
...@@ -204,18 +217,18 @@ def parseArgumentTupleAndReturnSlapgridObject(*argument_tuple): ...@@ -204,18 +217,18 @@ def parseArgumentTupleAndReturnSlapgridObject(*argument_tuple):
if 'key_file' in options: if 'key_file' in options:
repository_required = True repository_required = True
if not 'cert_file' in options: if not 'cert_file' in options:
missing_mandatory_parameter_list.append('cert_file') missing.append('cert_file')
if 'cert_file' in options: if 'cert_file' in options:
repository_required = True repository_required = True
if not 'key_file' in options: if not 'key_file' in options:
missing_mandatory_parameter_list.append('key_file') missing.append('key_file')
if repository_required: if repository_required:
if 'certificate_repository_path' not in options: if 'certificate_repository_path' not in options:
missing_mandatory_parameter_list.append('certificate_repository_path') missing.append('certificate_repository_path')
if len(missing_mandatory_parameter_list) > 0: if missing:
parser.error('Missing mandatory parameters:\n%s' % '\n'.join( raise RuntimeError('Missing mandatory parameters:\n%s' % '\n'.join(
missing_mandatory_parameter_list)) missing))
key_file = options.get('key_file') key_file = options.get('key_file')
cert_file = options.get('cert_file') cert_file = options.get('cert_file')
...@@ -235,12 +248,12 @@ def parseArgumentTupleAndReturnSlapgridObject(*argument_tuple): ...@@ -235,12 +248,12 @@ def parseArgumentTupleAndReturnSlapgridObject(*argument_tuple):
for f in mandatory_file_list: for f in mandatory_file_list:
if f is not None: if f is not None:
if not os.path.exists(f): if not os.path.exists(f):
parser.error('File %r does not exist.' % f) raise RuntimeError('File %r does not exist.' % f)
certificate_repository_path = options.get('certificate_repository_path') certificate_repository_path = options.get('certificate_repository_path')
if certificate_repository_path: if certificate_repository_path:
if not os.path.isdir(certificate_repository_path): if not os.path.isdir(certificate_repository_path):
parser.error('Directory %r does not exist' % certificate_repository_path) raise RuntimeError('Directory %r does not exist' % certificate_repository_path)
# Supervisord configuration location # Supervisord configuration location
if not options.get('supervisord_configuration_path'): if not options.get('supervisord_configuration_path'):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment