Skip to content

Commit

Permalink
cache file for remote vm
Browse files Browse the repository at this point in the history
  • Loading branch information
robbycochran committed Sep 13, 2024
1 parent b8f61ba commit 30382e5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
21 changes: 12 additions & 9 deletions ansible/ci-build-builder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
collector_root: "{{ ansible_env.HOME }}/collector"
local_branch: local
ccache_dir: "builder/.ccache"
ccache_archive: "docker.tar.gz"
ccache_path: "{{ ccache_dir }}/{{ ccache_archive }}"
container_ccache_dir: "/root/.ccache"

tasks:
Expand All @@ -26,20 +28,21 @@
recursive: true
when: arch == "s390x"

- name: Check if builder ccache exists
- name: Check if remote ccache exists on controller
ansible.builtin.stat:
path: "{{ github_workspace }}/{{ ccache_dir }}"
register: ccache_dir_check
path: "{{ github_workspace }}/{{ ccache_path }}
register: ccache_check
delegate_to: localhost
when: arch == "s390x"

- name: Copy builder ccache from the controller to build VM
- name: Copy ccache from the controller to build VM
ansible.builtin.copy:
src: "{{ github_workspace }}/{{ ccache_dir }}"
dest: "{{ collector_root }}/{{ ccache_dir }}"
src: "{{ github_workspace }}/{{ ccache_path }}"
dest: "{{ collector_root }}/{{ ccache_path }}"
mode: preserve
remote_src: false
delegate_to: localhost
when: arch == "s390x" and ccache_dir_check.stat.exists
when: arch == "s390x" and ccache_check.stat.exists

- name: Inject docker cache manually
ansible.builtin.shell:
Expand All @@ -60,9 +63,9 @@
chdir: "{{ collector_root }}"
when: arch == "s390x"

- name: Copy ccache back to local machine
- name: Copy ccache from build machine to controller
ansible.builtin.fetch:
src: "{{ collector_root }}/{{ ccache_dir }}"
src: "{{ collector_root }}/{{ ccache_path }}"
dest: "{{ github_workspace }}/{{ ccache_dir }}"
flat: yes
when: arch == "s390x"
Expand Down
16 changes: 10 additions & 6 deletions ansible/scripts/extract_docker_cache.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ TARGET_PATH="$2"

# Create a temporary scratch directory
SCRATCH_DIR=$(mktemp -d)
CACHE_ARCHIVE=$(mktemp)
# shellcheck disable=SC2064
trap "rm -rf ${SCRATCH_DIR}" EXIT
trap "rm -rf ${SCRATCH_DIR}; rm -f ${CACHE_ARCHIVE}" EXIT

# Timestamp to bust cache
date -Iseconds > "${SCRATCH_DIR}/buildstamp"
Expand All @@ -27,8 +28,8 @@ cat << EOF > "${SCRATCH_DIR}/Dockerfile.extract"
FROM busybox:1
COPY buildstamp buildstamp
RUN --mount=type=cache,target=${TARGET_PATH} \\
mkdir -p /var/dance-cache/ \\
&& cp -p -R ${TARGET_PATH}/. /var/dance-cache/ || true
mkdir -p /var/docker-cache/ \\
&& cp -p -R ${TARGET_PATH}/. /var/docker-cache/ || true
EOF

echo "Generated Dockerfile.extract"
Expand All @@ -44,10 +45,13 @@ docker rm -f cache-container || true
docker create --name cache-container cache:extract

# Extract the cache from the container
docker cp -L cache-container:/var/dance-cache - | gtar -H posix -x -C "${SCRATCH_DIR}"
docker cp -L cache-container:/var/docker-cache - | gtar -H posix -x -C "${SCRATCH_DIR}"

# Compress the cache from the container
(cd "${SCRATCH_DIR}/docker-cache" && tar czf "${CACHE_ARCHIVE}" .)

# Move the cache into its dest
rm -rf "$CACHE_DEST"
mv "${SCRATCH_DIR}/dance-cache" "$CACHE_DEST"
rm -rf "${CACHE_DEST}/docker.tar.gz"
mv "${CACHE_ARCHIVE}" "${CACHE_DEST}/docker.tar.gz"

echo "Docker cache extraction completed successfully."
7 changes: 4 additions & 3 deletions ansible/scripts/inject_docker_cache.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ cat << EOF > "${SCRATCH_DIR}/Dockerfile.inject"
FROM busybox:1
COPY buildstamp buildstamp
RUN --mount=type=cache,target=${TARGET_PATH} \\
--mount=type=bind,source=.,target=/var/dance-cache \\
cp -p -R /var/dance-cache/. ${TARGET_PATH} || true
--mount=type=bind,source=.,target=/var/docker-cache \\
tar -xzf /var/docker-cache/docker.tar.gz -C ${TARGET_PATH} || true
EOF

echo "Generated Dockerfile.inject"
Expand All @@ -42,6 +42,7 @@ cat "${SCRATCH_DIR}/Dockerfile.inject"
cd "$CACHE_SOURCE"

# Inject data into Docker cache
docker buildx build -f "${SCRATCH_DIR}/Dockerfile.inject" --tag cache:inject .
docker buildx build --progress plain -f "${SCRATCH_DIR}/Dockerfile.inject" --tag cache:inject .
#docker buildx build -f "${SCRATCH_DIR}/Dockerfile.inject" --tag cache:inject .

echo "Cache injection completed successfully."

0 comments on commit 30382e5

Please sign in to comment.