feat: Creates hpa-init script and hpa.env.

This commit is contained in:
2025-11-06 14:35:28 -05:00
parent c1eae18c67
commit 43f18eb45a
4 changed files with 147 additions and 60 deletions

View File

@@ -7,73 +7,29 @@ set -o pipefail
# A wrapper script to run swift-hpa in a docker container and
# mount the correct volumes, etc.
#
# To download templates, playbooks, setup secrets, generate shell completions, etc.
# you can call this script with 'init'.
#
# Otherwise it will run the hpa script inside of a docker container with any passed
# in arguments.
# Make sure to run 'hpa-init' first on this machine to setup
# dependencies, if you have not done so already.
# XDG vars.
XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-$HOME/.config}
XDG_DATA_HOME=${XDG_DATA_HOME:-$HOME/.local/share}
# Docker vars.
HPA_DOCKER_IMAGE="git.housh.dev/michael/swift-hpa"
HPA_DOCKER_TAG=${HPA_DOCKER_TAG:-"latest"}
# Local vars.
HPA_DATA_DIR="$XDG_DATA_HOME/hpa"
HPA_CONFIG_DIR="$XDG_CONFIG_HOME/hpa"
CONSULTS_DIR=${CONSULTS_DIR:-$HOME/work/consults}
CONSULT_TEMPLATE_URL="ssh://git@git.housh.dev:2222/hhe/consult-template.git"
HPA_PLAYBOOK_URL="ssh://git@git.housh.dev:2222/michael/ansible-hpa-playbook.git"
HPA_VAULT_SECRET_KEY="${HPA_VAULT_SECRET_KEY:-vault-pass}"
generate-completion() {
local output_dir output type
type=${1:-"zsh"}
output=${2:-$HOME/.zsh/completions/_hpa}
output_dir=$(dirname "$output")
[[ ! -d "$output_dir" ]] && mkdir -p "$output_dir"
(
podman run --rm -it "$HPA_DOCKER_IMAGE:$HPA_DOCKER_TAG" \
--generate-completion-script "$type" |
tr -d '\r'
) >"$output"
}
gnnerate-secret() {
local secret=""
secret="$(pass -c ansible/vault-pass)"
printf "%s" "$secret" | podman secret create "$HPA_VAULT_SECRET_KEY" -
}
# Ensure the dependencies are installed / setup.
init() {
generate-completion "$@"
gnnerate-secret
mkdir -p "$CONSULTS_DIR" &>/dev/null
mkdir "$HPA_DATA_DIR" &>/dev/null
[[ ! -d "$HPA_DATA_DIR/playbook" ]] && git clone "$HPA_PLAYBOOK_URL" "$HPA_DATA_DIR/playbook"
[[ ! -d "$HPA_DATA_DIR/template" ]] && git clone "$CONSULT_TEMPLATE_URL" "$HPA_DATA_DIR/template"
}
run() {
podman run --rm -it \
--volume "$HPA_DATA_DIR/template":/template \
--volume "$HPA_DATA_DIR/playbook":/playbook \
--volume "$HPA_CONFIG_DIR":/config/hpa \
--volume "$CONSULTS_DIR":/consults \
--secret "$HPA_VAULT_SECRET_KEY" \
"$HPA_DOCKER_IMAGE:$HPA_DOCKER_TAG" "$@"
}
############################## MAIN ##############################
first_arg=${1:-""}
if [[ $first_arg == "init" ]]; then
shift
init "$@"
else
run "$@"
fi
podman run --rm --interactive --tty \
--volume "$HPA_DATA_DIR/template":/template \
--volume "$HPA_DATA_DIR/playbook":/playbook \
--volume "$HPA_CONFIG_DIR":/config/hpa \
--volume "$CONSULTS_DIR":/consults \
--secret "$HPA_VAULT_SECRET_KEY" \
"$HPA_DOCKER_IMAGE:$HPA_DOCKER_TAG" "$@"

97
env/.local/scripts/hpa-init vendored Executable file
View File

