From 4be03b976435b33eefa1a4afb22614b06d03e25d Mon Sep 17 00:00:00 2001 From: Strix Date: Thu, 6 Jun 2024 02:00:49 +0200 Subject: [PATCH] feat: a3 and other things --- assets/saluco-header.txt | 6 -- bin/sc | 8 +- bin/sc-client | 37 ++++++---- lib/logger | 38 +++++++--- lib/pm | 153 ++++++++++++++++++++++++++++----------- lib/setup-env | 14 +++- lib/variables | 6 +- 7 files changed, 180 insertions(+), 82 deletions(-) delete mode 100644 assets/saluco-header.txt diff --git a/assets/saluco-header.txt b/assets/saluco-header.txt deleted file mode 100644 index 6bd2b62..0000000 --- a/assets/saluco-header.txt +++ /dev/null @@ -1,6 +0,0 @@ - _ - ___ __ _| |_ _ ___ ___ - / __|/ _` | | | | |/ __/ _ \ - \__ \ (_| | | |_| | (_| (_) | - |___/\__,_|_|\__,_|\___\___/ - diff --git a/bin/sc b/bin/sc index 5dc0145..c0062a3 100755 --- a/bin/sc +++ b/bin/sc @@ -1,8 +1,8 @@ #!/bin/sh SC_COMMAND="$0 $@" -SC_SRC=${SC_SRC:-/opt/sconnect} -. $SC_SRC/lib/setup-env +SC_LIB=${SC_LIB:-/usr/lib/sc} +. $SC_LIB/setup-env SC_HOSTS="" @@ -29,8 +29,8 @@ for host in $SC_HOSTS; do else LOGI "attempting to install on $host..." rsync -au $SC_SRC/. $host:/opt/sconnect - ssh $host SC_SKIP_WELCOME_HEADER=1 /opt/sconnect/bin/sc-client lib privileges escalate_command install -m a+rx -o root /opt/sconnect/bin/sc /usr/bin - ssh $host SC_SKIP_WELCOME_HEADER=1 /opt/sconnect/bin/sc-client lib privileges escalate_command install -m a+rx -o root /opt/sconnect/bin/sc-client /usr/bin + ssh $host SC_SKIP_WELCOME_HEADER=1 SC_INCLUDE_LIBS="privileges" /opt/sconnect/bin/sc-client eval escalate_command install -m a+rx -o root /opt/sconnect/bin/sc /usr/bin + ssh $host SC_SKIP_WELCOME_HEADER=1 SC_INCLUDE_LIBS="privileges" /opt/sconnect/bin/sc-client eval escalate_command install -m a+rx -o root /opt/sconnect/bin/sc-client /usr/bin LOGI "installed" fi ;; diff --git a/bin/sc-client b/bin/sc-client index a1db955..928e6a1 100755 --- a/bin/sc-client +++ b/bin/sc-client @@ -1,8 +1,8 @@ #!/bin/sh SC_COMMAND="$0 $@" -SC_SRC=${SC_SRC:-/opt/sconnect} -. $SC_SRC/lib/setup-env +SC_LIB=${SC_LIB:-/usr/lib/sc} +. $SC_LIB/setup-env show_welcome_header @@ -11,9 +11,12 @@ command=$1 shift case $command in -update) - LOGI attempting to update system... - escalate lib pm upgrade_packages +install-dotfiles) + [ -d "$HOME/.dotfiles" ] && fatal_log "dotfiles already exist" + LOGI installing dotfiles... + git clone https://git.saluco.nl/strix/dotfiles $HOME/.dotfiles + cd $HOME/.dotfiles + ./dot a ;; run) if [ -z "$@" ]; then @@ -27,18 +30,20 @@ run) else $@ fi + ;; +pm) + . $SC_LIB/pm + case $1 in + upgrade) pm_upgrade_packages ;; + install) shift; pm_install_packages $@ ;; + remove) shift; pm_remove_packages $@ ;; + esac ;; -lib) - [ $# -lt 2 ] && fatal_log "usage: $0 lib " - . $SC_LIB/$1 - shift - $@ - ;; -check) - exit 0 -;; -*) - fatal_log "invalid command" +eval) + LOGD "evaluating: $@..." + $@ ;; +check) exit 0 ;; +*) fatal_log "invalid command" ;; esac diff --git a/lib/logger b/lib/logger index 4522a14..1884b8f 100644 --- a/lib/logger +++ b/lib/logger @@ -1,16 +1,36 @@ #!/bin/sh -__sc_log() { - level=$1; shift - echo "[$level] $@" +# levels: +# DEBUG = 0 +# INFO = 1 +# WARN = 2 +# ERROR = 3 +SC_LOGGER_LEVEL=${SC_LOGGER_LEVEL:-1} + +__sc_log_level_to_text() { + case $1 in + 0) echo "debug" ;; + 1) echo "info" ;; + 2) echo "warn" ;; + 3) echo "error" ;; + 4) echo "fatal" ;; + esac } -alias LOGD="__sc_log debug" -alias LOGI="__sc_log info" -alias LOGW="__sc_log warn" -alias LOGE="__sc_log error" +__sc_log() { + level=$1 + shift + if [ "$level" -ge "$SC_LOGGER_LEVEL" ]; then + echo "[$(__sc_log_level_to_text $level)] $@" + fi +} + +alias LOGD="__sc_log 0" +alias LOGI="__sc_log 1" +alias LOGW="__sc_log 2" +alias LOGE="__sc_log 3" fatal_log() { - __sc_log fatal $@ + __sc_log 4 $@ exit 1 -} \ No newline at end of file +} diff --git a/lib/pm b/lib/pm index e4bb284..d5a5dc3 100644 --- a/lib/pm +++ b/lib/pm @@ -2,22 +2,22 @@ # sc.requires: logger -detect_package_manager() { - if command -v apt > /dev/null; then +pm_detect_package_manager() { + if command -v apt >/dev/null; then echo apt - elif command -v pacman > /dev/null; then - echo arch - elif command -v dnf > /dev/null; then + elif command -v pacman >/dev/null; then + echo pacman + elif command -v dnf >/dev/null; then echo dnf - elif command -v yum > /dev/null; then + elif command -v yum >/dev/null; then echo yum - elif command -v zypper > /dev/null; then + elif command -v zypper >/dev/null; then echo zypper - elif command -v brew > /dev/null; then + elif command -v brew >/dev/null; then echo brew - elif command -v apk > /dev/null; then + elif command -v apk >/dev/null; then echo apk - elif command -v nix-env > /dev/null; then + elif command -v nix-env >/dev/null; then echo nix else fatal_log "no supported package manager found" @@ -25,40 +25,111 @@ detect_package_manager() { fi } -SC_PACKAGE_MANAGER="${SC_PACKAGE_MANAGER:-$(detect_package_manager)}" +SC_PACKAGE_MANAGER="${SC_PACKAGE_MANAGER:-$(pm_detect_package_manager)}" -upgrade_packages() { - case $SC_PACKAGE_MANAGER in - apt) apt update && apt upgrade -y ;; - arch) pacman -Syyu --noconfirm ;; - dnf) dnf upgrade -y ;; - yum) yum update -y ;; - zypper) zypper refresh && zypper update -y ;; - brew) brew update && brew upgrade ;; - apk) apk update && apk upgrade ;; - nix) nix-channel --update && nix-env -u ;; - *) - fatal_log "unsupported package manager: $SC_PACKAGE_MANAGER" - exit 1 - ;; +pm_upgrade_packages() { + LOGI "Upgrading packages using ${SC_PACKAGE_MANAGER}..." + + case "$SC_PACKAGE_MANAGER" in + apt) + sudo apt update && sudo apt upgrade -y + ;; + pacman) + sudo pacman -Syu + ;; + dnf) + sudo dnf upgrade --refresh -y + ;; + yum) + sudo yum update -y + ;; + zypper) + sudo zypper refresh && sudo zypper update -y + ;; + brew) + brew update && brew upgrade + ;; + apk) + sudo apk update && sudo apk upgrade + ;; + nix) + nix-env --upgrade + ;; + *) + fatal_log "Unsupported package manager: ${SC_PACKAGE_MANAGER}" + exit 1 + ;; esac } -install_package() { - [ $# -lt 1 ] && fatal_log "usage: install_package " && exit 1 - package=$1 - case $SC_PACKAGE_MANAGER in - apt) apt install -y "$package" ;; - arch) pacman -S --noconfirm "$package" ;; - dnf) dnf install -y "$package" ;; - yum) yum install -y "$package" ;; - zypper) zypper install -y "$package" ;; - brew) brew install "$package" ;; - apk) apk add "$package" ;; - nix) nix-env -iA nixpkgs."$package" ;; - *) - fatal_log "unsupported package manager: $SC_PACKAGE_MANAGER" - exit 1 - ;; +pm_install_packages() { + LOGI "Installing packages using ${SC_PACKAGE_MANAGER}..." + packages="$@" + + case "$SC_PACKAGE_MANAGER" in + apt) + sudo apt install -y $packages + ;; + pacman) + sudo pacman -S --noconfirm $packages + ;; + dnf) + sudo dnf install -y $packages + ;; + yum) + sudo yum install -y $packages + ;; + zypper) + sudo zypper install -y $packages + ;; + brew) + brew install $packages + ;; + apk) + sudo apk add $packages + ;; + nix) + nix-env -i $packages + ;; + *) + fatal_log "Unsupported package manager: ${SC_PACKAGE_MANAGER}" + exit 1 + ;; + esac +} + +pm_remove_packages() { + LOGI "Removing packages using ${SC_PACKAGE_MANAGER}..." + packages="$@" + + case "$SC_PACKAGE_MANAGER" in + apt) + sudo apt remove -y $packages + ;; + pacman) + sudo pacman -Rns --noconfirm $packages + ;; + dnf) + sudo dnf remove -y $packages + ;; + yum) + sudo yum remove -y $packages + ;; + zypper) + sudo zypper remove -y $packages + ;; + brew) + brew uninstall $packages + ;; + apk) + sudo apk del $packages + ;; + nix) + nix-env -e $packages + ;; + *) + fatal_log "Unsupported package manager: ${SC_PACKAGE_MANAGER}" + exit 1 + ;; esac } diff --git a/lib/setup-env b/lib/setup-env index da493b5..56b59f8 100644 --- a/lib/setup-env +++ b/lib/setup-env @@ -2,9 +2,19 @@ set -e -. $SC_SRC/lib/variables +if [ $SC_DEBUG ]; then + SC_LOGGER_LEVEL=0 + echo "*** SC DEBUG ***" +fi +. $SC_LIB/variables . $SC_LIB/headers . $SC_LIB/logger . $SC_LIB/manipulation -. $SC_LIB/privileges \ No newline at end of file +. $SC_LIB/privileges + +for l in $SC_INCLUDE_LIBS; do + [ -f "$SC_LIB/$l" ] || continue + LOGD "including $l..." + . $SC_LIB/$l +done \ No newline at end of file diff --git a/lib/variables b/lib/variables index c9f83d3..67120c4 100644 --- a/lib/variables +++ b/lib/variables @@ -1,6 +1,4 @@ #!/bin/sh -export SC_SRC="${SC_SRC:-/opt/sconnect}" - -export SC_LIB="$SC_SRC/lib" -export SC_ASSETS="$SC_SRC/assets" \ No newline at end of file +export SC_LIB=${SC_LIB:-/usr/lib/sc} +export SC_ASSETS="$SC_LIB/assets" \ No newline at end of file