Skip to content

Commit

Permalink
Add option for package manager
Browse files Browse the repository at this point in the history
- Fixes bug where we incorrecty assume dnf for python interpreter install
- Add pulp tests that verify check_ansible and new pkg manager option
  • Loading branch information
Shrews committed Mar 30, 2023
1 parent 68f363a commit cebaf9a
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/test-scripts/setup_pulp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export XDG_RUNTIME_DIR=/tmp/pulptests
mkdir $XDG_RUNTIME_DIR

skopeo login --username admin --password password localhost:8080 --tls-verify=false
skopeo copy docker://registry.access.redhat.com/ubi9/ubi-micro:latest docker://localhost:8080/testrepo/ubi-micro --dest-tls-verify=false
skopeo copy docker://registry.access.redhat.com/ubi9/ubi-minimal:latest docker://localhost:8080/testrepo/ubi-minimal --dest-tls-verify=false

podman login --username "$1" --password "$2" registry.redhat.io
skopeo copy docker://registry.redhat.io/ansible-automation-platform-21/ansible-builder-rhel8:latest docker://localhost:8080/testrepo/ansible-builder-rhel8 --dest-tls-verify=false
16 changes: 10 additions & 6 deletions ansible_builder/_target_scripts/assemble
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,16 @@ if [ -z $PKGMGR ]; then
PKGMGR=/usr/bin/dnf
if [ -f "/usr/bin/microdnf" ]; then
PKGMGR=/usr/bin/microdnf
if [ -z $PKGMGR_OPTS ]; then
# NOTE(pabelanger): skip install docs and weak dependencies to
# make smaller images. Sadly, setting these in dnf.conf don't
# appear to work.
PKGMGR_OPTS="--nodocs --setopt install_weak_deps=0"
fi
fi
fi

if [ "$PKGMGR" = "/usr/bin/microdnf" ]
then
if [ -z $PKGMGR_OPTS ]; then
# NOTE(pabelanger): skip install docs and weak dependencies to
# make smaller images. Sadly, setting these in dnf.conf don't
# appear to work.
PKGMGR_OPTS="--nodocs --setopt install_weak_deps=0"
fi
fi

Expand Down
8 changes: 6 additions & 2 deletions ansible_builder/containerfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ def prepare(self):

if not self.definition.builder_image:
if self.definition.python_package_name:
# FIXME: better dnf cleanup needed?
self.steps.append('RUN dnf install $PYPKG -y && dnf clean all')
self.steps.append('RUN $PKGMGR install $PYPKG -y && $PKGMGR clean all')

# We should always make sure pip is available for later stages.
self.steps.append('RUN $PYCMD -m ensurepip')
Expand Down Expand Up @@ -188,6 +187,9 @@ def _insert_global_args(self, include_values: bool = False):
'ANSIBLE_INSTALL_REFS': self.definition.ansible_ref_install_list,
}

if self.definition.version >= 3:
global_args['PKGMGR'] = self.definition.options['package_manager_path']

for arg, value in global_args.items():
if include_values and value:
# quote the value in case it includes spaces
Expand Down Expand Up @@ -362,6 +364,8 @@ def _prepare_introspect_assemble_steps(self):
introspect_cmd += " --write-bindep=/tmp/src/bindep.txt --write-pip=/tmp/src/requirements.txt"

self.steps.append(introspect_cmd)
if self.definition.version >= 3:
self.steps.append("ENV PKGMGR=$PKGMGR")
self.steps.append("RUN /output/scripts/assemble")

def _prepare_system_runtime_deps_steps(self):
Expand Down
11 changes: 10 additions & 1 deletion ansible_builder/ee_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,10 @@
"description": "Disables the check for Ansible/Runner in final image",
"type": "boolean",
},
"package_manager_path": {
"description": "Path to the system package manager to use",
"type": "string",
}
},
},
},
Expand Down Expand Up @@ -328,7 +332,9 @@ def validate_schema(ee_def: dict):
raise DefinitionError(msg=e.message, path=e.absolute_schema_path)

_handle_aliasing(ee_def)
_handle_options_defaults(ee_def)

if schema_version >= 3:
_handle_options_defaults(ee_def)


def _handle_aliasing(ee_def: dict):
Expand Down Expand Up @@ -361,3 +367,6 @@ def _handle_options_defaults(ee_def: dict):

if ee_def['options'].get('skip_ansible_check') is None:
ee_def['options']['skip_ansible_check'] = False

