Use buildah commands for yum update to improve speed

This allows directly mounting directories instead of copying them
twice. Also the resulting image has only one extra layer instead of
one per Dockerfile directive.

Change-Id: I8a3769c0b55572ba05cc29ecd28a131cc94e8c4d
This commit is contained in:
Steve Baker 2018-12-21 16:03:50 +13:00 committed by Emilien Macchi
parent d67f1ef794
commit 9dac7d6d28
5 changed files with 114 additions and 56 deletions

View File

@ -8,11 +8,6 @@
- modify_dir_path is defined
- modify_dir_path | length > 0
- name: Set default modified_append_tag
set_fact:
modified_append_tag: "{{ lookup('pipe','date +-modified-%Y%m%d%H%M%S') }}"
when: modified_append_tag is undefined
- name: Create Dockerfile tempfile name
tempfile:
path: "{{ modify_dir_path }}"

View File

@ -7,3 +7,8 @@
- name: Ensure that container_build_tool is correctly set
fail: msg="{{ container_build_tool }} is not a valid value for container_build_tool. Pick docker or buildah."
when: container_build_tool not in ['docker', 'buildah']
- name: Set default modified_append_tag
set_fact:
modified_append_tag: "{{ lookup('pipe','date +-modified-%Y%m%d%H%M%S') }}"
when: modified_append_tag is undefined

View File

@ -2,55 +2,8 @@
tags:
- always
- import_tasks: get_original_user.yml
- import_tasks: yum_update_buildah.yml
when: container_build_tool == 'buildah'
- 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.j2
dest: "{{ modify_dir_path }}/Dockerfile"
- name: Write yum_update.sh
copy:
src: yum_update.sh
dest: "{{ modify_dir_path }}/yum_update.sh"
mode: '0555'
- include_tasks: modify_image.yml
- name: Clean modify directory
file:
state: absent
path: "{{ modify_dir_path }}"
- import_tasks: yum_update_docker.yml
when: container_build_tool == 'docker'

View File

@ -0,0 +1,49 @@
- 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: Copy yum_update.sh
command: >
buildah copy
{{ from_image }}
files/yum_update.sh /tmp/yum_update.sh
- name: List file repos
shell: sed -n 's|baseurl=file://||p' *.repo
args:
chdir: "{{ yum_repos_dir_path }}"
register: file_repos
- name: Run yum_update.sh
command: >
buildah run
--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_update.sh "{{ update_repo }}"
- name: Commit changes to image {{ target_image | default(source_image) }}
command: >
buildah commit
{{ from_image }}
{{ target_image | default(source_image) }}

View File

@ -0,0 +1,56 @@
- 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.j2
dest: "{{ modify_dir_path }}/Dockerfile"
- name: Write yum_update.sh
copy:
src: yum_update.sh
dest: "{{ modify_dir_path }}/yum_update.sh"
mode: '0555'
- include_tasks: modify_image.yml
- name: Clean modify directory
file:
state: absent
path: "{{ modify_dir_path }}"