feat: Adds airflow assessment generation to hpa scripts.

This commit is contained in:
2025-12-15 12:21:13 -05:00
parent b9f248926b
commit fb0e4d705b
8 changed files with 230 additions and 76 deletions

75
env/.config/aap/config.toml vendored Normal file
View File

@@ -0,0 +1,75 @@
# This config is setup for running inside the docker container.
#
# NOTE:
# Configuration settings for the `hpa` command line tool.
# You can delete settings that are not applicable to your use case.
# Default arguments / options that get passed into `ansible-playbook` commands.
# WARNING: Do not put arguments / options that contain spaces in the same string,
# they should be separate strings, for example do not do something like
# ['--tags debug'], instead use ['--tags', 'debug'].
#
args = ['--tags', 'debug']
# Set to true if you want to pass the vault args to `ansible-playbook` commands.
useVaultArgs = true
# NOTE:
# Configuration for running the generate command(s). This allows custimizations
# to the files that get used to generate the final output (generally a pdf).
# See `pandoc --help`. Below are the defaults that get used, which only need
# adjusted if your template does not follow the default template design or if
# you add extra files to your template that need to be included in the final
# output. Also be aware that any of the files specified in the `files` or
# `includeInHeader` options, need to be inside the `buildDirectory` when generating
# the final output file.
# [generate]
# this relative to the project directory.
# buildDirectory = '.build'
# pdfEngine = 'xelatex'
# includeInHeader = [
# 'head.tex',
# 'footer.tex'
# ]
# files = [
# 'Report.md',
# 'Definitions.md'
# ]
# outputFileName = 'Report'
# NOTE:
# These are more for local development of the ansible playbook and should not be needed
# in most cases. Uncomment the lines if you want to customize the playbook and use it
# instead of the provided / default playbook.
[playbook]
directory = '/root/.local/share/hpa/playbook'
inventory = '/root/.local/share/hpa/playbook/inventory.ini'
version = 'main'
# NOTE:
# These are to declare where your template files are either on your local system or
# a remote git repository.
[template]
# The directory path on your local system to the template files.
directory = '/root/.local/share/hpa/airflow-assessment-template'
vars = 'repo_vars'
# The url to a git repository that contains your template files.
# url = 'https://git.example.com/consult-template.git'
# The version, tag, branch, or sha of the template files to clone from the remote
# template repository. In general it is best practice to use a version instead of a
# branch.
# version = '1.0.0'
# NOTE:
# Holds settings for `ansible-vault` commands.
[vault]
# Arguments to pass to commands that use `ansible-vault`, such as encrypting or decrypting
# files.
args = ['--vault-password-file=/run/secrets/vault-pass']
# An id to use when encrypting `ansible-vault` files.
#encryptId = 'myId'

View File

@@ -38,6 +38,8 @@ if echo "$HPA_DOCKER_IMAGE" | grep -vq "localhost"; then
fi fi
fi fi
echo "ARGS: $@"
podman run --rm --interactive --tty \ podman run --rm --interactive --tty \
--volume "$HPA_CONFIG_DIR":/root/.config/hpa:ro \ --volume "$HPA_CONFIG_DIR":/root/.config/hpa:ro \
--volume "$HPA_DATA_DIR":/root/.local/share/hpa:ro \ --volume "$HPA_DATA_DIR":/root/.local/share/hpa:ro \

View File

@@ -4,7 +4,8 @@ title="Creating project..."
script="${SCRIPTS:-$HOME/.local/scripts}/utils/hpa/hpa-create" script="${SCRIPTS:-$HOME/.local/scripts}/utils/hpa/hpa-create"
first_arg=${1:-""} first_arg=${1:-""}
if [[ $first_arg == "-h" ]] || [[ $first_arg == "--help" ]]; then if [[ $first_arg == "-h" ]] || [[ $first_arg == "--help" ]]; then
. "$script" $* . "$script" $*
else else
gum spin --show-output --title "$title" -- bash -c "$script $*" | tr -d '\r' | head -1 # bash -c "$script $*" | tr -d '\r' | head -1
gum spin --show-output --title "$title" -- bash -c "$script $*" | tr -d '\r' | head -1
fi fi

