forked from finn/tinyboard
Compare commits
2 Commits
ccd324dc79
...
395ab4ed0e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
395ab4ed0e | ||
|
|
4c08f3b389 |
@@ -4,23 +4,29 @@ set -euo pipefail
|
|||||||
RCLONE_CONF="${HOME}/.config/rclone/rclone.conf"
|
RCLONE_CONF="${HOME}/.config/rclone/rclone.conf"
|
||||||
SSH_DIR="${HOME}/.ssh"
|
SSH_DIR="${HOME}/.ssh"
|
||||||
|
|
||||||
if [ "$(id -u)" -eq 0 ]; then
|
|
||||||
echo -e "\033[0;31m[WARNING]\033[0m Running as root — keys will be written to /root/.ssh. Run as armbian instead."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
mkdir -p "$SSH_DIR"
|
|
||||||
touch "$SSH_DIR/known_hosts"
|
|
||||||
chmod 700 "$SSH_DIR"
|
|
||||||
chmod 600 "$SSH_DIR/known_hosts"
|
|
||||||
|
|
||||||
check_deps ssh ssh-keygen ssh-keyscan ssh-copy-id rclone
|
|
||||||
|
|
||||||
RED='\033[0;31m'
|
RED='\033[0;31m'
|
||||||
GREEN='\033[0;32m'
|
GREEN='\033[0;32m'
|
||||||
YELLOW='\033[1;33m'
|
YELLOW='\033[1;33m'
|
||||||
CYAN='\033[0;36m'
|
CYAN='\033[0;36m'
|
||||||
NC='\033[0m'
|
NC='\033[0m'
|
||||||
|
|
||||||
|
info() { echo -e "${GREEN}[+]${NC} $*"; }
|
||||||
|
warn() { echo -e "${YELLOW}[!]${NC} $*"; }
|
||||||
|
die() { echo -e "${RED}[ERROR]${NC} $*" >&2; exit 1; }
|
||||||
|
header() { echo -e "\n${CYAN}══════════════════════════════════════════${NC}"; echo -e "${CYAN} $*${NC}"; echo -e "${CYAN}══════════════════════════════════════════${NC}"; }
|
||||||
|
|
||||||
|
check_deps() {
|
||||||
|
local missing=()
|
||||||
|
for cmd in "$@"; do
|
||||||
|
if ! command -v "$cmd" >/dev/null 2>&1; then
|
||||||
|
missing+=("$cmd")
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
if [ ${#missing[@]} -gt 0 ]; then
|
||||||
|
die "Missing required dependencies: ${missing[*]}"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
retry_or_abort() {
|
retry_or_abort() {
|
||||||
local test_cmd="$1"
|
local test_cmd="$1"
|
||||||
local fail_msg="$2"
|
local fail_msg="$2"
|
||||||
@@ -40,36 +46,30 @@ retry_or_abort() {
|
|||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
info() { echo -e "${GREEN}[+]${NC} $*"; }
|
if [ "$(id -u)" -eq 0 ]; then
|
||||||
check_deps() {
|
die "Running as root — keys will be written to /root/.ssh. Run as the hub user instead."
|
||||||
local missing=()
|
fi
|
||||||
for cmd in "$@"; do
|
mkdir -p "$SSH_DIR"
|
||||||
if ! command -v "$cmd" >/dev/null 2>&1; then
|
touch "$SSH_DIR/known_hosts"
|
||||||
missing+=("$cmd")
|
chmod 700 "$SSH_DIR"
|
||||||
fi
|
chmod 600 "$SSH_DIR/known_hosts"
|
||||||
done
|
|
||||||
if [ ${#missing[@]} -gt 0 ]; then
|
|
||||||
die "Missing required dependencies: ${missing[*]}"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
|
check_deps ssh ssh-keygen ssh-keyscan ssh-copy-id rclone
|
||||||
warn() { echo -e "${YELLOW}[!]${NC} $*"; }
|
|
||||||
die() { echo -e "${RED}[ERROR]${NC} $*" >&2; exit 1; }
|
|
||||||
header() { echo -e "\n${CYAN}══════════════════════════════════════════${NC}"; echo -e "${CYAN} $*${NC}"; echo -e "${CYAN}══════════════════════════════════════════${NC}"; }
|
|
||||||
|
|
||||||
header "TinyBoard Hub — Onboard New Spoke"
|
header "TinyBoard Hub — Onboard New Spoke"
|
||||||
|
|
||||||
|
read -rp "Spoke local user [armbian]: " SPOKE_USER
|
||||||
|
SPOKE_USER="${SPOKE_USER:-armbian}"
|
||||||
|
|
||||||
read -rp "Spoke name (e.g. rocky): " SPOKE_NAME
|
read -rp "Spoke name (e.g. rocky): " SPOKE_NAME
|
||||||
[ -n "$SPOKE_NAME" ] || die "Spoke name cannot be empty"
|
[ -n "$SPOKE_NAME" ] || die "Spoke name cannot be empty"
|
||||||
|
|
||||||
read -rp "Tunnel port for $SPOKE_NAME: " TUNNEL_PORT
|
read -rp "Tunnel port for $SPOKE_NAME: " TUNNEL_PORT
|
||||||
[[ "$TUNNEL_PORT" =~ ^[0-9]+$ ]] || die "Invalid port"
|
[[ "$TUNNEL_PORT" =~ ^[0-9]+$ ]] || die "Invalid port"
|
||||||
|
|
||||||
KEY_NAME="armbian-${SPOKE_NAME}-$(date +%Y%m)"
|
KEY_NAME="${SPOKE_USER}-${SPOKE_NAME}-$(date +%Y%m)"
|
||||||
KEY_PATH="$SSH_DIR/$KEY_NAME"
|
KEY_PATH="$SSH_DIR/$KEY_NAME"
|
||||||
|
|
||||||
command -v rclone >/dev/null || die "rclone is not installed"
|
|
||||||
mkdir -p "$(dirname "$RCLONE_CONF")"
|
mkdir -p "$(dirname "$RCLONE_CONF")"
|
||||||
|
|
||||||
header "Checking Tunnel"
|
header "Checking Tunnel"
|
||||||
@@ -80,7 +80,7 @@ echo "$KEYSCAN" >> "$SSH_DIR/known_hosts"
|
|||||||
|
|
||||||
info "Verifying spoke is reachable on port $TUNNEL_PORT..."
|
info "Verifying spoke is reachable on port $TUNNEL_PORT..."
|
||||||
retry_or_abort \
|
retry_or_abort \
|
||||||
"ssh -o BatchMode=yes -o ConnectTimeout=10 -p \"$TUNNEL_PORT\" armbian@localhost exit" \
|
"ssh -o BatchMode=yes -o ConnectTimeout=10 -p \"$TUNNEL_PORT\" \"$SPOKE_USER\"@localhost exit" \
|
||||||
"Spoke not reachable on port $TUNNEL_PORT. Make sure the tunnel is up."
|
"Spoke not reachable on port $TUNNEL_PORT. Make sure the tunnel is up."
|
||||||
|
|
||||||
header "Generating Hub SSH Key"
|
header "Generating Hub SSH Key"
|
||||||
@@ -92,14 +92,14 @@ else
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
header "Copying Hub Key to Spoke"
|
header "Copying Hub Key to Spoke"
|
||||||
info "Running ssh-copy-id to armbian@localhost:$TUNNEL_PORT..."
|
info "Running ssh-copy-id to $SPOKE_USER@localhost:$TUNNEL_PORT..."
|
||||||
info "(You will be prompted for the armbian password on the spoke)"
|
info "(You will be prompted for the $SPOKE_USER password on the spoke)"
|
||||||
ssh-copy-id -i "$KEY_PATH.pub" -p "$TUNNEL_PORT" armbian@localhost
|
ssh-copy-id -i "$KEY_PATH.pub" -p "$TUNNEL_PORT" "$SPOKE_USER"@localhost
|
||||||
info "Key copied."
|
info "Key copied."
|
||||||
|
|
||||||
header "Testing Hub -> Spoke Key Auth"
|
header "Testing Hub -> Spoke Key Auth"
|
||||||
retry_or_abort \
|
retry_or_abort \
|
||||||
"ssh -i \"$KEY_PATH\" -o BatchMode=yes -o ConnectTimeout=10 -p \"$TUNNEL_PORT\" armbian@localhost exit" \
|
"ssh -i \"$KEY_PATH\" -o BatchMode=yes -o ConnectTimeout=10 -p \"$TUNNEL_PORT\" \"$SPOKE_USER\"@localhost exit" \
|
||||||
"Key auth failed. Check authorized_keys on the spoke."
|
"Key auth failed. Check authorized_keys on the spoke."
|
||||||
info "Key auth to spoke successful."
|
info "Key auth to spoke successful."
|
||||||
|
|
||||||
|
|||||||
@@ -251,19 +251,6 @@ mkdir -p "$MOUNT_POINT"
|
|||||||
chown "$HUB_USER":"$HUB_USER" "$MOUNT_POINT"
|
chown "$HUB_USER":"$HUB_USER" "$MOUNT_POINT"
|
||||||
info "Mount point created at $MOUNT_POINT."
|
info "Mount point created at $MOUNT_POINT."
|
||||||
|
|
||||||
header "Crontab Setup"
|
|
||||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
||||||
HELPER="$SCRIPT_DIR/../hubspoke-helper.sh"
|
|
||||||
CRON_LINE="@reboot $HELPER hub start-background"
|
|
||||||
EXISTING=$(crontab -u "$HUB_USER" -l 2>/dev/null || true)
|
|
||||||
|
|
||||||
if echo "$EXISTING" | grep -qF "hub start-background"; then
|
|
||||||
warn "Crontab entry already exists, skipping."
|
|
||||||
else
|
|
||||||
(echo "$EXISTING"; echo "$CRON_LINE") | crontab -u "$HUB_USER" -
|
|
||||||
info "Added @reboot crontab entry for rclone mount."
|
|
||||||
fi
|
|
||||||
|
|
||||||
header "Hub Setup Complete"
|
header "Hub Setup Complete"
|
||||||
echo -e " Hub user: ${GREEN}$HUB_USER${NC}"
|
echo -e " Hub user: ${GREEN}$HUB_USER${NC}"
|
||||||
echo -e " SSH config: ${GREEN}GatewayPorts yes, AllowTcpForwarding yes${NC}"
|
echo -e " SSH config: ${GREEN}GatewayPorts yes, AllowTcpForwarding yes${NC}"
|
||||||
|
|||||||
9
setup.sh
9
setup.sh
@@ -17,9 +17,10 @@ header "TinyBoard Setup"
|
|||||||
echo ""
|
echo ""
|
||||||
echo " 1) Set up this device as a new spoke"
|
echo " 1) Set up this device as a new spoke"
|
||||||
echo " 2) Onboard a new spoke from the hub"
|
echo " 2) Onboard a new spoke from the hub"
|
||||||
echo " 3) Set up this device as a new hub"
|
echo " 3) Offboard a spoke from the hub"
|
||||||
|
echo " 4) Set up this device as a new hub"
|
||||||
echo ""
|
echo ""
|
||||||
read -rp "Choose [1/2/3]: " CHOICE
|
read -rp "Choose [1/2/3/4]: " CHOICE
|
||||||
|
|
||||||
case "$CHOICE" in
|
case "$CHOICE" in
|
||||||
1)
|
1)
|
||||||
@@ -32,6 +33,10 @@ case "$CHOICE" in
|
|||||||
exec "$SCRIPT_DIR/hub/onboard-spoke.sh"
|
exec "$SCRIPT_DIR/hub/onboard-spoke.sh"
|
||||||
;;
|
;;
|
||||||
3)
|
3)
|
||||||
|
info "Starting hub offboarding..."
|
||||||
|
exec "$SCRIPT_DIR/hub/offboard-spoke.sh"
|
||||||
|
;;
|
||||||
|
4)
|
||||||
[ "$(id -u)" -eq 0 ] || die "Hub setup must be run as root"
|
[ "$(id -u)" -eq 0 ] || die "Hub setup must be run as root"
|
||||||
info "Starting hub setup..."
|
info "Starting hub setup..."
|
||||||
exec "$SCRIPT_DIR/hub/setup-hub.sh"
|
exec "$SCRIPT_DIR/hub/setup-hub.sh"
|
||||||
|
|||||||
Reference in New Issue
Block a user