docker-install.sh 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. #!/usr/bin/env bash
  2. #
  3. # BamBuddy Docker Installation Script
  4. # Supports: Linux (all distros), macOS
  5. #
  6. # Usage:
  7. # Interactive: curl -fsSL https://raw.githubusercontent.com/maziggy/bambuddy/main/install/docker-install.sh | bash
  8. # Unattended: ./docker-install.sh --path /opt/bambuddy --port 8000 --yes
  9. #
  10. # Options:
  11. # --path PATH Installation directory (default: /opt/bambuddy)
  12. # --port PORT Port to expose (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. # --build Build from source instead of using pre-built image
  16. # --yes, -y Non-interactive mode, accept defaults
  17. # --help, -h Show this help message
  18. #
  19. set -e
  20. # Colors for output
  21. RED='\033[0;31m'
  22. GREEN='\033[0;32m'
  23. YELLOW='\033[1;33m'
  24. BLUE='\033[0;34m'
  25. CYAN='\033[0;36m'
  26. NC='\033[0m' # No Color
  27. BOLD='\033[1m'
  28. # Default values
  29. DEFAULT_INSTALL_PATH="/opt/bambuddy"
  30. DEFAULT_PORT="8000"
  31. DEFAULT_BIND_ADDRESS="0.0.0.0"
  32. # Script variables
  33. INSTALL_PATH=""
  34. PORT=""
  35. BIND_ADDRESS=""
  36. TIMEZONE=""
  37. BUILD_FROM_SOURCE="false"
  38. NON_INTERACTIVE="false"
  39. OS_TYPE=""
  40. DOCKER_CMD=""
  41. # -----------------------------------------------------------------------------
  42. # Helper Functions
  43. # -----------------------------------------------------------------------------
  44. print_banner() {
  45. echo -e "${CYAN}"
  46. echo "╔════════════════════════════════════════════════════════╗"
  47. echo "║ ║"
  48. echo "║ ____ _ _ _ ║"
  49. echo "║ | __ ) __ _ _ __ ___ | |__ _ _ __| | __| |_ _ ║"
  50. echo "║ | _ \\ / _\` | '_ \` _ \\| '_ \\| | | |/ _\` |/ _\` | | | | ║"
  51. echo "║ | |_) | (_| | | | | | | |_) | |_| | (_| | (_| | |_| | ║"
  52. echo "║ |____/ \\__,_|_| |_| |_|_.__/ \\__,_|\\__,_|\\__,_|\\__, | ║"
  53. echo "║ |___/ ║"
  54. echo "║ ║"
  55. echo "║ Docker Installation Script ║"
  56. echo "║ ║"
  57. echo "╚════════════════════════════════════════════════════════╝"
  58. echo -e "${NC}"
  59. }
  60. log_info() {
  61. echo -e "${BLUE}[INFO]${NC} $1"
  62. }
  63. log_success() {
  64. echo -e "${GREEN}[OK]${NC} $1"
  65. }
  66. log_warn() {
  67. echo -e "${YELLOW}[WARN]${NC} $1"
  68. }
  69. log_error() {
  70. echo -e "${RED}[ERROR]${NC} $1"
  71. }
  72. prompt() {
  73. local prompt_text="$1"
  74. local default_value="$2"
  75. local var_name="$3"
  76. if [[ "$NON_INTERACTIVE" == "true" ]]; then
  77. eval "$var_name=\"$default_value\""
  78. return
  79. fi
  80. if [[ -n "$default_value" ]]; then
  81. echo -en "${BOLD}$prompt_text${NC} [${CYAN}$default_value${NC}]: "
  82. else
  83. echo -en "${BOLD}$prompt_text${NC}: "
  84. fi
  85. read -r input
  86. if [[ -z "$input" ]]; then
  87. eval "$var_name=\"$default_value\""
  88. else
  89. eval "$var_name=\"$input\""
  90. fi
  91. }
  92. prompt_yes_no() {
  93. local prompt_text="$1"
  94. local default="$2" # y or n
  95. if [[ "$NON_INTERACTIVE" == "true" ]]; then
  96. [[ "$default" == "y" ]] && return 0 || return 1
  97. fi
  98. local yn_hint="[y/n]"
  99. [[ "$default" == "y" ]] && yn_hint="[Y/n]"
  100. [[ "$default" == "n" ]] && yn_hint="[y/N]"
  101. while true; do
  102. echo -en "${BOLD}$prompt_text${NC} $yn_hint: "
  103. read -r yn
  104. [[ -z "$yn" ]] && yn="$default"
  105. case "$yn" in
  106. [Yy]* ) return 0;;
  107. [Nn]* ) return 1;;
  108. * ) echo "Please answer yes or no.";;
  109. esac
  110. done
  111. }
  112. show_help() {
  113. echo "BamBuddy Docker Installation Script"
  114. echo ""
  115. echo "Usage: $0 [OPTIONS]"
  116. echo ""
  117. echo "Options:"
  118. echo " --path PATH Installation directory (default: /opt/bambuddy)"
  119. echo " --port PORT Port to expose (default: 8000)"
  120. echo " --bind ADDRESS Bind address: 0.0.0.0 (network) or 127.0.0.1 (local only)"
  121. echo " --tz TIMEZONE Timezone (default: system timezone or UTC)"
  122. echo " --build Build from source instead of using pre-built image"
  123. echo " --yes, -y Non-interactive mode, accept defaults"
  124. echo " --help, -h Show this help message"
  125. echo ""
  126. echo "Examples:"
  127. echo " Interactive installation:"
  128. echo " ./docker-install.sh"
  129. echo ""
  130. echo " Unattended installation with custom settings:"
  131. echo " ./docker-install.sh --path /srv/bambuddy --port 3000 --tz America/New_York --yes"
  132. echo ""
  133. echo " Build from source:"
  134. echo " ./docker-install.sh --build --yes"
  135. exit 0
  136. }
  137. # -----------------------------------------------------------------------------
  138. # System Detection
  139. # -----------------------------------------------------------------------------
  140. detect_os() {
  141. if [[ "$OSTYPE" == "darwin"* ]]; then
  142. OS_TYPE="macos"
  143. return
  144. fi
  145. if [[ -f /etc/os-release ]]; then
  146. OS_TYPE="linux"
  147. else
  148. log_error "Cannot detect operating system"
  149. exit 1
  150. fi
  151. }
  152. detect_docker() {
  153. # Check for docker compose (v2) or docker-compose (v1)
  154. if docker compose version &>/dev/null 2>&1; then
  155. DOCKER_CMD="docker compose"
  156. log_success "Found Docker Compose v2"
  157. return 0
  158. elif docker-compose --version &>/dev/null 2>&1; then
  159. DOCKER_CMD="docker-compose"
  160. log_success "Found Docker Compose v1"
  161. return 0
  162. fi
  163. return 1
  164. }
  165. detect_timezone() {
  166. if [[ -n "$TIMEZONE" ]]; then
  167. return 0
  168. fi
  169. # Try to get system timezone (with error handling for set -e)
  170. TIMEZONE=""
  171. if [[ -f /etc/timezone ]]; then
  172. TIMEZONE=$(cat /etc/timezone 2>/dev/null) || true
  173. fi
  174. if [[ -z "$TIMEZONE" ]] && [[ -L /etc/localtime ]]; then
  175. TIMEZONE=$(readlink /etc/localtime 2>/dev/null | sed 's|.*/zoneinfo/||') || true
  176. fi
  177. if [[ -z "$TIMEZONE" ]] && command -v timedatectl &>/dev/null; then
  178. TIMEZONE=$(timedatectl show --property=Timezone --value 2>/dev/null) || true
  179. fi
  180. # Default to UTC if not found (use if/then to avoid set -e issue with &&)
  181. if [[ -z "$TIMEZONE" ]]; then
  182. TIMEZONE="UTC"
  183. fi
  184. return 0
  185. }
  186. # -----------------------------------------------------------------------------
  187. # Installation Functions
  188. # -----------------------------------------------------------------------------
  189. install_docker() {
  190. log_info "Docker not found, installing..."
  191. case "$OS_TYPE" in
  192. linux)
  193. # Use Docker's convenience script
  194. curl -fsSL https://get.docker.com | sh
  195. # Add current user to docker group
  196. if [[ -n "$SUDO_USER" ]]; then
  197. sudo usermod -aG docker "$SUDO_USER"
  198. log_warn "Added $SUDO_USER to docker group. You may need to log out and back in."
  199. else
  200. sudo usermod -aG docker "$USER"
  201. log_warn "Added $USER to docker group. You may need to log out and back in."
  202. fi
  203. # Start Docker service
  204. sudo systemctl enable docker
  205. sudo systemctl start docker
  206. ;;
  207. macos)
  208. log_error "Docker Desktop not found."
  209. log_error "Please install Docker Desktop for Mac from: https://www.docker.com/products/docker-desktop"
  210. exit 1
  211. ;;
  212. esac
  213. log_success "Docker installed"
  214. }
  215. create_install_dir() {
  216. log_info "Creating installation directory..."
  217. mkdir -p "$INSTALL_PATH"
  218. cd "$INSTALL_PATH"
  219. log_success "Directory created: $INSTALL_PATH"
  220. }
  221. download_compose_file() {
  222. log_info "Downloading docker-compose.yml..."
  223. if [[ "$BUILD_FROM_SOURCE" == "true" ]]; then
  224. # Clone the full repo for building
  225. if [[ -d ".git" ]]; then
  226. log_info "Existing repository found, updating..."
  227. git fetch origin
  228. git reset --hard origin/main
  229. else
  230. git clone https://github.com/maziggy/bambuddy.git .
  231. fi
  232. else
  233. # Just download the compose file
  234. curl -fsSL -o docker-compose.yml \
  235. https://raw.githubusercontent.com/maziggy/bambuddy/main/docker-compose.yml
  236. fi
  237. log_success "docker-compose.yml ready"
  238. }
  239. create_env_file() {
  240. log_info "Creating environment configuration..."
  241. cat > .env << EOF
  242. # BamBuddy Docker Configuration
  243. # Generated by docker-install.sh on $(date)
  244. # Port BamBuddy runs on
  245. PORT=$PORT
  246. # Timezone
  247. TZ=$TIMEZONE
  248. EOF
  249. log_success "Environment file created"
  250. }
  251. customize_compose() {
  252. # Detect if we need to disable host networking (macOS/Windows in Docker Desktop)
  253. if [[ "$OS_TYPE" == "macos" ]]; then
  254. log_warn "Docker Desktop detected. Host networking is not supported."
  255. log_info "Modifying docker-compose.yml for port mapping..."
  256. # Create a modified compose file for macOS
  257. if [[ -f docker-compose.yml ]]; then
  258. # Comment out network_mode: host and uncomment ports section
  259. sed -i.bak \
  260. -e 's/^[[:space:]]*network_mode: host/# network_mode: host/' \
  261. -e 's/^[[:space:]]*#ports:/ ports:/' \
  262. -e 's/^[[:space:]]*#[[:space:]]*- "\${PORT:-8000}:8000"/ - "\${PORT:-8000}:8000"/' \
  263. docker-compose.yml
  264. log_warn "Printer discovery may not work. Add printers manually by IP address."
  265. fi
  266. fi
  267. }
  268. start_container() {
  269. log_info "Starting BamBuddy..."
  270. if [[ "$BUILD_FROM_SOURCE" == "true" ]]; then
  271. $DOCKER_CMD up -d --build
  272. else
  273. $DOCKER_CMD up -d
  274. fi
  275. # Wait for container to start
  276. log_info "Waiting for container to start..."
  277. local max_attempts=15
  278. local attempt=0
  279. while [[ $attempt -lt $max_attempts ]]; do
  280. # Check if container is running (Up)
  281. if $DOCKER_CMD ps | grep -q "Up"; then
  282. log_success "BamBuddy container is running"
  283. return 0
  284. fi
  285. # Check if container failed
  286. if $DOCKER_CMD ps -a | grep -q "Exited"; then
  287. log_error "Container failed to start"
  288. log_info "Check logs with: $DOCKER_CMD logs bambuddy"
  289. return 1
  290. fi
  291. sleep 2
  292. ((attempt++))
  293. done
  294. log_warn "Container may still be starting. Check with: $DOCKER_CMD ps"
  295. }
  296. # -----------------------------------------------------------------------------
  297. # Main Installation Flow
  298. # -----------------------------------------------------------------------------
  299. parse_args() {
  300. while [[ $# -gt 0 ]]; do
  301. case "$1" in
  302. --path)
  303. INSTALL_PATH="$2"
  304. shift 2
  305. ;;
  306. --port)
  307. PORT="$2"
  308. shift 2
  309. ;;
  310. --bind)
  311. BIND_ADDRESS="$2"
  312. shift 2
  313. ;;
  314. --tz)
  315. TIMEZONE="$2"
  316. shift 2
  317. ;;
  318. --build)
  319. BUILD_FROM_SOURCE="true"
  320. shift
  321. ;;
  322. --yes|-y)
  323. NON_INTERACTIVE="true"
  324. shift
  325. ;;
  326. --help|-h)
  327. show_help
  328. ;;
  329. *)
  330. log_error "Unknown option: $1"
  331. show_help
  332. ;;
  333. esac
  334. done
  335. }
  336. gather_config() {
  337. echo ""
  338. echo -e "${BOLD}Installation Configuration${NC}"
  339. echo -e "${CYAN}─────────────────────────────────────────${NC}"
  340. echo ""
  341. # Installation path
  342. [[ -z "$INSTALL_PATH" ]] && prompt "Installation directory" "$DEFAULT_INSTALL_PATH" INSTALL_PATH
  343. # Port
  344. [[ -z "$PORT" ]] && prompt "Port to expose" "$DEFAULT_PORT" PORT
  345. # Bind address
  346. if [[ -z "$BIND_ADDRESS" ]]; then
  347. echo ""
  348. echo "Network access:"
  349. echo " 0.0.0.0 - Accessible from other devices on your network (recommended)"
  350. echo " 127.0.0.1 - Only accessible from this machine"
  351. prompt "Bind address" "$DEFAULT_BIND_ADDRESS" BIND_ADDRESS
  352. fi
  353. # Timezone
  354. detect_timezone
  355. prompt "Timezone" "$TIMEZONE" TIMEZONE
  356. # Build from source?
  357. if [[ "$BUILD_FROM_SOURCE" != "true" ]] && [[ "$NON_INTERACTIVE" != "true" ]]; then
  358. if prompt_yes_no "Build from source? (No = use pre-built image)" "n"; then
  359. BUILD_FROM_SOURCE="true"
  360. fi
  361. fi
  362. # Confirm
  363. echo ""
  364. echo -e "${BOLD}Installation Summary${NC}"
  365. echo -e "${CYAN}─────────────────────────────────────────${NC}"
  366. echo -e " Install path: ${GREEN}$INSTALL_PATH${NC}"
  367. echo -e " Port: ${GREEN}$PORT${NC}"
  368. echo -e " Bind address: ${GREEN}$BIND_ADDRESS${NC}"
  369. echo -e " Timezone: ${GREEN}$TIMEZONE${NC}"
  370. echo -e " Build source: ${GREEN}$BUILD_FROM_SOURCE${NC}"
  371. echo ""
  372. if ! prompt_yes_no "Proceed with installation?" "y"; then
  373. echo "Installation cancelled."
  374. exit 0
  375. fi
  376. }
  377. main() {
  378. parse_args "$@"
  379. print_banner
  380. # Check if running via pipe (curl | bash) - interactive mode won't work
  381. if [[ ! -t 0 ]] && [[ "$NON_INTERACTIVE" != "true" ]]; then
  382. log_error "Interactive mode requires a terminal."
  383. log_info "When using 'curl | bash', you must use non-interactive mode:"
  384. echo ""
  385. echo " curl -fsSL URL | bash -s -- --yes"
  386. echo ""
  387. log_info "Or download and run directly:"
  388. echo ""
  389. echo " curl -fsSL URL -o docker-install.sh && chmod +x docker-install.sh && ./docker-install.sh"
  390. echo ""
  391. exit 1
  392. fi
  393. # Detect system
  394. log_info "Detecting system..."
  395. detect_os
  396. log_success "Detected: $OS_TYPE"
  397. # Check for Docker
  398. if ! command -v docker &>/dev/null; then
  399. install_docker
  400. fi
  401. if ! detect_docker; then
  402. log_error "Docker Compose not found. Please install Docker Compose."
  403. exit 1
  404. fi
  405. # Check if Docker daemon is running
  406. if ! docker info &>/dev/null; then
  407. log_error "Docker daemon is not running. Please start Docker and try again."
  408. exit 1
  409. fi
  410. # Gather configuration
  411. gather_config
  412. # Install steps
  413. echo ""
  414. echo -e "${BOLD}Starting Installation${NC}"
  415. echo -e "${CYAN}─────────────────────────────────────────${NC}"
  416. echo ""
  417. create_install_dir
  418. download_compose_file
  419. create_env_file
  420. customize_compose
  421. start_container
  422. # Done!
  423. echo ""
  424. echo -e "${GREEN}╔══════════════════════════════════════════════════════════════╗${NC}"
  425. echo -e "${GREEN}║ ║${NC}"
  426. echo -e "${GREEN}║ Installation Complete! ║${NC}"
  427. echo -e "${GREEN}║ ║${NC}"
  428. echo -e "${GREEN}╚══════════════════════════════════════════════════════════════╝${NC}"
  429. echo ""
  430. # Show appropriate URL based on bind address
  431. if [[ "$BIND_ADDRESS" == "0.0.0.0" ]]; then
  432. local ip_addr
  433. ip_addr=$(hostname -I 2>/dev/null | awk '{print $1}') || ip_addr="<your-ip>"
  434. echo -e " ${BOLD}Access BamBuddy:${NC} ${CYAN}http://localhost:$PORT${NC}"
  435. echo -e " ${CYAN}http://$ip_addr:$PORT${NC} (from other devices)"
  436. else
  437. echo -e " ${BOLD}Access BamBuddy:${NC} ${CYAN}http://localhost:$PORT${NC}"
  438. fi
  439. echo ""
  440. echo -e " ${BOLD}Manage container:${NC}"
  441. echo -e " Status: cd $INSTALL_PATH && $DOCKER_CMD ps"
  442. echo -e " Logs: cd $INSTALL_PATH && $DOCKER_CMD logs -f bambuddy"
  443. echo -e " Stop: cd $INSTALL_PATH && $DOCKER_CMD down"
  444. echo -e " Start: cd $INSTALL_PATH && $DOCKER_CMD up -d"
  445. echo -e " Restart: cd $INSTALL_PATH && $DOCKER_CMD restart"
  446. echo ""
  447. echo -e " ${BOLD}Update BamBuddy:${NC}"
  448. if [[ "$BUILD_FROM_SOURCE" == "true" ]]; then
  449. echo -e " cd $INSTALL_PATH && git pull && $DOCKER_CMD up -d --build"
  450. else
  451. echo -e " cd $INSTALL_PATH && $DOCKER_CMD pull && $DOCKER_CMD up -d"
  452. fi
  453. echo ""
  454. echo -e " ${BOLD}Data location:${NC} Docker volumes (bambuddy_data, bambuddy_logs)"
  455. echo ""
  456. echo -e " ${BOLD}Documentation:${NC} ${CYAN}https://wiki.bambuddy.cool${NC}"
  457. echo ""
  458. if [[ "$OS_TYPE" == "macos" ]]; then
  459. echo -e " ${YELLOW}Note:${NC} Printer discovery may not work with Docker Desktop."
  460. echo -e " Add printers manually using their IP address."
  461. echo ""
  462. fi
  463. }
  464. main "$@"