Skip to content

Commit

Permalink
Add pip_install target script
Browse files Browse the repository at this point in the history
  • Loading branch information
Shrews committed Jan 26, 2024
1 parent 576b3f0 commit f8843a4
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 4 deletions.
6 changes: 6 additions & 0 deletions docs/definition.rst
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,12 @@ builder runtime functionality. Valid keys for this section are:
of Ansible and Ansible Runner is performed on the final image. Set this
value to ``True`` to not perform this check. The default is ``False``.

``skip_pip_install``
This boolean value controls whether or not we attempt to install pip into
the base image. Pip is necessary for Python requirement installation, among
other things. You may choose to disable this step and handle installing
pip manually if the current method of pip installation does not work for you.

``relax_passwd_permissions``
This boolean value controls whether the ``root`` group (GID 0) is explicitly granted
write permission to ``/etc/passwd`` in the final container image. The default entrypoint
Expand Down
56 changes: 56 additions & 0 deletions src/ansible_builder/_target_scripts/pip_install
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/bin/bash
# Copyright (c) 2024 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#####################################################################
# Script to encapsulate pip installation.
#
# Usage: pip_install <PYCMD>
#
# Options:
# PYCMD - The path to the python executable to use.
#####################################################################

set -x

PYCMD=$1

if [ -z "$PYCMD" ]
then
echo "Usage: pip_install <PYCMD>"
exit 1
fi

if [ ! -x "$PYCMD" ]
then
echo "$PYCMD is not an executable"
exit 1
fi

# This is going to be our default functionality for now. This will likely
# need to change if we add support for non-RHEL distros.
$PYCMD -m ensurepip --root /

if [ $? -ne 0 ]
then
cat<<EOF
**********************************************************************
ERROR - pip installation failed for Python $PYCMD
**********************************************************************
EOF
exit 1
fi

exit 0
9 changes: 5 additions & 4 deletions src/ansible_builder/containerfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,21 @@ def prepare(self) -> None:
])

self._insert_global_args()
self._create_folder_copy_files()
self._insert_custom_steps('prepend_base')

if not self.definition.builder_image:
if self.definition.python_package_system:
step = 'RUN $PKGMGR install $PYPKG -y ; if [ -z $PKGMGR_PRESERVE_CACHE ]; then $PKGMGR clean all; fi'
self.steps.append(step)

# We should always make sure pip is available for later stages.
self.steps.append('RUN $PYCMD -m ensurepip --root /')
# pip needs to be available for later stages.
if self.definition.version >= 3 and not self.definition.options['skip_pip_install']:
self.steps.append('RUN /output/scripts/pip_install $PYCMD')

if self.definition.ansible_ref_install_list:
self.steps.append('RUN $PYCMD -m pip install --no-cache-dir $ANSIBLE_INSTALL_REFS')

self._create_folder_copy_files()
self._insert_custom_steps('append_base')

######################################################################
Expand Down Expand Up @@ -283,7 +284,7 @@ def _create_folder_copy_files(self) -> None:
scriptres = importlib.resources.files('ansible_builder._target_scripts')
script_files = (
'assemble', 'install-from-bindep', 'introspect.py', 'check_galaxy',
'check_ansible', 'entrypoint'
'check_ansible', 'pip_install', 'entrypoint'
)
for script in script_files:
with importlib.resources.as_file(scriptres / script) as script_path:
Expand Down
5 changes: 5 additions & 0 deletions src/ansible_builder/ee_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,10 @@
"description": "Disables the check for Ansible/Runner in final image",
"type": "boolean",
},
"skip_pip_install": {
"description": "Disables the installation of pip in the base image",
"type": "boolean",
},
"workdir": {
"description": "Default working directory, also often the homedir for ephemeral UIDs",
"type": ["string", "null"],
Expand Down Expand Up @@ -444,6 +448,7 @@ def _handle_options_defaults(ee_def: dict):
entrypoint_path = os.path.join(constants.FINAL_IMAGE_BIN_PATH, "entrypoint")

options.setdefault('skip_ansible_check', False)
options.setdefault('skip_pip_install', False)
options.setdefault('relax_passwd_permissions', True)
options.setdefault('workdir', '/runner')
options.setdefault('package_manager_path', '/usr/bin/dnf')
Expand Down

0 comments on commit f8843a4

Please sign in to comment.