Skip to content

Commit b6a2f12

Browse files
feat(cache): allow cache to be persisted
We'll now persist the cache so that expensive operations can be cached to speed up building the slug.
1 parent dcad3f0 commit b6a2f12

8 files changed

Lines changed: 81 additions & 17 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ BINDIR := ./rootfs/bin
1010

1111
include versioning.mk
1212

13-
SHELL_SCRIPTS = $(wildcard _scripts/*.sh) $(wildcard rootfs/bin/*_object) $(wildcard rootfs/builder/*)
13+
SHELL_SCRIPTS = $(wildcard _scripts/*.sh) $(wildcard rootfs/bin/*_object) $(wildcard rootfs/bin/*_cache) rootfs/bin/normalize_storage $(wildcard rootfs/builder/*)
1414

1515
# The following variables describe the containerized development environment
1616
# and other build options

rootfs/Dockerfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,26 @@ RUN mkdir /app
44
RUN addgroup --quiet --gid 2000 slug && \
55
useradd slug --uid=2000 --gid=2000 --home-dir /app --no-create-home
66

7+
# disable source repos (speeds up apt-get update)
8+
RUN sed -i -e 's/^deb-src/#deb-src/' /etc/apt/sources.list && \
9+
apt-get update && \
10+
apt-get install -y md5deep && \
11+
apt-get clean && \
12+
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/man /usr/share/doc && \
13+
bash -c "mkdir -p /usr/share/man/man{1..8}"
14+
715
ADD . /
816
ENV PYTHONPATH $PYTHONPATH:/usr/local/lib/python3/site-packages
917
ADD https://storage.googleapis.com/object-storage-cli/bb8e054/objstorage-bb8e054-linux-amd64 /bin/objstorage
1018
RUN chmod +x /bin/objstorage
1119
RUN chown -R slug:slug /app
1220
RUN chown slug:slug /bin/get_object
21+
RUN chown slug:slug /bin/normalize_storage
1322
RUN chown slug:slug /bin/put_object
1423
RUN chown slug:slug /bin/objstorage
1524
RUN chown slug:slug /bin/read_procfile_keys
25+
RUN chown slug:slug /bin/restore_cache
26+
RUN chown slug:slug /bin/store_cache
1627

1728
USER slug
1829
ENV HOME /app

rootfs/bin/get_object

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
#!/usr/bin/env bash
2-
32
GET_PATH=/tmp/slug.tgz
4-
5-
export BUCKET_FILE=/var/run/secrets/deis/objectstore/creds/builder-bucket
6-
if [ "$BUILDER_STORAGE" == "minio" ]; then
7-
mkdir -p /app/objectstore/minio/
8-
echo "git" > /app/objectstore/minio/builder-bucket
9-
export BUCKET_FILE=/app/objectstore/minio/builder-bucket
10-
elif [ "$BUILDER_STORAGE" == "azure" ] || [ "$BUILDER_STORAGE" == "swift" ] ; then
11-
export CONTAINER_FILE=/var/run/secrets/deis/objectstore/creds/builder-container
12-
fi
3+
# shellcheck disable=SC1091
4+
source /bin/normalize_storage
135
objstorage --storage-type="$BUILDER_STORAGE" download "$TAR_PATH" "$GET_PATH"

rootfs/bin/normalize_storage

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
export BUCKET_FILE=/var/run/secrets/deis/objectstore/creds/builder-bucket
3+
if [ "$BUILDER_STORAGE" == "minio" ]; then
4+
mkdir -p /app/objectstore/minio/
5+
echo "git" > /app/objectstore/minio/builder-bucket
6+
export BUCKET_FILE=/app/objectstore/minio/builder-bucket
7+
elif [ "$BUILDER_STORAGE" == "azure" ] || [ "$BUILDER_STORAGE" == "swift" ] ; then
8+
export CONTAINER_FILE=/var/run/secrets/deis/objectstore/creds/builder-container
9+
fi

rootfs/bin/put_object

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@
33
SLUG_PATH=/tmp/slug.tgz
44
PROC_PATH=/tmp/build/Procfile
55

6-
export BUCKET_FILE=/var/run/secrets/deis/objectstore/creds/builder-bucket
7-
if [ "$BUILDER_STORAGE" == "minio" ]; then
8-
export BUCKET_FILE=/app/objectstore/minio/builder-bucket
9-
elif [ "$BUILDER_STORAGE" == "azure" ] || [ "$BUILDER_STORAGE" == "swift" ] ; then
10-
export CONTAINER_FILE=/var/run/secrets/deis/objectstore/creds/builder-container
11-
fi
6+
# shellcheck disable=SC1091
7+
source /bin/normalize_storage
128
objstorage --storage-type="$BUILDER_STORAGE" upload "$SLUG_PATH" "${PUT_PATH}/slug.tgz"
139
objstorage --storage-type="$BUILDER_STORAGE" upload "$PROC_PATH" "${PUT_PATH}/Procfile"

rootfs/bin/restore_cache

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env bash
2+
cache_file=/tmp/cache.tgz
3+
# shellcheck disable=SC1091
4+
source /bin/normalize_storage
5+
6+
# Download the cache (which might not exist, hence || true to return zero exit code)
7+
objstorage --storage-type="$BUILDER_STORAGE" download "${CACHE_PATH}/cache.tgz" "$cache_file" &>/tmp/objstorage.log || true

rootfs/bin/store_cache

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
cache_file=/tmp/cache.tgz
3+
# shellcheck disable=SC1091
4+
source /bin/normalize_storage
5+
objstorage --storage-type="$BUILDER_STORAGE" upload "$cache_file" "${CACHE_PATH}/cache.tgz"

rootfs/builder/build.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ unset DEIS_DEBUG
1313
app_dir=/app
1414
build_root=/tmp/build
1515
cache_root=/tmp/cache
16+
cache_file=/tmp/cache.tgz
1617
env_root=/tmp/env
1718
buildpack_root=/tmp/buildpacks
1819

@@ -60,6 +61,24 @@ function ensure_indent() {
6061
done
6162
}
6263

64+
function cache_fingerprint() {
65+
md5deep -r ${cache_root} | sort | uniq | md5sum
66+
}
67+
68+
# Restore cache when a $CACHE_PATH was supplied
69+
if ! [[ -z "${CACHE_PATH}" ]]; then
70+
echo_title "Restoring cache..."
71+
restore_cache
72+
if [[ -f ${cache_file} ]]; then
73+
tar -xzf ${cache_file} -C ${cache_root}
74+
echo_normal "Done!"
75+
else
76+
echo_normal "No cache file could be found. If you're deploying for the first time, it'll be created now."
77+
fi
78+
79+
original_cache_fingerprint=$(cache_fingerprint)
80+
fi
81+
6382
## Copy application code over
6483
if [ -d "/tmp/app" ]; then
6584
cp -rf /tmp/app/. $app_dir
@@ -191,6 +210,31 @@ if [[ ! -f "$build_root/Procfile" ]]; then
191210
fi
192211
fi
193212

213+
# Compress and save cache
214+
if ! [[ -z "${CACHE_PATH}" ]]; then
215+
echo_title "Checking for changes inside the cache directory..."
216+
# If there's any files in the cache_root folder, we'll create a tar and upload
217+
# it for future use
218+
if [ "$(ls -A ${cache_root})" ]; then
219+
# Let's check if the fingerprint changed, if it did, we'll be updating
220+
# the cache
221+
if [[ "$original_cache_fingerprint" != "$(cache_fingerprint)" ]]; then
222+
echo_normal "Files inside cache folder changed, uploading new cache..."
223+
224+
# Create a new cache file and check if it requires to be updated
225+
tar -z -C ${cache_root} -cf ${cache_file} .
226+
cache_size=$(du -Sh "$cache_file" | cut -f1)
227+
228+
store_cache
229+
echo_normal "Done: Uploaded cache (${cache_size})"
230+
else
231+
echo_normal "Cache unchanged, not updating"
232+
fi
233+
else
234+
echo_normal "No files were added to the cache folder, cache wasn't updated"
235+
fi
236+
fi
237+
194238
if [[ "$slug_file" != "-" ]]; then
195239
slug_size=$(du -Sh "$slug_file" | cut -f1)
196240
echo_title "Compiled slug size is $slug_size"

0 commit comments

Comments
 (0)