diff options
author | Chuck Lantz <clantz@microsoft.com> | 2020-10-08 17:27:19 +0300 |
---|---|---|
committer | Chuck Lantz <clantz@microsoft.com> | 2020-10-08 17:27:19 +0300 |
commit | 606b3a5f7c53eb89820372249e8d8bc232ae2665 (patch) | |
tree | 1a0a12169a217f25b40d8f7727e514317b30d5d3 /.devcontainer/cache | |
parent | 5813311b98e29404d0a90688109be849bd7cff5a (diff) |
Dev container cache image generation
Diffstat (limited to '.devcontainer/cache')
-rw-r--r-- | .devcontainer/cache/.gitignore | 1 | ||||
-rwxr-xr-x | .devcontainer/cache/before-cache.sh | 15 | ||||
-rwxr-xr-x | .devcontainer/cache/build-cache-image.sh | 28 | ||||
-rwxr-xr-x | .devcontainer/cache/cache-diff.sh | 21 | ||||
-rw-r--r-- | .devcontainer/cache/cache.Dockerfile | 14 | ||||
-rwxr-xr-x | .devcontainer/cache/restore-diff.sh | 23 |
6 files changed, 102 insertions, 0 deletions
diff --git a/.devcontainer/cache/.gitignore b/.devcontainer/cache/.gitignore new file mode 100644 index 00000000000..4f96ddff402 --- /dev/null +++ b/.devcontainer/cache/.gitignore @@ -0,0 +1 @@ +*.manifest diff --git a/.devcontainer/cache/before-cache.sh b/.devcontainer/cache/before-cache.sh new file mode 100755 index 00000000000..78da0a862ca --- /dev/null +++ b/.devcontainer/cache/before-cache.sh @@ -0,0 +1,15 @@ +#/bin/bash + +# This file establishes a basline for the reposuitory before any steps in the "prepare.sh" +# are run. Its just a find command that filters out a few things we don't need to watch. + +set -e + +SCRIPT_PATH="$(cd "$(dirname $0)" && pwd)" +SOURCE_FOLDER="${1:-"."}" + +cd "${SOURCE_FOLDER}" +echo "[$(date)] Generating ""before"" manifest..." +find -L . -not -path "*/.git/*" -and -not -path "${SCRIPT_PATH}/*.manifest" -type f > "${SCRIPT_PATH}/before.manifest" +echo "[$(date)] Done!" + diff --git a/.devcontainer/cache/build-cache-image.sh b/.devcontainer/cache/build-cache-image.sh new file mode 100755 index 00000000000..fb2886a3075 --- /dev/null +++ b/.devcontainer/cache/build-cache-image.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +# This file simply wraps the dockeer build command used to build the image with the +# cached result of the commands from "prepare.sh" and pushes it to the specified +# container image registry. + +set -e + +SCRIPT_PATH="$(cd "$(dirname $0)" && pwd)" +CONTAINER_IMAGE_REPOSITORY="$1" +BRANCH="${2:-"master"}" + +if [ "${CONTAINER_IMAGE_REPOSITORY}" = "" ]; then + echo "Container repository not specified!" + exit 1 +fi + +TAG="${BRANCH//\//-}" +echo "[$(date)] ${BRANCH} => ${TAG}" +cd "${SCRIPT_PATH}/../.." + +echo "[$(date)] Starting image build..." +docker build -t ${CONTAINER_IMAGE_REPOSITORY}:"${TAG}" -f "${SCRIPT_PATH}/cache.Dockerfile" . +echo "[$(date)] Image build complete." + +echo "[$(date)] Pushing image..." +docker push ${CONTAINER_IMAGE_REPOSITORY}:"${TAG}" +echo "[$(date)] Done!" diff --git a/.devcontainer/cache/cache-diff.sh b/.devcontainer/cache/cache-diff.sh new file mode 100755 index 00000000000..362337ce6eb --- /dev/null +++ b/.devcontainer/cache/cache-diff.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +# This file is used to archive off a copy of any differences in the source tree into another location +# in the image. Once the codespace is up, this will be restored into its proper location (which is +# quick and happens parallel to other startup activities) + +set -e + +SCRIPT_PATH="$(cd "$(dirname $0)" && pwd)" +SOURCE_FOLDER="${1:-"."}" +CACHE_FOLDER="${2:-"/usr/local/etc/devcontainer-cache"}" + +echo "[$(date)] Starting cache operation..." +cd "${SOURCE_FOLDER}" +echo "[$(date)] Determining diffs..." +find -L . -not -path "*/.git/*" -and -not -path "${SCRIPT_PATH}/*.manifest" -type f > "${SCRIPT_PATH}/after.manifest" +grep -Fxvf "${SCRIPT_PATH}/before.manifest" "${SCRIPT_PATH}/after.manifest" > "${SCRIPT_PATH}/cache.manifest" +echo "[$(date)] Archiving diffs..." +mkdir -p "${CACHE_FOLDER}" +tar -cf "${CACHE_FOLDER}/cache.tar" --totals --files-from "${SCRIPT_PATH}/cache.manifest" +echo "[$(date)] Done! $(du -h "${CACHE_FOLDER}/cache.tar")" diff --git a/.devcontainer/cache/cache.Dockerfile b/.devcontainer/cache/cache.Dockerfile new file mode 100644 index 00000000000..79af3ee8a35 --- /dev/null +++ b/.devcontainer/cache/cache.Dockerfile @@ -0,0 +1,14 @@ +# This dockerfile is used to build up from a base image to create an image with cached results of running "prepare.sh". +# Other image contents: https://github.com/microsoft/vscode-dev-containers/blob/master/repository-containers/images/github.com/microsoft/vscode/.devcontainer/base.Dockerfile +FROM mcr.microsoft.com/vscode/devcontainers/repos/microsoft/vscode:dev + +ARG USERNAME=node +COPY --chown=${USERNAME}:${USERNAME} . /repo-source-tmp/ +RUN mkdir /usr/local/etc/devcontainer-cache \ + && chown ${USERNAME} /usr/local/etc/devcontainer-cache /repo-source-tmp \ + && su ${USERNAME} -c "\ + cd /repo-source-tmp \ + && .devcontainer/cache/before-cache.sh \ + && .devcontainer/prepare.sh \ + && .devcontainer/cache/cache-diff.sh" \ + && rm -rf /repo-source-tmp diff --git a/.devcontainer/cache/restore-diff.sh b/.devcontainer/cache/restore-diff.sh new file mode 100755 index 00000000000..2f418d87480 --- /dev/null +++ b/.devcontainer/cache/restore-diff.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +# This file restores the results of the "prepare.sh" into their proper locations +# once the container has been created. It runs as a postCreateCommand which +# in GitHub Codespaces occurs parallel to other startup activities and does not +# really add to the overal startup time given how quick the operation ends up being. + +set -e + +SOURCE_FOLDER="$(cd "${1:-"."}" && pwd)" +CACHE_FOLDER="${2:-"/usr/local/etc/devcontainer-cache"}" + +if [ ! -d "${CACHE_FOLDER}" ]; then + echo "No cache folder found." + exit 0 +fi + +echo "[$(date)] Expanding $(du -h "${CACHE_FOLDER}/cache.tar") file to ${SOURCE_FOLDER}..." +cd "${SOURCE_FOLDER}" +tar -xf "${CACHE_FOLDER}/cache.tar" +rm -f "${CACHE_FOLDER}/cache.tar" +echo "[$(date)] Done!" + |