diff --git a/README.md b/README.md index d96d0ab0..a416d75c 100644 --- a/README.md +++ b/README.md @@ -1076,6 +1076,9 @@ To speed up tests, molecule uses docker hub images with automated build. #### Molecule selinux scenario +*** Warning: This scenario as been removed for the moment for molecule 3.0 compatibility reason. We it until +we can decide if it can be re-introduced or not *** + We included a second molecule `selinux` scenario. This one is not run on travis but can be used locally to: * test selinux integration (on centos). * run test and access the running vms under VirtualBox on you local machine. diff --git a/files/groovy/create_content_selector.groovy b/files/groovy/create_content_selector.groovy deleted file mode 100644 index bcb09351..00000000 --- a/files/groovy/create_content_selector.groovy +++ /dev/null @@ -1,34 +0,0 @@ -import groovy.json.JsonSlurper -import org.sonatype.nexus.selector.SelectorConfiguration -import org.sonatype.nexus.selector.SelectorManager - -parsed_args = new JsonSlurper().parseText(args) - -SelectorManager selectorManager = container.lookup(SelectorManager.class.name) - -boolean update = true - -SelectorConfiguration selectorConfig = selectorManager.browse().find { it -> it.name == parsed_args.name } - -if (selectorConfig == null) { - update = false - try { - selectorConfig = selectorManager.newSelectorConfiguration() - selectorConfig.setName(parsed_args.name) - } catch (MissingMethodException) { - // Compatibility with nexus versions older than 3.20 - selectorConfig = new SelectorConfiguration('name': parsed_args.name) - } -} - -selectorConfig.setDescription(parsed_args.description) -selectorConfig.setType('csel') -selectorConfig.setAttributes([ - 'expression': parsed_args.search_expression -] as Map<String, Object>) - -if (update) { - selectorManager.update(selectorConfig) -} else { - selectorManager.create(selectorConfig) -} diff --git a/files/groovy/create_content_selectors_from_list.groovy b/files/groovy/create_content_selectors_from_list.groovy new file mode 100644 index 00000000..26ee83e7 --- /dev/null +++ b/files/groovy/create_content_selectors_from_list.groovy @@ -0,0 +1,63 @@ +import groovy.json.JsonOutput +import groovy.json.JsonSlurper +import org.sonatype.nexus.selector.SelectorConfiguration +import org.sonatype.nexus.selector.SelectorManager + +parsed_args = new JsonSlurper().parseText(args) + +List<Map<String, String>> actionDetails = [] +Map scriptResults = [changed: false, error: false] +scriptResults.put('action_details', actionDetails) + +SelectorManager selectorManager = container.lookup(SelectorManager.class.name) + +parsed_args.each { currentSelector -> + + Map<String, String> currentResult = [name: currentSelector.name] + + try { + boolean update = true + + SelectorConfiguration selectorConfig = selectorManager.browse().find { it -> it.name == currentSelector.name } + + if (selectorConfig == null) { + update = false + try { + selectorConfig = selectorManager.newSelectorConfiguration() + } catch (MissingMethodException) { + selectorConfig = SelectorConfiguration.newInstance() + } + selectorConfig.setName(currentSelector.name) + } else { + existingConfigDump = selectorConfig.dump() + } + + selectorConfig.setDescription(currentSelector.description) + selectorConfig.setType('csel') + selectorConfig.setAttributes([ + 'expression': currentSelector.search_expression + ] as Map<String, Object>) + + if (update) { + if (existingConfigDump != selectorConfig.dump()) { + selectorManager.update(selectorConfig) + currentResult.put('status', 'updated') + scriptResults['changed'] = true + } else { + currentResult.put('status', 'no change') + } + } else { + selectorManager.create(selectorConfig) + currentResult.put('status', 'created') + scriptResults['changed'] = true + } + } catch (Exception e) { + currentResult.put('status', 'error') + currentResult.put('error_msg', e.toString()) + scriptResults['error'] = true + } + + scriptResults['action_details'].add(currentResult) +} + +return JsonOutput.toJson(scriptResults) diff --git a/files/groovy/create_repos_from_list.groovy b/files/groovy/create_repos_from_list.groovy index 7c9950cd..1a9c9533 100644 --- a/files/groovy/create_repos_from_list.groovy +++ b/files/groovy/create_repos_from_list.groovy @@ -10,6 +10,30 @@ scriptResults.put('action_details', actionDetails) repositoryManager = repository.repositoryManager +private Configuration newConfiguration(Map map) { + Configuration config + try { + config = repositoryManager.newConfiguration() + } catch (MissingMethodException) { + // Compatibility with nexus versions older than 3.21 + config = Configuration.newInstance() + } + config.with { + repositoryName = map.repositoryName + recipeName = map.recipeName + online = map.online + attributes = map.attributes as Map + } + return config +} + +private boolean configurationChanged(Configuration oldConfig, Configuration newConfig) { + if (oldConfig.attributes.httpclient) + if (oldConfig.attributes.httpclient.authentication == [:]) + oldConfig.attributes.httpclient.authentication = null + return oldConfig.properties == newConfig.properties +} + parsed_args.each { currentRepo -> Map<String, String> currentResult = [name: currentRepo.name, format: currentRepo.format, type: currentRepo.type] @@ -22,7 +46,7 @@ parsed_args.each { currentRepo -> if (existingRepository == null) { log.info('Creating configuration for new repo {} (Format: {}, Type: {})', currentRepo.name, currentRepo.format, currentRepo.type) // Default and/or immutable values - configuration = new Configuration( + configuration = newConfiguration( repositoryName: currentRepo.name, recipeName: recipeName, online: true, @@ -151,7 +175,7 @@ parsed_args.each { currentRepo -> scriptResults['changed'] = true log.info('Configuration for repo {} created', currentRepo.name) } else { - if (!(configuration.properties == existingRepository.configuration.properties)) { + if (!configurationChanged(existingRepository.configuration, configuration)) { repositoryManager.update(configuration) currentResult.put('status', 'updated') log.info('Configuration for repo {} saved', currentRepo.name) diff --git a/filter_plugins/nexus3_oss_filters.py b/filter_plugins/nexus3_oss_filters.py index fd75ca24..fb8cc556 100644 --- a/filter_plugins/nexus3_oss_filters.py +++ b/filter_plugins/nexus3_oss_filters.py @@ -1,3 +1,5 @@ +"""nexus3-oss custom filters.""" + from __future__ import (absolute_import, division, print_function) __metaclass__ = type @@ -6,10 +8,10 @@ class FilterModule(object): - """ - nexus3-oss role filters - """ + """nexus3-oss role filters.""" + def filters(self): + """Return the filter list.""" return { 'nexus_groovy_error': self.nexus_groovy_error, 'nexus_groovy_changed': self.nexus_groovy_changed, @@ -18,7 +20,7 @@ def filters(self): def nexus_groovy_error(self, data): """ - Check if the passed uri module call data has returned an error + Check if the passed uri module call data has returned an error. :param data: A registered var after calling the nexus groovy script though uri module :return: boolean: True if error, False otherwise @@ -27,7 +29,7 @@ def nexus_groovy_error(self, data): def nexus_groovy_changed(self, data): """ - Check if the passed uri module call data has returned a changed state + Check if the passed uri module call data has returned a changed state. :param data: A registered var after calling the nexus groovy script though uri module :return: boolean: True if changed, False otherwise @@ -36,8 +38,7 @@ def nexus_groovy_changed(self, data): def nexus_groovy_details(self, data): """ - Returns the action_details part of the groovy call result if available or - some as relevant as possible info + Return the action_details part of the groovy call result if available or some as relevant as possible info. :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 @@ -46,11 +47,10 @@ def nexus_groovy_details(self, data): def _nexus_groovy_result(self, data, element): """ - Inspect data from an uri module call to a custom groovy script in nexus - and return the required element. This is based on a specific json - we return in result for groovy script in this role. If the result does - not contain the expected params or is not in json format, changed will always - be False. + Inspect data from a uri module call to a custom groovy script in nexus and return the required element. + + This is based on a specific json we return in result for groovy script in this role. If the result does + not contain the expected params or is not in json format, changed will always be False. The element can be: - error: True if the call did not return a 200 status or error is True in result @@ -61,7 +61,6 @@ 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'] if element not in valid_elements: raise AnsibleFilterError("The element parameter must be one of {}".format(",".join(valid_elements))) diff --git a/molecule/default-centos7/converge.yml b/molecule/default-centos7/converge.yml new file mode 100644 index 00000000..fb285ca4 --- /dev/null +++ b/molecule/default-centos7/converge.yml @@ -0,0 +1,2 @@ +--- +- import_playbook: ../default-converge.yml diff --git a/molecule/default-centos7/molecule.yml b/molecule/default-centos7/molecule.yml index fcc5f134..ca571744 100644 --- a/molecule/default-centos7/molecule.yml +++ b/molecule/default-centos7/molecule.yml @@ -7,8 +7,10 @@ driver: safe_files: - nexus-downloads -lint: - name: yamllint +lint: | + yamllint . + ansible-lint + flake8 platforms: @@ -40,8 +42,6 @@ platforms: provisioner: name: ansible - lint: - name: ansible-lint scenario: check_sequence: @@ -67,5 +67,3 @@ scenario: verifier: name: testinfra - lint: - name: flake8 diff --git a/molecule/default-centos7/playbook.yml b/molecule/default-centos7/playbook.yml deleted file mode 100644 index 39958e7a..00000000 --- a/molecule/default-centos7/playbook.yml +++ /dev/null @@ -1,2 +0,0 @@ ---- -- import_playbook: ../default-playbook.yml diff --git a/molecule/default-playbook.yml b/molecule/default-converge.yml similarity index 100% rename from molecule/default-playbook.yml rename to molecule/default-converge.yml diff --git a/molecule/default-debian_buster/converge.yml b/molecule/default-debian_buster/converge.yml new file mode 100644 index 00000000..fb285ca4 --- /dev/null +++ b/molecule/default-debian_buster/converge.yml @@ -0,0 +1,2 @@ +--- +- import_playbook: ../default-converge.yml diff --git a/molecule/default-debian_buster/molecule.yml b/molecule/default-debian_buster/molecule.yml index 1aa8295e..6942a545 100644 --- a/molecule/default-debian_buster/molecule.yml +++ b/molecule/default-debian_buster/molecule.yml @@ -7,8 +7,10 @@ driver: safe_files: - nexus-downloads -lint: - name: yamllint +lint: | + yamllint . + ansible-lint + flake8 platforms: @@ -40,8 +42,6 @@ platforms: provisioner: name: ansible - lint: - name: ansible-lint scenario: check_sequence: @@ -67,5 +67,3 @@ scenario: verifier: name: testinfra - lint: - name: flake8 diff --git a/molecule/default-debian_buster/playbook.yml b/molecule/default-debian_buster/playbook.yml deleted file mode 100644 index 39958e7a..00000000 --- a/molecule/default-debian_buster/playbook.yml +++ /dev/null @@ -1,2 +0,0 @@ ---- -- import_playbook: ../default-playbook.yml diff --git a/molecule/default-debian_stretch/converge.yml b/molecule/default-debian_stretch/converge.yml new file mode 100644 index 00000000..fb285ca4 --- /dev/null +++ b/molecule/default-debian_stretch/converge.yml @@ -0,0 +1,2 @@ +--- +- import_playbook: ../default-converge.yml diff --git a/molecule/default-debian_stretch/molecule.yml b/molecule/default-debian_stretch/molecule.yml index 6d5e3108..44d86930 100644 --- a/molecule/default-debian_stretch/molecule.yml +++ b/molecule/default-debian_stretch/molecule.yml @@ -7,8 +7,10 @@ driver: safe_files: - nexus-downloads -lint: - name: yamllint +lint: | + yamllint . + ansible-lint + flake8 platforms: @@ -40,8 +42,6 @@ platforms: provisioner: name: ansible - lint: - name: ansible-lint scenario: check_sequence: @@ -67,5 +67,3 @@ scenario: verifier: name: testinfra - lint: - name: flake8 diff --git a/molecule/default-debian_stretch/playbook.yml b/molecule/default-debian_stretch/playbook.yml deleted file mode 100644 index 39958e7a..00000000 --- a/molecule/default-debian_stretch/playbook.yml +++ /dev/null @@ -1,2 +0,0 @@ ---- -- import_playbook: ../default-playbook.yml diff --git a/molecule/default-ubuntu_16.04/converge.yml b/molecule/default-ubuntu_16.04/converge.yml new file mode 100644 index 00000000..fb285ca4 --- /dev/null +++ b/molecule/default-ubuntu_16.04/converge.yml @@ -0,0 +1,2 @@ +--- +- import_playbook: ../default-converge.yml diff --git a/molecule/default-ubuntu_16.04/molecule.yml b/molecule/default-ubuntu_16.04/molecule.yml index 9adbeeff..37039369 100644 --- a/molecule/default-ubuntu_16.04/molecule.yml +++ b/molecule/default-ubuntu_16.04/molecule.yml @@ -7,8 +7,10 @@ driver: safe_files: - nexus-downloads -lint: - name: yamllint +lint: | + yamllint . + ansible-lint + flake8 platforms: @@ -40,8 +42,6 @@ platforms: provisioner: name: ansible - lint: - name: ansible-lint scenario: check_sequence: @@ -67,5 +67,3 @@ scenario: verifier: name: testinfra - lint: - name: flake8 diff --git a/molecule/default-ubuntu_16.04/playbook.yml b/molecule/default-ubuntu_16.04/playbook.yml deleted file mode 100644 index 39958e7a..00000000 --- a/molecule/default-ubuntu_16.04/playbook.yml +++ /dev/null @@ -1,2 +0,0 @@ ---- -- import_playbook: ../default-playbook.yml diff --git a/molecule/default-ubuntu_18.04/converge.yml b/molecule/default-ubuntu_18.04/converge.yml new file mode 100644 index 00000000..fb285ca4 --- /dev/null +++ b/molecule/default-ubuntu_18.04/converge.yml @@ -0,0 +1,2 @@ +--- +- import_playbook: ../default-converge.yml diff --git a/molecule/default-ubuntu_18.04/molecule.yml b/molecule/default-ubuntu_18.04/molecule.yml index ec907d44..8e152ddf 100644 --- a/molecule/default-ubuntu_18.04/molecule.yml +++ b/molecule/default-ubuntu_18.04/molecule.yml @@ -7,8 +7,10 @@ driver: safe_files: - nexus-downloads -lint: - name: yamllint +lint: | + yamllint . + ansible-lint + flake8 platforms: @@ -40,8 +42,6 @@ platforms: provisioner: name: ansible - lint: - name: ansible-lint scenario: check_sequence: @@ -67,5 +67,3 @@ scenario: verifier: name: testinfra - lint: - name: flake8 diff --git a/molecule/default-ubuntu_18.04/playbook.yml b/molecule/default-ubuntu_18.04/playbook.yml deleted file mode 100644 index 39958e7a..00000000 --- a/molecule/default-ubuntu_18.04/playbook.yml +++ /dev/null @@ -1,2 +0,0 @@ ---- -- import_playbook: ../default-playbook.yml diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml new file mode 100644 index 00000000..fb285ca4 --- /dev/null +++ b/molecule/default/converge.yml @@ -0,0 +1,2 @@ +--- +- import_playbook: ../default-converge.yml diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml index bdbfb2e6..072ba6a1 100644 --- a/molecule/default/molecule.yml +++ b/molecule/default/molecule.yml @@ -7,8 +7,10 @@ driver: safe_files: - nexus-downloads -lint: - name: yamllint +lint: | + yamllint . + ansible-lint + flake8 platforms: @@ -38,8 +40,6 @@ platforms: provisioner: name: ansible - lint: - name: ansible-lint scenario: check_sequence: @@ -58,5 +58,3 @@ scenario: verifier: name: testinfra - lint: - name: flake8 diff --git a/molecule/default/playbook.yml b/molecule/default/playbook.yml deleted file mode 100644 index 39958e7a..00000000 --- a/molecule/default/playbook.yml +++ /dev/null @@ -1,2 +0,0 @@ ---- -- import_playbook: ../default-playbook.yml diff --git a/molecule/selinux/INSTALL.rst b/molecule/selinux/INSTALL.rst deleted file mode 100644 index 44c26afd..00000000 --- a/molecule/selinux/INSTALL.rst +++ /dev/null @@ -1,17 +0,0 @@ -******* -Install -******* - -Requirements -============ - -* Vagrant -* Virtualbox, Parallels, VMware Fusion, VMware Workstation or VMware Desktop -* python-vagrant - -Install -======= - -.. code-block:: bash - - $ sudo pip install python-vagrant diff --git a/molecule/selinux/molecule.yml b/molecule/selinux/molecule.yml deleted file mode 100644 index 857b3f27..00000000 --- a/molecule/selinux/molecule.yml +++ /dev/null @@ -1,43 +0,0 @@ ---- -dependency: - name: galaxy - -driver: - name: vagrant - safe_files: - - nexus-downloads - provider: - name: virtualbox - -lint: - name: yamllint - -platforms: - - name: vb-centos7 - box: thoteam/vb-centos7 - box_url: http://vagrant.thoteam.com/vb-centos7/vb-centos7.json - cpu: 4 - memory: 4096 - instance_raw_config_args: - - "vm.network 'forwarded_port', guest: 443, host: 9101" - groups: - - nexus - - name: vb-debian-stretch - box: thoteam/vb-stretch - box_url: http://vagrant.thoteam.com/vb-stretch/vb-stretch.json - cpu: 4 - memory: 4096 - instance_raw_config_args: - - "vm.network 'forwarded_port', guest: 443, host: 9102" - groups: - - nexus - -provisioner: - name: ansible - lint: - name: ansible-lint - -verifier: - name: testinfra - lint: - name: flake8 diff --git a/molecule/selinux/playbook.yml b/molecule/selinux/playbook.yml deleted file mode 100644 index a5227be4..00000000 --- a/molecule/selinux/playbook.yml +++ /dev/null @@ -1,14 +0,0 @@ ---- -- name: Converge - hosts: nexus - become: yes - roles: - - role: nexus3-oss - vars_files: - - ../ssl-{{ ansible_os_family }}.yml - - ../nexus_common_test_vars.yml - - environment: - http_proxy: "{{ lookup('env', 'http_proxy') }}" - https_proxy: "{{ lookup('env', 'https_proxy') }}" - no_proxy: "{{ lookup('env', 'no_proxy') }}" diff --git a/molecule/selinux/prepare.yml b/molecule/selinux/prepare.yml deleted file mode 100644 index 88a27a9a..00000000 --- a/molecule/selinux/prepare.yml +++ /dev/null @@ -1,36 +0,0 @@ ---- -- name: Prepare - hosts: nexus - become: true - tasks: - - name: Install needed dependencies for role testing (redhat) - yum: - name: - - java-1.8.0-openjdk-headless - - httpd - - mod_ssl - - rsync - state: present - when: ansible_os_family == 'RedHat' - - - block: - - name: Install needed dependencies for role testing (debian/ubuntu) - apt: - name: - - apache2 - - openjdk-8-jre-headless - - rsync - state: present - update_cache: true - - - name: Enable needed apache modules - shell: a2enmod ssl rewrite proxy proxy_http headers - - when: ansible_os_family == 'Debian' - - environment: - http_proxy: "{{ lookup('env', 'http_proxy') }}" - https_proxy: "{{ lookup('env', 'https_proxy') }}" - no_proxy: "{{ lookup('env', 'no_proxy') }}" - -- import_playbook: "../sync-nexus-package.yml" diff --git a/molecule/selinux/tests/test_default.py b/molecule/selinux/tests/test_default.py deleted file mode 100644 index 36b610ad..00000000 --- a/molecule/selinux/tests/test_default.py +++ /dev/null @@ -1,14 +0,0 @@ -import os - -import testinfra.utils.ansible_runner - -testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( - os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('nexus') - - -def test_hosts_file(host): - f = host.file('/etc/hosts') - - assert f.exists - assert f.user == 'root' - assert f.group == 'root' diff --git a/pom.xml b/pom.xml index ccc4fa89..bd2c0865 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ </description> <properties> - <nx-version>3.20.1-01</nx-version> + <nx-version>3.21.0-05</nx-version> </properties> <build> diff --git a/requirements.txt b/requirements.txt index ea15698f..c60694bc 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,18 +13,12 @@ ## and produce unwanted errors. ################################## -# Latest ansible -ansible -# Latest molecule molecule -# git+https://github.com/ansible/molecule.git@96036591b74b5b2475151998df2a145f119d421c -# latest flake8 (missing dep in latest molecule) +ansible +ansible-lint flake8 -# Yamllint is not in molecule deps anymore +flake8-docstrings yamllint -# Latest docker +testinfra docker -# We use json_query in tasks which requires jmespath jmespath -# Python vagrant only needed for local test for selinux -# python-vagrant diff --git a/tasks/create_content_selector_each.yml b/tasks/create_content_selector_each.yml deleted file mode 100644 index 8300adcb..00000000 --- a/tasks/create_content_selector_each.yml +++ /dev/null @@ -1,5 +0,0 @@ ---- -- include: call_script.yml - vars: - script_name: create_content_selector - args: "{{ item }}" diff --git a/tasks/main.yml b/tasks/main.yml index 27a007a3..e4179a13 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -83,8 +83,12 @@ - include_tasks: setup_ldap_each.yml with_items: "{{ ldap_connections }}" - - include_tasks: create_content_selector_each.yml - with_items: "{{ nexus_content_selectors }}" + - name: Create/check content selectors + include_tasks: call_script.yml + vars: + script_name: create_content_selectors_from_list + args: "{{ nexus_content_selectors }}" + when: nexus_content_selectors | length > 0 - name: apply defaults to privileges # @todo: fix with easier syntax once the flip filter is released diff --git a/tests/test_groovySyntax.sh b/tests/test_groovySyntax.sh index 33dda82f..5333d1db 100755 --- a/tests/test_groovySyntax.sh +++ b/tests/test_groovySyntax.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash -thisScriptPath=`dirname $(readlink -f $0)` -rolePath=`dirname $thisScriptPath` +thisScriptPath="$( dirname "${BASH_SOURCE[0]}" )" +rolePath="$thisScriptPath/.." groovyScriptsPath=${rolePath}/files/groovy groovyChecker=$thisScriptPath/syntaxChecking.groovy exitStatus=0