Optional rendering of example job in the bundle dir

This commit is contained in:
Daniel Berteaud 2024-01-05 10:29:26 +01:00
parent 4bda44903c
commit 5af628ac7f
2 changed files with 61 additions and 10 deletions

68
ctctl
View File

@ -402,7 +402,6 @@ ctctl_build_selected_images(){
ctctl_build_image(){
local DOCKER_IMAGE=$1
local NO_CACHE=$2
export DOCKER_BUILDKIT=1
echo "Building image ${DOCKER_IMAGE}"
# Extract the basename of the image, removing the repo and the tag
@ -418,7 +417,7 @@ ctctl_build_image(){
local FOUND=0
for DIR in ./images ./output/images; do
if [ -d $DIR/${IMAGE_NAME} ]; then
docker build ${NO_CACHE} -t ${DOCKER_IMAGE} -t ${LATEST} ${CTCTL_DOCKER_BUILD_OPTS:-} --progress=plain $DIR/${IMAGE_NAME} &&\
DOCKER_BUILDKIT=1 BUILDKIT_PROGRESS=plain docker build ${NO_CACHE} -t ${DOCKER_IMAGE} -t ${LATEST} ${CTCTL_DOCKER_BUILD_OPTS:-} $DIR/${IMAGE_NAME} &&\
docker push ${DOCKER_IMAGE} &&\
docker push ${LATEST}
FOUND=1
@ -429,7 +428,6 @@ ctctl_build_image(){
echo "Couldn't find Docker image directory"
ctctl_exit
fi
unset DOCKER_BUILDKIT
}
# Run all executable in the render.d directory
@ -568,13 +566,13 @@ ctctl_render_templates(){
fi
local GOMPLATE_BUNDLE_ARGS=(--input-dir "bundles/${NAME}")
# Do not render templates from dependencies, variables files and images (images will be handled later)
GOMPLATE_BUNDLE_ARGS+=(--exclude .git* --exclude deps/** --exclude variables.yml --exclude images/** --exclude templates/**)
# Do not render templates from dependencies, variables files, optional files directory content and images (images will be handled later)
GOMPLATE_BUNDLE_ARGS+=(--exclude .git* --exclude deps/** --exclude bundles.yml --exclude variables.yml --exclude images/** --exclude templates/** --exclude files/** --exclude example/**)
# This is used for two things
# - Add the consul.suffix to every files (except job files). This allows ctctl to simply infer the policy name from the file name
# - Put job files in the current dir for conveniance, and everything else in the output dir
GOMPLATE_BUNDLE_ARGS+=(--output-map)
GOMPLATE_BUNDLE_ARGS+=('[[ if (regexp.Match ".*\\.nomad(\\.hcl)?" .in) ]][[ .in ]][[ else ]]output/[[ .in | path.Dir ]]/[[ .in | path.Base | regexp.Replace "^([^\\.]+)\\.(.*)$" (printf "%s%s.%s" "$1" .ctx.consul.suffix "$2") ]][[ end ]]')
GOMPLATE_OUT_ARGS+=(--output-map)
GOMPLATE_OUT_ARGS+=('[[ if (regexp.Match ".*\\.nomad(\\.hcl)?" .in) ]][[ .in ]][[ else ]]output/[[ .in | path.Dir ]]/[[ .in | path.Base | regexp.Replace "^([^\\.]+)\\.(.*)$" (printf "%s%s.%s" "$1" .ctx.consul.suffix "$2") ]][[ end ]]')
echo
@ -611,13 +609,13 @@ ctctl_render_templates(){
gomplate "${GOMPLATE_COMMON_ARGS[@]}" --context ${VAR_FILES} -f ${CONFIG} -o ${CONFIG}
GOMPLATE_COMMON_ARGS+=(--context .=file://${CONFIG})
echo "Redering bundles with gomplate ${GOMPLATE_COMMON_ARGS[@]} ${GOMPLATE_BUNDLE_ARGS[@]}"
echo "Redering bundles with gomplate ${GOMPLATE_COMMON_ARGS[@]} ${GOMPLATE_BUNDLE_ARGS[@]} ${GOMPLATE_OUT_ARGS[@]}"
# First, cleanup any previously rendered files
rm -rf output ./*.nomad ./*.nomad.hcl
# Now render the bundle files
gomplate "${GOMPLATE_COMMON_ARGS[@]}" "${GOMPLATE_BUNDLE_ARGS[@]}"
gomplate "${GOMPLATE_COMMON_ARGS[@]}" "${GOMPLATE_BUNDLE_ARGS[@]}" "${GOMPLATE_OUT_ARGS[@]}"
for IMGDIR in $(find . -name images -type d); do
for DOCKER_IMAGE in $(find ${IMGDIR} -mindepth 1 -maxdepth 1 -type d); do
@ -638,7 +636,57 @@ ctctl_render_templates(){
# Run prep.d scripts
ctctl_handle_render_scripts
# And now relete the config
# And now delete the merged config
rm -f ${CONFIG}
if [ -n "${CTCTL_RENDER_EXAMPLE}" -a "${CTCTL_RENDER_EXAMPLE}" = "true" ]; then
echo "Rendering example job with bundle variables only"
I=0
local VAR_FILES=""
for FILE in bundles/${NAME}/variables.yml \
bundles/${NAME}/variables.yaml \
bundles/*/variables.yml \
bundles/*/variables.yaml; do
if [ -e ${FILE} ]; then
if [ $I -eq 0 ]; then
VAR_FILES+='.=merge:'
else
VAR_FILES+='|'
fi
VAR_FILES+="${FILE}"
I=$((I+1))
fi
done
gomplate "${GOMPLATE_COMMON_ARGS[@]}" --context "${VAR_FILES[@]}" -i "[[ . | toYAML ]]" > ${CONFIG}
# And render it again so we can replace any templated values in the config itself
gomplate "${GOMPLATE_COMMON_ARGS[@]}" --context ${VAR_FILES} -f ${CONFIG} -o ${CONFIG}
# Render it twice so we can reference 2 levels of templates in variables
gomplate "${GOMPLATE_COMMON_ARGS[@]}" --context ${VAR_FILES} -f ${CONFIG} -o ${CONFIG}
#GOMPLATE_COMMON_ARGS+=(--context .=file://${CONFIG})
# First, cleanup any previously rendered files
rm -rf bundles/${NAME}/example/*
# Now render the bundle files
gomplate "${GOMPLATE_COMMON_ARGS[@]}" "${GOMPLATE_BUNDLE_ARGS[@]}" --output-dir=bundles/${NAME}/example
if [ -d "bundles/${NAME}/images" ]; then
for DOCKER_IMAGE in $(find bundles/${NAME}/images -mindepth 1 -maxdepth 1 -type d); do
echo "Redering example Docker image $(basename ${DOCKER_IMAGE})"
gomplate "${GOMPLATE_COMMON_ARGS[@]}" --input-dir ${DOCKER_IMAGE} --exclude resources/** --exclude root/** --output-dir bundles/${NAME}/example/images/$(basename ${DOCKER_IMAGE})/
for ROOT in resources root; do
if [ -d "${DOCKER_IMAGE}/${ROOT}" ]; then
cp -r "${DOCKER_IMAGE}/${ROOT}" bundles/${NAME}/example/images/$(basename ${DOCKER_IMAGE})/
fi
done
done
fi
fi
# Format example job files
find ./bundles/${NAME}/example -maxdepth 1 -type f \( -name \*nomad.hcl -o -name \*.nomad \) -exec nomad fmt {} \;
rm -f ${CONFIG}
done
else

View File

@ -24,3 +24,6 @@
# Alternatively, you can use a command to fetch the password on demande
# LOKI_PWD_CMD="vault read -field pwd kv/admin/loki"
# You can render a bundle in the example directory of the bundle itself
# using only the default variables
# CTCTL_RENDER_EXAMPLE=true