Support renaming files with the configured .instance name

This commit is contained in:
Daniel Berteaud 2024-01-26 22:10:25 +01:00
parent ea3461c523
commit 93f06c39a2
1 changed files with 15 additions and 8 deletions

23
ctctl
View File

@ -157,7 +157,8 @@ ctctl_auth_env(){
fi
if [ "${NEED_LOGIN}" = "1" ]; then
echo "You're not connected on vault. Please enter your account password"
export VAULT_TOKEN=$(vault login -field=token ${VAULT_AUTH_CONFIG:--method=ldap username=${CTCTL_USER:-$(whoami | sed -r 's/\@.*//')}} || ctctl_exit)
local CTCTL_VAULT_AUTH="vault login -field=token ${VAULT_AUTH_CONFIG:--method=ldap username=${CTCTL_USER:-$(whoami | sed -r 's/\@.*//')}}"
export VAULT_TOKEN=$(${CTCTL_VAULT_AUTH} || ctctl_exit)
echo "Logged on vault successfuly"
else
echo "Your vault token is valid"
@ -551,6 +552,12 @@ ctctl_render_templates(){
local GOMPLATE_COMMON_ARGS=(--left-delim '[[' --right-delim ']]')
# Setup a vault datasource
GOMPLATE_COMMON_ARGS+=(-d vault=vault:///)
# And another datasource for the config of the bundle
if [ -e "bundles/${NAME}/variables.yml" ]; then
GOMPLATE_COMMON_ARGS+=(-d bundle=./bundles/${NAME}/variables.yml)
elif [ -e "bundles/${NAME}/variables.yaml" ]; then
GOMPLATE_COMMON_ARGS+=(-d bundle=./bundles/${NAME}/variables.yaml)
fi
# Declare named, external templates
if [ -d "bundles/${NAME}/templates" ]; then
@ -575,13 +582,6 @@ ctctl_render_templates(){
local GOMPLATE_BUNDLE_ARGS=(--input-dir "bundles/${NAME}")
# 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
local 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
# Now, render the merged config in a temp file
@ -619,6 +619,13 @@ ctctl_render_templates(){
done
GOMPLATE_COMMON_ARGS+=(--context .=file://${CONFIG})
# 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
# - Rename all files by replacing the default .instance with the configured one
# - Put job files in the current dir for conveniance, and everything else in the output dir
local GOMPLATE_OUT_ARGS=(--output-map)
GOMPLATE_OUT_ARGS+=('[[ if (regexp.Match ".*\\.nomad(\\.hcl)?" .in) ]][[ .in | regexp.Replace (ds "bundle").instance .ctx.instance ]][[ else ]]output/[[ .in | path.Dir ]]/[[ .in | path.Base | regexp.Replace (ds "bundle").instance .ctx.instance | regexp.Replace "^([^\\.]+)\\.(.*)$" (printf "%s%s.%s" "$1" .ctx.consul.suffix "$2") ]][[ end ]]')
echo "Redering bundles with gomplate ${GOMPLATE_COMMON_ARGS[@]} ${GOMPLATE_BUNDLE_ARGS[@]} ${GOMPLATE_OUT_ARGS[@]}"
# Now render the bundle files