Initial Commit

This commit is contained in:
Dark98 committed 2026-05-14 02:27:26 +01:00
commit b0de8cf38c
107 files changed
+26832

No files matched your search

+95
View File
@@ -0,0 +1,95 @@
#!/bin/sh
set -eu
CONFIG_SRC="${CONFIG_SRC:-}"
RELEASE_BASE_URL="${RELEASE_BASE_URL:-https://github.com/klipperanywhere/klipperanywhere/releases/latest/download}"
ARCH_OVERRIDE="${ARCH_OVERRIDE:-}"
usage() {
cat >&2 <<'EOF'
usage: bootstrap-agent.sh --config <agent.json> [--release-base-url <url>] [--arch <arch>]
Downloads the matching agent binary for this host and installs it using the
unified installer. Supported arches: amd64, arm64, armv7, mipsle.
EOF
exit 2
}
while [ $# -gt 0 ]; do
case "$1" in
--config)
shift
CONFIG_SRC="${1:-}"
;;
--release-base-url)
shift
RELEASE_BASE_URL="${1:-}"
;;
--arch)
shift
ARCH_OVERRIDE="${1:-}"
;;
-h|--help)
usage
;;
*)
echo "unknown argument: $1" >&2
usage
;;
esac
shift
done
if [ -z "$CONFIG_SRC" ]; then
echo "missing --config" >&2
usage
fi
if [ "$(id -u)" -ne 0 ]; then
echo "run as root" >&2
exit 1
fi
if [ ! -f "$CONFIG_SRC" ]; then
echo "missing config file: $CONFIG_SRC" >&2
exit 1
fi
SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
ARCH="${ARCH_OVERRIDE:-$(detect_arch)}"
ASSET="klipperanywhere-agent-linux-$ARCH"
TMP_DIR="$(mktemp -d)"
trap 'rm -rf "$TMP_DIR"' EXIT INT TERM
download "$RELEASE_BASE_URL/$ASSET" "$TMP_DIR/$ASSET"
chmod 0755 "$TMP_DIR/$ASSET"
exec sh "$SCRIPT_DIR/install-agent.sh" "$TMP_DIR/$ASSET" "$CONFIG_SRC"
detect_arch() {
machine="$(uname -m 2>/dev/null || echo unknown)"
case "$machine" in
x86_64|amd64) echo amd64 ;;
aarch64|arm64) echo arm64 ;;
armv7l|armv7*) echo armv7 ;;
mips64el|mipsle) echo mipsle ;;
mipsel|mips*) echo mipsle ;;
*)
echo "unsupported architecture: $machine" >&2
exit 1
;;
esac
}
download() {
url="$1"
dest="$2"
if command -v curl >/dev/null 2>&1; then
curl -fsSL "$url" -o "$dest"
return
fi
if command -v wget >/dev/null 2>&1; then
wget -qO "$dest" "$url"
return
fi
echo "need curl or wget" >&2
exit 1
}
+50
View File
@@ -0,0 +1,50 @@
param(
[string]$OutputDir = "dist",
[string]$Version = ""
)
$ErrorActionPreference = "Stop"
New-Item -ItemType Directory -Force -Path $OutputDir | Out-Null
if ([string]::IsNullOrWhiteSpace($Version)) {
$versionScript = Join-Path $PSScriptRoot "version.ps1"
if (Test-Path $versionScript) {
$Version = & $versionScript
} else {
$Version = "dev"
}
}
$targets = @(
@{ Name = "linux-amd64"; GOOS = "linux"; GOARCH = "amd64"; GOARM = "" },
@{ Name = "linux-arm64"; GOOS = "linux"; GOARCH = "arm64"; GOARM = "" },
@{ Name = "linux-armv7"; GOOS = "linux"; GOARCH = "arm"; GOARM = "7" },
@{ Name = "linux-mipsle"; GOOS = "linux"; GOARCH = "mipsle"; GOARM = ""; GOMIPS = "softfloat" }
)
foreach ($target in $targets) {
$env:CGO_ENABLED = "0"
$env:GOOS = $target.GOOS
$env:GOARCH = $target.GOARCH
if ($target.GOARM) {
$env:GOARM = $target.GOARM
} else {
Remove-Item Env:\GOARM -ErrorAction SilentlyContinue
}
if ($target.GOMIPS) {
$env:GOMIPS = $target.GOMIPS
} else {
Remove-Item Env:\GOMIPS -ErrorAction SilentlyContinue
}
$out = Join-Path $OutputDir "klipperanywhere-agent-$($target.Name)"
Write-Host "Building $out"
go build -ldflags "-X main.agentVersion=$Version" -o $out ./cmd/agent
}
Remove-Item Env:\GOOS -ErrorAction SilentlyContinue
Remove-Item Env:\GOARCH -ErrorAction SilentlyContinue
Remove-Item Env:\GOARM -ErrorAction SilentlyContinue
Remove-Item Env:\GOMIPS -ErrorAction SilentlyContinue
Remove-Item Env:\CGO_ENABLED -ErrorAction SilentlyContinue
+14
View File
@@ -0,0 +1,14 @@
#!/bin/sh
set -eu
VERSION="${1:-${KA_VERSION:-dev}}"
OUT_DIR="${2:-dist}"
mkdir -p "$OUT_DIR"
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-X main.serverVersion=$VERSION" -o "$OUT_DIR/klipperanywhere-server-linux-amd64" ./cmd/server
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-X main.agentVersion=$VERSION" -o "$OUT_DIR/klipperanywhere-agent-linux-amd64" ./cmd/agent
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags "-X main.agentVersion=$VERSION" -o "$OUT_DIR/klipperanywhere-agent-linux-arm64" ./cmd/agent
CGO_ENABLED=0 GOOS=linux GOARCH=arm GOARM=7 go build -ldflags "-X main.agentVersion=$VERSION" -o "$OUT_DIR/klipperanywhere-agent-linux-armv7" ./cmd/agent
CGO_ENABLED=0 GOOS=linux GOARCH=mipsle GOMIPS=softfloat go build -ldflags "-X main.agentVersion=$VERSION" -o "$OUT_DIR/klipperanywhere-agent-linux-mipsle" ./cmd/agent
printf '%s\n' "$VERSION" > "$OUT_DIR/VERSION.txt"
+4
View File
@@ -0,0 +1,4 @@
#!/bin/sh
set -eu
exec "$(dirname "$0")/install-agent.sh" "$@"
+96
View File
@@ -0,0 +1,96 @@
#!/bin/sh
set -eu
BIN_SRC="${1:-}"
CONFIG_SRC="${2:-}"
if [ -z "$BIN_SRC" ] || [ -z "$CONFIG_SRC" ]; then
echo "usage: install-agent.sh <agent-binary> <agent.json>" >&2
exit 2
fi
if [ "$(id -u)" -ne 0 ]; then
echo "run as root" >&2
exit 1
fi
SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
if [ ! -f "$BIN_SRC" ]; then
echo "missing agent binary: $BIN_SRC" >&2
exit 1
fi
if [ ! -f "$CONFIG_SRC" ]; then
echo "missing agent config: $CONFIG_SRC" >&2
exit 1
fi
AGENT_DIR="/etc/klipperanywhere"
STATE_DIR="/var/lib/klipperanywhere"
ROLLBACK_DIR="$STATE_DIR/rollback"
AGENT_BIN="/usr/local/bin/klipperanywhere-agent"
AGENT_CFG="$AGENT_DIR/agent.json"
INITD_SCRIPT="/etc/init.d/klipperanywhere-agent"
install -d "$AGENT_DIR"
install -d "$STATE_DIR"
install -d /usr/local/bin
if [ -f "$AGENT_BIN" ] || [ -f "$AGENT_CFG" ]; then
install -d "$ROLLBACK_DIR"
if [ -f "$AGENT_BIN" ]; then
install -m 0755 "$AGENT_BIN" "$ROLLBACK_DIR/klipperanywhere-agent.bak"
fi
if [ -f "$AGENT_CFG" ]; then
install -m 0600 "$AGENT_CFG" "$ROLLBACK_DIR/agent.json.bak"
fi
fi
install -m 0755 "$BIN_SRC" "$AGENT_BIN"
install -m 0600 "$CONFIG_SRC" "$AGENT_CFG"
if command -v systemctl >/dev/null 2>&1 && [ -d /run/systemd/system ]; then
install -d /etc/systemd/system
install -m 0644 "$SCRIPT_DIR/../packaging/klipperanywhere-agent.service" /etc/systemd/system/klipperanywhere-agent.service
systemctl daemon-reload
systemctl enable klipperanywhere-agent
systemctl restart klipperanywhere-agent
systemctl status klipperanywhere-agent --no-pager
exit 0
fi
install -m 0755 "$SCRIPT_DIR/../packaging/klipperanywhere-agent.initd" "$INITD_SCRIPT"
if command -v rc-update >/dev/null 2>&1; then
rc-update add klipperanywhere-agent default 2>/dev/null || true
rc-service klipperanywhere-agent restart 2>/dev/null || rc-service klipperanywhere-agent start
rc-service klipperanywhere-agent status || true
exit 0
fi
if command -v update-rc.d >/dev/null 2>&1; then
update-rc.d klipperanywhere-agent defaults 2>/dev/null || true
"$INITD_SCRIPT" restart
"$INITD_SCRIPT" status || true
exit 0
fi
if command -v chkconfig >/dev/null 2>&1; then
chkconfig --add klipperanywhere-agent 2>/dev/null || true
"$INITD_SCRIPT" restart
"$INITD_SCRIPT" status || true
exit 0
fi
for dir in /etc/rcS.d /etc/rc.d /etc/init.d; do
if [ -d "$dir" ]; then
ln -sf "$INITD_SCRIPT" "$dir/S99klipperanywhere-agent"
"$INITD_SCRIPT" restart
echo "Installed BusyBox-style startup hook at $dir/S99klipperanywhere-agent"
echo "If this host uses a different boot hook, run: $INITD_SCRIPT start"
exit 0
fi
done
echo "Installed agent binary and init script, but could not auto-enable it."
echo "Start it manually with: $INITD_SCRIPT start"
+4
View File
@@ -0,0 +1,4 @@
#!/bin/sh
set -eu
exec "$(dirname "$0")/uninstall-agent.sh" "$@"
+35
View File
@@ -0,0 +1,35 @@
#!/bin/sh
set -eu
if [ "$(id -u)" -ne 0 ]; then
echo "run as root" >&2
exit 1
fi
if command -v systemctl >/dev/null 2>&1 && [ -d /run/systemd/system ]; then
systemctl stop klipperanywhere-agent 2>/dev/null || true
systemctl disable klipperanywhere-agent 2>/dev/null || true
rm -f /etc/systemd/system/klipperanywhere-agent.service
systemctl daemon-reload
fi
if [ -x /etc/init.d/klipperanywhere-agent ]; then
/etc/init.d/klipperanywhere-agent stop 2>/dev/null || true
fi
rm -f /etc/init.d/klipperanywhere-agent
rm -f /etc/rcS.d/S99klipperanywhere-agent
rm -f /etc/rc.d/S99klipperanywhere-agent
if command -v rc-update >/dev/null 2>&1; then
rc-update del klipperanywhere-agent default 2>/dev/null || true
fi
if command -v update-rc.d >/dev/null 2>&1; then
update-rc.d -f klipperanywhere-agent remove 2>/dev/null || true
fi
if command -v chkconfig >/dev/null 2>&1; then
chkconfig --del klipperanywhere-agent 2>/dev/null || true
fi
rm -f /usr/local/bin/klipperanywhere-agent
echo "Left /etc/klipperanywhere/agent.json in place."
+27
View File
@@ -0,0 +1,27 @@
param()
$ErrorActionPreference = "Stop"
try {
$version = git describe --tags --always --dirty 2>$null
if ($LASTEXITCODE -eq 0 -and $version) {
Write-Output $version
exit 0
}
} catch {
}
try {
$version = git rev-parse --short HEAD 2>$null
if ($LASTEXITCODE -eq 0 -and $version) {
Write-Output $version
exit 0
}
} catch {
}
if ($env:KA_VERSION) {
Write-Output $env:KA_VERSION
} else {
Write-Output "dev"
}
+15
View File
@@ -0,0 +1,15 @@
#!/bin/sh
set -eu
if command -v git >/dev/null 2>&1; then
if version="$(git describe --tags --always --dirty 2>/dev/null)"; then
printf '%s\n' "$version"
exit 0
fi
if version="$(git rev-parse --short HEAD 2>/dev/null)"; then
printf '%s\n' "$version"
exit 0
fi
fi
printf '%s\n' "${KA_VERSION:-dev}"