This commit is contained in:
Strix 2024-06-02 22:13:33 +02:00
commit 1816522a71
11 changed files with 229 additions and 0 deletions

6
README.md Normal file
View file

@ -0,0 +1,6 @@
# SConnect
Saluco Connect
## Installing sconnect
local: `SC_SRC=. bin/sc -h local install`
remote (when installed locally): `sc -h <remote> install`

6
assets/saluco-header.txt Normal file
View file

@ -0,0 +1,6 @@
_
___ __ _| |_ _ ___ ___
/ __|/ _` | | | | |/ __/ _ \
\__ \ (_| | | |_| | (_| (_) |
|___/\__,_|_|\__,_|\___\___/

46
bin/sc Executable file
View file

@ -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>"
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

44
bin/sc-client Executable file
View file

@ -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>"
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 <library> <command>"
. $SC_LIB/$1
shift
$@
;;
check)
exit 0
;;
*)
fatal_log "invalid command"
;;
esac

8
lib/headers Normal file
View file

@ -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 ""
}

16
lib/logger Normal file
View file

@ -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
}

5
lib/manipulation Normal file
View file

@ -0,0 +1,5 @@
#!/bin/sh
get_first_in_list() {
echo $1
}

64
lib/pm Normal file
View file

@ -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 <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
}

18
lib/privileges Normal file
View file

@ -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) $@
}

10
lib/setup-env Normal file
View file

@ -0,0 +1,10 @@
#!/bin/sh
set -e
. $SC_SRC/lib/variables
. $SC_LIB/headers
. $SC_LIB/logger
. $SC_LIB/manipulation
. $SC_LIB/privileges

6
lib/variables Normal file
View file

@ -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"