ctctl/ctctl

605 lines
17 KiB
Bash
Executable File

#!/usr/bin/env bash
# Print current environnement
current_env(){
if [ -z "${CTCTL_DOMAIN}" ]; then
echo "Unknown container domain"
kill -INT $$
fi
echo "Cluster: ${CTCTL_DOMAIN}"
if [ -z "${CTCTL_ENV}" ]; then
echo "Unknown container environment"
kill -INT $$
fi
echo "Namespace: ${CTCTL_ENV}"
}
check_env() {
if [ -n "${CTCTL_DOMAIN}" -a -n "${CTCTL_ENV}" ]; then
echo 1
else
echo 0
fi
}
# Run a shell in a container
# TODO : to implement
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
JOB=${J}
break
else
echo "Invalid selection"
fi
done
}
load_config(){
if [ -n "${CTCTL_DOMAIN}" -a -n "${CTCTL_ENV}" ]; then
# Load env configuration
if [ -e ~/.ctctl/${TARGET_DOM}/${CTCTL_ENV}.conf ]; then
set -o allexport
source ~/.ctctl/${TARGET_DOM}/${CTCTL_ENV}.conf
set +o allexport
fi
# Load post login configuration
if [ -e ~/.ctctl/${CTCTL_DOMAIN}/ctctl.local.conf ]; then
set -o allexport
source ~/.ctctl/${CTCTL_DOMAIN}/ctctl.local.conf
set +o allexport
fi
fi
}
# Switch to a target environment (either from no current, or from another current env)
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})
fi
if [ ! -e ~/.ctctl/${TARGET_DOM}/ctctl.conf ]; then
echo "Env ${TARGET_DOM} doesn't exist"
kill -INT $$
fi
# Clean any variable
for VAR in $(printenv | perl -ne '/^((CTCTL|CONSUL|VAULT|NOMAD)_[^=]+)=.*/ && print "$1\n"'); do
unset ${VAR}
done
export CTCTL_DOMAIN=${TARGET_DOM}
# Load default config
set -o allexport
source ~/.ctctl/${CTCTL_DOMAIN}/ctctl.conf
set +o allexport
# Load pre login env configuration
if [ -e ~/.ctctl/${CTCTL_DOMAIN}/pre-login.conf ]; then
set -o allexport
source ~/.ctctl/${CTCTL_DOMAIN}/pre-login.conf
set +o allexport
fi
# Authenticate
auth_env
local NAMESPACE_LIST=$(ls_namespace)
if [ -z "${TARGET_NAMESPACE}" ]; then
if [ $(echo -n ${NAMESPACE_LIST} | wc -w) -eq 1 ]; then
TARGET_NAMESPACE=${NAMESPACE_LIST}
else
echo "Select the namespace you are working in"
TARGET_NAMESPACE=$(ls_namespace | ${FZF_CMD})
fi
fi
export CTCTL_ENV=${TARGET_NAMESPACE}
# TODO : decide if we keep NOMAD_VAR_env
export NOMAD_VAR_env=${TARGET_NAMESPACE}
export NOMAD_NAMESPACE=${TARGET_NAMESPACE}
}
# Check if we have a valid token for vault
check_vault_token(){
vault read auth/token/lookup-self > /dev/null 2>&1
if [ $? == 0 ]; then
echo 1
else
echo 0
fi
}
# Check if we have a valid token for consul
check_consul_token(){
CONSUL_TOKEN_VALID=0
if [ -n "${CONSUL_HTTP_TOKEN}" ]; then
consul acl token read -self > /dev/null 2>&1
if [ $? == 0 ]; then
echo 1
else
echo 0
fi
else
echo 0
fi
}
# Check if we have a valid token for nomad
check_nomad_token(){
if [ -n "${NOMAD_TOKEN}" ]; then
nomad acl token self > /dev/null 2>&1
if [ $? == 0 ]; then
echo 1
else
echo 0
fi
else
echo 0
fi
}
# Auth on vault, consul and nomad on the current env
auth_env(){
if [ -z "${CTCTL_DOMAIN}" ]; then
echo "Unknown environment"
kill -INT $$
fi
if [ "$(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"
else
echo "Your vault token is valid"
vault token renew > /dev/null 2>&1
fi
VAULT_TOKEN_INFO=$(vault read -format=json auth/token/lookup-self)
# TODO make the role selection more generic
if [ "$(echo $VAULT_TOKEN_INFO | jq '.data.policies | any(. == "admin-policy" or .== "admin")')" == "true" ]; then
NOMAD_ROLE=admin
CONSUL_ROLE=admin
else
NOMAD_ROLE=user
CONSUL_ROLE=user
fi
# Root CA
vault read -field certificate pki/root/cert/ca > ~/.ctctl/${CTCTL_DOMAIN}/root_ca.crt
# Consul certificate
# Get/renew cert if required.
# Note 1: as the template is using pkiCert, the cert won't be renewed, unless necessary
# Note 2: don't pass CONSUL_CLIENT_CERT CONSUL_CLIENT_KEY and CONSUL_CACERT because they would prevent consul-template from starting
# to get/renew the cert if they are absent, or expired
env -u CONSUL_CLIENT_CERT \
-u CONSUL_CLIENT_KEY \
-u CONSUL_CACERT \
consul-template -config ~/.ctctl/${CTCTL_DOMAIN}/consul/consul-template.hcl -once
# Get/renew cert for Nomad now
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
echo "Fecthing a Nomad token from vault"
NOMAD_CREDS=$(vault read -format=json nomad/creds/${NOMAD_ROLE})
export NOMAD_TOKEN=$(echo -n ${NOMAD_CREDS} | jq -r .data.secret_id)
export NOMAD_LEASE=$(echo -n ${NOMAD_CREDS} | jq -r .lease_id)
unset NOMAD_CREDS
else
echo "Nomad token is valid, renewing lease"
vault lease renew ${NOMAD_LEASE} >/dev/null
fi
# Check if we have a valid consul token already
if [ "$(check_consul_token)" != "1" ]; then
echo "Fetching a Consul token from vault"
CONSUL_CREDS=$(vault read -format=json consul/creds/${CONSUL_ROLE})
export CONSUL_HTTP_TOKEN=$(echo -n ${CONSUL_CREDS} | jq -r .data.token)
export CONSUL_LEASE=$(echo -n ${CONSUL_CREDS} | jq -r .lease_id)
unset CONSUL_CREDS
else
echo "Consul token is valid, renewing lease"
vault lease renew ${CONSUL_LEASE} >/dev/null
fi
load_config
}
renew_leases(){
# Renew vault token
[ -n "${VAULT_TOKEN}" ] && vault token renew >/dev/null
[ -n "${NOMAD_LEASE}" ] && vault lease renew ${NOMAD_LEASE} >/dev/null
[ -n "${CONSUL_LEASE}" ] && vault lease renew ${CONSUL_LEASE} > /dev/null
}
# Logout from the current env
logout_env(){
if [ -z "${CTCTL_DOMAIN}" ]; then
echo "Unknown environment"
kill -INT $$
fi
echo "Disconecting from ${CTCTL_DOMAIN} environment"
vault token revoke -self
for VAR in $(printenv | perl -ne '/^((CTCTL|CONSUL|VAULT|NOMAD|LOKI)_[^=]+)=.*/ && print "$1\n"'); do
unset $VAR
done
rm -f ~/.vault-token
}
# List available env
ls_env(){
find ~/.ctctl/ -name ctctl.conf | xargs dirname | xargs basename -a
}
# List available namespaces
ls_namespace(){
nomad namespace list -json | jq -r ".[] | .Name"
}
# List buildable Docker images
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 "${DOCKER_BUILD_REPO_REGEX:-docker-repo.ehtrace.com}" | sort -u
}
# Load policies for vault, Consul and Nomad
load_policies(){
if [ "$(check_env)" = "0" ]; then
echo "Not currently in a valid env. Run ctctl (with no argument) and select your env first"
kill -INT $$
fi
if [ -d "./vault/policies" ]; then
if [ "$(check_vault_token)" != "1" ]; then
echo "No valid vault token. You have to authenticate first"
kill -INT $$
fi
for PFILE in $(ls ./vault/policies/*.hcl 2>/dev/null); do
PNAME=$(basename ${PFILE} .hcl)$(get_conf "env_suffix")
echo "Loading vault policy ${PNA}"
replace_conf_var ${PFILE} | vault policy write ${PNAME} -
done
fi
if [ -d "./consul/policies" ]; then
if [ "$(check_consul_token)" != "1" ]; then
echo "No valid consul token. You have to authenticate first"
kill -INT $$
fi
CONSUL_CUR_POLICIES=$(consul acl policy list -format=json)
for PFILE in $(ls ./consul/policies/*.hcl 2>/dev/null); do
PNAME=$(basename ${PFILE} .hcl)$(get_conf "env_suffix")
# Consul do not use the same command to create a new policy and to update an existing one
# 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=-
else
echo "Adding new consul policy ${PNAME}"
replace_conf_var ${PFILE} | consul acl policy create -name=${PNAME} -rules=-
fi
done
fi
if [ -d "./nomad/policies" ]; then
if [ "$(check_nomad_token)" != "1" ]; then
echo "No valid nomad token. You have to authenticate first"
kill -INT $$
fi
for PFILE in $(ls ./nomad/policies/*.hcl 2>/dev/null); do
PNAME=$(basename ${PFILE} .hcl)$(get_conf "env_suffix")
echo "Loading Nomad policy ${PNAME}"
replace_conf_var ${PFILE} | nomad acl policy apply ${PNAME} -
done
fi
}
# Load consul services
load_consul_services(){
if [ -d "./consul/services" ]; then
if [ "$(check_consul_token)" != "1" ]; then
echo "No valid consul token. You have to authenticate first"
kill -INT $$
fi
for FILE in $(ls ./consul/services/*.hcl 2>/dev/null); do
echo "Registering service from ${FILE}"
TEMP=$(mktemp).hcl
replace_conf_var ${FILE} > ${TEMP}
consul services register ${TEMP}
rm -f ${TEMP}
done
fi
}
# Load consul config
load_consul_conf(){
if [ -d "./consul/config" ]; then
if [ "$(check_consul_token)" != "1" ]; then
echo "No valid consul token. You have to authenticate first"
kill -INT $$
fi
# Note : service-defaults should be loaded before the others
# but that should be the case
for FILE in $(ls ./consul/config/*.hcl 2>/dev/null); do
echo "Loading consul conf from ${FILE}"
TEMP=$(mktemp)
replace_conf_var ${FILE} > ${TEMP}
consul config write ${TEMP}
rm -f ${TEMP}
done
# Support storing consul config in subdir eg consul/config/service-defaults/foo.hcl
# Or you can even omit service and use consul/config/defaults/bar.hcl, consul/config/intentions/bar.hcl
for KIND in service-defaults defaults service-intentions intentions service-router router service-resolver resolver; do
if [ -d ./consul/config/${KIND} ]; then
for FILE in $(ls ./consul/config/${KIND}/*.hcl 2>/dev/null); do
echo "Loading consul conf from ${FILE}"
TEMP=$(mktemp)
replace_conf_var ${FILE} > ${TEMP}
consul config write ${TEMP}
rm -f ${TEMP}
done
fi
done
fi
}
# Build all images for the current project
build_required_images(){
for DOCKER_IMAGE in $(ls_build_docker_images); do
if ! docker manifest inspect ${DOCKER_IMAGE} > /dev/null 2>&1; then
build_image ${DOCKER_IMAGE}
else
echo "Image ${DOCKER_IMAGE} already available"
fi
done
}
# Build selected images
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}
done
}
# Build a single image
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
local DIR=$(echo -n ${DOCKER_IMAGE} | sed -E 's/.+\/([^\/]+):.*/\1/')
export DOCKER_IMAGE=${DOCKER_IMAGE}
local LATEST=$(echo ${DOCKER_IMAGE} | sed 's/:.*/:latest/')
if [ "${NO_CACHE}" != "" ]; then
NO_CACHE="--no-cache"
else
NO_CACHE=""
fi
docker build ${NO_CACHE} -t ${DOCKER_IMAGE} -t ${LATEST} ${CTCTL_DOCKER_BUILD_OPTS:-} --progress=plain images/$DIR &&
docker push ${DOCKER_IMAGE}
docker push ${LATEST}
unset DOCKER_BUILDKIT
}
# Run all executable in the prep.d directory
handle_prep_scripts(){
if [ -d prep.d ]; then
for H in $(find prep.d -type f -o -type l | sort); do
if [ -x "${H}" ]; then
echo "Running script ${H}"
$H $1
else
echo "Skiping $H (not executable)"
fi
done
fi
}
# Render templates using levant
render_templates(){
MERGED_CONF=$(mktemp tmp.XXXXXXXX.yml)
get_merged_conf > ${MERGED_CONF}
handle_prep_scripts ${MERGED_CONF}
for TEMPLATE in $(find . -type f -name \*.tpl ! -path "*templates/*"); do
DIR=$(dirname ${TEMPLATE})
FILE=$(basename ${TEMPLATE} .tpl)
DEST=${DIR}/${FILE}
echo "Rendering ${TEMPLATE} into ${DEST}"
levant render -var-file ${MERGED_CONF} -log-level=WARN ${TEMPLATE} > ${DEST}
nomad fmt ${DEST}
done
rm -f ${MERGED_CONF}
}
# Print Consul and Nomad tokens (not vault, for security reasons)
print_tokens(){
if [ "$(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
echo "Consul token: ${CONSUL_HTTP_TOKEN}"
else
echo "No valid Consul token, you should auth with ctctl auth"
fi
}
# Handle CSI volumes definition
handle_volumes(){
if [ -d ./volumes ]; then
for FILE in $(ls ./volumes/*.hcl 2>/dev/null); do
echo "Handling volume definition ${FILE}"
# Linstor volumes are just registered, while the other are created
if [[ "$FILE" =~ ^linstor-.* ]]; then
replace_conf_var ${FILE} | nomad volume register -
else
replace_conf_var ${FILE} | nomad volume create -
fi
done
fi
}
# Follow current jobs logs
job_logs(){
# Remove the first arg passed to ctctl, which is logs
shift
local SELECTOR
local LOGCLI_CMD
if [ -z "${LOKI_ADDR}" ]; then
echo "You need to configure loki first (LOKI_ADDR, LOKI_USERNAME and LOKI_PASSWORD or LOKI_PWD_CMD)"
kill -INT $$
fi
if [ -n "${LOKI_PWD_CMD}" ]; then
export LOKI_PASSWORD=$(${LOKI_PWD_CMD})
fi
LOGCLI_CMD="logcli query --include-label=job --include-label=group --include-label=task"
echo -n "$*" | grep -qP '\{.+\}' >/dev/null 2>&1
# If a logcli filter was given, use it. Else, build one for jobs in the current dir
if [ $? == 0 ]; then
echo "Running ${LOGCLI_CMD} $@"
${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"}'
echo "Running ${LOGCLI_CMD} $@ ${SELECTOR}"
${LOGCLI_CMD} $@ "${SELECTOR}"
fi
unset LOKI_PASSWORD
}
### Helpers ###
# Merge the configuration files for the current env and return the result (as string)
get_merged_conf() {
CONF_FILES=""
if [ -e "./vars/defaults.yml" ]; then
CONF_FILES="./vars/defaults.yml"
fi
if [ -e "jobs/common/vars/${CTCTL_ENV}.yml" ]; then
CONF_FILES="${CONF_FILES} jobs/common/vars/${CTCTL_ENV}.yml"
fi
if [ -e "../common/vars/${CTCTL_ENV}.yml" ]; then
CONF_FILES="${CONF_FILES} ../common/vars/${CTCTL_ENV}.yml"
fi
if [ -e "./vars/${CTCTL_ENV}.yml" ]; then
CONF_FILES="${CONF_FILES} ./vars/${CTCTL_ENV}.yml"
fi
if [ "${CONF_FILES}" != "" ]; then
echo "---"
yq ea '. as $item ireduce ({}; . * $item | ... comments="")' $CONF_FILES
else
echo -n ""
fi
}
# 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() {
MERGED_CONF=$(mktemp)
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 \
perl -pe 'sub replace($) { my $val = shift; chomp(my $res = qx(yq .$val '$MERGED_CONF')); return $res; }; s!\$\{(local\.conf\.)?([^\}]+)\}! replace($2) !ge' | \
# Replace $(foo) with the output of foo command, mainly used to fetch secrets from vault \
perl -pe 'sub replace($) { my $val = shift; chomp(my $res = qx($val)); return $res; }; s!\$\(([^\)]+)\)! replace($1) !ge'
)
rm -f $MERGED_CONF
echo "${RES}"
}
# Get a value from the conf
get_conf(){
get_merged_conf | yq ".$1"
}
FZF_CMD=${CTCTL_FZF_CMD:-fzf --height=~10% --cycle --bind 'space:toggle' --marker='*'}
# Return a space separated list of jobs the current dir
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')"
done
echo $JOBS
}
case $1 in
current)
current_env
renew_leases
;;
auth)
auth_env
;;
disconnect)
logout_env
;;
ls|list)
ls_env
renew_leases
;;
prep)
render_templates
load_policies
load_consul_services
load_consul_conf
build_required_images
renew_leases
;;
volumes)
handle_volumes
renew_leases
;;
build)
build_selected_images
renew_leases
;;
build-no-cache)
build_selected_images "no-cache"
renew_leases
;;
tokens)
print_tokens
renew_leases
;;
logs)
renew_leases
job_logs "$@"
;;
conf)
renew_leases
get_merged_conf
;;
sh)
enter_ct
;;
switch)
shift
switch_env "$@"
;;
*)
switch_env "$@"
;;
esac