1
0
forked from finn/tinyboard

Compare commits

..

3 Commits

View File

@@ -3,8 +3,7 @@ set -euo pipefail
HUB_HOST="" HUB_HOST=""
HUB_USER="" HUB_USER=""
ARMBIAN_HOME="/home/armbian" SPOKE_USER=""
SSH_DIR="$ARMBIAN_HOME/.ssh"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SPOKE_DIR="$SCRIPT_DIR" SPOKE_DIR="$SCRIPT_DIR"
COMPOSE="$SPOKE_DIR/compose.yaml" COMPOSE="$SPOKE_DIR/compose.yaml"
@@ -46,19 +45,83 @@ read -rp "Hub hostname [oily.dad]: " HUB_HOST
HUB_HOST="${HUB_HOST:-oily.dad}" HUB_HOST="${HUB_HOST:-oily.dad}"
read -rp "Hub SSH user [armbian]: " HUB_USER read -rp "Hub SSH user [armbian]: " HUB_USER
HUB_USER="${HUB_USER:-armbian}" HUB_USER="${HUB_USER:-armbian}"
read -rp "Spoke local user [armbian]: " SPOKE_USER
SPOKE_USER="${SPOKE_USER:-armbian}"
ARMBIAN_HOME="/home/$SPOKE_USER"
SSH_DIR="$ARMBIAN_HOME/.ssh"
header "TinyBoard Spoke Setup" header "TinyBoard Spoke Setup"
info "Installing packages..." header "Detecting Package Manager"
apt-get update -q if command -v apt-get >/dev/null 2>&1; then
apt-get install -y -q vim autossh docker.io docker-compose-plugin git openssh-server PKG_MANAGER="apt"
PKG_INSTALL="apt-get install -y -q"
OPENSSH_PKG="openssh-server"
AUTOSSH_PKG="autossh"
info "Detected: apt (Debian/Ubuntu)"
apt-get update -q
elif command -v dnf >/dev/null 2>&1; then
PKG_MANAGER="dnf"
PKG_INSTALL="dnf install -y -q"
OPENSSH_PKG="openssh-server"
AUTOSSH_PKG="autossh"
info "Detected: dnf (Fedora/RHEL/Alma/Rocky)"
dnf check-update -q || true
elif command -v yum >/dev/null 2>&1; then
PKG_MANAGER="yum"
PKG_INSTALL="yum install -y -q"
OPENSSH_PKG="openssh-server"
AUTOSSH_PKG="autossh"
info "Detected: yum (older RHEL/CentOS)"
yum check-update -q || true
elif command -v pacman >/dev/null 2>&1; then
PKG_MANAGER="pacman"
PKG_INSTALL="pacman -S --noconfirm --quiet"
OPENSSH_PKG="openssh"
AUTOSSH_PKG="autossh"
info "Detected: pacman (Arch)"
pacman -Sy --quiet
else
die "No supported package manager found (apt, dnf, yum, pacman)"
fi
info "Adding armbian to docker group..." header "Installing Packages"
usermod -aG docker armbian 2>/dev/null || true if ! command -v curl >/dev/null 2>&1; then
$PKG_INSTALL curl
fi
$PKG_INSTALL vim "$AUTOSSH_PKG" "$OPENSSH_PKG" git
info "Installing Docker..."
if ! command -v docker >/dev/null 2>&1; then
if [ "$PKG_MANAGER" = "apt" ]; then
$PKG_INSTALL docker.io docker-compose-plugin
else
curl -fsSL https://get.docker.com | bash
fi
else
warn "Docker already installed, skipping."
fi
if ! docker compose version >/dev/null 2>&1; then
if [ "$PKG_MANAGER" = "apt" ]; then
$PKG_INSTALL docker-compose-plugin
else
warn "docker compose not available — Docker install script should have included it."
fi
fi
info "Adding $SPOKE_USER to docker group..."
usermod -aG docker "$SPOKE_USER" 2>/dev/null || true
info "Enabling SSH server..." info "Enabling SSH server..."
systemctl enable ssh if systemctl enable ssh 2>/dev/null; then
systemctl start ssh systemctl start ssh
elif systemctl enable sshd 2>/dev/null; then
systemctl start sshd
else
warn "Could not enable SSH service — please start it manually."
fi
header "Hostname Setup" header "Hostname Setup"
CURRENT_HOSTNAME=$(hostname) CURRENT_HOSTNAME=$(hostname)
@@ -82,22 +145,22 @@ case "$KEY_CHOICE" in
KEY_NAME="${KEY_NAME:-hubkey}" KEY_NAME="${KEY_NAME:-hubkey}"
KEY_PATH="$SSH_DIR/$KEY_NAME" KEY_PATH="$SSH_DIR/$KEY_NAME"
mkdir -p "$SSH_DIR" mkdir -p "$SSH_DIR"
chown armbian:armbian "$SSH_DIR" chown "$SPOKE_USER":"$SPOKE_USER" "$SSH_DIR"
chmod 700 "$SSH_DIR" chmod 700 "$SSH_DIR"
if [ -f "$KEY_PATH" ]; then if [ -f "$KEY_PATH" ]; then
warn "Key $KEY_PATH already exists, using it." warn "Key $KEY_PATH already exists, using it."
else else
info "Generating new ED25519 key..." info "Generating new ED25519 key..."
sudo -u armbian ssh-keygen -t ed25519 -f "$KEY_PATH" -N "" sudo -u "$SPOKE_USER" ssh-keygen -t ed25519 -f "$KEY_PATH" -N ""
chown armbian:armbian "$KEY_PATH" "$KEY_PATH.pub" chown "$SPOKE_USER":"$SPOKE_USER" "$KEY_PATH" "$KEY_PATH.pub"
chmod 600 "$KEY_PATH" chmod 600 "$KEY_PATH"
fi fi
echo "" echo ""
echo -e "${YELLOW}══════════════════════════════════════════${NC}" echo -e "${YELLOW}══════════════════════════════════════════${NC}"
echo -e "${YELLOW} Send this public key to the hub owner${NC}" echo -e "${YELLOW} Send this public key to the hub owner${NC}"
echo -e "${YELLOW} and ask them to add it to armbian@${HUB_HOST} authorized_keys:${NC}" echo -e "${YELLOW} and ask them to add it to ${HUB_USER}@${HUB_HOST} authorized_keys:${NC}"
echo -e "${YELLOW}══════════════════════════════════════════${NC}" echo -e "${YELLOW}══════════════════════════════════════════${NC}"
cat "$KEY_PATH.pub" cat "$KEY_PATH.pub"
echo -e "${YELLOW}══════════════════════════════════════════${NC}" echo -e "${YELLOW}══════════════════════════════════════════${NC}"
@@ -109,13 +172,13 @@ case "$KEY_CHOICE" in
KEY_NAME="${KEY_NAME:-hubkey}" KEY_NAME="${KEY_NAME:-hubkey}"
KEY_PATH="$SSH_DIR/$KEY_NAME" KEY_PATH="$SSH_DIR/$KEY_NAME"
mkdir -p "$SSH_DIR" mkdir -p "$SSH_DIR"
chown armbian:armbian "$SSH_DIR" chown "$SPOKE_USER":"$SPOKE_USER" "$SSH_DIR"
chmod 700 "$SSH_DIR" chmod 700 "$SSH_DIR"
echo "Paste the private key content below, then press ENTER and CTRL+D:" echo "Paste the private key content below, then press ENTER and CTRL+D:"
KEY_CONTENT=$(cat | tr -d '\r') KEY_CONTENT=$(cat | tr -d '\r')
printf '%s\n' "$KEY_CONTENT" > "$KEY_PATH" printf '%s\n' "$KEY_CONTENT" > "$KEY_PATH"
chown armbian:armbian "$KEY_PATH" chown "$SPOKE_USER":"$SPOKE_USER" "$KEY_PATH"
chmod 600 "$KEY_PATH" chmod 600 "$KEY_PATH"
info "Key saved to $KEY_PATH" info "Key saved to $KEY_PATH"
;; ;;
@@ -125,22 +188,22 @@ case "$KEY_CHOICE" in
esac esac
info "Scanning hub host key..." info "Scanning hub host key..."
sudo -u armbian touch "$SSH_DIR/known_hosts" sudo -u "$SPOKE_USER" touch "$SSH_DIR/known_hosts"
chown armbian:armbian "$SSH_DIR/known_hosts" chown "$SPOKE_USER":"$SPOKE_USER" "$SSH_DIR/known_hosts"
chmod 600 "$SSH_DIR/known_hosts" chmod 600 "$SSH_DIR/known_hosts"
sudo -u armbian ssh-keyscan -H "$HUB_HOST" >> "$SSH_DIR/known_hosts" 2>/dev/null sudo -u "$SPOKE_USER" ssh-keyscan -H "$HUB_HOST" >> "$SSH_DIR/known_hosts" 2>/dev/null
header "Testing SSH Connection" header "Testing SSH Connection"
info "Testing connection to $HUB_HOST..." info "Testing connection to $HUB_HOST..."
retry_or_abort \ retry_or_abort \
"sudo -u armbian ssh -i \"$KEY_PATH\" -o BatchMode=yes -o ConnectTimeout=10 \"$HUB_USER@$HUB_HOST\" exit" \ "sudo -u \"$SPOKE_USER\" ssh -i \"$KEY_PATH\" -o BatchMode=yes -o ConnectTimeout=10 \"$HUB_USER@$HUB_HOST\" exit" \
"SSH connection to $HUB_HOST failed. Check that the hub owner added your public key." "SSH connection to $HUB_HOST failed. Check that the hub owner added your public key."
header "Finding Available Tunnel Port" header "Finding Available Tunnel Port"
info "Scanning for a free port on $HUB_HOST starting from $START_PORT..." info "Scanning for a free port on $HUB_HOST starting from $START_PORT..."
TUNNEL_PORT="" TUNNEL_PORT=""
for PORT in $(seq "$START_PORT" $((START_PORT + 20))); do for PORT in $(seq "$START_PORT" $((START_PORT + 20))); do
RESULT=$(sudo -u armbian ssh -i "$KEY_PATH" "$HUB_USER@$HUB_HOST" "ss -tlnp | grep :$PORT" 2>/dev/null || true) RESULT=$(sudo -u "$SPOKE_USER" ssh -i "$KEY_PATH" "$HUB_USER@$HUB_HOST" "ss -tlnp | grep :$PORT" 2>/dev/null || true)
if [ -z "$RESULT" ]; then if [ -z "$RESULT" ]; then
TUNNEL_PORT="$PORT" TUNNEL_PORT="$PORT"
info "Port $TUNNEL_PORT is available." info "Port $TUNNEL_PORT is available."
@@ -156,7 +219,7 @@ header "Configuring compose.yaml"
info "Setting port to $TUNNEL_PORT and key to $KEY_NAME..." info "Setting port to $TUNNEL_PORT and key to $KEY_NAME..."
sed -i "s|-R [0-9]*:localhost:22|-R ${TUNNEL_PORT}:localhost:22|g" "$COMPOSE" sed -i "s|-R [0-9]*:localhost:22|-R ${TUNNEL_PORT}:localhost:22|g" "$COMPOSE"
sed -i "s|-i /home/armbian/.ssh/[^ ]*|-i /home/armbian/.ssh/${KEY_NAME}|g" "$COMPOSE" sed -i "s|-i /home/[^ ]*/\.ssh/[^ ]*|-i ${SSH_DIR}/${KEY_NAME}|g" "$COMPOSE"
sed -i "s|/home/armbian/.ssh/[^:]*:/home/armbian/.ssh/[^:]*|${SSH_DIR}/${KEY_NAME}:${SSH_DIR}/${KEY_NAME}|g" "$COMPOSE" sed -i "s|/home/armbian/.ssh/[^:]*:/home/armbian/.ssh/[^:]*|${SSH_DIR}/${KEY_NAME}:${SSH_DIR}/${KEY_NAME}|g" "$COMPOSE"
sed -i "s|container_name: spoke-autossh|container_name: ${SPOKE_NAME}-autossh|g" "$COMPOSE" sed -i "s|container_name: spoke-autossh|container_name: ${SPOKE_NAME}-autossh|g" "$COMPOSE"
sed -i "s|container_name: spoke-syncthing|container_name: ${SPOKE_NAME}-syncthing|g" "$COMPOSE" sed -i "s|container_name: spoke-syncthing|container_name: ${SPOKE_NAME}-syncthing|g" "$COMPOSE"
@@ -165,13 +228,13 @@ sed -i '/^version:/d' "$COMPOSE"
SYNCTHING_MOUNT="$ARMBIAN_HOME/st" SYNCTHING_MOUNT="$ARMBIAN_HOME/st"
mkdir -p "$SYNCTHING_MOUNT" mkdir -p "$SYNCTHING_MOUNT"
chown armbian:armbian "$SYNCTHING_MOUNT" chown "$SPOKE_USER":"$SPOKE_USER" "$SYNCTHING_MOUNT"
header "Building Docker Image" header "Building Docker Image"
cd "$SPOKE_DIR" cd "$SPOKE_DIR"
docker build \ docker build \
--build-arg UID="$(id -u armbian)" \ --build-arg UID="$(id -u "$SPOKE_USER")" \
--build-arg GID="$(id -g armbian)" \ --build-arg GID="$(id -g "$SPOKE_USER")" \
-t spoke-autossh . -t spoke-autossh .
header "Starting Containers" header "Starting Containers"
@@ -199,14 +262,14 @@ echo " 1. Generate a hub->spoke key:"
echo " ssh-keygen -t ed25519 -f ~/.ssh/armbian-${SPOKE_NAME}-$(date +%Y%m)" echo " ssh-keygen -t ed25519 -f ~/.ssh/armbian-${SPOKE_NAME}-$(date +%Y%m)"
echo "" echo ""
echo " 2. Copy it to this spoke through the tunnel:" echo " 2. Copy it to this spoke through the tunnel:"
echo " ssh-copy-id -i ~/.ssh/armbian-${SPOKE_NAME}-$(date +%Y%m).pub -p $TUNNEL_PORT armbian@localhost" echo " ssh-copy-id -i ~/.ssh/armbian-${SPOKE_NAME}-$(date +%Y%m).pub -p $TUNNEL_PORT ${HUB_USER}@localhost"
echo "" echo ""
echo " 3. Add an rclone remote in ~/.config/rclone/rclone.conf:" echo " 3. Add an rclone remote in ~/.config/rclone/rclone.conf:"
echo " [${SPOKE_NAME}-remote]" echo " [${SPOKE_NAME}-remote]"
echo " type = sftp" echo " type = sftp"
echo " host = localhost" echo " host = localhost"
echo " port = $TUNNEL_PORT" echo " port = $TUNNEL_PORT"
echo " key_file = /home/armbian/.ssh/armbian-${SPOKE_NAME}-$(date +%Y%m)" echo " key_file = /home/$HUB_USER/.ssh/armbian-${SPOKE_NAME}-$(date +%Y%m)"
echo " shell_type = unix" echo " shell_type = unix"
echo " md5sum_command = md5sum" echo " md5sum_command = md5sum"
echo " sha1sum_command = sha1sum" echo " sha1sum_command = sha1sum"