The branch, master has been updated via b724c1e6a66 utils: Avoid pylint warning via 319e27343d7 utils: Reformat lines that are longer than 80 columns via 98c7a38b711 utils: Tweak exception handling to stop flake8 complaining via 12d3e215a60 utils: Simplify log level logic, drop global variable via e323d16a9d1 utils: Inline defaults and help strings via af5aecced12 utils: Move argument processing into function and call from main() via e66637a079c utils: Reorder imports so that standard imports are first via bd0b2bb6ee9 utils: Clean up ctdb_etcd_lock using autopep8 via 939aed04982 utils: Use Python 3 from d961830cb59 examples: Make winreg.py sample work with python3 in current master
https://git.samba.org/?p=samba.git;a=shortlog;h=master - Log ----------------------------------------------------------------- commit b724c1e6a660eb6b9ceaf3c81d6dac0b0562206d Author: Martin Schwenke <mar...@meltin.net> Date: Tue Apr 27 15:45:17 2021 +1000 utils: Avoid pylint warning pylint warns: Use lazy % formatting in logging functions Signed-off-by: Martin Schwenke <mar...@meltin.net> Reviewed-by: David Disseldorp <dd...@samba.org> Reviewed-by: Jose A. Rivera <jar...@samba.org> Autobuild-User(master): Martin Schwenke <mart...@samba.org> Autobuild-Date(master): Tue Jul 20 05:29:18 UTC 2021 on sn-devel-184 commit 319e27343d7ee5f7f6045a19747ba85fb4bef768 Author: Martin Schwenke <mar...@meltin.net> Date: Tue Apr 27 15:37:43 2021 +1000 utils: Reformat lines that are longer than 80 columns Signed-off-by: Martin Schwenke <mar...@meltin.net> Reviewed-by: David Disseldorp <dd...@samba.org> Reviewed-by: Jose A. Rivera <jar...@samba.org> commit 98c7a38b711d38ac756ca7e34769eb277904f7d0 Author: Martin Schwenke <mar...@meltin.net> Date: Tue Apr 27 14:56:20 2021 +1000 utils: Tweak exception handling to stop flake8 complaining Don't bother with "as e" to avoid warning about unused variable. Don't use bare "except:" (though pylint still complains about this version). Signed-off-by: Martin Schwenke <mar...@meltin.net> Reviewed-by: David Disseldorp <dd...@samba.org> Reviewed-by: Jose A. Rivera <jar...@samba.org> commit 12d3e215a6096fc9862642b98dd8bca1421f2cae Author: Martin Schwenke <mar...@meltin.net> Date: Wed May 26 11:18:04 2021 +1000 utils: Simplify log level logic, drop global variable Signed-off-by: Martin Schwenke <mar...@meltin.net> Reviewed-by: David Disseldorp <dd...@samba.org> Reviewed-by: Jose A. Rivera <jar...@samba.org> commit e323d16a9d11c63640fca186c6f5a29360fb3c7b Author: Martin Schwenke <mar...@meltin.net> Date: Tue Apr 27 14:50:15 2021 +1000 utils: Inline defaults and help strings Removes an unnecessary level of indirection: defaults and help strings are now where they are expected. Also removes some global variables. Signed-off-by: Martin Schwenke <mar...@meltin.net> Reviewed-by: David Disseldorp <dd...@samba.org> Reviewed-by: Jose A. Rivera <jar...@samba.org> commit af5aecced12a8f6a9259602f8ddf3662fe6c1ba0 Author: Martin Schwenke <mar...@meltin.net> Date: Wed May 26 10:57:07 2021 +1000 utils: Move argument processing into function and call from main() Removes the need for the global variables currently associated with this processing. Also removes unnecessarily double-handling the defaults, which are assigned to the global variables and set via add_argument(). Signed-off-by: Martin Schwenke <mar...@meltin.net> Reviewed-by: David Disseldorp <dd...@samba.org> Reviewed-by: Jose A. Rivera <jar...@samba.org> commit e66637a079c070d29c685b6315e5427679dc778a Author: Martin Schwenke <mar...@meltin.net> Date: Tue Apr 27 13:00:49 2021 +1000 utils: Reorder imports so that standard imports are first Avoids numerous pylint warnings. Signed-off-by: Martin Schwenke <mar...@meltin.net> Reviewed-by: David Disseldorp <dd...@samba.org> Reviewed-by: Jose A. Rivera <jar...@samba.org> commit bd0b2bb6ee9d03a259e7d7e9f4397f4dbe3f1b91 Author: Martin Schwenke <mar...@meltin.net> Date: Tue Apr 27 12:59:17 2021 +1000 utils: Clean up ctdb_etcd_lock using autopep8 Signed-off-by: Martin Schwenke <mar...@meltin.net> Reviewed-by: David Disseldorp <dd...@samba.org> Reviewed-by: Jose A. Rivera <jar...@samba.org> commit 939aed0498269df3c1e012f3b68c314b583f25bd Author: Martin Schwenke <mar...@meltin.net> Date: Tue Apr 27 15:46:14 2021 +1000 utils: Use Python 3 Due to the number of flake8 and pylint warnings it is unclear if the source has Python 3 incompatibilities. These will be cleaned up in subsequent commits. Signed-off-by: "L.P.H. van Belle" <be...@bazuin.nl> Reviewed-by: Martin Schwenke <mar...@meltin.net> Reviewed-by: David Disseldorp <dd...@samba.org> Reviewed-by: Jose A. Rivera <jar...@samba.org> ----------------------------------------------------------------------- Summary of changes: ctdb/utils/etcd/ctdb_etcd_lock | 275 +++++++++++++++++++++-------------------- 1 file changed, 140 insertions(+), 135 deletions(-) Changeset truncated at 500 lines: diff --git a/ctdb/utils/etcd/ctdb_etcd_lock b/ctdb/utils/etcd/ctdb_etcd_lock index 000c6bb7208..5e722267cd3 100755 --- a/ctdb/utils/etcd/ctdb_etcd_lock +++ b/ctdb/utils/etcd/ctdb_etcd_lock @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -52,157 +52,162 @@ parameters, see here: https://github.com/jplana/python-etcd/ """ import signal import time -import etcd import sys import os import argparse import logging import subprocess -# Globals --------------------------------------------------------------------- -# -defaults = { 'config': os.path.join( - os.getenv('CTDB_BASE', '/usr/local/etc/ctdb'), - 'etcd'), - 'verbose' : 0, - } -helpmsg = { 'config': 'Configuration file to use. The default behavior ' + \ - 'is to look is the base CTDB configuration ' + \ - 'directory, which can be overwritten by setting the' + \ - 'CTDB_BASE environment variable, for a file called' + \ - '\'etcd\'. Default value is ' + defaults['config'], - 'verbose' : 'Display verbose output to stderr. Default is no output.', - } - -log_levels = { 0: logging.ERROR, - 1: logging.WARNING, - 2: logging.DEBUG, - } - -config_file = defaults['config'] -verbose = defaults['verbose'] +import etcd # Helper Functions ------------------------------------------------------------ # + + +def process_args(): + '''Process command-line arguments and return them. + ''' + parser = argparse.ArgumentParser( + description=__doc__, + epilog='', + formatter_class=argparse.RawDescriptionHelpFormatter) + parser.add_argument('-v', '--verbose', + action='count', + help='Display verbose output to stderr. ' + 'Default is no output.', + default=0, + ) + parser.add_argument('-c', '--config', + action='store', + help='Configuration file to use. The default behavior ' + 'is to look is the base CTDB configuration ' + 'directory, which can be overwritten by setting ' + 'the CTDB_BASE environment variable, for a file ' + 'called \'etcd\'. Default value is %(default)s.', + default=os.path.join(os.getenv('CTDB_BASE', + '/usr/local/etc/ctdb'), + 'etcd'), + ) + args = parser.parse_args() + + return args + + +def setup_logging(verbose): + '''Setup logging based on specified verbosity. + ''' + + log_levels = [logging.ERROR, logging.WARNING, logging.DEBUG] + logging.basicConfig(level=log_levels[min(verbose, len(log_levels)-1)]) + + def sigterm_handler(signum, frame): - """Handler for SIGTERM signals. - """ - sys.exit() + """Handler for SIGTERM signals. + """ + sys.exit() + def print_nonl(out): - """Dumb shortcut for printing to stdout with no newline. - """ - sys.stdout.write(str(out)) - sys.stdout.flush() + """Dumb shortcut for printing to stdout with no newline. + """ + sys.stdout.write(str(out)) + sys.stdout.flush() + def int_or_not(s): - """Try to convert input to an integer. - """ - try: - return int(s) - except ValueError: - return s + """Try to convert input to an integer. + """ + try: + return int(s) + except ValueError: + return s # Mainline -------------------------------------------------------------------- # + + def main(): - global config_file - global verbose - - logging.basicConfig(level=log_levels[verbose]) - - # etcd config defaults - etcd_config = { - 'port' : 2379, - 'locks_dir' : '_ctdb', - 'lock_ttl' : 9, - 'lock_refresh': 2, - } - # Find and read etcd config file - etcd_client_params = ( - 'host', - 'port', - 'srv_domain', - 'version_prefix', - 'read_timeout', - 'allow_redirect', - 'protocol', - 'cert', - 'ca_cert', - 'username', - 'password', - 'allow_reconnect', - 'use_proxies', - 'expected_cluster_id', - 'per_host_pool_size', - ) - if os.path.isfile(config_file): - f = open(config_file, 'r') - for line in f: - (key, value) = line.split("=",1) - etcd_config[key.strip()] = int_or_not(value.strip()) - - # Minor hack: call out to shell to retrieve CTDB netbios name and PNN. - tmp = subprocess.Popen("testparm -s --parameter-name 'netbios name'; \ + args = process_args() + + setup_logging(args.verbose) + + # etcd config defaults + etcd_config = { + 'port': 2379, + 'locks_dir': '_ctdb', + 'lock_ttl': 9, + 'lock_refresh': 2, + } + # Find and read etcd config file + etcd_client_params = ( + 'host', + 'port', + 'srv_domain', + 'version_prefix', + 'read_timeout', + 'allow_redirect', + 'protocol', + 'cert', + 'ca_cert', + 'username', + 'password', + 'allow_reconnect', + 'use_proxies', + 'expected_cluster_id', + 'per_host_pool_size', + ) + if os.path.isfile(args.config): + f = open(args.config, 'r') + for line in f: + (key, value) = line.split("=", 1) + etcd_config[key.strip()] = int_or_not(value.strip()) + + # Minor hack: call out to shell to retrieve CTDB netbios name and PNN. + tmp = subprocess.Popen("testparm -s --parameter-name 'netbios name'; \ ctdb pnn", - shell=True, - universal_newlines=True, - stdout=subprocess.PIPE - ).stdout.read().strip() - nb_name, pnn = tmp.split() - - # Try to get and hold the lock - try: - client = etcd.Client(**{k: etcd_config[k] for k in \ - set(etcd_client_params).intersection(etcd_config)}) - lock = etcd.Lock(client, etcd_config['locks_dir'] + "/" + nb_name) - lock._uuid = lock._uuid + "_" + pnn - logging.debug("Updated lock UUID: " + lock.uuid) - ppid = os.getppid() - while True: - lock.acquire(blocking=False, lock_ttl=etcd_config['lock_ttl']) - if lock.is_acquired: - print_nonl(0) - else: - locks = "No locks found." + shell=True, + universal_newlines=True, + stdout=subprocess.PIPE + ).stdout.read().strip() + nb_name, pnn = tmp.split() + + # Try to get and hold the lock + try: + client = etcd.Client( + **{k: etcd_config[k] for k in + set(etcd_client_params).intersection(etcd_config)}) + lock = etcd.Lock(client, etcd_config['locks_dir'] + "/" + nb_name) + lock._uuid = lock._uuid + "_" + pnn + logging.debug("Updated lock UUID: %s", lock.uuid) + ppid = os.getppid() + while True: + lock.acquire(blocking=False, lock_ttl=etcd_config['lock_ttl']) + if lock.is_acquired: + print_nonl(0) + else: + locks = "No locks found." + if logging.getLogger().getEffectiveLevel() == logging.DEBUG: + keys = client.read(lock.path, recursive=True) + if keys is not None: + locks = "Existing locks:\n " + locks += '\n '.join( + (child.key + ": " + child.value for child in + keys.children)) + logging.debug("Lock contention. %s", locks) + print_nonl(1) + break + os.kill(ppid, 0) + time.sleep(etcd_config['lock_refresh']) + except (OSError, SystemExit): + if lock is not None and lock.is_acquired: + lock.release() + except Exception: + print_nonl(3) if logging.getLogger().getEffectiveLevel() == logging.DEBUG: - keys = client.read(lock.path, recursive=True) - if keys is not None: - locks = "Existing locks:\n " - locks += '\n '.join((child.key + ": " + child.value for child in keys.children)) - logging.debug("Lock contention. " + locks) - print_nonl(1) - break - os.kill(ppid, 0) - time.sleep(etcd_config['lock_refresh']) - except (OSError, SystemExit) as e: - if lock is not None and lock.is_acquired: - lock.release() - except: - print_nonl(3) - if logging.getLogger().getEffectiveLevel() == logging.DEBUG: - raise - -if __name__== "__main__": - signal.signal(signal.SIGTERM, sigterm_handler) - - parser = argparse.ArgumentParser( - description=__doc__, - epilog='', - formatter_class=argparse.RawDescriptionHelpFormatter ) - parser.add_argument( '-v', '--verbose', - action='count', - help=helpmsg['verbose'], - default=defaults['verbose'], - ) - parser.add_argument( '-c', '--config', - action='store', - help=helpmsg['config'], - default=defaults['config'], - ) - args = parser.parse_args() - - config_file = args.config - verbose = args.verbose if args.verbose <= 2 else 2 - - main() + raise + + +if __name__ == "__main__": + signal.signal(signal.SIGTERM, sigterm_handler) + + main() -- Samba Shared Repository