if ee_def['options'].get('package_manager_path') is None:
ee_def['options']['package_manager_path'] = '/usr/bin/dnf'
3 changes: 1 addition & 2 deletions ansible_builder/user_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,7 @@ def additional_build_files(self):

@property
def options(self):
# Since some options have default values, the 'options' key is always available
return self.raw['options']
return self.raw.get('options', {})

def get_dep_abs_path(self, entry):
"""Unique to the user EE definition, files can be referenced by either
Expand Down
7 changes: 7 additions & 0 deletions docs/definition.rst
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,12 @@ options
This section is a dictionary that contains keywords/options that can affect
builder runtime functionality. Valid keys for this section are:

``package_manager_path``
A string with the path to the package manager (dnf or microdnf) to use.
The default is ``/usr/bin/dnf``. This value will be used to install a
python interpreter, if specified in ``dependencies``, and during the
build phase by the ``assemble`` script.

``skip_ansible_check``
This boolean value controls whether or not the check for an installation
of Ansible and Ansible Runner is performed on the final image. Set this
Expand All @@ -277,6 +283,7 @@ Example ``options`` section:
.. code:: yaml
options:
package_manager_path: /usr/bin/microdnf
skip_ansible_check: True
version
Expand Down
14 changes: 14 additions & 0 deletions test/data/v3/check_ansible/ee-missing-ansible.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
version: 3

images:
base_image:
name: localhost:8080/testrepo/ubi-minimal:latest

dependencies:
ansible_runner: ansible_runner
python_interpreter:
package_name: python3

options:
package_manager_path: /usr/bin/microdnf
14 changes: 14 additions & 0 deletions test/data/v3/check_ansible/ee-missing-runner.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
version: 3

images:
base_image:
name: localhost:8080/testrepo/ubi-minimal:latest

dependencies:
ansible_core: ansible_core
python_interpreter:
package_name: python3

options:
package_manager_path: /usr/bin/microdnf
14 changes: 14 additions & 0 deletions test/data/v3/check_ansible/ee-skip.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
version: 3

images:
base_image:
name: localhost:8080/testrepo/ubi-minimal:latest

dependencies:
python_interpreter:
package_name: python3

options:
skip_ansible_check: True
package_manager_path: /usr/bin/microdnf
48 changes: 48 additions & 0 deletions test/pulp_integration/test_v3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import pytest
import subprocess


class TestV3:

def test_ansible_check_is_skipped(self, cli, tmp_path, data_dir, podman_ee_tag):
"""
Test that the check_ansible script is skipped will NOT cause build failure.
"""
ee_def = data_dir / 'v3' / 'check_ansible' / 'ee-skip.yml'

result = cli(
f'ansible-builder build -c {tmp_path} -f {ee_def} -t {podman_ee_tag} '
f'--container-runtime=podman -v3'
)

assert result.rc == 0

def test_missing_ansible(self, cli, tmp_path, data_dir, podman_ee_tag):
"""
Test that the check_ansible script will cause build failure if
ansible-core is not installed.
"""
ee_def = data_dir / 'v3' / 'check_ansible' / 'ee-missing-ansible.yml'

with pytest.raises(subprocess.CalledProcessError) as einfo:
cli(
f'ansible-builder build -c {tmp_path} -f {ee_def} -t {podman_ee_tag} '
f'--container-runtime=podman -v3'
)

assert "ERROR - Missing Ansible installation" in einfo.value.stdout

def test_missing_runner(self, cli, tmp_path, data_dir, podman_ee_tag):
"""
Test that the check_ansible script will cause build failure if
ansible-runner is not installed.
"""
ee_def = data_dir / 'v3' / 'check_ansible' / 'ee-missing-runner.yml'

with pytest.raises(subprocess.CalledProcessError) as einfo:
cli(
f'ansible-builder build -c {tmp_path} -f {ee_def} -t {podman_ee_tag} '
f'--container-runtime=podman -v3'
)

assert "ERROR - Missing Ansible Runner installation" in einfo.value.stdout
4 changes: 3 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ commands = pytest {posargs:test/unit}
# Some of these tests must run serially because of a shared resource
# (the system policy.json file).
description = Run pulp integration tests
commands = pytest -n 1 -m "serial" {posargs:test/pulp_integration}
commands =
pytest -n 1 -m "serial" {posargs:test/pulp_integration}
pytest -m "not serial" {posargs:test/pulp_integration}

[testenv:integration{,-py39,-py310}]
description = Run integration tests
Expand Down

0 comments on commit cebaf9a

Please sign in to comment.