Skip to content

Commit

Permalink
Compat work for nexus 3.21 and molecule 3.0 (#247)
Browse files Browse the repository at this point in the history
* CI now uses molecule 3.0
* Cross-platform test_groovySyntax.sh (fix for MacOS)
* Fix repo creation for Nexus 3.21+
  (integration of #246 + idempotency)
* Backward compatibility for content selectors
* Content selectors creation script is now idempotent

Co-authored-by: Oleksii <[email protected]>
  • Loading branch information
zeitounator and noroutine authored Feb 23, 2020
1 parent 56f2d1d commit e959cc3
Show file tree
Hide file tree
Showing 34 changed files with 153 additions and 241 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
34 changes: 0 additions & 34 deletions files/groovy/create_content_selector.groovy

This file was deleted.

63 changes: 63 additions & 0 deletions files/groovy/create_content_selectors_from_list.groovy
Original file line number Diff line number Diff line change
@@ -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)
28 changes: 26 additions & 2 deletions files/groovy/create_repos_from_list.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
25 changes: 12 additions & 13 deletions filter_plugins/nexus3_oss_filters.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""nexus3-oss custom filters."""

from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)))
Expand Down
2 changes: 2 additions & 0 deletions molecule/default-centos7/converge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
- import_playbook: ../default-converge.yml
10 changes: 4 additions & 6 deletions molecule/default-centos7/molecule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ driver:
safe_files:
- nexus-downloads

lint:
name: yamllint
lint: |
yamllint .
ansible-lint
flake8
platforms:

Expand Down Expand Up @@ -40,8 +42,6 @@ platforms:

provisioner:
name: ansible
lint:
name: ansible-lint

scenario:
check_sequence:
Expand All @@ -67,5 +67,3 @@ scenario:

verifier:
name: testinfra
lint:
name: flake8
2 changes: 0 additions & 2 deletions molecule/default-centos7/playbook.yml

This file was deleted.

File renamed without changes.
2 changes: 2 additions & 0 deletions molecule/default-debian_buster/converge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
- import_playbook: ../default-converge.yml
10 changes: 4 additions & 6 deletions molecule/default-debian_buster/molecule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ driver:
safe_files:
- nexus-downloads

lint:
name: yamllint
lint: |
yamllint .
ansible-lint
flake8
platforms:

Expand Down Expand Up @@ -40,8 +42,6 @@ platforms:

provisioner:
name: ansible
lint:
name: ansible-lint

scenario:
check_sequence:
Expand All @@ -67,5 +67,3 @@ scenario:

verifier:
name: testinfra
lint:
name: flake8
2 changes: 0 additions & 2 deletions molecule/default-debian_buster/playbook.yml

This file was deleted.

2 changes: 2 additions & 0 deletions molecule/default-debian_stretch/converge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
- import_playbook: ../default-converge.yml
10 changes: 4 additions & 6 deletions molecule/default-debian_stretch/molecule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ driver:
safe_files:
- nexus-downloads

lint:
name: yamllint
lint: |
yamllint .
ansible-lint
flake8
platforms:

Expand Down Expand Up @@ -40,8 +42,6 @@ platforms:

provisioner:
name: ansible
lint:
name: ansible-lint

scenario:
check_sequence:
Expand All @@ -67,5 +67,3 @@ scenario:

verifier:
name: testinfra
lint:
name: flake8
2 changes: 0 additions & 2 deletions molecule/default-debian_stretch/playbook.yml

This file was deleted.

2 changes: 2 additions & 0 deletions molecule/default-ubuntu_16.04/converge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
- import_playbook: ../default-converge.yml
10 changes: 4 additions & 6 deletions molecule/default-ubuntu_16.04/molecule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ driver:
safe_files:
- nexus-downloads

lint:
name: yamllint
lint: |
yamllint .
ansible-lint
flake8
platforms:

Expand Down Expand Up @@ -40,8 +42,6 @@ platforms:

provisioner:
name: ansible
lint:
name: ansible-lint

scenario:
check_sequence:
Expand All @@ -67,5 +67,3 @@ scenario:

verifier:
name: testinfra
lint:
name: flake8
2 changes: 0 additions & 2 deletions molecule/default-ubuntu_16.04/playbook.yml

This file was deleted.

2 changes: 2 additions & 0 deletions molecule/default-ubuntu_18.04/converge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
- import_playbook: ../default-converge.yml
10 changes: 4 additions & 6 deletions molecule/default-ubuntu_18.04/molecule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ driver:
safe_files:
- nexus-downloads

lint:
name: yamllint
lint: |
yamllint .
ansible-lint
flake8
platforms:

Expand Down Expand Up @@ -40,8 +42,6 @@ platforms:

provisioner:
name: ansible
lint:
name: ansible-lint

scenario:
check_sequence:
Expand All @@ -67,5 +67,3 @@ scenario:

verifier:
name: testinfra
lint:
name: flake8
2 changes: 0 additions & 2 deletions molecule/default-ubuntu_18.04/playbook.yml

This file was deleted.

Loading

0 comments on commit e959cc3

Please sign in to comment.