View File

@@ -14,7 +14,7 @@ LOG_FILE=${LOG_FILE:-"$LOG_LABEL.log"}
. "$SCRIPTS/utils/hpa/hpa.env" . "$SCRIPTS/utils/hpa/hpa.env"
usage() { usage() {
cat <<EOF cat <<EOF
Setup dependencies for running hpa script in docker. This only needs to be Setup dependencies for running hpa script in docker. This only needs to be
ran once on a new machine. ran once on a new machine.
@@ -44,30 +44,30 @@ EOF
# Logging utility function, use in place of echo. # Logging utility function, use in place of echo.
log() { log() {
logging log --source "$THIS_FILE" "$@" logging log --source "$THIS_FILE" "$@"
} }
generate-completion() { generate-completion() {
local output_dir output type local output_dir output type
type=${1:-"zsh"} type=${1:-"zsh"}
output=${2:-$HOME/.zsh/completions/_hpa} output=${2:-$HOME/.zsh/completions/_hpa}
output_dir=$(dirname "$output") output_dir=$(dirname "$output")
log "Generating completion: type: '$type', to: $output" log "Generating completion: type: '$type', to: $output"
[[ ! -d "$output_dir" ]] && mkdir -p "$output_dir" [[ ! -d "$output_dir" ]] && mkdir -p "$output_dir"
( (
podman run --rm -it "$HPA_DOCKER_IMAGE:$HPA_DOCKER_TAG" \ podman run --rm -it "$HPA_DOCKER_IMAGE:$HPA_DOCKER_TAG" \
--generate-completion-script "$type" | --generate-completion-script "$type" |
tr -d '\r' tr -d '\r'
) >"$output" ) >"$output"
} }
generate-secret() { generate-secret() {
log "Generating vault secret for key: '$HPA_VAULT_SECRET_KEY'" log "Generating vault secret for key: '$HPA_VAULT_SECRET_KEY'"
local secret local secret
secret="$(pass -c ansible/vault-pass)" secret="$(pass -c ansible/vault-pass)"
printf "%s" "$secret" | podman secret create "$HPA_VAULT_SECRET_KEY" - printf "%s" "$secret" | podman secret create "$HPA_VAULT_SECRET_KEY" -
} }
################################################################################ ################################################################################
@@ -76,22 +76,23 @@ generate-secret() {
first_arg=${1:-""} first_arg=${1:-""}
if [[ $first_arg == "-h" ]] || [[ $first_arg == "--help" ]]; then if [[ $first_arg == "-h" ]] || [[ $first_arg == "--help" ]]; then
usage && exit 0 usage && exit 0
else else
# Setup logging file and label. # Setup logging file and label.
source "$SCRIPTS/hypr/logging" source "$SCRIPTS/hypr/logging"
setup-logging "$LOG_FILE" "$LOG_LABEL" setup-logging "$LOG_FILE" "$LOG_LABEL"
log "Starting init..." log "Starting init..."
generate-completion "$@" generate-completion "$@"
generate-secret generate-secret
log "Generating directories, if they don't exist." log "Generating directories, if they don't exist."
mkdir -p "$HPA_CONSULTS_DIR" &>/dev/null mkdir -p "$HPA_CONSULTS_DIR" &>/dev/null
mkdir "$HPA_DATA_DIR" &>/dev/null mkdir "$HPA_DATA_DIR" &>/dev/null
log "Cloning required template and playbook, if they don't exist" log "Cloning required template and playbook, if they don't exist"
[[ ! -d "$HPA_PLAYBOOK_DIR" ]] && git clone "$HPA_PLAYBOOK_URL" "$HPA_PLAYBOOK_DIR" [[ ! -d "$HPA_PLAYBOOK_DIR" ]] && git clone "$HPA_PLAYBOOK_URL" "$HPA_PLAYBOOK_DIR"
[[ ! -d "$HPA_CONSULT_TEMPLATE_DIR" ]] && git clone "$HPA_CONSULT_TEMPLATE_URL" "$HPA_CONSULT_TEMPLATE_DIR" [[ ! -d "$HPA_CONSULT_TEMPLATE_DIR" ]] && git clone "$HPA_CONSULT_TEMPLATE_URL" "$HPA_CONSULT_TEMPLATE_DIR"
[[ ! -d "$HPA_AIRFLOW_TEMPLATE_DIR" ]] && git clone "$HPA_AIRFLOW_TEMPLATE_URL" "$HPA_AIRFLOW_TEMPLATE_DIR"
fi fi

View File

@@ -13,7 +13,7 @@ LOG_FILE=${LOG_FILE:-"$LOG_LABEL.log"}
. "$SCRIPTS/utils/hpa/hpa.env" . "$SCRIPTS/utils/hpa/hpa.env"
usage() { usage() {
cat <<EOF cat <<EOF
Pulls / updates template, playbook, docker image, etc. Pulls / updates template, playbook, docker image, etc.
@@ -34,35 +34,38 @@ EOF
# Logging utility function, use in place of echo. # Logging utility function, use in place of echo.
log() { log() {
logging log --source "$THIS_FILE" "$@" logging log --source "$THIS_FILE" "$@"
} }
pull-repo() { pull-repo() {
local dir=${1:-""} local dir=${1:-""}
[[ -z "$dir" ]] && [[ -z "$dir" ]] &&
log --error "Directory not supplied to pull git repo." && log --error "Directory not supplied to pull git repo." &&
exit 1 exit 1
pushd "$dir" &>/dev/null || exit 1 pushd "$dir" &>/dev/null || exit 1
( (
git pull git pull
) )
popd &>/dev/null popd &>/dev/null
} }
pull-docker() { pull-docker() {
log --echo "Pulling docker image: '$HPA_DOCKER_IMAGE:$HPA_DOCKER_TAG'" log --echo "Pulling docker image: '$HPA_DOCKER_IMAGE:$HPA_DOCKER_TAG'"
podman pull "$HPA_DOCKER_IMAGE:$HPA_DOCKER_TAG" podman pull "$HPA_DOCKER_IMAGE:$HPA_DOCKER_TAG"
} }
pull-playbook() { pull-playbook() {
log --echo "Pulling playbook: '$HPA_PLAYBOOK_DIR'" log --echo "Pulling playbook: '$HPA_PLAYBOOK_DIR'"
pull-repo "$HPA_PLAYBOOK_DIR" pull-repo "$HPA_PLAYBOOK_DIR"
} }
pull-template() { pull-template() {
log --echo "Pulling template: '$HPA_CONSULT_TEMPLATE_DIR'" log --echo "Pulling template: '$HPA_CONSULT_TEMPLATE_DIR'"
pull-repo "$HPA_CONSULT_TEMPLATE_DIR" pull-repo "$HPA_CONSULT_TEMPLATE_DIR"
log --echo "Pulling airflow assessment template: '$HPA_AIRFLOW_TEMPLATE_DIR'"
pull-repo "$HPA_AIRFLOW_TEMPLATE_DIR"
} }
################################################################################ ################################################################################
@@ -78,31 +81,32 @@ playbook_flag="0"
template_flag="0" template_flag="0"
while [[ $# -gt 0 ]]; do while [[ $# -gt 0 ]]; do
if [[ $1 == "-a" ]] || [[ $1 == "--all" ]]; then if [[ $1 == "-a" ]] || [[ $1 == "--all" ]]; then
all_flag="1" all_flag="1"
break break
elif [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then elif [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then
usage && exit 0 usage && exit 0
elif [[ $1 == "-d" ]] || [[ $1 == "--docker" ]]; then elif [[ $1 == "-d" ]] || [[ $1 == "--docker" ]]; then
all_flag="0" all_flag="0"
docker_flag="1" docker_flag="1"
elif [[ $1 == "-p" ]] || [[ $1 == "--playbook" ]]; then elif [[ $1 == "-p" ]] || [[ $1 == "--playbook" ]]; then
all_flag="0" all_flag="0"
playbook_flag="1" playbook_flag="1"
elif [[ $1 == "-t" ]] || [[ $1 == "--template" ]]; then elif [[ $1 == "-t" ]] || [[ $1 == "--template" ]]; then
all_flag="0" all_flag="0"
template_flag="1" template_flag="1"
fi fi
shift shift
done done
if [[ $all_flag == "1" ]]; then if [[ $all_flag == "1" ]]; then
docker_flag="1" docker_flag="1"
playbook_flag="1" playbook_flag="1"
template_flag="1" template_flag="1"
fi fi
[[ $docker_flag == "1" ]] && pull-docker [[ $docker_flag == "1" ]] && pull-docker
[[ $playbook_flag == "1" ]] && pull-playbook [[ $playbook_flag == "1" ]] && pull-playbook
[[ $template_flag == "1" ]] && pull-template [[ $template_flag == "1" ]] && pull-template
date +%s >"$HPA_CONFIG_DIR/.lastpull" date +%s >"$HPA_CONFIG_DIR/.lastpull"
date +%s >"$HPA_AIRFLOW_CONFIG_DIR/.lastpull"

47
env/.local/scripts/utils/hpa/aap vendored Executable file
View File

@@ -0,0 +1,47 @@
#!/usr/bin/env bash
set -e
set -o nounset
set -o pipefail
# A wrapper script to run swift-hpa in a docker container and
# mount the correct volumes, etc.
#
# Make sure to run 'hpa-init' first on this machine to setup
# dependencies, if you have not done so already.
#
# To attach to a shell inside the container run:
# `hpa bash`
#
# To run ansible vault commands inside the container run:
# `hpa ansible-vault ...`
#
SCRIPTS=${SCRIPTS:-$HOME/.local/scripts}
. "$SCRIPTS/utils/hpa/hpa.env"
############################## MAIN ##############################
# Don't pull images if they're prefixed with 'localhost'.
if echo "$HPA_DOCKER_IMAGE" | grep -vq "localhost"; then
# Check if we should pull the image prior to running.
last_pull="0"
if [[ -f "$HPA_AIRFLOW_CONFIG_DIR/.lastpull" ]]; then
last_pull=$(cat "$HPA_AIRFLOW_CONFIG_DIR/.lastpull")
fi
curr=$(date +%s)
diff=$((curr - last_pull))
if [[ $HPA_AUTO_PULL == "1" ]] && [[ $diff -gt "$HPA_AUTO_PULL_INTERVAL" ]]; then
. "$SCRIPTS/hpa-pull" "$HPA_AUTO_PULL_OPTS"
fi
fi
podman run --rm --interactive --tty \
--volume "$HPA_AIRFLOW_CONFIG_DIR":/root/.config/hpa:ro \
--volume "$HPA_DATA_DIR":/root/.local/share/hpa:ro \
--volume "$HPA_CONSULTS_DIR":/consults \
--volume "$PWD":/root/project \
--secret "$HPA_VAULT_SECRET_KEY" \
"$HPA_DOCKER_IMAGE:$HPA_DOCKER_TAG" "$@"

View File

@@ -17,7 +17,8 @@ LOG_FILE=${LOG_FILE:-"$LOG_LABEL.log"}
# Load environment / shared variables. # Load environment / shared variables.
. "$SCRIPTS/utils/hpa/hpa.env" . "$SCRIPTS/utils/hpa/hpa.env"
declare no_git_flag no_push_flag date_opt declare airflow_flag no_git_flag no_push_flag date_opt
airflow_flag="0"
no_git_flag="0" no_git_flag="0"
no_push_flag="0" no_push_flag="0"
date_opt=$(date '+%Y.%m.%d') date_opt=$(date '+%Y.%m.%d')
@@ -25,7 +26,8 @@ date_opt=$(date '+%Y.%m.%d')
usage() { usage() {
cat <<EOF cat <<EOF
A utility script to generate a new home performance assessment project. A utility script to generate a new home performance assessment project or
airflow assessment project.
All output from the 'hpa' command get suppressed so that this script can All output from the 'hpa' command get suppressed so that this script can
be piped / used to automatically cd into the directory after creation to be piped / used to automatically cd into the directory after creation to
@@ -41,6 +43,7 @@ USAGE:
$ $THIS <flags> <customer> $ $THIS <flags> <customer>
FLAGS: FLAGS:
-a | --airflow: Generate using the airflow assessment project template.
-d | --date: Override the date the project was started. -d | --date: Override the date the project was started.
-g | --no-git: Do not initialize a git repository for the project. -g | --no-git: Do not initialize a git repository for the project.
-h | --help: Show this help page. -h | --help: Show this help page.
@@ -62,21 +65,32 @@ log() {
} }
create() { create() {
local customer container_dir local customer container_dir script suffix
customer=${1:-""} customer=${1:-""}
[[ -z $customer ]] && [[ -z $customer ]] &&
log --error "Must supply a customer name for the project" && log --error "Must supply a customer name for the project" &&
exit 1 exit 1
[[ ! -f $SCRIPTS/hpa ]] &&
log --error "Unable to find the 'hpa' script." &&
exit 1
log "Generating project for: '$customer'" log "Generating project for: '$customer'"
# Setup approriate configuration and project suffix.
if [[ $airflow_flag == "1" ]]; then
suffix="AAP"
script="$SCRIPTS/utils/hpa/aap"
else
suffix="HPA"
script="$SCRIPTS/hpa"
fi
[[ ! -f $SCRIPTS/hpa ]] &&
log --error "Unable to find the script: '$script'." &&
exit 1
log "Using script: '$script'"
container_dir=$( container_dir=$(
"$SCRIPTS/hpa" create --quiet "/consults/$date_opt.$customer" "$script" create --quiet "/consults/$date_opt.$customer.$suffix"
) )
echo "$HPA_CONSULTS_DIR/$(basename "$container_dir")" echo "$HPA_CONSULTS_DIR/$(basename "$container_dir")"
} }
@@ -129,6 +143,8 @@ declare customer output
while [[ $# -gt 0 ]]; do while [[ $# -gt 0 ]]; do
if [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then if [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then
usage && exit 0 usage && exit 0
elif [[ $1 == "-a" ]] || [[ $1 == "--airflow" ]]; then
airflow_flag="1"
elif [[ $1 == "-d" ]] || [[ $1 == "--date" ]]; then elif [[ $1 == "-d" ]] || [[ $1 == "--date" ]]; then
shift shift
date_opt="$1" date_opt="$1"

View File

@@ -33,6 +33,11 @@ HPA_CONSULT_ORIGIN_BASE_URL=${CONSULT_ORIGIN_BASE_URL:-"ssh://git@git.housh.dev:
HPA_CONSULT_TEMPLATE_DIR=${HPA_DATA_DIR}/template HPA_CONSULT_TEMPLATE_DIR=${HPA_DATA_DIR}/template
HPA_CONSULT_TEMPLATE_URL="ssh://git@git.housh.dev:2222/hhe/consult-template.git" HPA_CONSULT_TEMPLATE_URL="ssh://git@git.housh.dev:2222/hhe/consult-template.git"
# Airflow assessment vars.
HPA_AIRFLOW_CONFIG_DIR="${XDG_CONFIG_HOME}/aap"
HPA_AIRFLOW_TEMPLATE_DIR="${HPA_DATA_DIR}/airflow-assessment-template"
HPA_AIRFLOW_TEMPLATE_URL="ssh://git@git.housh.dev:2222/hhe/airflow-assessment-template.git"
export HPA_AUTO_PULL export HPA_AUTO_PULL
export HPA_AUTO_PULL_INTERVAL export HPA_AUTO_PULL_INTERVAL
export HPA_AUTO_PULL_OPTS export HPA_AUTO_PULL_OPTS
@@ -47,3 +52,6 @@ export HPA_DOCKER_TAG
export HPA_PLAYBOOK_URL export HPA_PLAYBOOK_URL
export HPA_PLAYBOOK_DIR export HPA_PLAYBOOK_DIR
export HPA_VAULT_SECRET_KEY export HPA_VAULT_SECRET_KEY
export HPA_AIRFLOW_CONFIG_DIR
export HPA_AIRFLOW_TEMPLATE_DIR
export HPA_AIRFLOW_TEMPLATE_URL