Prefix functions with ctctl_

This commit is contained in:
Daniel Berteaud 2023-09-06 22:47:45 +02:00
parent 6245d801e1
commit 46292743d0
1 changed files with 90 additions and 90 deletions

180
ctctl
View File

@ -1,7 +1,7 @@
#!/usr/bin/env bash
# Print current environnement
current_env(){
ctctl_current_env(){
if [ -z "${CTCTL_DOMAIN}" ]; then
echo "Unknown container domain"
kill -INT $$
@ -14,7 +14,7 @@ current_env(){
echo "Namespace: ${CTCTL_ENV}"
}
check_env() {
ctctl_check_env() {
if [ -n "${CTCTL_DOMAIN}" -a -n "${CTCTL_ENV}" ]; then
echo 1
else
@ -24,10 +24,10 @@ check_env() {
# Run a shell in a container
# TODO : to implement
enter_ct(){
ctctl_enter_ct(){
echo "Select the job"
select J in $(get_job_list); do
if [ "${REPLY}" -ge 1 ] && [ "${REPLY}" -le $(get_job_list | wc -w) ]; then
select J in $(ctctl_get_job_list); do
if [ "${REPLY}" -ge 1 ] && [ "${REPLY}" -le $(ctctl_get_job_list | wc -w) ]; then
JOB=${J}
break
else
@ -37,7 +37,7 @@ enter_ct(){
}
load_config(){
ctctl_load_config(){
if [ -n "${CTCTL_DOMAIN}" -a -n "${CTCTL_ENV}" ]; then
# Load env configuration
if [ -e ~/.ctctl/${TARGET_DOM}/${CTCTL_ENV}.conf ]; then
@ -56,13 +56,13 @@ load_config(){
}
# Switch to a target environment (either from no current, or from another current env)
switch_env(){
ctctl_switch_env(){
TARGET_DOM=$1
TARGET_NAMESPACE=$2
if [ -z "${TARGET_DOM}" ]; then
echo "Select the container install you want to work on"
TARGET_DOM=$(ls_env | ${FZF_CMD})
TARGET_DOM=$(ctctl_ls_env | ${FZF_CMD})
fi
if [ ! -e ~/.ctctl/${TARGET_DOM}/ctctl.conf ]; then
@ -90,14 +90,14 @@ switch_env(){
fi
# Authenticate
auth_env
ctctl_auth_env
if [ -z "${TARGET_NAMESPACE}" ]; then
if [ $(ls_namespace | wc -w) -eq 1 ]; then
TARGET_NAMESPACE=$(ls_namespace)
if [ $(ctctl_ls_namespace | wc -w) -eq 1 ]; then
TARGET_NAMESPACE=$(ctctl_ls_namespace)
else
echo "Select the namespace you are working in"
TARGET_NAMESPACE=$(ls_namespace | ${FZF_CMD})
TARGET_NAMESPACE=$(ctctl_ls_namespace | ${FZF_CMD})
fi
fi
export CTCTL_ENV=${TARGET_NAMESPACE}
@ -108,7 +108,7 @@ switch_env(){
}
# Check if we have a valid token for vault
check_vault_token(){
ctctl_check_vault_token(){
vault token lookup > /dev/null 2>&1
if vault token lookup > /dev/null 2>&1; then
echo 1
@ -128,7 +128,7 @@ check_vault_token(){
}
# Check if we have a valid token for consul
check_consul_token(){
ctctl_check_consul_token(){
CONSUL_TOKEN_VALID=0
if [ -n "${CONSUL_HTTP_TOKEN}" ]; then
consul acl token read -self > /dev/null 2>&1
@ -143,7 +143,7 @@ check_consul_token(){
}
# Check if we have a valid token for nomad
check_nomad_token(){
ctctl_check_nomad_token(){
if [ -n "${NOMAD_TOKEN}" ]; then
nomad acl token self > /dev/null 2>&1
if [ $? == 0 ]; then
@ -157,12 +157,12 @@ check_nomad_token(){
}
# Auth on vault, consul and nomad on the current env
auth_env(){
ctctl_auth_env(){
if [ -z "${CTCTL_DOMAIN}" ]; then
echo "Unknown environment"
kill -INT $$
fi
if [ "$(check_vault_token)" != "1" ]; then
if [ "$(ctctl_check_vault_token)" != "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/\@.*//')}} || kill -INT $$)
echo "Logged on vault successfuly"
@ -197,7 +197,7 @@ auth_env(){
consul-template -config ~/.ctctl/${CTCTL_DOMAIN}/nomad/consul-template.hcl -once
# Check if we have a valid nomad token already
if [ "$(check_nomad_token)" != "1" ]; then
if [ "$(ctctl_check_nomad_token)" != "1" ]; then
echo "Fecthing a Nomad token from vault"
NOMAD_CREDS=$(vault read -format=json ${VAULT_PREFIX:-}nomad/creds/${NOMAD_ROLE})
export NOMAD_TOKEN=$(echo -n ${NOMAD_CREDS} | jq -r .data.secret_id)
@ -208,7 +208,7 @@ auth_env(){
vault lease renew ${NOMAD_LEASE} >/dev/null
fi
# Check if we have a valid consul token already
if [ "$(check_consul_token)" != "1" ]; then
if [ "$(ctctl_check_consul_token)" != "1" ]; then
echo "Fetching a Consul token from vault"
CONSUL_CREDS=$(vault read -format=json ${VAULT_PREFIX:-}consul/creds/${CONSUL_ROLE})
export CONSUL_HTTP_TOKEN=$(echo -n ${CONSUL_CREDS} | jq -r .data.token)
@ -219,10 +219,10 @@ auth_env(){
vault lease renew ${CONSUL_LEASE} >/dev/null
fi
load_config
ctctl_load_config
}
renew_leases(){
ctctl_renew_leases(){
# Renew vault token
[ -n "${VAULT_TOKEN}" ] && vault token renew >/dev/null
[ -n "${NOMAD_LEASE}" ] && vault lease renew ${NOMAD_LEASE} >/dev/null
@ -230,7 +230,7 @@ renew_leases(){
}
# Logout from the current env
logout_env(){
ctctl_logout_env(){
if [ -z "${CTCTL_DOMAIN}" ]; then
echo "Unknown environment"
kill -INT $$
@ -244,32 +244,32 @@ logout_env(){
}
# List available env
ls_env(){
ctctl_ls_env(){
find ~/.ctctl/ -name ctctl.conf | xargs dirname | xargs basename -a
}
# List available namespaces
ls_namespace(){
ctctl_ls_namespace(){
nomad namespace list -json | jq -r ".[] | .Name"
}
# List buildable Docker images
ls_build_docker_images(){
ctctl_ls_build_docker_images(){
(for JOB in $(find . -maxdepth 1 \( -name \*.nomad -o -name \*.nomad.hcl \)); do
nomad run -output $JOB | jq '.Job.TaskGroups' | jq '.[] | .Tasks' | jq -r '.[] | .Config.image' 2>/dev/null
done) | grep -E "${CTCTL_DOCKER_BUILD_REPO_REGEX:-docker-repo.ehtrace.com}" | sort -u
}
# Load policies for vault, Consul and Nomad
load_policies(){
if [ "$(check_env)" = "0" ]; then
ctctl_load_policies(){
if [ "$(ctctl_check_env)" = "0" ]; then
echo "Not currently in a valid env. Run ctctl (with no argument) and select your env first"
kill -INT $$
fi
for DIR in ./output ./; do
if [ -d "${DIR}/vault/policies" ]; then
if [ "$(check_vault_token)" != "1" ]; then
if [ "$(ctctl_check_vault_token)" != "1" ]; then
echo "No valid vault token. You have to authenticate first"
kill -INT $$
fi
@ -280,11 +280,11 @@ load_policies(){
fi
PNAME=$(basename ${PFILE} .hcl)
echo "Loading vault policy ${PNA}"
replace_conf_var ${PFILE} | vault policy write ${PNAME} -
ctctl_replace_conf_var ${PFILE} | vault policy write ${PNAME} -
done
fi
if [ -d "${DIR}/consul/policies" ]; then
if [ "$(check_consul_token)" != "1" ]; then
if [ "$(ctctl_check_consul_token)" != "1" ]; then
echo "No valid consul token. You have to authenticate first"
kill -INT $$
fi
@ -299,15 +299,15 @@ load_policies(){
# so we need to detect if the policy already exists
if [ "$(echo ${CONSUL_CUR_POLICIES} | jq -r '.[] | select(.Name=='\"${PNAME}\"') | .Name')" == "${PNAME}" ]; then
echo "Updating consul policy ${PNAME}"
replace_conf_var ${PFILE} | consul acl policy update -name=${PNAME} -rules=-
ctctl_replace_conf_var ${PFILE} | consul acl policy update -name=${PNAME} -rules=-
else
echo "Adding new consul policy ${PNAME}"
replace_conf_var ${PFILE} | consul acl policy create -name=${PNAME} -rules=-
ctctl_replace_conf_var ${PFILE} | consul acl policy create -name=${PNAME} -rules=-
fi
done
fi
if [ -d "${DIR}/nomad/policies" ]; then
if [ "$(check_nomad_token)" != "1" ]; then
if [ "$(ctctl_check_nomad_token)" != "1" ]; then
echo "No valid nomad token. You have to authenticate first"
kill -INT $$
fi
@ -318,18 +318,18 @@ load_policies(){
continue
fi
echo "Loading Nomad policy ${PNAME}"
replace_conf_var ${PFILE} | nomad acl policy apply ${PNAME} -
ctctl_replace_conf_var ${PFILE} | nomad acl policy apply ${PNAME} -
done
fi
done
}
# Load consul config
load_consul_conf(){
ctctl_load_consul_conf(){
for DIR in ./output ./; do
if [ -d "${DIR}/consul/config" ]; then
if [ "$(check_consul_token)" != "1" ]; then
if [ "$(ctctl_check_consul_token)" != "1" ]; then
echo "No valid consul token. You have to authenticate first"
kill -INT $$
fi
@ -342,7 +342,7 @@ load_consul_conf(){
fi
echo "Loading consul conf from ${FILE}"
TEMP=$(mktemp)
replace_conf_var ${FILE} > ${TEMP}
ctctl_replace_conf_var ${FILE} > ${TEMP}
consul config write ${TEMP}
rm -f ${TEMP}
done
@ -358,7 +358,7 @@ load_consul_conf(){
fi
echo "Loading consul conf from ${FILE}"
TEMP=$(mktemp)
replace_conf_var ${FILE} > ${TEMP}
ctctl_replace_conf_var ${FILE} > ${TEMP}
consul config write ${TEMP}
rm -f ${TEMP}
done
@ -369,10 +369,10 @@ load_consul_conf(){
}
# Build all images for the current project
build_required_images(){
for DOCKER_IMAGE in $(ls_build_docker_images); do
ctctl_build_required_images(){
for DOCKER_IMAGE in $(ctctl_ls_build_docker_images); do
if ! docker manifest inspect ${DOCKER_IMAGE} > /dev/null 2>&1; then
build_image ${DOCKER_IMAGE}
ctctl_build_image ${DOCKER_IMAGE}
else
echo "Image ${DOCKER_IMAGE} already available"
fi
@ -380,15 +380,15 @@ build_required_images(){
}
# Build selected images
build_selected_images(){
ctctl_build_selected_images(){
local NO_CACHE=$1
for DOCKER_IMAGE in $(ls_build_docker_images | ${FZF_CMD} -m); do
build_image "${DOCKER_IMAGE}" ${NO_CACHE}
for DOCKER_IMAGE in $(ctctl_ls_build_docker_images | ${FZF_CMD} -m); do
ctctl_build_image "${DOCKER_IMAGE}" ${NO_CACHE}
done
}
# Build a single image
build_image(){
ctctl_build_image(){
local DOCKER_IMAGE=$1
local NO_CACHE=$2
export DOCKER_BUILDKIT=1
@ -422,7 +422,7 @@ build_image(){
}
# Run all executable in the prep.d directory
handle_prep_scripts(){
ctctl_handle_prep_scripts(){
for DIR in ./output ./; do
if [ -d "${DIR}/prep.d" ]; then
for H in $(find ${DIR}/prep.d -type f -o -type l | sort); do
@ -438,7 +438,7 @@ handle_prep_scripts(){
}
# Update ctctl bundles with git
update_submodules(){
ctctl_update_submodules(){
if [ -e ctctl.bundle.conf ]; then
set -o allexport
source ./ctctl.bundle.conf
@ -464,7 +464,7 @@ update_submodules(){
}
# Render templates using gomplate (or levant for backward compat)
render_templates(){
ctctl_render_templates(){
# If a ctctl.bundle.conf file exist, use the new gomplate rendering method
if [ -e ctctl.bundle.conf ]; then
@ -482,7 +482,7 @@ render_templates(){
echo "Working on the ${NAME} bundle"
if [ ! -d bundles/${NAME} ]; then
update_submodules
ctctl_update_submodules
fi
# Use [[ and ]] so it won't clash with consul-template fragments
@ -558,7 +558,7 @@ render_templates(){
find ./ -maxdepth 1 -type f \( -name \*nomad.hcl -o -name \*.nomad \) -exec nomad fmt {} \;
# Run prep.d scripts
handle_prep_scripts
ctctl_handle_prep_scripts
done
# Cleanup variables
@ -569,10 +569,10 @@ render_templates(){
else
# backward compatible, levant based rendering
MERGED_CONF=$(mktemp tmp.XXXXXXXX.yml)
get_merged_conf > ${MERGED_CONF}
ctctl_get_merged_conf > ${MERGED_CONF}
# TODO : handle prep scripts with the new bundles system
handle_prep_scripts ${MERGED_CONF}
ctctl_handle_prep_scripts ${MERGED_CONF}
for TEMPLATE in $(find . -type f -name \*.tpl ! -path "*templates/*"); do
local DIR=$(dirname ${TEMPLATE})
@ -589,13 +589,13 @@ render_templates(){
}
# Print Consul and Nomad tokens (not vault, for security reasons)
print_tokens(){
if [ "$(check_nomad_token)" == "1" ]; then
ctctl_print_tokens(){
if [ "$(ctctl_check_nomad_token)" == "1" ]; then
echo "Nomad token: ${NOMAD_TOKEN}"
else
echo "No valid Nomad token, you should auth with ctctl auth"
fi
if [ "$(check_consul_token)" == "1" ]; then
if [ "$(ctctl_check_consul_token)" == "1" ]; then
echo "Consul token: ${CONSUL_HTTP_TOKEN}"
else
echo "No valid Consul token, you should auth with ctctl auth"
@ -603,7 +603,7 @@ print_tokens(){
}
# Follow current jobs logs
job_logs(){
ctctl_job_logs(){
# Remove the first arg passed to ctctl, which is logs
shift
local SELECTOR
@ -627,7 +627,7 @@ job_logs(){
${LOGCLI_CMD} $@
else
# Exclude connect-proxy logs as it's often not wanted
SELECTOR='{job=~"'$(get_job_list | sed 's/\s/|/g')'", task!~"connect-proxy-.+|tls-proxy|metrics-proxy"}'
SELECTOR='{job=~"'$(ctctl_get_job_list | sed 's/\s/|/g')'", task!~"connect-proxy-.+|tls-proxy|metrics-proxy"}'
echo "Running ${LOGCLI_CMD} $@ ${SELECTOR}"
${LOGCLI_CMD} $@ "${SELECTOR}"
fi
@ -637,7 +637,7 @@ job_logs(){
### Helpers ###
# Merge the configuration files for the current env and return the result (as string)
get_merged_conf() {
ctctl_get_merged_conf() {
CONF_FILES=""
if [ -e "./vars/defaults.yml" ]; then
CONF_FILES="./vars/defaults.yml"
@ -661,9 +661,9 @@ get_merged_conf() {
# Replace ${local.conf.foo} or ${foo} with the value of foo from the various merged configuration files
# This is used to have policies (vault, consul, nomad) and config (consul intentions etc.) with variables
replace_conf_var() {
ctctl_replace_conf_var() {
MERGED_CONF=$(mktemp)
get_merged_conf > $MERGED_CONF
ctctl_get_merged_conf > $MERGED_CONF
RES=$(cat $1 | \
# Replace ${local.conf.foo} or ${foo} with the value of foo from the various merged configuration files \
# This is used to have policies (vault, consul, nomad) and config (consul intentions etc.) with variables \
@ -676,12 +676,12 @@ replace_conf_var() {
}
# Get a value from the conf
get_conf(){
get_merged_conf | yq ".$1"
ctctl_get_conf(){
ctctl_get_merged_conf | yq ".$1"
}
# Return a space separated list of jobs the current dir
get_job_list(){
ctctl_get_job_list(){
local JOBS=""
for JOBFILE in $(find . -maxdepth 1 \( -name \*.nomad -o -name \*.nomad.hcl \)); do
JOBS="${JOBS} $(nomad run -output ${JOBFILE} | jq -r '.Job.Name')"
@ -693,59 +693,59 @@ FZF_CMD=${CTCTL_FZF_CMD:-fzf --height=~10% --cycle --bind 'space:toggle' --marke
case $1 in
current)
current_env
renew_leases
ctctl_current_env
ctctl_renew_leases
;;
auth)
auth_env
ctctl_auth_env
;;
disconnect)
logout_env
ctctl_logout_env
;;
ls|list)
ls_env
renew_leases
ctctl_ls_env
ctctl_renew_leases
;;
render)
render_templates
renew_leases
ctctl_render_templates
ctctl_renew_leases
;;
prep)
update_submodules
render_templates
load_policies
load_consul_conf
build_required_images
renew_leases
ctctl_update_submodules
ctctl_render_templates
ctctl_load_policies
ctctl_load_consul_conf
ctctl_build_required_images
ctctl_renew_leases
;;
build)
build_selected_images
renew_leases
ctctl_build_selected_images
ctctl_renew_leases
;;
build-no-cache)
build_selected_images "no-cache"
renew_leases
ctctl_build_selected_images "no-cache"
ctctl_renew_leases
;;
tokens)
print_tokens
renew_leases
ctctl_print_tokens
ctctl_renew_leases
;;
logs)
renew_leases
job_logs "$@"
ctctl_renew_leases
ctctl_job_logs "$@"
;;
conf)
renew_leases
get_merged_conf
ctctl_renew_leases
ctctl_get_merged_conf
;;
sh)
enter_ct
ctctl_enter_ct
;;
switch)
shift
switch_env "$@"
ctctl_switch_env "$@"
;;
*)
switch_env "$@"
ctctl_switch_env "$@"
;;
esac