@@ -0,0 +1,97 @@
#!/usr/bin/env bash
set -e
set -o nounset
set -o pipefail
SCRIPTS=${SCRIPTS:-$HOME/.local/scripts}
THIS_FILE=${BASH_SOURCE[0]}
LOG_LABEL=$(basename "$THIS_FILE")
THIS=${THIS:-$LOG_LABEL}
LOG_FILE=${LOG_FILE:-"$LOG_LABEL.log"}
# Load environment / shared variables.
. "$SCRIPTS/utils/hpa/hpa.env"
usage() {
cat <<EOF
Setup dependencies for running hpa script in docker. This only needs to be
ran once on a new machine.
USAGE:
$ $THIS <flags> <shell> <completion-file>
FLAGS:
-h | --help: Show this help page.
DEFAULTS:
shell: Default is 'zsh', accepts 'zsh' | 'bash' | 'fish'
completion-file: Default is '~/.zsh/completions/_hpa'
ENVIRONMENT:
CONSULTS_DIR: Set the directory where consults / projects are stored.
(default: '~/work/consults')
HPA_DOCKER_TAG: Set the docker image tag to use for the hpa docker image
(default: 'latest')
HPA_VAULT_SECRET_KEY: Set the key used for the ansible-vault secret.
(default: 'vault-pass')
EOF
}
# Logging utility function, use in place of echo.
log() {
logging log --source "$THIS_FILE" "$@"
}
generate-completion() {
local output_dir output type
type=${1:-"zsh"}
output=${2:-$HOME/.zsh/completions/_hpa}
output_dir=$(dirname "$output")
log "Generating completion: type: '$type', to: $output"
[[ ! -d "$output_dir" ]] && mkdir -p "$output_dir"
(
podman run --rm -it "$HPA_DOCKER_IMAGE:$HPA_DOCKER_TAG" \
--generate-completion-script "$type" |
tr -d '\r'
) >"$output"
}
generate-secret() {
log "Generating vault secret for key: '$HPA_VAULT_SECRET_KEY'"
local secret
secret="$(pass -c ansible/vault-pass)"
printf "%s" "$secret" | podman secret create "$HPA_VAULT_SECRET_KEY" -
}
################################################################################
# MAIN
################################################################################
first_arg=${1:-""}
if [[ $first_arg == "-h" ]] || [[ $first_arg == "--help" ]]; then
usage && exit 0
else
# Setup logging file and label.
source "$SCRIPTS/hypr/logging"
setup-logging "$LOG_FILE" "$LOG_LABEL"
log "Starting init..."
generate-completion "$@"
generate-secret
log "Generating directories, if they don't exist."
mkdir -p "$HPA_CONSULTS_DIR" &>/dev/null
mkdir "$HPA_DATA_DIR" &>/dev/null
log "Cloning required template and playbook, if they don't exist"
[[ ! -d "$HPA_DATA_DIR/playbook" ]] && git clone "$HPA_PLAYBOOK_URL" "$HPA_DATA_DIR/playbook"
[[ ! -d "$HPA_DATA_DIR/template" ]] && git clone "$HPA_CONSULT_TEMPLATE_URL" "$HPA_DATA_DIR/template"
fi

View File

@@ -14,8 +14,8 @@ THIS_FILE=${BASH_SOURCE[0]}
LOG_LABEL=$(basename "$THIS_FILE")
THIS=${THIS:-$LOG_LABEL}
LOG_FILE=${LOG_FILE:-"$LOG_LABEL.log"}
CONSULTS_DIR=${CONSULTS_DIR:-$HOME/work/consults}
CONSULT_ORIGIN_BASE_URL=${CONSULT_ORIGIN_BASE_URL:-"ssh://git@git.housh.dev:2222/consults"}
# Load environment / shared variables.
. "$SCRIPTS/utils/hpa/hpa.env"
declare no_git_flag no_push_flag
no_git_flag="0"
@@ -76,7 +76,7 @@ create() {
container_dir=$(
"$SCRIPTS/hpa" create --quiet "/consults/$(date '+%Y.%m.%m').$customer"
)
echo "$CONSULTS_DIR/$(basename "$container_dir")"
echo "$HPA_CONSULTS_DIR/$(basename "$container_dir")"
}
initialize-git() {
@@ -98,7 +98,7 @@ initialize-git() {
git init
git add .
git commit --all --message="Initial commit"
git remote add origin "$CONSULT_ORIGIN_BASE_URL/$(basename "$dir")"
git remote add origin "$HPA_CONSULT_ORIGIN_BASE_URL/$(basename "$dir")"
if [[ $no_push_flag == "0" ]]; then
git push --set-upstream origin main
fi

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

@@ -0,0 +1,34 @@
#!/usr/bin/env bash
# Set's up environment variables used in the various 'hpa*' scripts.
#
# This allows them all to be set / declared in one place, then those
# scripts just need to source this file to use them.
# XDG vars.
XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-$HOME/.config}
XDG_DATA_HOME=${XDG_DATA_HOME:-$HOME/.local/share}
# Docker vars.
HPA_DOCKER_IMAGE="git.housh.dev/michael/swift-hpa"
HPA_DOCKER_TAG=${HPA_DOCKER_TAG:-"latest"}
# Consults vars.
HPA_CONSULTS_DIR=${CONSULTS_DIR:-$HOME/work/consults}
HPA_CONSULT_ORIGIN_BASE_URL=${CONSULT_ORIGIN_BASE_URL:-"ssh://git@git.housh.dev:2222/consults"}
HPA_CONSULT_TEMPLATE_URL="ssh://git@git.housh.dev:2222/hhe/consult-template.git"
HPA_CONFIG_DIR="$XDG_CONFIG_HOME/hpa"
HPA_DATA_DIR="$XDG_DATA_HOME/hpa"
HPA_PLAYBOOK_URL="ssh://git@git.housh.dev:2222/michael/ansible-hpa-playbook.git"
HPA_VAULT_SECRET_KEY="${HPA_VAULT_SECRET_KEY:-vault-pass}"
export HPA_CONSULTS_DIR
export HPA_CONSULT_ORIGIN_BASE_URL
export HPA_CONSULT_TEMPLATE_URL
export HPA_CONFIG_DIR
export HPA_DATA_DIR
export HPA_DOCKER_IMAGE
export HPA_DOCKER_TAG
export HPA_PLAYBOOK_URL
export HPA_VAULT_SECRET_KEY