From 9934ff847045876447fda3d3448030a07045f652 Mon Sep 17 00:00:00 2001
From: Wouter Deconinck <wdconinc@gmail.com>
Date: Mon, 3 Apr 2023 21:41:05 +0000
Subject: [PATCH] feat: install environment and push to buildcache with
 Makefile

---
 containers/jug/dev.Dockerfile | 68 +----------------------------------
 spack-environment/Makefile    | 29 +++++++++++++++
 2 files changed, 30 insertions(+), 67 deletions(-)
 create mode 100644 spack-environment/Makefile

diff --git a/containers/jug/dev.Dockerfile b/containers/jug/dev.Dockerfile
index 382b63855..371973ac1 100644
--- a/containers/jug/dev.Dockerfile
+++ b/containers/jug/dev.Dockerfile
@@ -104,73 +104,7 @@ RUN rm -r /usr/local                                                    \
  && cd /opt/spack-environment                                           \
  && source $SPACK_ROOT/share/spack/setup-env.sh                         \
  && spack env activate --dir /opt/spack-environment/${ENV}              \
- && spack concretize --fresh
-
-
-## Now execute the main build (or fetch from cache if possible)
-## note, no-check-signature is needed to allow the quicker signature-less
-## packages from the internal (docker) buildcache
-##
-## Optional, nuke the buildcache after install, before (re)caching
-## This is useful when going to completely different containers,
-## or intermittently to keep the buildcache step from taking too much time
-##
-## Update the local build cache if needed. Consists of 3 steps:
-## 1. Remove the eic-spack buildcache on S3
-## 2. Get a list of all packages, and compare with what is already on
-##    the buildcache (using package hash)
-## 3. Add packages that need to be added to buildcache if any
-RUN --mount=type=cache,target=/var/cache/spack-mirror,sharing=locked    \
-    cd /opt/spack-environment                                           \
- && source $SPACK_ROOT/share/spack/setup-env.sh                         \
- && spack env activate --dir /opt/spack-environment/${ENV}              \
- && status=0                                                            \
- && spack install -j64 --no-check-signature                             \
-    || spack install -j64 --no-check-signature                          \
-    || spack install -j64 --no-check-signature --show-log-on-error      \
-    || status=$?                                                        \
- && spack mirror rm --scope site eic-spack                              \
- && [ -z "${CACHE_NUKE}" ]                                              \
-    || rm -rf /var/cache/spack-mirror/build_cache/*                     \
- && mkdir -p /var/cache/spack-mirror/build_cache                        \
- && spack buildcache update-index -d /var/cache/spack-mirror            \
- && spack buildcache list --allarch --very-long                         \
-    | sed '/^$/d;/^--/d;s/@.\+//;s/\([a-z0-9]*\) \(.*\)/\2\/\1/'        \
-    | sort > buildcache.local.txt                                       \
- && spack find --format {name}/{hash} | sort                            \
-    | comm -23 - buildcache.local.txt                                   \
-    | xargs --verbose --no-run-if-empty                                 \
-      spack buildcache create --allow-root --only package --unsigned    \
-                              --directory /var/cache/spack-mirror       \
-                              --rebuild-index                           \
- && spack clean -a                                                      \
- && exit $status
-
-## Update the S3 build cache (without local cache mount)
-ARG S3RW_ACCESS_KEY=""
-ARG S3RW_SECRET_KEY=""
-RUN cd /opt/spack-environment                                           \
- && source $SPACK_ROOT/share/spack/setup-env.sh                         \
- && spack env activate --dir /opt/spack-environment/${ENV}              \
- && if [ -n "${S3RW_ACCESS_KEY}" ] ; then                               \
-    spack mirror add --scope site                                       \
-      --s3-endpoint-url https://eics3.sdcc.bnl.gov:9000                 \
-      --s3-access-key-id "${S3RW_ACCESS_KEY}"                           \
-      --s3-access-key-secret "${S3RW_SECRET_KEY}"                       \
-      eic-spack s3://eictest/EPIC/spack                                 \
- && spack mirror list                                                   \
- && spack buildcache list --allarch --very-long                         \
-    | sed '/^$/d;/^--/d;s/@.\+//;s/\([a-z0-9]*\) \(.*\)/\2\/\1/'        \
-    | sort > buildcache.eic-spack.txt                                   \
- && spack find --format {name}/{hash} | sort                            \
-    | comm -23 - buildcache.eic-spack.txt                               \
-    | xargs --verbose --no-run-if-empty                                 \
-      spack buildcache create --allow-root --only package --unsigned    \
-                              --mirror-name eic-spack                   \
- && spack buildcache update-index --mirror-url eic-spack                \
- && spack mirror rm --scope site eic-spack                              \
-    ; fi                                                                \
- && spack mirror list
+ && make -C /opt/spack-environment BUILDCACHE=/var/cache/spack-mirror
 
 ## Extra post-spack steps:
 ##   - Python packages
diff --git a/spack-environment/Makefile b/spack-environment/Makefile
new file mode 100644
index 000000000..983b5e73d
--- /dev/null
+++ b/spack-environment/Makefile
@@ -0,0 +1,29 @@
+SPACK ?= spack
+SPACK_INSTALL_FLAGS += --no-check-signature
+
+BUILDCACHE := $(SPACK_ENV)/cache
+
+.PHONY: all clean
+
+all: push
+
+ifeq (,$(filter clean,$(MAKECMDGOALS)))
+include ${SPACK_ENV}/spack.mk
+endif
+
+${SPACK_ENV}/push: $(addprefix ${SPACK_ENV}/push/,$(SPACK_PACKAGE_IDS))
+        $(SPACK) buildcache update-index --directory $(BUILDCACHE)
+
+${SPACK_ENV}/push/%: ${SPACK_ENV}/install/%
+        @mkdir -p $(dir $@)
+        $(foreach buildcache, $(BUILDCACHE), $(SPACK) buildcache create --allow-root --only=package --unsigned $(buildcache) /$(HASH);)
+        @touch $@
+
+${SPACK_ENV}/spack.lock: ${SPACK_ENV}/spack.yaml
+        $(SPACK) concretize --force
+
+${SPACK_ENV}/spack.mk: ${SPACK_ENV}/spack.lock
+        $(SPACK) env depfile --output $@ --make-target-prefix ""
+
+clean:
+        rm -rf ${SPACK_ENV}/spack.lock ${SPACK_ENV}/spack.mk
-- 
GitLab