From f9d7c98916a626a2034383a9ed2ecb279698caff Mon Sep 17 00:00:00 2001 From: Coby Powers Date: Tue, 20 Jun 2023 23:12:12 -0500 Subject: [PATCH] Improve setup and update scripts pt.2 --- scripts/setup.sh | 52 +++++++++++++++++++++++++++--------- scripts/tasks.txt | 13 --------- scripts/update.sh | 18 +++++++++---- scripts/util/color.sh | 27 +++++++++++++++++++ scripts/util/pkg-mgr.sh | 23 ++++++++++++++++ sh/.vimrc | 3 ++- sh/custom-scripts/aliases.sh | 14 +--------- sh/custom-scripts/os.sh | 29 -------------------- sh/custom-scripts/pkg-mgr.sh | 22 --------------- {sshd => ssh}/sshd_config | 0 10 files changed, 105 insertions(+), 96 deletions(-) mode change 100644 => 100755 scripts/setup.sh delete mode 100644 scripts/tasks.txt create mode 100755 scripts/util/color.sh create mode 100755 scripts/util/pkg-mgr.sh delete mode 100755 sh/custom-scripts/os.sh delete mode 100755 sh/custom-scripts/pkg-mgr.sh rename {sshd => ssh}/sshd_config (100%) diff --git a/scripts/setup.sh b/scripts/setup.sh old mode 100644 new mode 100755 index 9772388..992215d --- a/scripts/setup.sh +++ b/scripts/setup.sh @@ -3,29 +3,55 @@ set -e SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) +TOOLKIT_DIR="$SCRIPT_DIR"/.. -# Load default shell scripts -. $SCRIPT_DIR/../sh/load.sh +# Load package manager script +. "$SCRIPT_DIR"/util/pkg-mgr.sh -update_default_shell() { - /usr/sbin/usermod -sf /usr/bin/zsh $1 +# Load color script +. "$SCRIPT_DIR"/util/color.sh + +update_user_shell() { + /usr/sbin/usermod -s /usr/bin/zsh "$1" } -install_shell_config() { - /usr/bin/ln -s /etc/toolkit/sh/.zshrc $1 +install_user_config() { + /usr/bin/ln -s /etc/toolkit/sh/"$1" "$2" } +print_info Installing necessary packages... + install_package zsh -update_default_shell root -update_default_shell coby +print_info Setting up user shells... -install_shell_config /root/.zshrc -install_shell_config /home/coby/.zshrc +update_user_shell root +update_user_shell coby + +install_user_config .zshrc /root/. +install_user_config .zshrc /home/coby/. + +install_user_config .vimrc /root/. +install_user_config .vimrc /home/coby/. # Replace old ssh server config -rm /etc/ssh/sshd_config -ln -sf /etc/toolkit/sshd/sshd_config /etc/ssh/. +if [[ -d /etc/ssh ]]; then + ln -sf "$TOOLKIT_DIR"/ssh/sshd_config /etc/ssh/. +elif + print_warn /etc/ssh does not exist, failed to install sshd config +fi + +# Install cronjobs +if [[ -d /etc/cron.d ]]; then + ln -sf "$TOOLKIT_DIR"/cron/* /etc/cron.d/. +elif + print_warn /etc/cron.d does not exist, failed to install cronjobs + print_warn Are you running a debian-based distro? +fi + +print_info Installing necessary submodules... # Pull submodules -. $SCRIPT_DIR/update.sh +git submodule update --init --recursive + +print_info Setup complete diff --git a/scripts/tasks.txt b/scripts/tasks.txt deleted file mode 100644 index bec485f..0000000 --- a/scripts/tasks.txt +++ /dev/null @@ -1,13 +0,0 @@ -# Setup tasks - -- Determine which package manager to use based on distro - -- Update & upgrade system - -- Install necessary packages (zsh) - -- Ask which user wants shell and dotfiles & install them - -- Ask to install cronjobs & install them - -- Ask to install SSH configurations & install them diff --git a/scripts/update.sh b/scripts/update.sh index de1fc54..d5b14ec 100755 --- a/scripts/update.sh +++ b/scripts/update.sh @@ -1,16 +1,21 @@ #!/bin/bash -# If repo isn't out of date, abort update +# Only begin update process if necessary if [[ -z "$(git remote show origin | grep "out of date")" ]]; then exit 0 fi SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) +TOOLKIT_DIR="$SCRIPT_DIR"/.. + +# Load color script +. "$SCRIPT_DIR"/util/color.sh last_hash=$( git rev-parse HEAD ) - recent_hashes=$( git log --format=format:%H | grep -B 100 $last_hash) +print_info Resetting repo and pulling latest commits... + # Resets the repo to last state git reset --hard @@ -24,8 +29,11 @@ git submodule update --init --recursive git clean -fd --force # Runs patch scripts to ensure machine state -# is up to date +# matches toolkit state while IFS= read -r hash; do - short_hash=$( echo $hash | cut -c -7 ) - /usr/bin/bash "$SCRIPT_DIR"/../patches/*-"$short_hash".sh 2>/dev/null + short_hash=$( echo "$hash" | cut -c -7 ) + print_info Running patch script \'"$short_hash"\' + . "$SCRIPT_DIR"/../patches/*-"$short_hash".sh 2>/dev/null done <<< "$recent_hashes" + +print_info Update complete diff --git a/scripts/util/color.sh b/scripts/util/color.sh new file mode 100755 index 0000000..a34ca82 --- /dev/null +++ b/scripts/util/color.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +BLACK="\033[30m" +RED="\033[31m" +GREEN="\033[32m" +YELLOW="\033[33m" +BLUE="\033[34m" +PINK="\033[35m" +CYAN="\033[36m" +WHITE="\033[37m" +RESET="\033[0;39m" + +print_info() { + echo -e "$RESET"["$BLUE"i"$RESET"] $@ +} + +print_warn () { + echo -e "$RESET"["$YELLOW"!"$RESET"] $@ +} + +print_error () { + echo -e "$RESET"["$RED"x"$RESET"] $@ +} + +print_info Hello +print_warn Hello +print_error Hello diff --git a/scripts/util/pkg-mgr.sh b/scripts/util/pkg-mgr.sh new file mode 100755 index 0000000..36a5b19 --- /dev/null +++ b/scripts/util/pkg-mgr.sh @@ -0,0 +1,23 @@ +declare -A osInfo; + +osInfo[/etc/redhat-release]=yum +osInfo[/etc/arch-release]=pacman +osInfo[/etc/gentoo-release]=emerge +osInfo[/etc/SuSE-release]=zypp +osInfo[/etc/debian_version]=apt-get +osInfo[/etc/alpine-release]=apk + +for key val in "${(@kv)osInfo}"; do + if [[ -f $key ]]; then + PACKAGE_MANAGER=$val + fi +done + +install_package() { + if [ "$PACKAGE_MANAGER" == "yum" ]; then yum install -y "$@" + elif [ "$PACKAGE_MANAGER" == "pacman" ]; then pacman -S --noconfirm "$@" + elif [ "$PACKAGE_MANAGER" == "emerge" ]; then emerge "$@" + elif [ "$PACKAGE_MANAGER" == "zypp" ]; then zypper install "$@" + elif [ "$PACKAGE_MANAGER" == "apt-get" ]; then apt-get install -y "$@" + elif [ "$PACKAGE_MANAGER" == "apk" ]; then apk add "$@"; fi +} diff --git a/sh/.vimrc b/sh/.vimrc index b70d510..89d086b 100644 --- a/sh/.vimrc +++ b/sh/.vimrc @@ -5,7 +5,8 @@ " \ \ / / \ \ \ \ \ \ \ \ \ \\ \\ \ \____ " \ \__/ / \ \__\ \__\ \ \__\ \__\\ _\\ \_______\ " \|__|/ \|__|\|__| \|__|\|__|\|__|\|_______| -" by Coby Powers (inspired by https://www.freecodecamp.org/news/vimrc-configuration-guide-customize-your-vim-editor/) +" by Coby Powers +" (inspired by https://www.freecodecamp.org/news/vimrc-configuration-guide-customize-your-vim-editor/) " Remove compatibility with vi set nocompatible diff --git a/sh/custom-scripts/aliases.sh b/sh/custom-scripts/aliases.sh index f17019a..9263dd9 100755 --- a/sh/custom-scripts/aliases.sh +++ b/sh/custom-scripts/aliases.sh @@ -18,28 +18,16 @@ alias md='mkdir -p' alias rd='rm -rf' alias q='exit' - alias c='clear' alias journal='journalctl -xe' alias journal-err='journalctl -p 3 -b' -alias fstab='sudo vim /etc/fstab' +alias fstab='_ vim /etc/fstab' alias rcron='sudo crontab -e' alias cron='crontab -e' -alias upd-arch='paru -S archlinux-keyring && paru' -alias upd-ubt='sudo apt update -y && sudo apt upgrade -y' - -if [[ "$OS" == "Arch Linux" ]]; then - alias upd='upd-arch' -elif [[ $OS == "Ubuntu" ]]; then - alias upd='upd-ubt' -else - alias upd='echo Failed to detect distribution. The script needs to be able to determine the distribution to know which package manager to use' -fi - alias reload='source ~/.zshrc' alias aliases='cat $ALIASES' diff --git a/sh/custom-scripts/os.sh b/sh/custom-scripts/os.sh deleted file mode 100755 index e392b89..0000000 --- a/sh/custom-scripts/os.sh +++ /dev/null @@ -1,29 +0,0 @@ -if [ -f /etc/os-release ]; then - # freedesktop.org and systemd - . /etc/os-release - OS=$NAME - VER=$VERSION_ID -elif type lsb_release >/dev/null 2>&1; then - # linuxbase.org - OS=$(lsb_release -si) - VER=$(lsb_release -sr) -elif [ -f /etc/lsb-release ]; then - # For some versions of Debian/Ubuntu without lsb_release command - . /etc/lsb-release - OS=$DISTRIB_ID - VER=$DISTRIB_RELEASE -elif [ -f /etc/debian_version ]; then - # Older Debian/Ubuntu/etc. - OS=Debian - VER=$(cat /etc/debian_version) -elif [ -f /etc/SuSe-release ]; then - # Older SuSE/etc. - ... -elif [ -f /etc/redhat-release ]; then - # Older Red Hat, CentOS, etc. - ... -else - # Fall back to uname, e.g. "Linux ", also works for BSD, etc. - OS=$(uname -s) - VER=$(uname -r) -fi diff --git a/sh/custom-scripts/pkg-mgr.sh b/sh/custom-scripts/pkg-mgr.sh deleted file mode 100755 index b1ca58a..0000000 --- a/sh/custom-scripts/pkg-mgr.sh +++ /dev/null @@ -1,22 +0,0 @@ -declare -A osInfo; -osInfo[/etc/redhat-release]=yum -osInfo[/etc/arch-release]=pacman -osInfo[/etc/gentoo-release]=emerge -osInfo[/etc/SuSE-release]=zypp -osInfo[/etc/debian_version]=apt-get -osInfo[/etc/alpine-release]=apk - -for key val in "${(@kv)osInfo}"; do - if [[ -f $key ]];then - PACKAGE_MANAGER=$val - fi -done - -install_package() { -if [ "$PACKAGE_MANAGER" == "yum" ]; then yum install -y $1 -elif [ "$PACKAGE_MANAGER" == "pacman" ]; then pacman -S --noconfirm $1 -elif [ "$PACKAGE_MANAGER" == "emerge" ]; then emerge $1 -elif [ "$PACKAGE_MANAGER" == "zypp" ]; then zypper install $1 -elif [ "$PACKAGE_MANAGER" == "apt-get" ]; then apt-get install -y $1 -elif [ "$PACKAGE_MANAGER" == "apk" ]; then apk add $1; fi -} diff --git a/sshd/sshd_config b/ssh/sshd_config similarity index 100% rename from sshd/sshd_config rename to ssh/sshd_config