Files
Ansible-Build/task
T
2024-11-07 15:53:20 +01:00

194 lines
5.5 KiB
Bash
Executable File

#!/bin/bash
function help() {
echo
echo "$1 <command> [options]"
echo
echo "commands:"
echo
cmd_width=24
opt_width=10
desc_width=45
printf "| %-${cmd_width}s | %-${opt_width}s | %-${desc_width}s |\n" "Command" "Option" "Description"
echo "|$(printf '%*s' $((cmd_width + 2)) '' | tr ' ' '-')|$(printf '%*s' $((opt_width + 2)) '' | tr ' ' '-')|$(printf '%*s' $((desc_width + 2)) '' | tr ' ' '-')|"
printf "| %-${cmd_width}s | %-${opt_width}s | %-${desc_width}s |\n" "generate-passwordfile" "[password]" "Generate a password file for Ansible vault."
printf "| %-${cmd_width}s | %-${opt_width}s | %-${desc_width}s |\n" "install" "" "Install Ansible and dependencies with pip."
printf "| %-${cmd_width}s | %-${opt_width}s | %-${desc_width}s |\n" "install-exo" "" "Install Exoscale CLI."
printf "| %-${cmd_width}s | %-${opt_width}s | %-${desc_width}s |\n" "upgrade" "" "Upgrade Ansible and dependencies with pip."
printf "| %-${cmd_width}s | %-${opt_width}s | %-${desc_width}s |\n" "lint" "" "Lint Ansible roles and playbooks."
printf "| %-${cmd_width}s | %-${opt_width}s | %-${desc_width}s |\n" "galaxy-build" "" "Create tarball of collection."
printf "| %-${cmd_width}s | %-${opt_width}s | %-${desc_width}s |\n" "galaxy-publish" "" "Publish tarball to Ansible Galaxy."
printf "| %-${cmd_width}s | %-${opt_width}s | %-${desc_width}s |\n" "node-install" "" "Install build dependencies."
printf "| %-${cmd_width}s | %-${opt_width}s | %-${desc_width}s |\n" "node-dev" "" "Start vuepress development server."
printf "| %-${cmd_width}s | %-${opt_width}s | %-${desc_width}s |\n" "node-build" "" "Create vuepress build."
printf "| %-${cmd_width}s | %-${opt_width}s | %-${desc_width}s |\n" "node-serve-build" "" "Serve vuepress build."
printf "| %-${cmd_width}s | %-${opt_width}s | %-${desc_width}s |\n" "update-docs" "" "Update project docs."
printf "| %-${cmd_width}s | %-${opt_width}s | %-${desc_width}s |\n" "list-hosts" "[path]" "Lists all hosts of an inventory."
printf "| %-${cmd_width}s | %-${opt_width}s | %-${desc_width}s |\n" "version" "" "Show tool versions."
printf "| %-${cmd_width}s | %-${opt_width}s | %-${desc_width}s |\n" "source" "" "Source the Python virtual env."
printf "| %-${cmd_width}s | %-${opt_width}s | %-${desc_width}s |\n" "show-passwordless-sudo" "[username]" "Give instructions to setup passwordless sudo."
echo
}
# Set env vars
GIT_BRANCH=$(git symbolic-ref --short -q HEAD)
if [[ -a ".env" ]]; then
export $(cat .env | sed 's/^#.*//g' | xargs)
fi
function generate-passwordfile() {
if test -z "$1"; then echo "\$1 is empty"; exit; fi
export VAULTPASSWORD=$1
echo "$VAULTPASSWORD" > .vault_pass
chmod 600 .vault_pass
echo "Password file .vault_pass created."
}
function init-venv() {
if [ ! -d "venv$GIT_BRANCH" ]; then
echo "Create virtualenv venv$GIT_BRANCH."
python3 -m venv "venv$GIT_BRANCH"
fi
}
function activate-venv() {
echo "Source virtualenv venv$GIT_BRANCH."
source "venv$GIT_BRANCH/bin/activate"
}
function install() {
echo "Setup Ansible log file"
sudo touch /var/log/ansible.log
sudo chown $USER: /var/log/ansible.log
init-venv
activate-venv
echo "Install Ansible and Python dependencies"
sudo apt install graphviz
pip install -r requirements.txt
ansible-galaxy collection install community.general
}
function install-exo() {
curl -fsSL https://raw.githubusercontent.com/exoscale/cli/master/install-latest.sh | sh
}
function upgrade() {
echo "Update Ansible"
pip install --upgrade ansible
}
function update-docs() {
activate-venv
python3 ./library/graph.py
echo "Update docs success."
}
function list-hosts() {
if test -z "$1"; then echo "\$1 is empty"; exit; fi
activate-venv
ansible -i $1 all --list-hosts
}
function version() {
printf "\npython:\n"
python --version
printf "\npip:\n"
pip --version
activate-venv
printf "\npython venv:\n"
python --version
printf "\npip venv:\n"
pip --version
printf "\nansible:\n"
ansible --version
}
function show-passwordless-sudo() {
USER=${1:-"$USERNAME"}
echo "echo \"$USER ALL=(ALL) NOPASSWD: ALL\" >> /etc/sudoers"
}
function galaxy-build() {
activate-venv
VERSION=$(yq -r .version galaxy.yml)
NAMESPACE=$(yq -r .namespace galaxy.yml)
NAME=$(yq -r .name galaxy.yml)
ansible-galaxy collection build
mv $NAMESPACE-$NAME-$VERSION.tar.gz tmp/
}
function galaxy-publish() {
activate-venv
VERSION=$(yq -r .version galaxy.yml)
NAMESPACE=$(yq -r .namespace galaxy.yml)
NAME=$(yq -r .name galaxy.yml)
ansible-galaxy collection publish tmp/$NAMESPACE-$NAME-$VERSION.tar.gz --token "$GALAXY_API_TOKEN"
}
case "$1" in
generate-passwordfile)
generate-passwordfile $2
;;
install)
install
;;
install-exo)
install-exo
;;
upgrade)
upgrade
;;
version)
version
;;
update-docs)
update-docs
;;
list-hosts)
list-hosts $2
;;
galaxy-build)
galaxy-build
;;
galaxy-publish)
galaxy-publish
;;
node-install)
npm install
;;
node-dev)
npm run dev
;;
node-build)
npm run build
;;
node-serve-build)
cd .vuepress/dist
npx serve
;;
lint)
activate-venv
ansible-lint --write --offline roles/*
;;
source)
activate-venv
;;
show-passwordless-sudo)
show-passwordless-sudo $2
;;
*)
help task
exit 1
;;
esac