Skip to content

Commit

Permalink
Maintenance/lint with recent rules (#363)
Browse files Browse the repository at this point in the history
* Re-enable ansible-lint as dependency and a linter in molecule
* Remove version constraint on molecule as schema validation is fixed in 4.0.3
* ignore possible venvs for linting
* Fix all errors/warning reported by ansible lint
    * jinja spacing
    * schema in meta/main.yaml
    * tasks/playbooks names related warnings / errors
    * file permissions not set in modules
    * name/when/block key order
    * FQCNs for every module
* Disable molecule dependency manager as we have no deps
* Added pre-commit configuration and configured some plugins. Let them do their job
    * Black now reformats python code
    * new line at end automatically added
    * trailing spaces automatically removed
* Use python f-strings wherever possible
  • Loading branch information
zeitounator committed Dec 9, 2022
1 parent df33938 commit f836c34
Show file tree
Hide file tree
Showing 50 changed files with 527 additions and 456 deletions.
3 changes: 1 addition & 2 deletions .ansible-lint
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
---
skip_list:
- role-name
- meta-no-info
- role-name
3 changes: 3 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
[flake8]
max-line-length = 120
extend-exclude =
venv,
.venv
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.idea/
*.iml
.venv
venv
molecule/**/.molecule/
molecule/**/__pycache__/
molecule/**/.cache
Expand Down
24 changes: 24 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files

- repo: https://github.com/ansible-community/ansible-lint
rev: v6.9.1
hooks:
- id: ansible-lint
additional_dependencies:
- keyring
- jmespath

- repo: https://github.com/psf/black
rev: 22.10.0
hooks:
- id: black
3 changes: 3 additions & 0 deletions .yamllint
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
---
extends: default

ignore-from-file:
.gitignore

rules:
indentation:
spaces: 2
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ Allow to change the nexus user default home directory

Nexus directories.
* `nexus_installation_dir` contains the installed executable(s)
* `nexus_data_dir` contains all configuration, repositories and uploaded artifacts. Custom blobstores paths outside
* `nexus_data_dir` contains all configuration, repositories and uploaded artifacts. Custom blobstores paths outside
of `nexus_data_dir` can be configured, see `nexus_blobstores` below.
* `nexus_tmp_dir` contains all temporary files. Default path for redhat has been moved out of `/tmp` to overcome
potential problems with automatic cleaning procedures. See #168.
Expand Down Expand Up @@ -272,7 +272,7 @@ These vars control how the role connects to the nexus API for provisionning.
**For advance usage only. You most probably do not want to change these default settings**

Note: the `nexus_api_timeout` was added in v2.4.19 and overrides the default
[`uri` module timeout](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/uri_module.html) of 30s
[`uri` module timeout](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/uri_module.html) of 30s
for all calls to the API

### Branding capabalities
Expand Down Expand Up @@ -1039,8 +1039,8 @@ Feel free to use them or implement your own install scenario at your convenience


roles:


- { role: geerlingguy.java, vars: See role doc for your distribution/version }
# Debian/Ubuntu only
# - { role: geerlingguy.apache, apache_create_vhosts: no, apache_mods_enabled: ["proxy.load", "proxy_http.load", "headers.load", "ssl.load", "rewrite.load"], apache_remove_default_vhost: true, tags: ["geerlingguy.apache"] }
Expand Down
4 changes: 2 additions & 2 deletions files/groovy/create_cleanup_policies_from_list.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ parsed_args.each { currentPolicy ->
existingPolicy.setNotes(currentPolicy.notes)
existingPolicy.setCriteria(criteriaMap)
cleanupPolicyStorage.update(existingPolicy)

currentResult.put('status', 'updated')
scriptResults['changed'] = true
}
Expand Down Expand Up @@ -122,7 +122,7 @@ def Boolean isPolicyEqual(existingPolicy, currentPolicy) {

isequal &= existingPolicy.getNotes() == currentPolicy.notes
isequal &= existingPolicy.getFormat() == currentPolicy.format

isequal &= (((! existingPolicy.getCriteria().containsKey(LAST_BLOB_UPDATED_KEY)) && (! currentCriteria.containsKey(LAST_BLOB_UPDATED_KEY)))
|| (existingPolicy.getCriteria().containsKey(LAST_BLOB_UPDATED_KEY)
&& currentCriteria.containsKey(LAST_BLOB_UPDATED_KEY)
Expand Down
2 changes: 1 addition & 1 deletion files/groovy/setup_email.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ config.with {
nexusTrustStoreEnabled = parsed_args.email_trust_store_enabled
}

emailMgr.setConfiguration(config)
emailMgr.setConfiguration(config)
2 changes: 1 addition & 1 deletion files/groovy/setup_roles_from_list.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ parsed_args.each { roleDef ->
scriptResults['action_details'].add(currentResult)
}

return JsonOutput.toJson(scriptResults)
return JsonOutput.toJson(scriptResults)
59 changes: 36 additions & 23 deletions filter_plugins/nexus3_oss_filters.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""nexus3-oss custom filters."""

from __future__ import (absolute_import, division, print_function)
from __future__ import absolute_import, division, print_function

__metaclass__ = type

from ansible.errors import AnsibleFilterError
Expand All @@ -13,9 +14,9 @@ class FilterModule(object):
def filters(self):
"""Return the filter list."""
return {
'nexus_groovy_error': self.nexus_groovy_error,
'nexus_groovy_changed': self.nexus_groovy_changed,
'nexus_groovy_details': self.nexus_groovy_details
"nexus_groovy_error": self.nexus_groovy_error,
"nexus_groovy_changed": self.nexus_groovy_changed,
"nexus_groovy_details": self.nexus_groovy_details,
}

def nexus_groovy_error(self, data):
Expand All @@ -25,7 +26,7 @@ def nexus_groovy_error(self, data):
:param data: A registered var after calling the nexus groovy script though uri module
:return: boolean: True if error, False otherwise
"""
return self._nexus_groovy_result(data, 'error')
return self._nexus_groovy_result(data, "error")

def nexus_groovy_changed(self, data):
"""
Expand All @@ -34,7 +35,7 @@ def nexus_groovy_changed(self, data):
:param data: A registered var after calling the nexus groovy script though uri module
:return: boolean: True if changed, False otherwise
"""
return self._nexus_groovy_result(data, 'changed')
return self._nexus_groovy_result(data, "changed")

def nexus_groovy_details(self, data):
"""
Expand All @@ -43,7 +44,7 @@ def nexus_groovy_details(self, data):
:param data: A registered var after calling the nexus groovy script though uri module
:return: A list of maps for each action in the script if available or a string with the best relevant info
"""
return self._nexus_groovy_result(data, 'action_details')
return self._nexus_groovy_result(data, "action_details")

def _nexus_groovy_result(self, data, element):
"""
Expand All @@ -61,28 +62,34 @@ def _nexus_groovy_result(self, data, element):
:param element: The desired element (error, changed, action_details)
:return: True/False or a list of maps with details.
"""
valid_elements = ['error', 'changed', 'action_details']
valid_elements = ["error", "changed", "action_details"]
if element not in valid_elements:
raise AnsibleFilterError("The element parameter must be one of {}".format(",".join(valid_elements)))
raise AnsibleFilterError(
f"The element parameter must be one of {','.join(valid_elements)}"
)

return self._get_script_run_results(data)[element]

def _get_script_run_results(self, data):

try:
request_status = data['status']
request_status = data["status"]
except KeyError:
raise AnsibleFilterError("The input data is not valid. It does not contain the key 'status'. "
"Is it a var registered from a uri: module call ?")
raise AnsibleFilterError(
"The input data is not valid. It does not contain the key 'status'. "
"Is it a var registered from a uri: module call ?"
)

try:
json_data = data['json']
json_data = data["json"]
except KeyError:
raise AnsibleFilterError("The input data is not valid. It does not contain the key 'json'. "
"Is it a var registered from a uri: module call ?")
raise AnsibleFilterError(
"The input data is not valid. It does not contain the key 'json'. "
"Is it a var registered from a uri: module call ?"
)

try:
raw_result = json_data['result']
raw_result = json_data["result"]
if raw_result == "null":
raise KeyError
except KeyError:
Expand All @@ -96,17 +103,23 @@ def _get_script_run_results(self, data):
"""This is not a result in json format or result key is absent"""
if request_status == 200:
result = {
'error': False,
'changed': False,
'action_details': raw_result if raw_result else 'Script return in empty'
"error": False,
"changed": False,
"action_details": raw_result
if raw_result
else "Script return in empty",
}
else:
result = {
'error': True,
'changed': False,
'action_details': raw_result if raw_result else "Global script failure"
"error": True,
"changed": False,
"action_details": raw_result
if raw_result
else "Global script failure",
}
except Exception as e:
raise AnsibleFilterError('Filter encountered an unexpected exception: {} {}'.format(type(e), e))
raise AnsibleFilterError(
f"Filter encountered an unexpected exception: {type(e)} {e}"
)

return result
41 changes: 23 additions & 18 deletions handlers/main.yml
Original file line number Diff line number Diff line change
@@ -1,60 +1,65 @@
---
- name: systemd-reload
systemd:
- name: Reload systemd
ansible.builtin.systemd:
daemon-reload: yes
name: nexus.service
listen: systemd-reload

- name: nexus systemd service restart
systemd:
- name: Nexus systemd service restart
ansible.builtin.systemd:
name: nexus.service
state: restarted
no_block: yes
listen: nexus-service-restart
when: "ansible_service_mgr == 'systemd'"

- name: nexus sysv service restart
service:
- name: Nexus sysv service restart
ansible.builtin.service:
name: nexus
state: restarted
listen: nexus-service-restart
when: "ansible_service_mgr != 'systemd'"

- name: nexus systemd service stop
systemd:
- name: Nexus systemd service stop
ansible.builtin.systemd:
name: nexus.service
state: stopped
listen: nexus-service-stop
when: nexus_systemd_service_file.stat.exists

- name: nexus sysv service stop
service:
- name: Nexus sysv service stop
ansible.builtin.service:
name: nexus
state: stopped
listen: nexus-service-stop
when: nexus_sysv_service_file.stat.exists

- name: wait-for-nexus
wait_for:
- name: Wait for Nexus to start
ansible.builtin.wait_for:
path: "{{ nexus_data_dir }}/log/nexus.log"
search_regex: "Started Sonatype Nexus .*"
timeout: 1800
listen: wait-for-nexus

- name: wait-for-nexus-port
wait_for:
- name: Wait for Nexus port to be available
ansible.builtin.wait_for:
port: "{{ nexus_default_port }}"
timeout: "{{ nexus_wait_for_port_timeout | default(omit) }}"
retries: "{{ nexus_wait_for_port_retries | default(omit) }}"
register: wait_for_result
until: wait_for_result is success
listen: wait-for-nexus-port

- name: httpd-service-reload
systemd:
- name: Reload Apache service
ansible.builtin.systemd:
name: "{{ httpd_package_name }}.service"
state: reloaded
enabled: yes
no_block: yes
listen: httpd-service-reload

- name: wait-for-httpd
wait_for:
- name: Wait for Apache port to be available
ansible.builtin.wait_for:
port: 443
delay: 5
listen: wait-for-httpd
6 changes: 2 additions & 4 deletions meta/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ galaxy_info:

min_ansible_version: 2.12.10

github_branch: main

platforms:
- name: EL
versions:
- 7
- 8
- "7"
- "8"
- name: Ubuntu
versions:
- xenial
Expand Down
3 changes: 2 additions & 1 deletion molecule/default-centos7/converge.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
---
- import_playbook: ../default-converge.yml
- name: Import default converge playbook
import_playbook: ../default-converge.yml
1 change: 1 addition & 0 deletions molecule/default-centos7/molecule.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
dependency:
name: galaxy
enabled: False

driver:
name: docker
Expand Down
3 changes: 2 additions & 1 deletion molecule/default-centos8/converge.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
---
- import_playbook: ../default-converge.yml
- name: Import default converge playbook
import_playbook: ../default-converge.yml
1 change: 1 addition & 0 deletions molecule/default-centos8/molecule.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
dependency:
name: galaxy
enabled: False

driver:
name: docker
Expand Down
3 changes: 2 additions & 1 deletion molecule/default-debian_bullseye/converge.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
---
- import_playbook: ../default-converge.yml
- name: Import default converge playbook
import_playbook: ../default-converge.yml
1 change: 1 addition & 0 deletions molecule/default-debian_bullseye/molecule.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
dependency:
name: galaxy
enabled: False

driver:
name: docker
Expand Down
Loading

0 comments on commit f836c34

Please sign in to comment.