Implement a yum install command

Yum update works if you want to update existing packages, but sometimes
you want to install a package that's available via the yum repositories
that is extra. This implements a yum install action similiar to the yum
update action except it takes package names instead of repos to install.

Change-Id: Ia47a1ea9eb51a37f6d75d39c524e97fd4ec94fba
This commit is contained in:
Alex Schultz 2019-07-03 11:59:23 -06:00
parent 1415b042f5
commit 3a4caf4587
7 changed files with 235 additions and 0 deletions

View File

@ -56,6 +56,33 @@ Role Variables
- See modify image variables
.. list-table:: Variables used for yum install
:widths: auto
:header-rows: 1
* - Name
- Default Value
- Description
* - `source_image`
- `[undefined]`
- See modify image variables
* - `modified_append_tag`
- `date +-modified-%Y%m%d%H%M%S`
- See modify image variables
* - `target_image`
- `''`
- See modify image variables
* - `yum_packages`
- `[]`
- Provide a list of packages to install via yum
* - `yum_repos_dir_path`
- `None`
- Optional path of directory to be used as `/etc/yum.repos.d` during the update
* - `container_build_tool`
- `docker`
- See modify image variables
.. list-table:: Variables used for dev install
:widths: auto
:header-rows: 1
@ -161,6 +188,29 @@ of an `import_role` parameter.
modified_append_tag: updated
container_build_tool: docker # or buildah
Yum install
~~~~~~~~~~-
The following playbook will produce a modified image with the tag
`:latest-updated` which will do a yum install of the requested packages
using the host's /etc/yum.repos.d. In this playbook the tasks\_from is set as
a variable instead of an `import_role` parameter.
.. code-block::
- hosts: localhost
tasks:
- name: include tripleo-modify-image
import_role:
name: tripleo-modify-image
vars:
tasks_from: yum_install.yml
source_image: docker.io/tripleomaster/centos-binary-nova-api:latest
compare_host_packages: true
yum_repos_dir_path: /etc/yum.repos.d
yum_packages: ['foobar-nova-plugin', 'fizzbuzz-nova-plugin']
container_build_tool: docker # or buildah
RPM install
~~~~~~~~~~~

View File

@ -3,3 +3,4 @@ update_repo: ''
container_build_tool: 'docker'
python_dir: []
refspecs: []
yum_packages: []

16
files/yum_install.sh Executable file
View File

@ -0,0 +1,16 @@
#!/bin/bash
set -eou pipefail
PKG="$(command -v dnf || command -v yum)"
PKG_MGR="$(echo ${PKG:(-3)})"
if [ -z "$1" ]; then
echo "No packages were specified to install..."
exit 1
fi
YUM_PACKAGES=$1
$PKG -y install $YUM_PACKAGES
rm -rf /var/cache/$PKG_MGR

10
tasks/yum_install.yml Normal file
View File

@ -0,0 +1,10 @@
---
- import_tasks: precheck.yml
tags:
- always
- import_tasks: yum_install_buildah.yml
when: container_build_tool == 'buildah'
- import_tasks: yum_install_docker.yml
when: container_build_tool == 'docker'

View File

@ -0,0 +1,84 @@
---
- import_tasks: precheck.yml
tags:
- always
- name: From image {{ source_image }}
command: buildah from {{ source_image }}
register: from_image_cmd
- name: Set from_image
set_fact:
from_image: "{{ from_image_cmd.stdout }}"
- name: Run buildah config
command: >
buildah config
--label modified_append_tag={{ modified_append_tag }}
--workingdir / {{ from_image }}
- name: Create tempfile name for yum_install.sh
tempfile:
state: file
register: yum_install
- name: Prepare yum_install.sh script
copy:
src: files/yum_install.sh
dest: "{{ yum_install.path }}"
mode: 0755
- name: List file repos
shell: sed -n 's|baseurl=file://||p' *.repo
args:
chdir: "{{ yum_repos_dir_path }}"
register: file_repos
- block:
- name: Run yum_install.sh
command: >
buildah run
--volume {{ yum_install.path }}:/tmp/yum_install.sh
--volume {{ yum_repos_dir_path }}:/etc/yum.repos.d
{% for repo in file_repos.stdout_lines %}
{% if repo|exists %}
--volume {{ repo }}:{{ repo }}
{% endif %}
{% endfor %}
--user root
--net host
{{ from_image }}
/tmp/yum_install.sh "{{ yum_packages | join(' ') }}"
register: result
rescue:
- name: Run yum_install.sh (retry)
command: >
buildah --debug run
--volume {{ yum_install.path }}:/tmp/yum_install.sh
--volume {{ yum_repos_dir_path }}:/etc/yum.repos.d
{% for repo in file_repos.stdout_lines %}
{% if repo|exists %}
--volume {{ repo }}:{{ repo }}
{% endif %}
{% endfor %}
--user root
--net host
{{ from_image }}
bash -x /tmp/yum_install.sh "{{ yum_packages | join(' ') }}"
retries: 2
delay: 3
register: result
until: result.rc == 0
- name: Remove temporary yum_install.sh script
file:
path: "{{ yum_install.path }}"
state: absent
- name: Commit changes to image
({{ target_image | default(source_image) }}{{ modified_append_tag }})
command: >
buildah commit
{{ from_image }}
{{ target_image | default(source_image) }}{{ modified_append_tag }}

View File

@ -0,0 +1,57 @@
---
- import_tasks: precheck.yml
tags:
- always
- import_tasks: get_original_user.yml
- name: Create image build context directory
tempfile:
state: directory
prefix: tripleo-modify-image
register: context_dir
- name: Set modify_dir_path
set_fact:
modify_dir_path: "{{ context_dir.path }}"
- name: Copy local file repos to context directory
shell: |
#!/bin/sh
set -ex
cp -a {{ yum_repos_dir_path }} {{ modify_dir_path }}/yum.repos.d
# discover repos with local packages
repos=$(sed -n 's/baseurl=file:\/\///p' {{ yum_repos_dir_path }}/*.repo)
mkdir repos
for repo in $repos ; do
if [ -d $repo ]; then
target_dir=repos$repo
echo "copying $repo to $target_dir"
mkdir -p $target_dir
cp -a $repo/* $target_dir
fi
done
args:
chdir: "{{ modify_dir_path }}"
when: yum_repos_dir_path is defined
- name: Write Dockerfile to {{ modify_dir_path }}
template:
src: Dockerfile-yum-install.j2
dest: "{{ modify_dir_path }}/Dockerfile"
- name: Write yum_install.sh
copy:
src: yum_install.sh
dest: "{{ modify_dir_path }}/yum_install.sh"
mode: '0555'
- include_tasks: modify_image.yml
- name: Clean modify directory
file:
state: absent
path: "{{ modify_dir_path }}"

View File

@ -0,0 +1,17 @@
FROM {{ source_image }}
LABEL modified_append_tag={{ modified_append_tag }}
WORKDIR /
USER root
COPY yum_install.sh /tmp/
{% if yum_repos_dir_path is defined %}
RUN rm -rf /etc/yum.repos.d/
COPY yum.repos.d /etc/yum.repos.d
COPY repos /
{% endif %}
RUN /tmp/yum_install.sh "{{ yum_packages | join(' ') }}"
USER "{{ original_user }}"