From 1fa4b3ed1a9f64abfd8c70d87f52fd83ab490bfb Mon Sep 17 00:00:00 2001 From: LinuxSquare Date: Mon, 26 Aug 2024 21:48:52 +0200 Subject: [PATCH] test: add basic functionality --- .gitignore | 1 + functions/configmgmt | 36 +++++++++++++++++++++++++++++++++--- novos | 24 ++++++++++++------------ 3 files changed, 46 insertions(+), 15 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..09d12f8 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +ansible-playbooks/ diff --git a/functions/configmgmt b/functions/configmgmt index a5bc6aa..dd3427c 100644 --- a/functions/configmgmt +++ b/functions/configmgmt @@ -1,22 +1,52 @@ #!/usr/bin/env bash -readonly CONFIG_ROOT="/srv/ansible" +#readonly CONFIG_ROOT="/srv/ansible/playbooks" +readonly CONFIG_ROOT="$PWD/ansible-playbooks" function config_help() { - echo "Printing config help" + cat << EOF +$(basename ${0}) config [...] +commands: + $(basename ${0}) config help + $(basename ${0}) config pull + $(basename ${0}) config full + $(basename ${0}) config single [playbook] +EOF } # Pulls all ansible playbooks from the remote git repo function config_pull() { echo "Pulling latest changes of repo" + # git -C "$CONFIG_ROOT" pull } # Executes an ansible-playbook on top.ansible.yml function config_full() { echo "Executing full playbook run" + # ansible-playbook $([[ $debuglevel -eq 4 ]] && echo '-v') $([[ $dryrun -eq 1 ]] && echo '-C') "${CONFIG_ROOT}/top.ansible.yml" } # Execute on a single playbook ansible-playbook function config_single() { - echo "Executing single playbook" + echo "Executing single playbook: $1" + local path="$CONFIG_ROOT/${1/\./\/}" # replace dots in path with forward-slashes + local newpath="$path" # set $newpath to $path for later checking + + # Check if $path is a directory => add init.ansible.yml + if [[ -d "$path" ]]; then + newpath="$path/init.ansible.yml" + fi + + # Check if $path is a file => add .ansible.yml + if [[ -f "${path}.ansible.yml" ]]; then + newpath="${path}.ansible.yml" + fi + + # Check if original $path still matches with $newpath => Fail due to unknown module + if [[ "$path" == "$newpath" ]]; then + echo "Module '$1' not found" + exit 1 + fi + echo "$newpath $([[ $debuglevel -eq 4 ]] && echo '-v') $([[ $dryrun -eq 1 ]] && echo '-C')" + # ansible-playbook $([[ $debuglevel -eq 4 ]] && echo '-v') $([[ $dryrun -eq 1 ]] && echo '-C') "$newpath" } diff --git a/novos b/novos index 879c58f..dfe97cb 100755 --- a/novos +++ b/novos @@ -13,22 +13,19 @@ done # VARIABLES declare debuglevel=3 +declare dryrun=0 function usage() { cat << EOF -usage: $(basename ${0}) [...] +usage: $(basename ${0}) [command] ... operations: $(basename ${0}) {-h --help} $(basename ${0}) {-V --version} $(basename ${0}) {-v --verbose} - $(basename ${0}) {--init} [servername] [network name] - $(basename ${0}) {--datapack} [servername] [datapack dl url] - $(basename ${0}) {--start} [servername] [port] (ip) - $(basename ${0}) {--stop} [servername] - $(basename ${0}) {--restart} [servername] [port] (ip) - $(basename ${0}) {--attach} [servername] - $(basename ${0}) {--reset} [servername] - $(basename ${0}) {--delete} [servername] + $(basename ${0}) {-n --dryrun} + +commands: + $(basename ${0}) config EOF } @@ -50,15 +47,17 @@ while true; do case "${1}" in -h|--help) usage - exit 0 + break ;; -V|--version) version - exit 0 + break ;; -v|--verbose) debuglevel=4 - break + ;; + -n|--dryrun) + dryrun=1 ;; config) parent="$1" @@ -80,3 +79,4 @@ while true; do esac shift done +exit 0