126865cf13
README, NODES.md, install scripts, palette help text, and example flows — node .js logic unchanged.
184 lines
6.4 KiB
Bash
184 lines
6.4 KiB
Bash
#!/usr/bin/env bash
|
|
# bootstrap-install.sh — clone from Gitea and integrate into existing nextcloud-node-red
|
|
#
|
|
# Does NOT create a new Node-RED container. Patches nextcloud-node-red and rebuilds.
|
|
#
|
|
# One-liner (run as install user, NOT root):
|
|
# curl -fsSL https://kode.hannesahlin.se/n3wk/nextcloud-collabora-nodered/raw/branch/main/bootstrap-install.sh | sudo -u ncadmin bash
|
|
#
|
|
# If git "dubious ownership" (root owned the repo):
|
|
# sudo bash scripts/fix-repo-ownership.sh
|
|
#
|
|
set -euo pipefail
|
|
|
|
VERSION="1.0.0"
|
|
GIT_REPO_URL="${GIT_REPO_URL:-https://kode.hannesahlin.se/n3wk/nextcloud-collabora-nodered.git}"
|
|
BOOTSTRAP_URL="${BOOTSTRAP_URL:-https://kode.hannesahlin.se/n3wk/nextcloud-collabora-nodered/raw/branch/main/bootstrap-install.sh}"
|
|
GIT_BRANCH="${GIT_BRANCH:-main}"
|
|
INSTALL_MODE="${INSTALL_MODE:-integrate}"
|
|
|
|
# Som root: använd ncadmin-sökvägar (inte /root)
|
|
if [ "$(id -u)" -eq 0 ] && [ -d "/home/ncadmin" ]; then
|
|
INSTALL_DIR="${INSTALL_DIR:-/home/ncadmin/nextcloud-collabora-nodered}"
|
|
NODERED_DIR="${NODERED_DIR:-/home/ncadmin/nextcloud-node-red}"
|
|
else
|
|
INSTALL_DIR="${INSTALL_DIR:-${HOME}/nextcloud-collabora-nodered}"
|
|
NODERED_DIR="${NODERED_DIR:-${HOME}/nextcloud-node-red}"
|
|
fi
|
|
|
|
NC_HOST="${NC_HOST:-cloud.hannesahlin.se}"
|
|
CODE_CONTAINER="${CODE_CONTAINER:-code}"
|
|
NODERED_CONTAINER="${NODERED_CONTAINER:-nextcloud-node-red}"
|
|
NODERED_PORT="${NODERED_PORT:-1880}"
|
|
SKIP_APT="${SKIP_APT:-0}"
|
|
SKIP_DOCKER_INSTALL="${SKIP_DOCKER_INSTALL:-1}"
|
|
|
|
log() { printf '[bootstrap] %s\n' "$*"; }
|
|
warn() { printf '[bootstrap] WARNING: %s\n' "$*" >&2; }
|
|
die() { printf '[bootstrap] ERROR: %s\n' "$*" >&2; exit 1; }
|
|
|
|
run_as_root() {
|
|
if [ "$(id -u)" -eq 0 ]; then "$@"; elif command -v sudo >/dev/null 2>&1; then sudo "$@"; else die "sudo required"; fi
|
|
}
|
|
|
|
runtime_user() {
|
|
if [ -n "${INSTALL_USER:-}" ]; then
|
|
echo "${INSTALL_USER}"
|
|
return
|
|
fi
|
|
if [ "$(id -u)" -eq 0 ] && id ncadmin >/dev/null 2>&1; then
|
|
echo "ncadmin"
|
|
return
|
|
fi
|
|
id -un
|
|
}
|
|
|
|
# Root + curl|bash → kör om som ncadmin (undvik root-ägt repo i /home/ncadmin)
|
|
reexec_as_install_user() {
|
|
local user
|
|
user="$(runtime_user)"
|
|
if [ "$(id -un)" = "root" ] && [ "${user}" != "root" ] && [ -z "${BOOTSTRAP_REEXEC:-}" ]; then
|
|
log "Re-running as ${user} (not root)..."
|
|
export BOOTSTRAP_REEXEC=1
|
|
export INSTALL_DIR NODERED_DIR INSTALL_MODE GIT_REPO_URL GIT_BRANCH
|
|
exec sudo -u "${user}" -H env \
|
|
BOOTSTRAP_REEXEC=1 \
|
|
INSTALL_DIR="${INSTALL_DIR}" \
|
|
NODERED_DIR="${NODERED_DIR}" \
|
|
INSTALL_MODE="${INSTALL_MODE}" \
|
|
GIT_REPO_URL="${GIT_REPO_URL}" \
|
|
GIT_BRANCH="${GIT_BRANCH}" \
|
|
BOOTSTRAP_URL="${BOOTSTRAP_URL}" \
|
|
bash -c "curl -fsSL '${BOOTSTRAP_URL}' | bash"
|
|
fi
|
|
}
|
|
|
|
fix_repo_ownership() {
|
|
local user group owner
|
|
user="$(runtime_user)"
|
|
group="$(id -gn "${user}" 2>/dev/null || echo "${user}")"
|
|
[ -d "${INSTALL_DIR}" ] || return 0
|
|
owner="$(stat -c '%U' "${INSTALL_DIR}" 2>/dev/null || echo "")"
|
|
if [ -n "${owner}" ] && [ "${owner}" != "${user}" ]; then
|
|
log "Repo owned by ${owner} — setting owner ${user}:${group}"
|
|
run_as_root chown -R "${user}:${group}" "${INSTALL_DIR}"
|
|
fi
|
|
}
|
|
|
|
ensure_git_safe_directory() {
|
|
local user
|
|
user="$(runtime_user)"
|
|
if [ "$(id -un)" = "${user}" ]; then
|
|
git config --global --add safe.directory "${INSTALL_DIR}" 2>/dev/null || true
|
|
else
|
|
run_as_root sudo -u "${user}" git config --global --add safe.directory "${INSTALL_DIR}" 2>/dev/null || true
|
|
fi
|
|
}
|
|
|
|
install_apt_packages() {
|
|
[ "${SKIP_APT}" = "1" ] && return 0
|
|
command -v apt-get >/dev/null 2>&1 || return 0
|
|
log "Installing git, curl, rsync..."
|
|
run_as_root apt-get update -qq
|
|
run_as_root apt-get install -y -qq git curl ca-certificates rsync
|
|
}
|
|
|
|
install_docker() {
|
|
[ "${SKIP_DOCKER_INSTALL}" = "1" ] && return 0
|
|
command -v docker >/dev/null 2>&1 && docker info >/dev/null 2>&1 && return 0
|
|
log "Installing Docker..."
|
|
curl -fsSL https://get.docker.com | run_as_root sh
|
|
}
|
|
|
|
clone_or_update_repo() {
|
|
fix_repo_ownership
|
|
ensure_git_safe_directory
|
|
|
|
if [ -d "${INSTALL_DIR}/.git" ]; then
|
|
log "Updating ${INSTALL_DIR}..."
|
|
git -C "${INSTALL_DIR}" fetch origin
|
|
git -C "${INSTALL_DIR}" checkout "${GIT_BRANCH}"
|
|
git -C "${INSTALL_DIR}" pull --ff-only origin "${GIT_BRANCH}" || warn "git pull failed"
|
|
return 0
|
|
fi
|
|
log "Cloning ${GIT_REPO_URL} → ${INSTALL_DIR}"
|
|
mkdir -p "$(dirname "${INSTALL_DIR}")"
|
|
git clone --branch "${GIT_BRANCH}" --depth 1 "${GIT_REPO_URL}" "${INSTALL_DIR}"
|
|
fix_repo_ownership
|
|
ensure_git_safe_directory
|
|
}
|
|
|
|
preflight() {
|
|
command -v docker >/dev/null 2>&1 || die "Docker not found"
|
|
docker ps --format '{{.Names}}' | grep -qx "${NODERED_CONTAINER}" \
|
|
|| die "'${NODERED_CONTAINER}' is not running — this script integrates into existing Node-RED, does not create a new one"
|
|
docker ps --format '{{.Names}}' | grep -qx "${CODE_CONTAINER}" \
|
|
|| die "'${CODE_CONTAINER}' (Collabora) is not running"
|
|
[ -d "${NODERED_DIR}" ] || die "nextcloud-node-red directory missing: ${NODERED_DIR}"
|
|
log "OK: ${NODERED_CONTAINER} + ${CODE_CONTAINER} running"
|
|
}
|
|
|
|
main() {
|
|
reexec_as_install_user
|
|
|
|
log "bootstrap v${VERSION} — mode: ${INSTALL_MODE} (existing Node-RED :${NODERED_PORT})"
|
|
log "User: $(id -un)"
|
|
log "Repo → ${INSTALL_DIR}"
|
|
log "Integrate → ${NODERED_DIR}"
|
|
|
|
if [ "$(id -u)" -eq 0 ] && [ "$(runtime_user)" = "root" ]; then
|
|
warn "Running as root — recommended: curl ... | sudo -u ncadmin bash"
|
|
fi
|
|
|
|
install_apt_packages
|
|
install_docker
|
|
clone_or_update_repo
|
|
preflight
|
|
|
|
chmod +x "${INSTALL_DIR}/install.sh" "${INSTALL_DIR}/scripts/integrate-into-nodered.sh"
|
|
INSTALL_DIR="${INSTALL_DIR}" \
|
|
INSTALL_MODE="${INSTALL_MODE}" \
|
|
NODERED_DIR="${NODERED_DIR}" \
|
|
CODE_CONTAINER="${CODE_CONTAINER}" \
|
|
bash "${INSTALL_DIR}/install.sh"
|
|
|
|
local host_ip
|
|
host_ip="$(hostname -I 2>/dev/null | awk '{print $1}' || echo "127.0.0.1")"
|
|
|
|
cat <<EOF
|
|
|
|
================================================================================
|
|
Done — Collabora nodes in existing nextcloud-node-red
|
|
================================================================================
|
|
Node-RED: http://${host_ip}:${NODERED_PORT} (same as before)
|
|
Repo: ${INSTALL_DIR}
|
|
Patched: ${NODERED_DIR}
|
|
|
|
Update later:
|
|
cd ${INSTALL_DIR} && git pull && ./install.sh
|
|
|
|
EOF
|
|
}
|
|
|
|
main "$@"
|