install.sh 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. #!/usr/bin/env bash
  2. #
  3. # BamBuddy Native Installation Script
  4. # Supports: Debian/Ubuntu, RHEL/Fedora/CentOS, Arch Linux, macOS
  5. #
  6. # Usage:
  7. # Interactive: curl -fsSL https://raw.githubusercontent.com/maziggy/bambuddy/main/install/install.sh -o install.sh && chmod +x install.sh && ./install.sh
  8. # Unattended: ./install.sh --path /opt/bambuddy --port 8000 --yes
  9. #
  10. # Options:
  11. # --path PATH Installation directory (default: /opt/bambuddy)
  12. # --port PORT Port to listen on (default: 8000)
  13. # --bind ADDRESS Bind address: 0.0.0.0 (network) or 127.0.0.1 (local only)
  14. # --tz TIMEZONE Timezone (default: system timezone or UTC)
  15. # --data-dir PATH Data directory (default: INSTALL_PATH/data)
  16. # --log-dir PATH Log directory (default: INSTALL_PATH/logs)
  17. # --debug Enable debug mode
  18. # --log-level LEVEL Log level: DEBUG, INFO, WARNING, ERROR (default: INFO)
  19. # --branch BRANCH Git branch to install (default: main)
  20. # --no-service Skip systemd service setup (Linux only)
  21. # --set-system-tz Set system timezone to match (for unattended installs)
  22. # --yes, -y Non-interactive mode, accept defaults
  23. # --help, -h Show this help message
  24. #
  25. set -e
  26. # Colors for output
  27. RED='\033[0;31m'
  28. GREEN='\033[0;32m'
  29. YELLOW='\033[1;33m'
  30. BLUE='\033[0;34m'
  31. CYAN='\033[0;36m'
  32. NC='\033[0m' # No Color
  33. BOLD='\033[1m'
  34. # Default values
  35. DEFAULT_INSTALL_PATH="/opt/bambuddy"
  36. DEFAULT_PORT="8000"
  37. DEFAULT_BIND_ADDRESS="0.0.0.0"
  38. DEFAULT_LOG_LEVEL="INFO"
  39. DEFAULT_DEBUG="false"
  40. # Script variables
  41. INSTALL_PATH=""
  42. PORT=""
  43. BIND_ADDRESS=""
  44. TIMEZONE=""
  45. DATA_DIR=""
  46. LOG_DIR=""
  47. DEBUG_MODE=""
  48. LOG_LEVEL=""
  49. SKIP_SERVICE="false"
  50. SET_SYSTEM_TZ=""
  51. NON_INTERACTIVE="false"
  52. OS_TYPE=""
  53. PKG_MANAGER=""
  54. PYTHON_CMD=""
  55. BRANCH=""
  56. SERVICE_USER="bambuddy"
  57. # -----------------------------------------------------------------------------
  58. # Helper Functions
  59. # -----------------------------------------------------------------------------
  60. print_banner() {
  61. echo -e "${CYAN}"
  62. echo "╔════════════════════════════════════════════════════════╗"
  63. echo "║ ║"
  64. echo "║ ____ _ _ _ ║"
  65. echo "║ | __ ) __ _ _ __ ___ | |__ _ _ __| | __| |_ _ ║"
  66. echo "║ | _ \\ / _\` | '_ \` _ \\| '_ \\| | | |/ _\` |/ _\` | | | | ║"
  67. echo "║ | |_) | (_| | | | | | | |_) | |_| | (_| | (_| | |_| | ║"
  68. echo "║ |____/ \\__,_|_| |_| |_|_.__/ \\__,_|\\__,_|\\__,_|\\__, | ║"
  69. echo "║ |___/ ║"
  70. echo "║ ║"
  71. echo "║ Native Installation Script ║"
  72. echo "║ ║"
  73. echo "╚════════════════════════════════════════════════════════╝"
  74. echo -e "${NC}"
  75. }
  76. log_info() {
  77. echo -e "${BLUE}[INFO]${NC} $1"
  78. }
  79. log_success() {
  80. echo -e "${GREEN}[OK]${NC} $1"
  81. }
  82. log_warn() {
  83. echo -e "${YELLOW}[WARN]${NC} $1"
  84. }
  85. log_error() {
  86. echo -e "${RED}[ERROR]${NC} $1"
  87. }
  88. prompt() {
  89. local prompt_text="$1"
  90. local default_value="$2"
  91. local var_name="$3"
  92. if [[ "$NON_INTERACTIVE" == "true" ]]; then
  93. eval "$var_name=\"$default_value\""
  94. return
  95. fi
  96. if [[ -n "$default_value" ]]; then
  97. echo -en "${BOLD}$prompt_text${NC} [${CYAN}$default_value${NC}]: "
  98. else
  99. echo -en "${BOLD}$prompt_text${NC}: "
  100. fi
  101. read -r input
  102. if [[ -z "$input" ]]; then
  103. eval "$var_name=\"$default_value\""
  104. else
  105. eval "$var_name=\"$input\""
  106. fi
  107. }
  108. prompt_yes_no() {
  109. local prompt_text="$1"
  110. local default="$2" # y or n
  111. if [[ "$NON_INTERACTIVE" == "true" ]]; then
  112. [[ "$default" == "y" ]] && return 0 || return 1
  113. fi
  114. local yn_hint="[y/n]"
  115. [[ "$default" == "y" ]] && yn_hint="[Y/n]"
  116. [[ "$default" == "n" ]] && yn_hint="[y/N]"
  117. while true; do
  118. echo -en "${BOLD}$prompt_text${NC} $yn_hint: "
  119. read -r yn
  120. [[ -z "$yn" ]] && yn="$default"
  121. case "$yn" in
  122. [Yy]* ) return 0;;
  123. [Nn]* ) return 1;;
  124. * ) echo "Please answer yes or no.";;
  125. esac
  126. done
  127. }
  128. show_help() {
  129. echo "BamBuddy Native Installation Script"
  130. echo ""
  131. echo "Usage: $0 [OPTIONS]"
  132. echo ""
  133. echo "Options:"
  134. echo " --path PATH Installation directory (default: /opt/bambuddy)"
  135. echo " --port PORT Port to listen on (default: 8000)"
  136. echo " --bind ADDRESS Bind address: 0.0.0.0 (network) or 127.0.0.1 (local only)"
  137. echo " --tz TIMEZONE Timezone (default: system timezone or UTC)"
  138. echo " --data-dir PATH Data directory (default: INSTALL_PATH/data)"
  139. echo " --log-dir PATH Log directory (default: INSTALL_PATH/logs)"
  140. echo " --debug Enable debug mode"
  141. echo " --log-level LEVEL Log level: DEBUG, INFO, WARNING, ERROR (default: INFO)"
  142. echo " --branch BRANCH Git branch to install (default: main)"
  143. echo " --no-service Skip systemd service setup (Linux only)"
  144. echo " --set-system-tz Set system timezone to match (for unattended installs)"
  145. echo " --yes, -y Non-interactive mode, accept defaults"
  146. echo " --help, -h Show this help message"
  147. echo ""
  148. echo "Examples:"
  149. echo " Interactive installation:"
  150. echo " ./install.sh"
  151. echo ""
  152. echo " Unattended installation with custom settings:"
  153. echo " ./install.sh --path /srv/bambuddy --port 3000 --tz America/New_York --yes"
  154. echo ""
  155. echo " Minimal unattended installation:"
  156. echo " ./install.sh -y"
  157. exit 0
  158. }
  159. # -----------------------------------------------------------------------------
  160. # System Detection
  161. # -----------------------------------------------------------------------------
  162. detect_os() {
  163. if [[ "$OSTYPE" == "darwin"* ]]; then
  164. OS_TYPE="macos"
  165. PKG_MANAGER="brew"
  166. return
  167. fi
  168. if [[ -f /etc/os-release ]]; then
  169. . /etc/os-release
  170. case "$ID" in
  171. ubuntu|debian|raspbian|linuxmint|pop)
  172. OS_TYPE="debian"
  173. PKG_MANAGER="apt"
  174. ;;
  175. fedora|rhel|centos|rocky|almalinux|ol)
  176. OS_TYPE="rhel"
  177. if command -v dnf &>/dev/null; then
  178. PKG_MANAGER="dnf"
  179. else
  180. PKG_MANAGER="yum"
  181. fi
  182. ;;
  183. arch|manjaro|endeavouros)
  184. OS_TYPE="arch"
  185. PKG_MANAGER="pacman"
  186. ;;
  187. opensuse*|sles)
  188. OS_TYPE="suse"
  189. PKG_MANAGER="zypper"
  190. ;;
  191. *)
  192. log_error "Unsupported Linux distribution: $ID"
  193. exit 1
  194. ;;
  195. esac
  196. else
  197. log_error "Cannot detect operating system"
  198. exit 1
  199. fi
  200. }
  201. detect_python() {
  202. # Try python3 first, then python
  203. if command -v python3 &>/dev/null; then
  204. PYTHON_CMD="python3"
  205. elif command -v python &>/dev/null; then
  206. local version
  207. version=$(python --version 2>&1 | cut -d' ' -f2 | cut -d'.' -f1)
  208. if [[ "$version" -ge 3 ]]; then
  209. PYTHON_CMD="python"
  210. fi
  211. fi
  212. if [[ -z "$PYTHON_CMD" ]]; then
  213. return 1
  214. fi
  215. # Check version >= 3.10
  216. local version
  217. version=$($PYTHON_CMD -c 'import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")')
  218. local major minor
  219. major=$(echo "$version" | cut -d'.' -f1)
  220. minor=$(echo "$version" | cut -d'.' -f2)
  221. if [[ "$major" -lt 3 ]] || { [[ "$major" -eq 3 ]] && [[ "$minor" -lt 10 ]]; }; then
  222. log_warn "Python $version found, but 3.10 or newer is required"
  223. return 1
  224. fi
  225. log_success "Found Python $version"
  226. return 0
  227. }
  228. detect_timezone() {
  229. if [[ -n "$TIMEZONE" ]]; then
  230. return 0
  231. fi
  232. # Try to get system timezone (with error handling for set -e)
  233. TIMEZONE=""
  234. if [[ -f /etc/timezone ]]; then
  235. TIMEZONE=$(cat /etc/timezone 2>/dev/null) || true
  236. fi
  237. if [[ -z "$TIMEZONE" ]] && [[ -L /etc/localtime ]]; then
  238. TIMEZONE=$(readlink /etc/localtime 2>/dev/null | sed 's|.*/zoneinfo/||') || true
  239. fi
  240. if [[ -z "$TIMEZONE" ]] && command -v timedatectl &>/dev/null; then
  241. TIMEZONE=$(timedatectl show --property=Timezone --value 2>/dev/null) || true
  242. fi
  243. # Default to UTC if not found (use if/then to avoid set -e issue with &&)
  244. if [[ -z "$TIMEZONE" ]]; then
  245. TIMEZONE="UTC"
  246. fi
  247. return 0
  248. }
  249. # -----------------------------------------------------------------------------
  250. # Package Installation
  251. # -----------------------------------------------------------------------------
  252. install_dependencies() {
  253. log_info "Installing system dependencies..."
  254. case "$PKG_MANAGER" in
  255. apt)
  256. sudo apt-get update
  257. sudo apt-get install -y python3 python3-pip python3-venv git curl ffmpeg
  258. ;;
  259. dnf|yum)
  260. sudo $PKG_MANAGER install -y python3 python3-pip git curl ffmpeg
  261. ;;
  262. pacman)
  263. sudo pacman -Sy --noconfirm python python-pip git curl ffmpeg
  264. ;;
  265. zypper)
  266. sudo zypper install -y python3 python3-pip git curl ffmpeg
  267. ;;
  268. brew)
  269. # Check if Homebrew is installed
  270. if ! command -v brew &>/dev/null; then
  271. log_error "Homebrew not found. Please install it first: https://brew.sh"
  272. exit 1
  273. fi
  274. brew install python git curl ffmpeg
  275. ;;
  276. esac
  277. log_success "System dependencies installed"
  278. }
  279. # -----------------------------------------------------------------------------
  280. # Installation Steps
  281. # -----------------------------------------------------------------------------
  282. create_user() {
  283. if [[ "$OS_TYPE" == "macos" ]]; then
  284. return # Skip user creation on macOS
  285. fi
  286. if id "$SERVICE_USER" &>/dev/null; then
  287. log_info "User '$SERVICE_USER' already exists"
  288. return
  289. fi
  290. log_info "Creating service user '$SERVICE_USER'..."
  291. sudo useradd --system --shell /usr/sbin/nologin --home-dir "$INSTALL_PATH" "$SERVICE_USER"
  292. log_success "Service user created"
  293. }
  294. download_bambuddy() {
  295. log_info "Downloading BamBuddy..."
  296. # Validate branch exists on remote before proceeding
  297. if ! git ls-remote --exit-code --heads https://github.com/maziggy/bambuddy.git "$BRANCH" &>/dev/null; then
  298. log_error "Branch '$BRANCH' not found in the BamBuddy repository."
  299. log_info "Available branches:"
  300. git ls-remote --heads https://github.com/maziggy/bambuddy.git | sed 's|.*refs/heads/| - |'
  301. exit 1
  302. fi
  303. if [[ -d "$INSTALL_PATH/.git" ]]; then
  304. log_info "Existing installation found, updating..."
  305. # Add safe.directory to avoid "dubious ownership" error when running as root
  306. git config --global --add safe.directory "$INSTALL_PATH" 2>/dev/null || true
  307. cd "$INSTALL_PATH"
  308. git fetch origin
  309. git checkout "$BRANCH" 2>/dev/null || git checkout -b "$BRANCH" "origin/$BRANCH"
  310. git reset --hard "origin/$BRANCH"
  311. # Ensure correct ownership after update
  312. sudo chown -R "$SERVICE_USER:$SERVICE_USER" "$INSTALL_PATH" 2>/dev/null || true
  313. else
  314. # Clone as root so we have write access regardless of the installing user,
  315. # then hand ownership to the service user. Previously we chown'd the empty
  316. # dir to the service user before the clone, which left the install-running
  317. # user (not root, not bambuddy) unable to write .git into it.
  318. sudo mkdir -p "$INSTALL_PATH"
  319. sudo git clone --branch "$BRANCH" https://github.com/maziggy/bambuddy.git "$INSTALL_PATH"
  320. sudo chown -R "$SERVICE_USER:$SERVICE_USER" "$INSTALL_PATH" 2>/dev/null || true
  321. fi
  322. log_success "BamBuddy downloaded to $INSTALL_PATH (branch: $BRANCH)"
  323. }
  324. setup_virtualenv() {
  325. log_info "Setting up Python virtual environment..."
  326. cd "$INSTALL_PATH"
  327. if [[ "$OS_TYPE" == "macos" ]]; then
  328. $PYTHON_CMD -m venv venv
  329. source venv/bin/activate
  330. else
  331. sudo -u "$SERVICE_USER" $PYTHON_CMD -m venv venv 2>/dev/null || $PYTHON_CMD -m venv venv
  332. source venv/bin/activate
  333. fi
  334. pip install --upgrade pip
  335. pip install -r requirements.txt
  336. log_success "Virtual environment configured"
  337. }
  338. check_node_version() {
  339. # Returns 0 if Node.js 20+ is available, 1 otherwise
  340. if ! command -v node &>/dev/null; then
  341. return 1
  342. fi
  343. local version
  344. version=$(node --version 2>/dev/null | sed 's/^v//')
  345. local major
  346. major=$(echo "$version" | cut -d'.' -f1)
  347. if [[ "$major" -ge 20 ]]; then
  348. log_success "Found Node.js v$version"
  349. return 0
  350. else
  351. log_warn "Found Node.js v$version (need 20+)"
  352. return 1
  353. fi
  354. }
  355. install_nodejs() {
  356. log_info "Installing Node.js 22..."
  357. case "$PKG_MANAGER" in
  358. apt)
  359. # Remove old nodejs if present
  360. sudo apt-get remove -y nodejs npm 2>/dev/null || true
  361. curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
  362. sudo apt-get install -y nodejs
  363. ;;
  364. dnf|yum)
  365. sudo $PKG_MANAGER remove -y nodejs npm 2>/dev/null || true
  366. curl -fsSL https://rpm.nodesource.com/setup_22.x | sudo bash -
  367. sudo $PKG_MANAGER install -y nodejs
  368. ;;
  369. pacman)
  370. sudo pacman -S --noconfirm nodejs npm
  371. ;;
  372. zypper)
  373. sudo zypper install -y nodejs22
  374. ;;
  375. brew)
  376. brew install node@22
  377. brew link --overwrite node@22
  378. ;;
  379. *)
  380. log_error "Please install Node.js 20+ manually: https://nodejs.org/"
  381. exit 1
  382. ;;
  383. esac
  384. # Refresh PATH
  385. hash -r 2>/dev/null || true
  386. }
  387. build_frontend() {
  388. log_info "Building frontend..."
  389. cd "$INSTALL_PATH/frontend"
  390. # Check for Node.js 20+
  391. if ! check_node_version; then
  392. install_nodejs
  393. # Verify installation
  394. if ! check_node_version; then
  395. log_error "Failed to install Node.js 20+. Please install manually."
  396. exit 1
  397. fi
  398. fi
  399. npm ci
  400. npm run build
  401. log_success "Frontend built"
  402. }
  403. create_directories() {
  404. log_info "Creating data directories..."
  405. sudo mkdir -p "$DATA_DIR" "$LOG_DIR"
  406. if [[ "$OS_TYPE" != "macos" ]]; then
  407. sudo chown -R "$SERVICE_USER:$SERVICE_USER" "$DATA_DIR" "$LOG_DIR"
  408. fi
  409. log_success "Directories created"
  410. }
  411. create_env_file() {
  412. log_info "Creating environment configuration..."
  413. local env_file="$INSTALL_PATH/.env"
  414. # Note: Only include settings recognized by the app's pydantic Settings class
  415. # Other settings (PORT, BIND_ADDRESS, DATA_DIR, LOG_DIR, TZ) are set in systemd service
  416. cat > /tmp/bambuddy.env << EOF
  417. # BamBuddy Configuration
  418. # Generated by install.sh on $(date)
  419. # Debug mode (true = verbose logging)
  420. DEBUG=$DEBUG_MODE
  421. # Log level (only used when DEBUG=false)
  422. # Options: DEBUG, INFO, WARNING, ERROR
  423. LOG_LEVEL=$LOG_LEVEL
  424. # Enable file logging
  425. LOG_TO_FILE=true
  426. EOF
  427. sudo mv /tmp/bambuddy.env "$env_file"
  428. if [[ "$OS_TYPE" != "macos" ]]; then
  429. sudo chown "$SERVICE_USER:$SERVICE_USER" "$env_file"
  430. fi
  431. sudo chmod 600 "$env_file"
  432. log_success "Environment file created at $env_file"
  433. }
  434. create_systemd_service() {
  435. if [[ "$OS_TYPE" == "macos" ]] || [[ "$SKIP_SERVICE" == "true" ]]; then
  436. return
  437. fi
  438. log_info "Creating systemd service..."
  439. cat > /tmp/bambuddy.service << EOF
  440. [Unit]
  441. Description=BamBuddy - Bambu Lab Print Management
  442. Documentation=https://github.com/maziggy/bambuddy
  443. After=network.target
  444. [Service]
  445. Type=simple
  446. User=$SERVICE_USER
  447. Group=$SERVICE_USER
  448. WorkingDirectory=$INSTALL_PATH
  449. # App settings from .env file
  450. EnvironmentFile=$INSTALL_PATH/.env
  451. # Service settings (not in .env to avoid pydantic validation errors)
  452. Environment="DATA_DIR=$DATA_DIR"
  453. Environment="LOG_DIR=$LOG_DIR"
  454. Environment="TZ=$TIMEZONE"
  455. ExecStart=$INSTALL_PATH/venv/bin/uvicorn backend.app.main:app --host $BIND_ADDRESS --port $PORT
  456. Restart=on-failure
  457. RestartSec=5
  458. StandardOutput=journal
  459. StandardError=journal
  460. # Allow binding to privileged ports (322, 990, 2024-2026) for Virtual Printer proxy mode
  461. AmbientCapabilities=CAP_NET_BIND_SERVICE
  462. # Security hardening
  463. NoNewPrivileges=true
  464. PrivateTmp=true
  465. ProtectSystem=strict
  466. ProtectHome=true
  467. ReadWritePaths=$DATA_DIR $LOG_DIR $INSTALL_PATH
  468. [Install]
  469. WantedBy=multi-user.target
  470. EOF
  471. sudo mv /tmp/bambuddy.service /etc/systemd/system/bambuddy.service
  472. sudo systemctl daemon-reload
  473. log_success "Systemd service created"
  474. if prompt_yes_no "Enable BamBuddy to start on boot?" "y"; then
  475. sudo systemctl enable bambuddy
  476. log_success "Service enabled"
  477. fi
  478. if prompt_yes_no "Start BamBuddy now?" "y"; then
  479. sudo systemctl start bambuddy
  480. sleep 2
  481. if sudo systemctl is-active --quiet bambuddy; then
  482. log_success "BamBuddy is running"
  483. else
  484. log_warn "Service may have failed to start. Check: sudo journalctl -u bambuddy -f"
  485. fi
  486. fi
  487. }
  488. create_launchd_service() {
  489. if [[ "$OS_TYPE" != "macos" ]] || [[ "$SKIP_SERVICE" == "true" ]]; then
  490. return
  491. fi
  492. log_info "Creating launchd service..."
  493. local plist_path="$HOME/Library/LaunchAgents/com.bambuddy.app.plist"
  494. cat > "$plist_path" << EOF
  495. <?xml version="1.0" encoding="UTF-8"?>
  496. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  497. <plist version="1.0">
  498. <dict>
  499. <key>Label</key>
  500. <string>com.bambuddy.app</string>
  501. <key>ProgramArguments</key>
  502. <array>
  503. <string>$INSTALL_PATH/venv/bin/uvicorn</string>
  504. <string>backend.app.main:app</string>
  505. <string>--host</string>
  506. <string>$BIND_ADDRESS</string>
  507. <string>--port</string>
  508. <string>$PORT</string>
  509. </array>
  510. <key>WorkingDirectory</key>
  511. <string>$INSTALL_PATH</string>
  512. <key>EnvironmentVariables</key>
  513. <dict>
  514. <key>DEBUG</key>
  515. <string>$DEBUG_MODE</string>
  516. <key>LOG_LEVEL</key>
  517. <string>$LOG_LEVEL</string>
  518. <key>DATA_DIR</key>
  519. <string>$DATA_DIR</string>
  520. <key>LOG_DIR</key>
  521. <string>$LOG_DIR</string>
  522. <key>TZ</key>
  523. <string>$TIMEZONE</string>
  524. </dict>
  525. <key>RunAtLoad</key>
  526. <true/>
  527. <key>KeepAlive</key>
  528. <true/>
  529. <key>StandardOutPath</key>
  530. <string>$LOG_DIR/bambuddy.log</string>
  531. <key>StandardErrorPath</key>
  532. <string>$LOG_DIR/bambuddy.error.log</string>
  533. </dict>
  534. </plist>
  535. EOF
  536. log_success "Launchd plist created at $plist_path"
  537. if prompt_yes_no "Load BamBuddy service now?" "y"; then
  538. launchctl load "$plist_path"
  539. sleep 2
  540. if launchctl list | grep -q "com.bambuddy.app"; then
  541. log_success "BamBuddy is running"
  542. else
  543. log_warn "Service may have failed to start. Check: cat $LOG_DIR/bambuddy.error.log"
  544. fi
  545. fi
  546. }
  547. # -----------------------------------------------------------------------------
  548. # Main Installation Flow
  549. # -----------------------------------------------------------------------------
  550. parse_args() {
  551. while [[ $# -gt 0 ]]; do
  552. case "$1" in
  553. --path)
  554. INSTALL_PATH="$2"
  555. shift 2
  556. ;;
  557. --port)
  558. PORT="$2"
  559. shift 2
  560. ;;
  561. --bind)
  562. BIND_ADDRESS="$2"
  563. shift 2
  564. ;;
  565. --tz)
  566. TIMEZONE="$2"
  567. shift 2
  568. ;;
  569. --data-dir)
  570. DATA_DIR="$2"
  571. shift 2
  572. ;;
  573. --log-dir)
  574. LOG_DIR="$2"
  575. shift 2
  576. ;;
  577. --debug)
  578. DEBUG_MODE="true"
  579. shift
  580. ;;
  581. --log-level)
  582. LOG_LEVEL="$2"
  583. shift 2
  584. ;;
  585. --branch)
  586. BRANCH="$2"
  587. shift 2
  588. ;;
  589. --no-service)
  590. SKIP_SERVICE="true"
  591. shift
  592. ;;
  593. --set-system-tz)
  594. SET_SYSTEM_TZ="true"
  595. shift
  596. ;;
  597. --yes|-y)
  598. NON_INTERACTIVE="true"
  599. shift
  600. ;;
  601. --help|-h)
  602. show_help
  603. ;;
  604. *)
  605. log_error "Unknown option: $1"
  606. show_help
  607. ;;
  608. esac
  609. done
  610. }
  611. gather_config() {
  612. echo ""
  613. echo -e "${BOLD}Installation Configuration${NC}"
  614. echo -e "${CYAN}─────────────────────────────────────────${NC}"
  615. echo ""
  616. # Installation path
  617. [[ -z "$INSTALL_PATH" ]] && prompt "Installation directory" "$DEFAULT_INSTALL_PATH" INSTALL_PATH
  618. # Branch
  619. [[ -z "$BRANCH" ]] && prompt "Git branch" "main" BRANCH
  620. # Port
  621. [[ -z "$PORT" ]] && prompt "Port to listen on" "$DEFAULT_PORT" PORT
  622. # Bind address
  623. if [[ -z "$BIND_ADDRESS" ]]; then
  624. echo ""
  625. echo "Network access:"
  626. echo " 0.0.0.0 - Accessible from other devices on your network (recommended)"
  627. echo " 127.0.0.1 - Only accessible from this machine"
  628. prompt "Bind address" "$DEFAULT_BIND_ADDRESS" BIND_ADDRESS
  629. fi
  630. # Timezone
  631. detect_timezone
  632. prompt "Timezone" "$TIMEZONE" TIMEZONE
  633. # Offer to set system timezone if different from current (skip if already set via --set-system-tz)
  634. if [[ -z "$SET_SYSTEM_TZ" ]]; then
  635. local current_tz
  636. current_tz=$(timedatectl show --property=Timezone --value 2>/dev/null) || true
  637. if [[ -n "$TIMEZONE" ]] && [[ "$TIMEZONE" != "$current_tz" ]]; then
  638. # Default to "n" so unattended installs don't change system TZ unless --set-system-tz is used
  639. if prompt_yes_no "Set system timezone to $TIMEZONE?" "n"; then
  640. SET_SYSTEM_TZ="true"
  641. else
  642. SET_SYSTEM_TZ="false"
  643. fi
  644. else
  645. SET_SYSTEM_TZ="false"
  646. fi
  647. fi
  648. # Data directory
  649. [[ -z "$DATA_DIR" ]] && DATA_DIR="$INSTALL_PATH/data"
  650. prompt "Data directory" "$DATA_DIR" DATA_DIR
  651. # Log directory
  652. [[ -z "$LOG_DIR" ]] && LOG_DIR="$INSTALL_PATH/logs"
  653. prompt "Log directory" "$LOG_DIR" LOG_DIR
  654. # Debug mode
  655. if [[ -z "$DEBUG_MODE" ]]; then
  656. if prompt_yes_no "Enable debug mode?" "n"; then
  657. DEBUG_MODE="true"
  658. else
  659. DEBUG_MODE="false"
  660. fi
  661. fi
  662. # Log level
  663. if [[ -z "$LOG_LEVEL" ]]; then
  664. echo ""
  665. echo "Log levels: DEBUG, INFO, WARNING, ERROR"
  666. prompt "Log level" "$DEFAULT_LOG_LEVEL" LOG_LEVEL
  667. fi
  668. # Confirm
  669. echo ""
  670. echo -e "${BOLD}Installation Summary${NC}"
  671. echo -e "${CYAN}─────────────────────────────────────────${NC}"
  672. echo -e " Install path: ${GREEN}$INSTALL_PATH${NC}"
  673. if [[ "$BRANCH" != "main" ]]; then
  674. echo -e " Branch: ${YELLOW}$BRANCH${NC} (beta)"
  675. else
  676. echo -e " Branch: ${GREEN}$BRANCH${NC}"
  677. fi
  678. echo -e " Port: ${GREEN}$PORT${NC}"
  679. echo -e " Bind address: ${GREEN}$BIND_ADDRESS${NC}"
  680. echo -e " Timezone: ${GREEN}$TIMEZONE${NC}"
  681. echo -e " Data dir: ${GREEN}$DATA_DIR${NC}"
  682. echo -e " Log dir: ${GREEN}$LOG_DIR${NC}"
  683. echo -e " Debug mode: ${GREEN}$DEBUG_MODE${NC}"
  684. echo -e " Log level: ${GREEN}$LOG_LEVEL${NC}"
  685. echo ""
  686. if ! prompt_yes_no "Proceed with installation?" "y"; then
  687. echo "Installation cancelled."
  688. exit 0
  689. fi
  690. }
  691. main() {
  692. parse_args "$@"
  693. print_banner
  694. # Check if running via pipe (curl | bash) - interactive mode won't work
  695. if [[ ! -t 0 ]] && [[ "$NON_INTERACTIVE" != "true" ]]; then
  696. log_error "Interactive mode requires a terminal."
  697. log_info "When using 'curl | bash', you must use non-interactive mode:"
  698. echo ""
  699. echo " curl -fsSL URL | bash -s -- --yes"
  700. echo ""
  701. log_info "Or download and run directly:"
  702. echo ""
  703. echo " curl -fsSL URL -o install.sh && chmod +x install.sh && ./install.sh"
  704. echo ""
  705. exit 1
  706. fi
  707. # Check for root (we need sudo for some operations)
  708. if [[ "$EUID" -eq 0 ]] && [[ "$OS_TYPE" != "macos" ]]; then
  709. log_warn "Running as root. Consider using a regular user with sudo privileges."
  710. fi
  711. # Detect system
  712. log_info "Detecting system..."
  713. detect_os
  714. log_success "Detected: $OS_TYPE (package manager: $PKG_MANAGER)"
  715. # Check/install Python
  716. if ! detect_python; then
  717. log_info "Python 3.10+ not found, will install..."
  718. fi
  719. # Gather configuration
  720. gather_config
  721. # Install steps
  722. echo ""
  723. echo -e "${BOLD}Starting Installation${NC}"
  724. echo -e "${CYAN}─────────────────────────────────────────${NC}"
  725. echo ""
  726. install_dependencies
  727. detect_python || { log_error "Failed to install Python"; exit 1; }
  728. # Set system timezone if requested
  729. if [[ "$SET_SYSTEM_TZ" == "true" ]]; then
  730. log_info "Setting system timezone to $TIMEZONE..."
  731. if [[ "$OS_TYPE" == "macos" ]]; then
  732. sudo systemsetup -settimezone "$TIMEZONE" 2>/dev/null || true
  733. else
  734. sudo timedatectl set-timezone "$TIMEZONE" 2>/dev/null || true
  735. fi
  736. log_success "System timezone set to $TIMEZONE"
  737. fi
  738. if [[ "$OS_TYPE" != "macos" ]]; then
  739. create_user
  740. else
  741. SERVICE_USER="$USER"
  742. fi
  743. download_bambuddy
  744. setup_virtualenv
  745. build_frontend
  746. create_directories
  747. create_env_file
  748. if [[ "$OS_TYPE" == "macos" ]]; then
  749. create_launchd_service
  750. else
  751. create_systemd_service
  752. fi
  753. # Done!
  754. echo ""
  755. echo -e "${GREEN}╔══════════════════════════════════════════════════════════════╗${NC}"
  756. echo -e "${GREEN}║ ║${NC}"
  757. echo -e "${GREEN}║ Installation Complete! ║${NC}"
  758. echo -e "${GREEN}║ ║${NC}"
  759. echo -e "${GREEN}╚══════════════════════════════════════════════════════════════╝${NC}"
  760. echo ""
  761. # Show appropriate URL based on bind address
  762. if [[ "$BIND_ADDRESS" == "0.0.0.0" ]]; then
  763. local ip_addr
  764. ip_addr=$(hostname -I 2>/dev/null | awk '{print $1}') || ip_addr="<your-ip>"
  765. echo -e " ${BOLD}Access BamBuddy:${NC} ${CYAN}http://localhost:$PORT${NC}"
  766. echo -e " ${CYAN}http://$ip_addr:$PORT${NC} (from other devices)"
  767. else
  768. echo -e " ${BOLD}Access BamBuddy:${NC} ${CYAN}http://localhost:$PORT${NC}"
  769. fi
  770. echo ""
  771. if [[ "$OS_TYPE" == "macos" ]]; then
  772. echo -e " ${BOLD}Manage service:${NC}"
  773. echo -e " Start: launchctl load ~/Library/LaunchAgents/com.bambuddy.app.plist"
  774. echo -e " Stop: launchctl unload ~/Library/LaunchAgents/com.bambuddy.app.plist"
  775. echo -e " Logs: tail -f $LOG_DIR/bambuddy.log"
  776. else
  777. echo -e " ${BOLD}Manage service:${NC}"
  778. echo -e " Status: sudo systemctl status bambuddy"
  779. echo -e " Start: sudo systemctl start bambuddy"
  780. echo -e " Stop: sudo systemctl stop bambuddy"
  781. echo -e " Logs: sudo journalctl -u bambuddy -f"
  782. fi
  783. echo ""
  784. echo -e " ${BOLD}Update BamBuddy:${NC}"
  785. echo -e " cd $INSTALL_PATH && git pull && source venv/bin/activate"
  786. echo -e " pip install -r requirements.txt && cd frontend && npm ci && npm run build"
  787. if [[ "$OS_TYPE" != "macos" ]]; then
  788. echo -e " sudo systemctl restart bambuddy"
  789. fi
  790. echo ""
  791. echo -e " ${BOLD}Documentation:${NC} ${CYAN}https://wiki.bambuddy.cool${NC}"
  792. echo ""
  793. }
  794. main "$@"