Add sh, exec logs command (and rename old logs to loki)

This commit is contained in:
Daniel Berteaud 2023-09-29 12:34:54 +02:00
parent dbc00815ae
commit 679f082910
2 changed files with 84 additions and 24 deletions

View File

@ -3,7 +3,7 @@ _ctctl(){
_init_completion || return
case $prev in
auth|disconnect|render|current|tokens|list|build|build-no-cache|logs|conf)
auth|disconnect|render|fetch|prep|current|tokens|list|build|build-no-cache|exec|sh|logs|loki|conf)
return
;;
switch)
@ -12,7 +12,7 @@ _ctctl(){
;;
esac
COMPREPLY=($(compgen -W 'auth switch current disconnect render prep tokens list build build-no-cache logs conf' -- "$cur"))
COMPREPLY=($(compgen -W 'auth switch current disconnect render fetch prep tokens list build build-no-cache exec sh logs loki conf' -- "$cur"))
} &&
complete -F _ctctl ctctl

104
ctctl
View File

@ -22,21 +22,6 @@ check_env() {
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
@ -688,7 +673,7 @@ print_tokens(){
}
# Follow current jobs logs
job_logs(){
loki_logs(){
# Remove the first arg passed to ctctl, which is logs
shift
local SELECTOR
@ -712,7 +697,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=~"'$(ls_jobs | sed -zE 's/\n/|/g' | sed -E 's/\s+//')'", task!~"connect-proxy-.+|tls-proxy|metrics-proxy"}'
echo "Running ${LOGCLI_CMD} $@ ${SELECTOR}"
${LOGCLI_CMD} $@ "${SELECTOR}"
fi
@ -766,12 +751,70 @@ get_conf(){
}
# Return a space separated list of jobs the current dir
get_job_list(){
ls_jobs(){
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')"
echo $(nomad run -output ${JOBFILE} | jq -r '.Job.Name')
done
echo $JOBS
}
# Return a list of allocation for the given job
ls_alloc_of_job(){
local JOB=$1
local IFS=$'\n'
for ALLOC in $(nomad alloc status -json | jq -c ".[] | select(.JobID==\"${JOB}\") | select(.ClientStatus==\"running\")"); do
local ID=$(echo ${ALLOC} | jq -r .ID)
local GROUP=$(echo ${ALLOC} | jq -r .TaskGroup)
local ALLOC_INDEX=$(echo ${ALLOC} | jq -r .Name | sed -E "s/.*\[([0-9]+)\].*/\1/")
local HOST=$(echo ${ALLOC} | jq -r .NodeName)
echo "${ID} (Task group ${GROUP}, allocation index ${ALLOC_INDEX} on host ${HOST}"
done
}
# Return a list of tasks for the given allocation
ls_tasks_of_alloc(){
local ALLOC=$1
local IFS=$'\n'
for TASK in $(nomad alloc status -json ${ALLOC} | jq -r '.TaskResources | keys[]'); do
echo ${TASK}
done
}
# Exec a command in a container
exec_ct(){
local IFS=$'\n'
local CMD=$1
ALLOC=$((for JOB in $(ls_jobs); do ls_alloc_of_job ${JOB}; done) | fzf -m)
ALLOC=$(echo ${ALLOC} | sed -E 's/^([^ \(]+).*/\1/')
TASK=$(ls_tasks_of_alloc ${ALLOC})
if [ $(echo "${TASKS}" | wc -l) -eq 1 ]; then
TASK=${TASKS}
else
TASK=$(echo "${TASKS}" | fzf -m)
fi
echo "Running nomad alloc exec -task ${TASK} ${ALLOC} ${CMD}"
nomad alloc exec -task ${TASK} ${ALLOC} ${CMD}
}
# Enter a container by execing sh
# This is just a shortcut for exec sh
enter_ct(){
exec_ct sh
}
# Follow logs of a task
alloc_logs(){
local IFS=$'\n'
ALLOC=$((for JOB in $(ls_jobs); do ls_alloc_of_job ${JOB}; done) | fzf -m)
ALLOC=$(echo ${ALLOC} | sed -E 's/^([^ \(]+).*/\1/')
TASK=$(ls_tasks_of_alloc ${ALLOC})
if [ $(echo "${TASKS}" | wc -l) -eq 1 ]; then
TASK=${TASKS}
else
TASK=$(echo "${TASKS}" | fzf -m)
fi
echo "Running nomad alloc logs -f ${ALLOC} ${TASK}"
nomad alloc logs -f ${ALLOC} ${TASK}
}
export FZF_DEFAULT_OPTS=${FZF_DEFAULT_OPTS:-"--height=~10% --cycle --bind 'space:toggle' --marker='*'"}
@ -795,6 +838,12 @@ case $1 in
render_templates
renew_leases
;;
fetch)
update_submodules
render_templates
handle_prep_scripts
renew_leases
;;
prep)
update_submodules
render_templates
@ -817,15 +866,26 @@ case $1 in
renew_leases
;;
logs)
shift
alloc_logs "$@"
renew_leases
;;
loki)
loki_logs "$@"
renew_leases
job_logs "$@"
;;
conf)
renew_leases
get_merged_conf
renew_leases
;;
exec)
shift
exec_ct "$@"
renew_leases
;;
sh)
enter_ct
renew_leases
;;
switch)
shift