From ea9e69d75f6ccd8748e894fa7fe303f25cd0d869 Mon Sep 17 00:00:00 2001 From: Dominik Kaminski Date: Mon, 30 Aug 2021 19:03:37 +0200 Subject: [PATCH] feat: align modules to linter --- .ansible-lint | 56 +++++++++++++ .../univention/ucs_modules | 1 + .flake8 | 2 + .gitignore | 1 + .yamllint | 4 +- README.md | 4 +- galaxy.yml | 44 +++++----- plugins/modules/univention_config_registry.py | 80 ++++++++++--------- 8 files changed, 131 insertions(+), 61 deletions(-) create mode 100644 .ansible-lint create mode 120000 .cache/collections/ansible_collections/univention/ucs_modules create mode 100644 .flake8 create mode 100644 .gitignore diff --git a/.ansible-lint b/.ansible-lint new file mode 100644 index 0000000..8d33da8 --- /dev/null +++ b/.ansible-lint @@ -0,0 +1,56 @@ +--- +# .ansible-lint +exclude_paths: [] +# parseable: true +# quiet: true +# verbosity: 1 + +# Mock modules or roles in order to pass ansible-playbook --syntax-check +mock_modules: [] +mock_roles: [] + +# Enable checking of loop variable prefixes in roles +loop_var_prefix: "{role}_" + +# Enforce variable names to follow pattern below, in addition to Ansible own +# requirements, like avoiding python identifiers. To disable add `var-naming` +# to skip_list. +# var_naming_pattern: "^[a-z_][a-z0-9_]*$" + +use_default_rules: true +# Load custom rules from this specific folder +# rulesdir: +# - ./rule/directory/ + +# This makes linter to fully ignore rules/tags listed below +skip_list: [] + +# Any rule that has the 'opt-in' tag will not be loaded unless its 'id' is +# mentioned in the enable_list: +enable_list: + - "fqcn-builtins" # opt-in + - "no-log-password" # opt-in + - "no-same-owner" # opt-in + # add yaml here if you want to avoid ignoring yaml checks when yamllint + # library is missing. Normally its absence just skips using that rule. + # - yaml +# Report only a subset of tags and fully ignore any others +# tags: +# - var-spacing + +# This makes the linter display but not fail for rules/tags listed below: +warn_list: [] + +# Offline mode disables installation of requirements.yml +offline: false + +# Define required Ansible's variables to satisfy syntax check +extra_vars: {} + +# Uncomment to enforce action validation with tasks, usually is not +# needed as Ansible syntax check also covers it. +# skip_action_validation: false + +# List of additional kind:pattern to be added at the top of the default +# match list, first match determines the file kind. +kinds: [] diff --git a/.cache/collections/ansible_collections/univention/ucs_modules b/.cache/collections/ansible_collections/univention/ucs_modules new file mode 120000 index 0000000..c866b86 --- /dev/null +++ b/.cache/collections/ansible_collections/univention/ucs_modules @@ -0,0 +1 @@ +../../../.. \ No newline at end of file diff --git a/.flake8 b/.flake8 new file mode 100644 index 0000000..6deafc2 --- /dev/null +++ b/.flake8 @@ -0,0 +1,2 @@ +[flake8] +max-line-length = 120 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..16d3c4d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.cache diff --git a/.yamllint b/.yamllint index 68f1160..32cb6f5 100644 --- a/.yamllint +++ b/.yamllint @@ -16,6 +16,7 @@ rules: level: "error" line-length: + max: 120 level: "warning" new-line-at-end-of-file: @@ -23,8 +24,7 @@ rules: quoted-strings: quote-type: "double" - required: only-when-needed - extra-required: ["^(?:(?!(var_)).)*$"] + required: true truthy: level: "error" diff --git a/README.md b/README.md index 4dc277b..ff3675c 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,3 @@ -# ansible-modules +# ucs_modules Ansible Modules for UCS - -The modules are located in library. An example playbook using the modules is test_play.yml. It can also be used to test the functionality of the modules. diff --git a/galaxy.yml b/galaxy.yml index e6b1224..6c5289c 100644 --- a/galaxy.yml +++ b/galaxy.yml @@ -1,44 +1,45 @@ +--- ### REQUIRED # The namespace of the collection. This can be a company/brand/organization or product namespace under which all # content lives. May only contain alphanumeric lowercase characters and underscores. Namespaces cannot start with # underscores or numbers and cannot contain consecutive underscores -namespace: univention +namespace: "univention" # The name of the collection. Has the same character restrictions as 'namespace' -name: ucs_modules +name: "ucs_modules" # The version of the collection. Must be compatible with semantic versioning -version: 0.0.0 +version: "0.0.0" # The path to the Markdown (.md) readme file. This path is relative to the root of the collection -readme: README.md +readme: "README.md" # A list of the collection's content authors. Can be just the name or in the format 'Full Name (url) # @nicks:irc/im.site#channel' authors: - - Univention GmbH + - "Univention GmbH" ### OPTIONAL but strongly recommended # A short summary description of the collection -description: Ansible modules for Univention UCS +description: "Ansible modules for Univention UCS" # Either a single license or a list of licenses for content inside of a collection. Ansible Galaxy currently only # accepts L(SPDX,https://spdx.org/licenses/) licenses. This key is mutually exclusive with 'license_file' license: -- GPL-3.0-or-later + - "GPL-3.0-or-later" # The path to the license file for the collection. This path is relative to the root of the collection. This key is # mutually exclusive with 'license' -license_file: '' +license_file: "LICENSE" # A list of tags you want to associate with the collection for indexing/searching. A tag name has the same character # requirements as 'namespace' and 'name' tags: - - univention - - ucs + - "univention" + - "ucs" # Collections that this collection requires to be installed for it to be usable. The key of the dict is the # collection label 'namespace.name'. The value is a version range @@ -47,21 +48,26 @@ tags: dependencies: {} # The URL of the originating SCM repository -repository: https://github.com/univention/ansible-modules +repository: "https://github.com/univention/ansible-modules" # The URL to any online docs -#documentation: http://docs.example.com +documentation: "https://github.com/univention/ansible-modules" # The URL to the homepage of the collection/project -homepage: https://www.univention.com +homepage: "https://www.univention.com" # The URL to the collection issue tracker -issues: https://forge.univention.org +issues: "https://forge.univention.org" # Ignore irrelevant files build_ignore: - - .gitlab-ci.yml - - .gitkeep - - .gitignore - - '*.tar.gz' - - ci + - ".ansible-lint" + - ".cache/" + - ".flake8" + - ".gitkeep" + - ".gitignore" + - ".gitlab-ci.yml" + - ".pre-commit-config.yaml" + - ".releaserc" + - ".yamllint" + - "*.tar.gz" diff --git a/plugins/modules/univention_config_registry.py b/plugins/modules/univention_config_registry.py index fc2363c..3f3d45f 100755 --- a/plugins/modules/univention_config_registry.py +++ b/plugins/modules/univention_config_registry.py @@ -1,4 +1,7 @@ #!/usr/bin/python +import datetime +from ansible.module_utils.basic import AnsibleModule + DOCUMENTATION = r''' --- module: univention_config_registry @@ -87,8 +90,6 @@ message: description: A human-readable information about which keys where changed ''' -import datetime -from ansible.module_utils.basic import AnsibleModule try: from univention.config_registry.backend import ConfigRegistry @@ -97,6 +98,7 @@ try: except ImportError: have_config_registry = False + def _commit_files(files, result, module): result['changed'] = len(files) > 0 @@ -122,13 +124,13 @@ def _commit_files(files, result, module): ucr_handlers.commit(ucr, files) - endd = datetime.datetime.now() - result['start'] = str(startd) - result['end'] = str(endd) - result['delta'] = str(endd - startd) + endd = datetime.datetime.now() + result['start'] = str(startd) + result['end'] = str(endd) + result['delta'] = str(endd - startd) result['meta']['commited_templates'] = files - result['message'] = "These files were be commited: {}".format(" ".join(files)) - result['failed'] = 0 + result['message'] = "These files were be commited: {}".format(" ".join(files)) + result['failed'] = 0 # FIXME: Currently the function cannot fail # if error != 0: @@ -140,7 +142,7 @@ def _set_keys(keys, result, module): ucr.load() def needs_change(key): - if not key in ucr: + if key not in ucr: return True if isinstance(keys[key], bool): if keys[key] and not ucr.is_true(key): @@ -165,30 +167,31 @@ def _set_keys(keys, result, module): if not result['changed']: return - args = ["/usr/sbin/univention-config-registry", "set"] + [ "{0}={1}".format(key, keys[key]) for key in to_set ] - startd = datetime.datetime.now() + args = ["/usr/sbin/univention-config-registry", "set"] + ["{0}={1}".format(key, keys[key]) for key in to_set] + startd = datetime.datetime.now() - rc, out, err = module.run_command(args) + rc, out, err = module.run_command(args) - endd = datetime.datetime.now() - result['start'] = str(startd) - result['end'] = str(endd) - result['delta'] = str(endd - startd) - result['out'] = out.rstrip("\r\n") - result['err'] = err.rstrip("\r\n") - result['rc'] = rc - result['message'] = "These keys were set: {}".format(" ".join(to_set)) + endd = datetime.datetime.now() + result['start'] = str(startd) + result['end'] = str(endd) + result['delta'] = str(endd - startd) + result['out'] = out.rstrip("\r\n") + result['err'] = err.rstrip("\r\n") + result['rc'] = rc + result['message'] = "These keys were set: {}".format(" ".join(to_set)) result['meta']['changed_keys'] = to_set - result['failed'] = rc != 0 or len(err) > 0 + result['failed'] = rc != 0 or len(err) > 0 if rc != 0: module.fail_json(msg='non-zero return code', **result) + def _unset_keys(keys, result, module): ucr = ConfigRegistry() ucr.load() - to_unset = [ key for key in keys if key in ucr ] + to_unset = [key for key in keys if key in ucr] result['changed'] = len(to_unset) > 0 if not result['changed']: @@ -202,25 +205,26 @@ def _unset_keys(keys, result, module): if not result['changed']: return - args = ["/usr/sbin/univention-config-registry", "unset"] + to_unset - startd = datetime.datetime.now() + args = ["/usr/sbin/univention-config-registry", "unset"] + to_unset + startd = datetime.datetime.now() - rc, out, err = module.run_command(args) + rc, out, err = module.run_command(args) - endd = datetime.datetime.now() - result['start'] = str(startd) - result['end'] = str(endd) - result['delta'] = str(endd - startd) - result['out'] = out.rstrip("\r\n") - result['err'] = err.rstrip("\r\n") - result['rc'] = rc - result['message'] = "These keys were unset: {}".format(" ".join(to_unset)) + endd = datetime.datetime.now() + result['start'] = str(startd) + result['end'] = str(endd) + result['delta'] = str(endd - startd) + result['out'] = out.rstrip("\r\n") + result['err'] = err.rstrip("\r\n") + result['rc'] = rc + result['message'] = "These keys were unset: {}".format(" ".join(to_unset)) result['meta']['changed_keys'] = to_unset - result['failed'] = rc != 0 + result['failed'] = rc != 0 if rc != 0: module.fail_json(msg='non-zero return code', **result) + def run_module(): # define available arguments/parameters a user can pass to the module module_args = dict( @@ -244,11 +248,13 @@ def run_module(): if not have_config_registry: module.fail_json(msg='The Python "univention.config_registry.backend" is not available', **result) - if not (('keys' in module.params and module.params['keys']) or ('kvlist' in module.params and module.params['kvlist']) or ('commit' in module.params and module.params['commit'])): + if not (('keys' in module.params and module.params['keys']) + or ('kvlist' in module.params and module.params['kvlist']) + or ('commit' in module.params and module.params['commit'])): module.fail_json(msg='Either "keys", "kvlist" or "commit" is required.', **result) - state = module.params['state'] - keys = module.params['keys'] if 'keys' in module.params and module.params['keys'] else dict() + state = module.params['state'] + keys = module.params['keys'] if 'keys' in module.params and module.params['keys'] else dict() commit = module.params['commit'] if 'commit' in module.params and module.params['commit'] else list() if 'kvlist' in module.params and module.params['kvlist']: