diff options
author | Patrick Spek <p.spek@tyil.nl> | 2022-04-15 16:32:43 +0200 |
---|---|---|
committer | Patrick Spek <p.spek@tyil.nl> | 2022-04-15 16:32:43 +0200 |
commit | d8a2f732b300cdbb892e0878fe87dbb7a0ef6d03 (patch) | |
tree | d364845506af8f3080c79df9a91bb3e32cc4b4d8 /lib/main.bash |
Initial commit
Diffstat (limited to 'lib/main.bash')
-rw-r--r-- | lib/main.bash | 163 |
1 files changed, 163 insertions, 0 deletions
diff --git a/lib/main.bash b/lib/main.bash new file mode 100644 index 0000000..8075b59 --- /dev/null +++ b/lib/main.bash @@ -0,0 +1,163 @@ +#!/usr/bin/env bash + +# shellcheck source=lib/util.bash +source "$(dirname "${BASH_SOURCE[0]}")/util.bash" + +# shellcheck source=lib/logging.bash +source "$(dirname "${BASH_SOURCE[0]}")/logging.bash" + +main() { + debug "$BASHTARD_NAME/main" "Running from $BASHTARD_BASEDIR" + debug "$BASHTARD_NAME/main" "Configuration dir is at $BASHTARD_ETCDIR" + debug "$BASHTARD_NAME/main" "> $0 $*" + + [[ -z $1 ]] && usage && exit 2 + + local subcommand="$1" + shift + + debug "$BASHTARD_NAME/main" "Handling subcommand '$subcommand'" + + subcommand_src="$BASHTARD_BASEDIR/lib/subcommands/$subcommand.bash" + + debug "$BASHTARD_NAME/main" "Checking $subcommand_src" + + if [[ ! -f $subcommand_src ]] + then + debug "$BASHTARD_NAME/main" "No script found to handle action, showing usage" + usage + exit 2 + fi + + # Declare some global variables + declare -A BASHTARD_PLATFORM + + # Figure out system details + debug "$BASHTARD_NAME/main" "Discovering system information" + discover_system + + # Export BASHTARD_ variables + export BASHTARD_PLATFORM + export BASHTARD_SYSTEM="${subcommand%%/*}" + + # Source the file defining the subcommand. + debug "$BASHTARD_NAME/main" "Sourcing $subcommand_src" + source "$subcommand_src" + + # Maintain our own tempdir + export TMPDIR="$BASEDIR/tmp/$RANDOM" + mkdir -p -- "$TMPDIR" + debug "$BASHTARD_NAME/main" "\$TMPDIR set to $TMPDIR" + + # Actually perform the subcommand + debug "$BASHTARD_NAME/main" "Running subcommand" + subcommand "$@" + local subcommand_exit=$? + + # Clean up if necessary + if [[ -z $BASHTARD_MESSY ]] + then + debug "$BASHTARD_NAME/main" "Cleaning up tempfiles at $TMPDIR" + rm -rf -- "$TMPDIR" + fi + + ## Use the subcommand's exit code + exit $subcommand_exit +} + +usage() { + cat <<EOF +Usage: + $BASHTARD_NAME -h + $BASHTARD_NAME add <playbook> + $BASHTARD_NAME bootstrap + $BASHTARD_NAME del <playbook> + $BASHTARD_NAME ssh <command> + $BASHTARD_NAME sync + $BASHTARD_NAME sysinfo + +Perform maintenance on your infra. + +Commands: + add Add a configuration playbook to this machine. + bootstrap Bootstrap the $BASHTARD_NAME configuration system. + del Remove a configuration playbook from this machine. + ssh Run a given command on all known hosts. + sync Pull latest changes through git, and synchronize all added + playbooks. + sysinfo Show gathered information about this system. + +Playbooks: +EOF +} + +# Discover information about the system. If any bugs are reported and you want +# more information about the system the user is running on, additional checks +# can be added here, and the user will simply have to include the output of the +# sysinfo command in their message to you. +discover_system() { + BASHTARD_PLATFORM["os"]="$(discover_system_os)" + BASHTARD_PLATFORM["arch"]="$(discover_system_arch)" + BASHTARD_PLATFORM["version"]="$(discover_system_version)" + BASHTARD_PLATFORM["term"]="$TERM" + BASHTARD_PLATFORM["fqdn"]="$(hostname -f)" + + # When on a Linux-using OS, check for the specific distribution in use. + if [[ ${BASHTARD_PLATFORM[os]} == *"linux"* ]] + then + BASHTARD_PLATFORM["distro"]="$(discover_system_distro)" + fi + + BASHTARD_PLATFORM[key]="$(discover_system_key)" +} + +discover_system_arch() { + uname -m +} + +discover_system_distro() { + if [[ -f /etc/os-release ]] + then + ( + source /etc/os-release + printf "%s" "$NAME" \ + | awk '{print tolower($0)}' \ + | sed 's@[/+ ]@_@g' + ) + return + fi + + crit "No /etc/os-release found. Are you sure you're on a sane GNU+Linux distribution?" + + if command -v pacman > /dev/null + then + warn "$BASHTARD_NAME/main" "Found pacman, assuming Archlinux as distro." + printf "%s" "archlinux" + return + fi +} + +discover_system_version() { + printf "%s" "$(uname -r | awk '{print tolower($0)}')" +} + +discover_system_key() { + key+="${BASHTARD_PLATFORM[os]}" + + if [[ ${BASHTARD_PLATFORM[distro]} ]] + then + key+="-${BASHTARD_PLATFORM[distro]}" + fi + + printf "%s" "$key" +} + +discover_system_os() { + if command -v uname > /dev/null + then + printf "%s" "$(uname -s | awk '{print tolower($0)}' | sed 's@[/+ ]@_@g')" + return + fi +} + +main "$@" |