From 9dac7d6d280c61475afbc236a376aeda125d59f6 Mon Sep 17 00:00:00 2001 From: Steve Baker Date: Fri, 21 Dec 2018 16:03:50 +1300 Subject: [PATCH] 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 --- tasks/modify_image.yml | 5 ---- tasks/precheck.yml | 5 ++++ tasks/yum_update.yml | 55 +++-------------------------------- tasks/yum_update_buildah.yml | 49 +++++++++++++++++++++++++++++++ tasks/yum_update_docker.yml | 56 ++++++++++++++++++++++++++++++++++++ 5 files changed, 114 insertions(+), 56 deletions(-) create mode 100644 tasks/yum_update_buildah.yml create mode 100644 tasks/yum_update_docker.yml diff --git a/tasks/modify_image.yml b/tasks/modify_image.yml index 9464aa7..567504e 100644 --- a/tasks/modify_image.yml +++ b/tasks/modify_image.yml @@ -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 }}" diff --git a/tasks/precheck.yml b/tasks/precheck.yml index adf54fd..6649784 100644 --- a/tasks/precheck.yml +++ b/tasks/precheck.yml @@ -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 diff --git a/tasks/yum_update.yml b/tasks/yum_update.yml index 13e27b0..47497ac 100644 --- a/tasks/yum_update.yml +++ b/tasks/yum_update.yml @@ -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' diff --git a/tasks/yum_update_buildah.yml b/tasks/yum_update_buildah.yml new file mode 100644 index 0000000..36f6350 --- /dev/null +++ b/tasks/yum_update_buildah.yml @@ -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) }} diff --git a/tasks/yum_update_docker.yml b/tasks/yum_update_docker.yml new file mode 100644 index 0000000..13e27b0 --- /dev/null +++ b/tasks/yum_update_docker.yml @@ -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 }}"