synth.download/helperbot

205 lines
No EOL
7.6 KiB
Bash
Executable file

#!/usr/bin/env bash
# ╭────────────────────────────────────────────────────╮
# │ _ _ _ _ owo │
# │ | |__ ___| |_ __ ___ _ __| |__ ___ | |_ │
# │ | '_ \ / _ \ | '_ \ / _ \ '__| '_ \ / _ \| __| │
# │ | | | | __/ | |_) | __/ | | |_) | (_) | |_ │
# │ |_| |_|\___|_| .__/ \___|_| |_.__/ \___/ \__| │
# │ |_| │
# ╰────────────────────────────────────────────────────╯
# helperbot - synth.download's all in one script for managing everything. beep!
# =============================================================================
# ╭─────────────────────────╮
# │ functions and variables │
# ╰─────────────────────────╯
# unset everything - ensure we're working with a clean state
unset synth_help && unset synth_upgrade && unset synth_backup && unset synth_vacuum && unset synth_invalid && unset synth_current_system
# exit immediately if an error occurs somewhere to prevent Fucked Up Shit
set -e
# defining colors for text output
if [[ -t 1 ]]; then
red=$( tput setaf 1 );
green=$( tput setaf 2 );
yellow=$( tput setaf 3 );
blue=$( tput setaf 4 );
pink=$( tput setaf 5 );
cyan=$( tput setaf 6 );
gray=$( tput setaf 8 );
normal=$( tput sgr 0 );
fi
# attempt to detect the system based on hostname
function detect_system {
if [ "$(hostname)" = "phosphorus" ]; then
synth_current_system=phosphorus
echo "Detected ${blue}phosphorus${normal}."
elif [ "$(hostname)" = "neptunium" ]; then
synth_current_system=neptunium
echo "Detected ${blue}neptunium${normal}."
elif [ "$(hostname)" = "cerium" ]; then
synth_current_system=cerium
echo "Detected ${blue}cerium${normal}."
elif [ "$(hostname)" = "synthnix" ]; then
synth_current_system=synthnix
echo "Detected ${blue}synthnix${normal}."
else
echo "${red}Failed to detect system.${normal}"
echo "We're most likely being run in an environment we don't know of."
echo "Exiting..."
exit 1
fi
}
# base system upgrade - generic steps for debian/ubuntu based systems
function base_system_upgrade {
echo "${cyan}Upgrading base system.${normal}"
echo "${blue}Doing standard apt upgrade...${normal}"
apt update | tee ${synth_upgrade_log}
apt upgrade -y | tee -a ${synth_upgrade_log}
echo "${blue}Try upgrading distro base...${normal}"
apt dist-upgrade -y | tee -a ${synth_upgrade_log}
echo "${blue}Apt cleanup...${normal}"
apt clean | tee -a ${synth_upgrade_log}
echo "${green}Base system upgraded!.${normal}"
}
# docker container updates
# reusable steps to update containers - upgrade_docker_container "/srv/docker" "name_of_service_or_folder" "compose.yaml"
function upgrade_docker_container {
if [ -d "$1/$2" ]; then
cd "$1"/"$2" && docker compose -f "$1/$2/$3" pull # pull the container
docker compose -f "$1/$2/$3" down && docker compose -f "$1/$2/$3" up -d
else
echo "${red}docker:${normal} Folder $1/$2 does not exist."
fi
}
# psql vacuuming
# reusable step to vacuum databases - postgres_vacuum [user_and_db_name] [password]
function postgres_vacuum {
docker exec -it postgres-db-1 /bin/bash -c "POSTGRES_PASSWORD="$2" psql -U "$1" -d "$1" -c 'VACUUM ANALYZE;'"
}
# ╭───────────────────╮
# │ defining messages │
# ╰───────────────────╯
# header
function header {
echo "╭────────────────╮"
echo "│ helperbot! owo │"
echo "╰────────────────╯"
echo
sleep 1 # grace period
}
# help info
function info_help {
echo "${blue}Usage:${normal} helperbot [-h|-u|-b|-v]"
echo
echo "${green}Options:${normal}"
echo "-h, --help Print this help page."
echo "-u, --upgrade Update the system."
echo "-b, --backup Backup the system."
echo "-v, --vacuum Vacuum the postgresql databases."
echo
echo "helperbot automatically knows what to do based on this system's hostname! Beep!"
echo
echo "${yellow}This script is still generally a work-in-progress.${normal}"
echo "Report breakage or suggestions or improvments or whatever to here:"
echo "https://forged.synth.download/synth.download/synth.download"
echo
}
# =============================================================================
# ╭──────────────╮
# │ main program │
# ╰──────────────╯
# check to see if we're running as root
#if [[ ${UID} != 0 ]]; then
# echo "${red}helperbot must be run as root or with sudo permissions!${normal} thanks!"
# exit 1
#fi
# display the header
header
# evaluate arguments and set environment variables to enable each command and see what should be executed
while [ -n "$1" ]; do
case "$1" in
-h | --help) # display help info
synth_help=1;;
-u | --upgrade) # upgrade system
synth_upgrade=1
if [ ! -v synth_current_system ]; then
detect_system
fi;;
-b | --backup) # backup system
synth_backup=1
if [ ! -v synth_current_system ]; then
detect_system
fi;;
-v | --vacuum) # vacuum database
synth_vacuum=1
if [ ! -v synth_current_system ]; then
detect_system
fi;;
*) # invalid option was given
synth_invalid=1;;
esac
shift 1
done
# say invalid option if we get an invalid option (duh)
if [ -v synth_invalid ]; then
echo "${red}Error:${normal} Invalid option."
echo "\"helperbot is very confused... >~<\""
echo
echo "Run with --help to see all options."
exit 1
fi
# runs if no option was specified; throw up the help menu
# otherwise: also run if specified
if [[ ! -v synth_args_exist || -v synth_help ]]; then
info_help
exit 0
fi
# ╭──────────────╮
# │ upgrade step │
# ╰──────────────╯
if [ -v synth_upgrade ]; then
timestamp=$(date +'%Y%m%d%H%M%S')
synth_upgrade_log=/tmp/upgrade-output-${timestamp}.txt
# phosphorus
if [ "$synth_current_system" = "phosphorus" ]; then
echo "${blue}upgrade:${normal} Running full system upgrade for ${green}${synth_current_system}${normal}."
echo "Upgrade will be logged into ${yellow}${synth_upgrade_log}${normal} if needed."
# apt/system related upgrade
base_system_upgrade
# docker
upgrade_docker_container "/srv/docker" "sharkey" "compose.yaml"
upgrade_docker_container "/srv/docker" "iceshrimp" "compose.yaml"
upgrade_docker_container "/srv/docker" "mastodon" "compose.yaml"
upgrade_docker_container "/srv/docker" "pds" "compose.yaml"
# done
echo "${green}System upgrade finished! beep!~${normal}"
fi
fi
#if [[ -v system_vacuum ]]; then
# echo "${yellow}NOTICE:${normal} You've also passed in the --vacuum command. Note that upgrading also automatically vacuums the databases beforehand."
# sleep 1
#fi
# unset everything
unset synth_help && unset synth_upgrade && unset synth_backup && unset synth_vacuum && unset synth_invalid && unset synth_current_system