commit 1816522a71332423d102563b599c04fbaa47ee09 Author: Raine Date: Sun Jun 2 22:13:33 2024 +0200 init diff --git a/README.md b/README.md new file mode 100644 index 0000000..4e6df5b --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +# SConnect +Saluco Connect + +## Installing sconnect +local: `SC_SRC=. bin/sc -h local install` +remote (when installed locally): `sc -h install` \ No newline at end of file diff --git a/assets/saluco-header.txt b/assets/saluco-header.txt new file mode 100644 index 0000000..6bd2b62 --- /dev/null +++ b/assets/saluco-header.txt @@ -0,0 +1,6 @@ + _ + ___ __ _| |_ _ ___ ___ + / __|/ _` | | | | |/ __/ _ \ + \__ \ (_| | | |_| | (_| (_) | + |___/\__,_|_|\__,_|\___\___/ + diff --git a/bin/sc b/bin/sc new file mode 100755 index 0000000..987587c --- /dev/null +++ b/bin/sc @@ -0,0 +1,46 @@ +#!/bin/sh + +SC_COMMAND="$0 $@" +SC_SRC=${SC_SRC:-/opt/sconnect} +. $SC_SRC/lib/setup-env + +SC_HOSTS="" + +while getopts "h:" opt; do + case $opt in + h) + SC_HOSTS="$SC_HOSTS $OPTARG" + ;; + esac +done +shift $((OPTIND - 1)) + +([ $# -lt 1 ] || [ -z "$1" ]) && fatal_log "usage: $0 [...args] " +command=$1 +shift + +for host in $SC_HOSTS; do + case $command in + install) + if [ "$host" = "local" ]; then + escalate_command cp -r $SC_SRC/. /opt/sconnect + escalate_command install -m a+rx -o root /opt/sconnect/bin/sc /usr/bin + escalate_command install -m a+rx -o root /opt/sconnect/bin/sc-client /usr/bin + 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 + LOGI "installed" + fi + ;; + *) + if [ "$host" = "local" ]; then + $SC_SRC/bin/sc-client $@ + else + ssh -t $host SC_SKIP_WELCOME_HEADER=1 /opt/sconnect/bin/sc-client check 2>/dev/null || fatal_log not installed + ssh -t $host /opt/sconnect/bin/sc-client $command $@ + fi + ;; + esac +done diff --git a/bin/sc-client b/bin/sc-client new file mode 100755 index 0000000..a1db955 --- /dev/null +++ b/bin/sc-client @@ -0,0 +1,44 @@ +#!/bin/sh + +SC_COMMAND="$0 $@" +SC_SRC=${SC_SRC:-/opt/sconnect} +. $SC_SRC/lib/setup-env + +show_welcome_header + +[ $# -lt 1 ] && fatal_log "usage: $0 " +command=$1 +shift + +case $command in +update) + LOGI attempting to update system... + escalate lib pm upgrade_packages + ;; +run) + if [ -z "$@" ]; then + if command -v zsh >/dev/null; then + zsh + elif command -v bash >/dev/null; then + bash + else + sh + fi + else + $@ + fi + + ;; +lib) + [ $# -lt 2 ] && fatal_log "usage: $0 lib " + . $SC_LIB/$1 + shift + $@ + ;; +check) + exit 0 +;; +*) + fatal_log "invalid command" + ;; +esac diff --git a/lib/headers b/lib/headers new file mode 100644 index 0000000..509e8e1 --- /dev/null +++ b/lib/headers @@ -0,0 +1,8 @@ +#!/bin/sh + +show_welcome_header() { + [ "$SC_SKIP_WELCOME_HEADER" = "1" ] && return 0 + cat $SC_ASSETS/saluco-header.txt + echo "Welcome, $(whoami) ($(id -u))!" + echo "" +} diff --git a/lib/logger b/lib/logger new file mode 100644 index 0000000..4522a14 --- /dev/null +++ b/lib/logger @@ -0,0 +1,16 @@ +#!/bin/sh + +__sc_log() { + level=$1; shift + echo "[$level] $@" +} + +alias LOGD="__sc_log debug" +alias LOGI="__sc_log info" +alias LOGW="__sc_log warn" +alias LOGE="__sc_log error" + +fatal_log() { + __sc_log fatal $@ + exit 1 +} \ No newline at end of file diff --git a/lib/manipulation b/lib/manipulation new file mode 100644 index 0000000..66e4452 --- /dev/null +++ b/lib/manipulation @@ -0,0 +1,5 @@ +#!/bin/sh + +get_first_in_list() { + echo $1 +} \ No newline at end of file diff --git a/lib/pm b/lib/pm new file mode 100644 index 0000000..e4bb284 --- /dev/null +++ b/lib/pm @@ -0,0 +1,64 @@ +#!/bin/sh + +# sc.requires: logger + +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 + echo dnf + elif command -v yum > /dev/null; then + echo yum + elif command -v zypper > /dev/null; then + echo zypper + elif command -v brew > /dev/null; then + echo brew + elif command -v apk > /dev/null; then + echo apk + elif command -v nix-env > /dev/null; then + echo nix + else + fatal_log "no supported package manager found" + exit 1 + fi +} + +SC_PACKAGE_MANAGER="${SC_PACKAGE_MANAGER:-$(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 + ;; + 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 + ;; + esac +} diff --git a/lib/privileges b/lib/privileges new file mode 100644 index 0000000..9a9ce0a --- /dev/null +++ b/lib/privileges @@ -0,0 +1,18 @@ +#!/bin/sh + +escalate_command() { + if [ "$(id -u)" = "0" ]; then + $@ + else + sudo -E $@ + fi +} + +escalate() { + export SC_ESCALATED=1 + export SC_SKIP_WELCOME_HEADER=1 + + echo "e: sc-client $@" + + escalate_command $(get_first_in_list $SC_COMMAND) $@ +} \ No newline at end of file diff --git a/lib/setup-env b/lib/setup-env new file mode 100644 index 0000000..da493b5 --- /dev/null +++ b/lib/setup-env @@ -0,0 +1,10 @@ +#!/bin/sh + +set -e + +. $SC_SRC/lib/variables + +. $SC_LIB/headers +. $SC_LIB/logger +. $SC_LIB/manipulation +. $SC_LIB/privileges \ No newline at end of file diff --git a/lib/variables b/lib/variables new file mode 100644 index 0000000..c9f83d3 --- /dev/null +++ b/lib/variables @@ -0,0 +1,6 @@ +#!/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