beepzone-helper.sh 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. # Simple TUI-based BeepZone setup helper using `dialog`.
  4. # Targets macOS + Debian-ish Linux (podman, rustup mysql-client already installed).
  5. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  6. WORK_DIR="${SCRIPT_DIR}"
  7. : "${DIALOG:=dialog}"
  8. if ! command -v "$DIALOG" >/dev/null 2>&1; then
  9. echo "\n[ERROR] 'dialog' is not installed or not in PATH." >&2
  10. echo "On macOS: brew install dialog" >&2
  11. echo "On Debian: sudo apt install dialog" >&2
  12. exit 1
  13. fi
  14. # Determine whether the dialog implementation supports custom OK label
  15. # 'dialog' uses --ok-label, 'whiptail' uses --ok-button
  16. declare -a DIALOG_OK_FLAG=()
  17. if "$DIALOG" --help 2>&1 | grep -q -- "--ok-label"; then
  18. DIALOG_OK_FLAG=(--ok-label "Continue")
  19. elif "$DIALOG" --help 2>&1 | grep -q -- "--ok-button"; then
  20. DIALOG_OK_FLAG=(--ok-button "Continue")
  21. fi
  22. HAS_PODMAN=true
  23. if ! command -v podman >/dev/null 2>&1; then
  24. HAS_PODMAN=false
  25. fi
  26. IS_DEBIAN_NATIVE=false
  27. if [[ -f /etc/debian_version ]]; then
  28. if grep -qE "^(12|13)" /etc/debian_version; then
  29. IS_DEBIAN_NATIVE=true
  30. fi
  31. fi
  32. # Check for MariaDB/MySQL client - try common brew locations on macOS
  33. MYSQL_CLIENT=""
  34. if command -v mariadb >/dev/null 2>&1; then
  35. MYSQL_CLIENT="mariadb"
  36. elif command -v mysql >/dev/null 2>&1; then
  37. MYSQL_CLIENT="mysql"
  38. elif [[ -x /opt/homebrew/opt/mysql-client/bin/mysql ]]; then
  39. MYSQL_CLIENT="/opt/homebrew/opt/mysql-client/bin/mysql"
  40. export PATH="/opt/homebrew/opt/mysql-client/bin:$PATH"
  41. elif [[ -x /opt/homebrew/opt/mariadb/bin/mariadb ]]; then
  42. MYSQL_CLIENT="/opt/homebrew/opt/mariadb/bin/mariadb"
  43. export PATH="/opt/homebrew/opt/mariadb/bin:$PATH"
  44. elif [[ -x /opt/homebrew/bin/mariadb ]]; then
  45. MYSQL_CLIENT="/opt/homebrew/bin/mariadb"
  46. export PATH="/opt/homebrew/bin:$PATH"
  47. elif [[ -x /opt/homebrew/bin/mysql ]]; then
  48. MYSQL_CLIENT="/opt/homebrew/bin/mysql"
  49. export PATH="/opt/homebrew/bin:$PATH"
  50. elif [[ -x /usr/local/opt/mysql-client/bin/mysql ]]; then
  51. MYSQL_CLIENT="/usr/local/opt/mysql-client/bin/mysql"
  52. export PATH="/usr/local/opt/mysql-client/bin:$PATH"
  53. elif [[ -x /usr/local/bin/mariadb ]]; then
  54. MYSQL_CLIENT="/usr/local/bin/mariadb"
  55. export PATH="/usr/local/bin:$PATH"
  56. elif [[ -x /usr/local/bin/mysql ]]; then
  57. MYSQL_CLIENT="/usr/local/bin/mysql"
  58. export PATH="/usr/local/bin:$PATH"
  59. fi
  60. # Cargo / Git detection (hide menu options if missing)
  61. HAS_CARGO=false
  62. if command -v cargo >/dev/null 2>&1; then
  63. HAS_CARGO=true
  64. fi
  65. HAS_GIT=false
  66. if command -v git >/dev/null 2>&1; then
  67. HAS_GIT=true
  68. fi
  69. # Selected DB target for user/role and imports
  70. DB_TARGET=""
  71. TMP_FILE="$(mktemp)"
  72. CHOICE_FILE="$(mktemp)"
  73. trap 'rm -f "$TMP_FILE" "$CHOICE_FILE"' EXIT
  74. LOG_FILE="/tmp/beepzone-helper.log"
  75. > "$LOG_FILE"
  76. ENV_FILE="$SCRIPT_DIR/.env"
  77. # Load existing settings from .env if present
  78. if [[ -f "$ENV_FILE" ]]; then
  79. source "$ENV_FILE"
  80. fi
  81. # Set defaults if not loaded from .env
  82. : "${BEEPZONE_DB_CONTAINER_NAME:=beepzone-mariadb}"
  83. : "${BEEPZONE_DB_IMAGE:=mariadb:12}"
  84. : "${DB_HOST:=127.0.0.1}"
  85. : "${DB_PORT:=3306}"
  86. : "${DB_NAME:=beepzone}"
  87. : "${DB_USER:=beepzone}"
  88. : "${DB_PASS:=beepzone}"
  89. : "${DB_ROOT_PASSWORD:=root}"
  90. : "${SECKELAPI_REPO:=https://git.teleco.ch/crt/seckelapi.git}"
  91. : "${CLIENT_REPO:=https://git.teleco.ch/crt/beepzone-client-egui-emo.git}"
  92. : "${DEPLOYMENT_TYPE:=clean}"
  93. save_env() {
  94. cat > "$ENV_FILE" << EOF
  95. # BeepZone Setup Configuration
  96. # Mostly autogenerated by beepzone-helper
  97. BEEPZONE_DB_CONTAINER_NAME="$BEEPZONE_DB_CONTAINER_NAME"
  98. BEEPZONE_DB_IMAGE="$BEEPZONE_DB_IMAGE"
  99. DB_HOST="$DB_HOST"
  100. DB_PORT="$DB_PORT"
  101. DB_NAME="$DB_NAME"
  102. DB_USER="$DB_USER"
  103. DB_PASS="$DB_PASS"
  104. DB_ROOT_PASSWORD="$DB_ROOT_PASSWORD"
  105. SECKELAPI_REPO="$SECKELAPI_REPO"
  106. CLIENT_REPO="$CLIENT_REPO"
  107. DEPLOYMENT_TYPE="$DEPLOYMENT_TYPE"
  108. EOF
  109. }
  110. choose_db_target() {
  111. local prompt native_available
  112. prompt="${1:-Select where to run the DB operation}"
  113. native_available=false
  114. if $IS_DEBIAN_NATIVE && [[ -n "$MYSQL_CLIENT" ]]; then
  115. native_available=true
  116. fi
  117. if $HAS_PODMAN && $native_available; then
  118. $DIALOG --menu "$prompt" 14 70 2 \
  119. podman "Podman container: $BEEPZONE_DB_CONTAINER_NAME" \
  120. native "Native MariaDB/MySQL (using $MYSQL_CLIENT)" 2>"$CHOICE_FILE" || return 1
  121. cat "$CHOICE_FILE"
  122. return 0
  123. elif $HAS_PODMAN; then
  124. echo "podman"
  125. return 0
  126. elif $native_available; then
  127. echo "native"
  128. return 0
  129. else
  130. "$DIALOG" --msgbox "No database target available.\n\nNative is only supported on Debian 12/13 with a MariaDB/MySQL client.\nInstall podman or meet the native requirements." 12 74
  131. return 1
  132. fi
  133. }
  134. db_query() {
  135. local sql="$1"; local target="${2:-$DB_TARGET}"
  136. if [[ -z "$target" ]]; then
  137. echo "DB target not set" >&2; return 1
  138. fi
  139. if [[ "$target" == "podman" ]]; then
  140. podman exec -i "$BEEPZONE_DB_CONTAINER_NAME" \
  141. mariadb -h"$DB_HOST" -P"$DB_PORT" -uroot -p"$DB_ROOT_PASSWORD" "$DB_NAME" \
  142. -e "$sql" -s -N
  143. else
  144. "$MYSQL_CLIENT" -h"$DB_HOST" -P"$DB_PORT" -uroot -p"$DB_ROOT_PASSWORD" "$DB_NAME" \
  145. -e "$sql" -s -N
  146. fi
  147. }
  148. db_exec_sql() {
  149. local sql="$1"; local target="${2:-$DB_TARGET}"
  150. if [[ -z "$target" ]]; then
  151. echo "DB target not set" >&2; return 1
  152. fi
  153. if [[ "$target" == "podman" ]]; then
  154. echo "$sql" | podman exec -i "$BEEPZONE_DB_CONTAINER_NAME" \
  155. mariadb -h"$DB_HOST" -P"$DB_PORT" -uroot -p"$DB_ROOT_PASSWORD" "$DB_NAME"
  156. else
  157. echo "$sql" | "$MYSQL_CLIENT" -h"$DB_HOST" -P"$DB_PORT" -uroot -p"$DB_ROOT_PASSWORD" "$DB_NAME"
  158. fi
  159. }
  160. show_capabilities_screen() {
  161. local cf
  162. cf="$(mktemp)"
  163. {
  164. echo "Dependency Overview"
  165. echo ""
  166. echo "What you can do :"
  167. if $HAS_PODMAN; then
  168. echo " [X] Configure and run MariaDB using podman"
  169. else
  170. echo " [ ] Configure and run MariaDB using podman - requires podman in PATH"
  171. fi
  172. if $IS_DEBIAN_NATIVE; then
  173. echo " [X] Configure and run MariaDB natively on your system"
  174. else
  175. echo " [ ] Configure and run MariaDB natively on your system - requires Debian 12/13"
  176. fi
  177. if [[ -n "$MYSQL_CLIENT" ]]; then
  178. echo " [X] Import DB schema or seeded demo DB"
  179. echo " [X] Manage users and roles"
  180. else
  181. echo " [ ] Import DB schema or seeded demo DB - requires MariaDB/MySQL client"
  182. echo " [ ] Manage users and roles - requires MariaDB/MySQL client"
  183. fi
  184. if $HAS_PODMAN; then
  185. echo " [X] Configure and setup SeckelAPI using podman"
  186. else
  187. echo " [ ] Configure and setup SeckelAPI using podman - requires podman in PATH"
  188. fi
  189. if $IS_DEBIAN_NATIVE && $HAS_CARGO; then
  190. echo " [X] Setup SeckelAPI natively on your system"
  191. else
  192. echo " [ ] Setup SeckelAPI natively on your system - requires Debian 12/13 and cargo"
  193. fi
  194. if $HAS_CARGO; then
  195. echo " [X] Build desktop client"
  196. else
  197. echo " [ ] Build desktop client - requires cargo (Rust toolchain)"
  198. fi
  199. if $HAS_GIT; then
  200. echo " [X] Update from git masters"
  201. else
  202. echo " [ ] Update from git masters - requires git"
  203. fi
  204. echo " [X] Clean sources"
  205. echo " [X] Quit"
  206. echo "Options that depend on missing tools are hidden in the main menu."
  207. echo ""
  208. if $IS_DEBIAN_NATIVE; then
  209. echo "Quick Setup for Debian Native:"
  210. echo " sudo apt install"
  211. echo " mariadb-server mariadb-client apache2-utils git build-essential libssl-dev pkg-config libclang-dev"
  212. echo ""
  213. echo "For Rust/Cargo:"
  214. echo " curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh"
  215. fi
  216. } > "$cf"
  217. "$DIALOG" --title "BeepZone Helper" "${DIALOG_OK_FLAG[@]}" --textbox "$cf" 24 80 || true
  218. rm -f "$cf"
  219. }
  220. ask_main_menu() {
  221. while true; do
  222. local options=()
  223. if $HAS_PODMAN; then
  224. options+=(1 "Configure and run MariaDB (podman)")
  225. fi
  226. if $IS_DEBIAN_NATIVE; then
  227. options+=(9 "Configure and run MariaDB (native Debian)")
  228. if $HAS_CARGO; then
  229. options+=(10 "Setup SeckelAPI (Native Debian)")
  230. fi
  231. fi
  232. if [[ -n "$MYSQL_CLIENT" ]]; then
  233. options+=(
  234. 2 "Import DB schema and data"
  235. 3 "Manage users and roles"
  236. )
  237. fi
  238. if $HAS_PODMAN; then
  239. options+=(4 "Configure and setup SeckelAPI (podman)")
  240. fi
  241. if $HAS_CARGO; then
  242. options+=(5 "Build desktop client")
  243. fi
  244. options+=(6 "Update from git masters")
  245. options+=(7 "Clean sources")
  246. options+=(8 "Quit")
  247. $DIALOG --clear \
  248. --title "BeepZone Setup" \
  249. --menu "Choose an action" 17 76 7 \
  250. "${options[@]}" 2>"$CHOICE_FILE"
  251. choice=$(<"$CHOICE_FILE")
  252. case $choice in
  253. 1) configure_and_run_db ;;
  254. 9) configure_and_run_native_db ;;
  255. 10) setup_seckelapi_native ;;
  256. 2) import_schema_and_seed ;;
  257. 3) manage_users_and_roles ;;
  258. 4) setup_seckelapi ;;
  259. 5) build_desktop_client ;;
  260. 6) update_from_git_masters ;;
  261. 7) clean_sources ;;
  262. 8) exit 0 ;;
  263. esac
  264. done
  265. }
  266. configure_and_run_db() {
  267. $DIALOG --form "MariaDB container configuration" 18 70 8 \
  268. "DB Host" 1 1 "$DB_HOST" 1 18 30 30 \
  269. "DB Port" 2 1 "$DB_PORT" 2 18 30 30 \
  270. "DB Name" 3 1 "$DB_NAME" 3 18 30 30 \
  271. "DB User" 4 1 "$DB_USER" 4 18 30 30 \
  272. "DB Password" 5 1 "$DB_PASS" 5 18 30 30 \
  273. "Root Password" 6 1 "$DB_ROOT_PASSWORD" 6 18 30 30 \
  274. "Container Name" 7 1 "$BEEPZONE_DB_CONTAINER_NAME" 7 18 30 30 \
  275. "Image" 8 1 "$BEEPZONE_DB_IMAGE" 8 18 30 30 2>"$TMP_FILE" || return
  276. # Parse dialog output (8 lines, one per field)
  277. {
  278. read -r DB_HOST
  279. read -r DB_PORT
  280. read -r DB_NAME
  281. read -r DB_USER
  282. read -r DB_PASS
  283. read -r DB_ROOT_PASSWORD
  284. read -r BEEPZONE_DB_CONTAINER_NAME
  285. read -r BEEPZONE_DB_IMAGE
  286. } < "$TMP_FILE"
  287. save_env
  288. if podman ps -a --format '{{.Names}}' | grep -q "^${BEEPZONE_DB_CONTAINER_NAME}$"; then
  289. $DIALOG --yesno "Container '$BEEPZONE_DB_CONTAINER_NAME' already exists.\n\nStart (or restart) it now?" 10 60
  290. if [[ $? -eq 0 ]]; then
  291. podman start "$BEEPZONE_DB_CONTAINER_NAME" >/dev/null 2>&1 || podman restart "$BEEPZONE_DB_CONTAINER_NAME" >/dev/null 2>&1 || true
  292. $DIALOG --msgbox "Container '$BEEPZONE_DB_CONTAINER_NAME' started (or already running)." 7 60
  293. fi
  294. return
  295. fi
  296. run_cmd=(
  297. podman run -d
  298. --name "${BEEPZONE_DB_CONTAINER_NAME}"
  299. -e "MARIADB_ROOT_PASSWORD=${DB_ROOT_PASSWORD}"
  300. -e "MARIADB_DATABASE=${DB_NAME}"
  301. -e "MARIADB_USER=${DB_USER}"
  302. -e "MARIADB_PASSWORD=${DB_PASS}"
  303. -p "${DB_PORT}:3306"
  304. "${BEEPZONE_DB_IMAGE}"
  305. )
  306. pretty_cmd="podman run -d \\
  307. --name ${BEEPZONE_DB_CONTAINER_NAME} \\
  308. -e MARIADB_ROOT_PASSWORD=${DB_ROOT_PASSWORD} \\
  309. -e MARIADB_DATABASE=${DB_NAME} \\
  310. -e MARIADB_USER=${DB_USER} \\
  311. -e MARIADB_PASSWORD=${DB_PASS} \\
  312. -p ${DB_PORT}:3306 ${BEEPZONE_DB_IMAGE}"
  313. if $DIALOG --yesno "About to run:\n\n${pretty_cmd}\n\nProceed?" 17 76; then
  314. if "${run_cmd[@]}" >>"$LOG_FILE" 2>&1; then
  315. $DIALOG --msgbox "MariaDB container '${BEEPZONE_DB_CONTAINER_NAME}' started successfully." 8 70
  316. else
  317. error_log=$(tail -20 "$LOG_FILE")
  318. $DIALOG --title "Error" --msgbox "Failed to start MariaDB container.\n\nError:\n${error_log}" 20 76
  319. fi
  320. fi
  321. }
  322. configure_and_run_native_db() {
  323. "$DIALOG" --msgbox "You'll need this for the Native MariaDB Setup:\n\n1. Debian 12/13\n2. Sudo privileges\n3. The following packages installed:\n - mariadb-server\n - mariadb-client\n - sudo\n\nIf you are unsure or know you're missing one hit CTRL-C now" 14 60
  324. # Ask if root password is set
  325. if ! "$DIALOG" --yesno "Have you already set up a root password for MariaDB?" 10 60; then
  326. # User said No (exit code 1)
  327. clear
  328. echo "Running database secure installation..."
  329. if command -v mariadb-secure-installation >/dev/null 2>&1; then
  330. sudo mariadb-secure-installation || true
  331. elif command -v mysql_secure_installation >/dev/null 2>&1; then
  332. sudo mysql_secure_installation || true
  333. else
  334. "$DIALOG" --msgbox "Neither 'mariadb-secure-installation' nor 'mysql_secure_installation' was found in PATH.\n\nPlease install MariaDB/MySQL server packages and rerun this step." 12 70
  335. fi
  336. fi
  337. $DIALOG --form "MariaDB configuration" 16 70 6 \
  338. "DB Host" 1 1 "$DB_HOST" 1 18 30 30 \
  339. "DB Port" 2 1 "$DB_PORT" 2 18 30 30 \
  340. "DB Name" 3 1 "$DB_NAME" 3 18 30 30 \
  341. "DB User" 4 1 "$DB_USER" 4 18 30 30 \
  342. "DB Password" 5 1 "$DB_PASS" 5 18 30 30 \
  343. "Root Password" 6 1 "$DB_ROOT_PASSWORD" 6 18 30 30 2>"$TMP_FILE" || return
  344. # Parse dialog output (6 lines)
  345. {
  346. read -r DB_HOST
  347. read -r DB_PORT
  348. read -r DB_NAME
  349. read -r DB_USER
  350. read -r DB_PASS
  351. read -r DB_ROOT_PASSWORD
  352. } < "$TMP_FILE"
  353. save_env
  354. # Check if mariadb is even running
  355. if ! systemctl is-active --quiet mariadb; then
  356. if $DIALOG --yesno "MariaDB service aint running. Try and start it?" 10 60; then
  357. sudo systemctl start mariadb || {
  358. $DIALOG --msgbox "Crap, Failed to start MariaDB service" 8 60
  359. return
  360. }
  361. else
  362. return
  363. fi
  364. fi
  365. local create_sql="
  366. CREATE DATABASE IF NOT EXISTS \`${DB_NAME}\`;
  367. CREATE USER IF NOT EXISTS '${DB_USER}'@'%' IDENTIFIED BY '${DB_PASS}';
  368. GRANT ALL PRIVILEGES ON \`${DB_NAME}\`.* TO '${DB_USER}'@'%';
  369. FLUSH PRIVILEGES;
  370. "
  371. if "$DIALOG" --yesno "Script will now do the following:\n\nCreate DB: $DB_NAME\nCreate User: $DB_USER\n\nThis alright?" 12 70; then
  372. if echo "$create_sql" | mysql -u root -p"$DB_ROOT_PASSWORD" 2>>"$LOG_FILE"; then
  373. "$DIALOG" --msgbox "Database '$DB_NAME' and user '$DB_USER' created successfully." 8 70
  374. else
  375. "$DIALOG" --msgbox "Fuck something went wrong Check $LOG_FILE for details." 8 70
  376. fi
  377. fi
  378. }
  379. import_schema_and_seed() {
  380. local target
  381. target=$(choose_db_target "Where should the import be applied?") || return
  382. local import_type schema_file full_dump_file
  383. schema_file="$WORK_DIR/backend/database/schema/beepzone-schema-dump.sql"
  384. full_dump_file="$WORK_DIR/backend/database/dev/beepzone-full-dump.sql"
  385. # Ask what type of import
  386. $DIALOG --clear \
  387. --title "Database Import" \
  388. --menu "Select import type" 12 70 2 \
  389. 1 "Full dump (schema + data in one file, not recommended unless you know what you're doing)" \
  390. 2 "Clean schema only (no data, recommended but make sure to read docs on how to get started)" 2>"$CHOICE_FILE" || return
  391. import_type=$(<"$CHOICE_FILE")
  392. if [[ "$import_type" == "1" ]]; then
  393. # Full dump import
  394. if [[ ! -f "$full_dump_file" ]]; then
  395. $DIALOG --msgbox "full dump file not found at:\n$full_dump_file" 10 70
  396. return
  397. fi
  398. $DIALOG --yesno "import full database dump from:\n$full_dump_file\n\nThis will DROP and recreate the database.\n\nYou Sure?" 12 70 || return
  399. if [[ "$target" == "podman" ]]; then
  400. {
  401. echo "DROP DATABASE IF EXISTS \`$DB_NAME\`;"
  402. echo "CREATE DATABASE \`$DB_NAME\` CHARACTER SET utf8mb4 COLLATE utf8mb4_uca1400_ai_ci;"
  403. echo "USE \`$DB_NAME\`;"
  404. echo "SET FOREIGN_KEY_CHECKS=0;"
  405. cat "$full_dump_file"
  406. echo "SET FOREIGN_KEY_CHECKS=1;"
  407. } | podman exec -i "$BEEPZONE_DB_CONTAINER_NAME" \
  408. mariadb -h"$DB_HOST" -P"$DB_PORT" -uroot -p"$DB_ROOT_PASSWORD" >>"$LOG_FILE" 2>&1 || {
  409. error_log=$(tail -30 "$LOG_FILE")
  410. $DIALOG --title "Fuck" --msgbox "full dump import failed.\n\nError:\n${error_log}" 22 76
  411. return
  412. }
  413. else
  414. {
  415. echo "DROP DATABASE IF EXISTS \`$DB_NAME\`;"
  416. echo "CREATE DATABASE \`$DB_NAME\` CHARACTER SET utf8mb4 COLLATE utf8mb4_uca1400_ai_ci;"
  417. echo "USE \`$DB_NAME\`;"
  418. echo "SET FOREIGN_KEY_CHECKS=0;"
  419. cat "$full_dump_file"
  420. echo "SET FOREIGN_KEY_CHECKS=1;"
  421. } | "$MYSQL_CLIENT" -h"$DB_HOST" -P"$DB_PORT" -uroot -p"$DB_ROOT_PASSWORD" >>"$LOG_FILE" 2>&1 || {
  422. error_log=$(tail -30 "$LOG_FILE")
  423. $DIALOG --title "Fuck" --msgbox "full dump import failed.\n\nError:\n${error_log}" 22 76
  424. return
  425. }
  426. fi
  427. DEPLOYMENT_TYPE="dev"
  428. save_env
  429. $DIALOG --msgbox "full database dump imported successfully!" 8 70
  430. else
  431. # Clean schema import
  432. if [[ ! -f "$schema_file" ]]; then
  433. $DIALOG --msgbox "schema file not found at:\n$schema_file" 10 60
  434. return
  435. fi
  436. $DIALOG --yesno "import clean schema from:\n$schema_file\n\nThis will DROP and recreate the database.\n\nProceed?" 12 70 || return
  437. if [[ "$target" == "podman" ]]; then
  438. {
  439. echo "DROP DATABASE IF EXISTS \`$DB_NAME\`;"
  440. echo "CREATE DATABASE \`$DB_NAME\` CHARACTER SET utf8mb4 COLLATE utf8mb4_uca1400_ai_ci;"
  441. echo "USE \`$DB_NAME\`;"
  442. echo "SET FOREIGN_KEY_CHECKS=0;"
  443. cat "$schema_file"
  444. echo "SET FOREIGN_KEY_CHECKS=1;"
  445. } | podman exec -i "$BEEPZONE_DB_CONTAINER_NAME" \
  446. mariadb -h"$DB_HOST" -P"$DB_PORT" -uroot -p"$DB_ROOT_PASSWORD" >>"$LOG_FILE" 2>&1 || {
  447. error_log=$(tail -30 "$LOG_FILE")
  448. $DIALOG --title "Error" --msgbox "schema import failed.\n\nError:\n${error_log}" 22 76
  449. return
  450. }
  451. else
  452. {
  453. echo "DROP DATABASE IF EXISTS \`$DB_NAME\`;"
  454. echo "CREATE DATABASE \`$DB_NAME\` CHARACTER SET utf8mb4 COLLATE utf8mb4_uca1400_ai_ci;"
  455. echo "USE \`$DB_NAME\`;"
  456. echo "SET FOREIGN_KEY_CHECKS=0;"
  457. cat "$schema_file"
  458. echo "SET FOREIGN_KEY_CHECKS=1;"
  459. } | "$MYSQL_CLIENT" -h"$DB_HOST" -P"$DB_PORT" -uroot -p"$DB_ROOT_PASSWORD" >>"$LOG_FILE" 2>&1 || {
  460. error_log=$(tail -30 "$LOG_FILE")
  461. $DIALOG --title "Error" --msgbox "schema import failed.\n\nError:\n${error_log}" 22 76
  462. return
  463. }
  464. fi
  465. DEPLOYMENT_TYPE="clean"
  466. save_env
  467. $DIALOG --msgbox "schema imported successfully!\n\nI recommend you make an Admin role with power 100 and and Admin user as the next step." 8 70
  468. fi
  469. }
  470. manage_users_and_roles() {
  471. DB_TARGET=$(choose_db_target "Apply user/role management to which database?") || return
  472. while true; do
  473. $DIALOG --clear \
  474. --title "Manage Users & Roles" \
  475. --menu "Choose an action:" 17 60 6 \
  476. 1 "Create a role" \
  477. 2 "Create a user" \
  478. 3 "Delete a role" \
  479. 4 "Delete a user" \
  480. 5 "Back to main menu" 2>"$CHOICE_FILE"
  481. [[ ! -s "$CHOICE_FILE" ]] && return 0
  482. choice=$(<"$CHOICE_FILE")
  483. case $choice in
  484. 1) create_role ;;
  485. 2) create_user ;;
  486. 3) delete_role ;;
  487. 4) delete_user ;;
  488. 5) return 0 ;;
  489. esac
  490. done
  491. }
  492. create_role() {
  493. $DIALOG --title "Create Role" \
  494. --form "Enter role details:" 12 60 2 \
  495. "Role name:" 1 1 "" 1 20 30 0 \
  496. "Power (1-100):" 2 1 "" 2 20 10 0 \
  497. 2>"$TMP_FILE"
  498. [[ ! -s "$TMP_FILE" ]] && return 1
  499. local name power
  500. {
  501. read -r name
  502. read -r power
  503. } < "$TMP_FILE"
  504. [[ -z "$name" || -z "$power" ]] && {
  505. $DIALOG --msgbox "Role name and power are required." 8 50
  506. return 1
  507. }
  508. # Validate power is a number between 1-100
  509. if ! [[ "$power" =~ ^[0-9]+$ ]] || [[ "$power" -lt 1 || "$power" -gt 100 ]]; then
  510. $DIALOG --msgbox "Power must be a number between 1 and 100." 8 50
  511. return 1
  512. fi
  513. local sql="INSERT INTO roles (name, power) VALUES ('$name', $power);"
  514. db_exec_sql "$sql" "$DB_TARGET" >>"$LOG_FILE" 2>&1 || {
  515. error_log=$(tail -20 "$LOG_FILE")
  516. $DIALOG --title "Error" --msgbox "Failed to create role.\n\nError:\n${error_log}" 20 76
  517. return 1
  518. }
  519. $DIALOG --msgbox "Role '$name' created successfully!" 8 50
  520. }
  521. delete_role() {
  522. # Fetch roles from the database
  523. local roles_list
  524. roles_list=$(db_query "SELECT id, name, power FROM roles ORDER BY power DESC;" "$DB_TARGET" 2>>"$LOG_FILE") || {
  525. $DIALOG --msgbox "Failed to fetch roles from database." 10 60
  526. return
  527. }
  528. # Build role selection menu
  529. local role_options=()
  530. while IFS=$'\t' read -r role_id role_name role_power; do
  531. role_options+=("$role_id" "$role_name (power: $role_power)")
  532. done <<< "$roles_list"
  533. if [[ ${#role_options[@]} -eq 0 ]]; then
  534. $DIALOG --msgbox "No roles found in database." 10 60
  535. return
  536. fi
  537. # Select role to delete
  538. $DIALOG --menu "Select role to delete:" 20 70 10 "${role_options[@]}" 2>"$TMP_FILE" || return
  539. local selected_role_id
  540. selected_role_id=$(<"$TMP_FILE")
  541. # Get role name for confirmation
  542. local selected_role_name
  543. selected_role_name=$(echo "$roles_list" | awk -v id="$selected_role_id" -F'\t' '$1 == id {print $2}')
  544. # Check if any users are using this role
  545. local user_count
  546. user_count=$(db_query "SELECT COUNT(*) FROM users WHERE role_id = $selected_role_id;" "$DB_TARGET" 2>>"$LOG_FILE")
  547. if [[ "$user_count" -gt 0 ]]; then
  548. $DIALOG --msgbox "Cannot delete role '$selected_role_name'.\n$user_count user(s) are currently assigned this role." 10 60
  549. return
  550. fi
  551. # Confirm deletion
  552. $DIALOG --yesno "Are you sure you want to delete role '$selected_role_name' (ID: $selected_role_id)?\n\nThis action cannot be undone." 10 60 || return
  553. # Delete role
  554. local sql="DELETE FROM roles WHERE id = $selected_role_id;"
  555. db_exec_sql "$sql" "$DB_TARGET" >>"$LOG_FILE" 2>&1 || {
  556. error_log=$(tail -20 "$LOG_FILE")
  557. $DIALOG --title "Error" --msgbox "Failed to delete role.\n\nError:\n${error_log}" 20 76
  558. return
  559. }
  560. $DIALOG --msgbox "Role '$selected_role_name' deleted successfully!" 8 60
  561. }
  562. create_user() {
  563. # Get available roles
  564. local roles_list
  565. roles_list=$(db_query "SELECT id, name, power FROM roles ORDER BY power DESC;" "$DB_TARGET" 2>>"$LOG_FILE") || {
  566. $DIALOG --msgbox "Failed to fetch roles from database.\nEnsure schema is imported and roles exist." 10 60
  567. return
  568. }
  569. # Build role selection menu
  570. local role_options=()
  571. while IFS=$'\t' read -r role_id role_name role_power; do
  572. role_options+=("$role_id" "$role_name (power: $role_power)")
  573. done <<< "$roles_list"
  574. if [[ ${#role_options[@]} -eq 0 ]]; then
  575. $DIALOG --msgbox "No roles found in database.\nPlease create a role first." 10 60
  576. return
  577. fi
  578. # Select role
  579. $DIALOG --menu "Select user role" 15 60 5 "${role_options[@]}" 2>"$TMP_FILE" || return
  580. local selected_role_id
  581. selected_role_id=$(<"$TMP_FILE")
  582. # Get user details
  583. $DIALOG --form "Create BeepZone user" 14 70 5 \
  584. "Name (full name)" 1 1 "" 1 20 40 40 \
  585. "Username" 2 1 "" 2 20 40 40 \
  586. "Password" 3 1 "" 3 20 40 40 \
  587. "Email" 4 1 "" 4 20 40 40 \
  588. "Phone" 5 1 "" 5 20 40 40 2>"$TMP_FILE" || return
  589. local name username password email phone
  590. {
  591. read -r name
  592. read -r username
  593. read -r password
  594. read -r email
  595. read -r phone
  596. } < "$TMP_FILE"
  597. if [[ -z "$name" || -z "$username" || -z "$password" ]]; then
  598. $DIALOG --msgbox "Name, username, and password are required." 8 60
  599. return
  600. fi
  601. # Hash password with bcrypt (cost 12) and avoid set -e aborts
  602. local password_hash status
  603. status=1
  604. # Try htpasswd first (native Unix tool)
  605. if command -v htpasswd >/dev/null 2>&1; then
  606. set +e
  607. password_hash=$(htpasswd -nbB -C 12 "$username" "$password" 2>>"$LOG_FILE")
  608. status=$?
  609. set -e
  610. password_hash=$(echo "$password_hash" | cut -d: -f2)
  611. # Fall back to Python bcrypt
  612. elif command -v python3 >/dev/null 2>&1; then
  613. set +e
  614. password_hash=$(python3 - "$password" "$username" 2>>"$LOG_FILE" <<'PY'
  615. import sys, bcrypt
  616. pw = sys.argv[1]
  617. print(bcrypt.hashpw(pw.encode('utf-8'), bcrypt.gensalt(12)).decode('utf-8'))
  618. PY
  619. )
  620. status=$?
  621. set -e
  622. fi
  623. if [[ $status -ne 0 || -z "$password_hash" ]]; then
  624. $DIALOG --msgbox "Error: Failed to hash password.\n\nInstall one of these:\n- htpasswd: brew install httpd (macOS) or apt install apache2-utils (Linux)\n- Python bcrypt: pip3 install bcrypt" 12 70
  625. return
  626. fi
  627. # Insert user with hashed password
  628. local sql
  629. sql="INSERT INTO users (name, username, password, role_id, email, phone, active) VALUES
  630. ('$name', '$username', '$password_hash', $selected_role_id, "
  631. [[ -n "$email" ]] && sql+="'$email'" || sql+="NULL"
  632. sql+=", "
  633. [[ -n "$phone" ]] && sql+="'$phone'" || sql+="NULL"
  634. sql+=", 1);"
  635. db_exec_sql "$sql" "$DB_TARGET" >>"$LOG_FILE" 2>&1 || {
  636. error_log=$(tail -20 "$LOG_FILE")
  637. $DIALOG --title "Error" --msgbox "Failed to create user.\n\nError:\n${error_log}" 20 76
  638. return
  639. }
  640. $DIALOG --msgbox "User '$username' created successfully!" 8 60
  641. }
  642. delete_user() {
  643. # Fetch users from the database
  644. local users_list
  645. users_list=$(db_query "SELECT id, username, name FROM users ORDER BY id;" "$DB_TARGET" 2>>"$LOG_FILE") || {
  646. $DIALOG --msgbox "Failed to fetch users from database." 10 60
  647. return
  648. }
  649. # Build user selection menu
  650. local user_options=()
  651. while IFS=$'\t' read -r user_id username name; do
  652. user_options+=("$user_id" "$username - $name")
  653. done <<< "$users_list"
  654. if [[ ${#user_options[@]} -eq 0 ]]; then
  655. $DIALOG --msgbox "No users found in database." 10 60
  656. return
  657. fi
  658. # Select user to delete
  659. $DIALOG --menu "Select user to delete:" 20 70 10 "${user_options[@]}" 2>"$TMP_FILE" || return
  660. local selected_user_id
  661. selected_user_id=$(<"$TMP_FILE")
  662. # Get username for confirmation
  663. local selected_username
  664. selected_username=$(echo "$users_list" | awk -v id="$selected_user_id" -F'\t' '$1 == id {print $2}')
  665. # Confirm deletion
  666. $DIALOG --yesno "Are you sure you want to delete user '$selected_username' (ID: $selected_user_id)?\n\nThis action cannot be undone." 10 60 || return
  667. # Delete user
  668. local sql="DELETE FROM users WHERE id = $selected_user_id;"
  669. db_exec_sql "$sql" "$DB_TARGET" >>"$LOG_FILE" 2>&1 || {
  670. error_log=$(tail -20 "$LOG_FILE")
  671. $DIALOG --title "Error" --msgbox "Failed to delete user.\n\nError:\n${error_log}" 20 76
  672. return
  673. }
  674. $DIALOG --msgbox "User '$selected_username' deleted successfully!" 8 60
  675. }
  676. clone_if_missing() {
  677. local repo_url="$1" dest_dir="$2"
  678. if [[ -d "$dest_dir/.git" ]]; then
  679. return
  680. fi
  681. git clone "$repo_url" "$dest_dir" >>"$LOG_FILE" 2>&1 || {
  682. error_log=$(tail -20 "$LOG_FILE")
  683. $DIALOG --title "Error" --msgbox "Failed to clone $repo_url.\n\nError:\n${error_log}" 20 76
  684. return 1
  685. }
  686. }
  687. setup_seckelapi() {
  688. local sources_dir="$WORK_DIR/backend/seckelapi/sources"
  689. local config_dir="$WORK_DIR/backend/seckelapi/config"
  690. local sources_basics_config="$sources_dir/config/basics.toml"
  691. # Check if sources are already cloned
  692. if [[ ! -d "$sources_dir/.git" ]]; then
  693. mkdir -p "$sources_dir"
  694. clone_if_missing "$SECKELAPI_REPO" "$sources_dir" || return
  695. fi
  696. # Ask about config update
  697. $DIALOG --clear \
  698. --title "SeckelAPI Configuration" \
  699. --menu "How do you want to handle the config?" 12 70 2 \
  700. 1 "Auto-update from database settings" \
  701. 2 "Manually edit config file" 2>"$CHOICE_FILE"
  702. [[ ! -s "$CHOICE_FILE" ]] && return 0
  703. config_choice=$(<"$CHOICE_FILE")
  704. # Ensure sources config directory exists and copy all template configs
  705. mkdir -p "$sources_dir/config"
  706. # Always copy all template configs from config/ to sources/config/
  707. for conf_file in "$config_dir"/*.toml; do
  708. conf_name=$(basename "$conf_file")
  709. cp "$conf_file" "$sources_dir/config/$conf_name" 2>>"$LOG_FILE"
  710. done
  711. if [[ "$config_choice" == "1" ]]; then
  712. # Auto-update basics.toml in sources with database settings (only [database] section)
  713. if [[ -f "$sources_basics_config" ]]; then
  714. # Determine database host - use host.containers.internal for container deployments
  715. local db_host_value="$DB_HOST"
  716. sed -i.bak \
  717. -e '/^\[database\]/,/^\[/ {
  718. /^host = /s|= .*|= "'"$db_host_value"'"|
  719. /^port = /s|= .*|= '"$DB_PORT"'|
  720. /^database = /s|= .*|= "'"$DB_NAME"'"|
  721. /^username = /s|= .*|= "'"$DB_USER"'"|
  722. /^password = /s|= .*|= "'"$DB_PASS"'"|
  723. }' \
  724. "$sources_basics_config"
  725. $DIALOG --msgbox "Config updated with database settings from .env" 8 60
  726. else
  727. $DIALOG --msgbox "Error: basics.toml not found at $sources_basics_config" 8 60
  728. return 1
  729. fi
  730. else
  731. # Open config file for manual editing (in sources/config/)
  732. if [[ -f "$sources_basics_config" ]]; then
  733. ${EDITOR:-nano} "$sources_basics_config"
  734. else
  735. $DIALOG --msgbox "Error: basics.toml not found at $sources_basics_config" 8 60
  736. return 1
  737. fi
  738. fi
  739. # Ask about build and deployment
  740. $DIALOG --clear \
  741. --title "Build & Deploy SeckelAPI" \
  742. --menu "Choose an action:" 12 70 2 \
  743. 1 "Build and deploy to podman container" \
  744. $( $HAS_CARGO && echo "2 \"Build natively on host\"" ) 2>"$CHOICE_FILE"
  745. [[ ! -s "$CHOICE_FILE" ]] && return 0
  746. build_choice=$(<"$CHOICE_FILE")
  747. if [[ "$build_choice" == "2" ]]; then
  748. # Native host build with live output
  749. clear
  750. echo "Building SeckelAPI..."
  751. echo "===================="
  752. echo ""
  753. if (cd "$sources_dir" && cargo build --release); then
  754. # Copy config files to the build output directory
  755. local target_dir="$sources_dir/target/release"
  756. mkdir -p "$target_dir/config"
  757. cp "$sources_dir/config"/*.toml "$target_dir/config/" 2>>"$LOG_FILE"
  758. echo ""
  759. echo "Build completed successfully!"
  760. echo "Binary location: $target_dir/seckelapi"
  761. echo "Config location: $target_dir/config/"
  762. read -p "Press Enter to continue..."
  763. else
  764. echo ""
  765. echo "Build failed! Check the output above for errors."
  766. read -p "Press Enter to continue..."
  767. return 1
  768. fi
  769. elif [[ "$build_choice" == "1" ]]; then
  770. # Build and deploy to podman container
  771. clear
  772. echo "Building SeckelAPI container..."
  773. echo "================================"
  774. echo ""
  775. # Get the host gateway IP for container to access host services
  776. # For Podman, we'll use the gateway IP from the default bridge network
  777. local host_gateway="host.containers.internal"
  778. # Update database host for container networking
  779. if [[ -f "$sources_basics_config" ]]; then
  780. echo "Updating database host to: $host_gateway"
  781. sed -i.container-bak \
  782. -e '/^\[database\]/,/^\[/ {
  783. /^host = /s|= .*|= "'"$host_gateway"'"|
  784. }' \
  785. "$sources_basics_config"
  786. fi
  787. local container_name="beepzone-seckelapi"
  788. local image_name="beepzone-seckelapi:latest"
  789. local containerfile="$WORK_DIR/backend/seckelapi/Containerfile"
  790. # Stop and remove existing container if running
  791. if podman ps -a --format "{{.Names}}" | grep -q "^${container_name}$"; then
  792. echo "Stopping and removing existing container..."
  793. podman stop "$container_name" 2>/dev/null || true
  794. podman rm "$container_name" 2>/dev/null || true
  795. fi
  796. # Build container image
  797. echo "Building container image..."
  798. if podman build -t "$image_name" -f "$containerfile" "$WORK_DIR/backend/seckelapi"; then
  799. echo ""
  800. echo "Container image built successfully!"
  801. echo ""
  802. # Ask to run the container
  803. if $DIALOG --yesno "Start the SeckelAPI container now?" 8 50; then
  804. echo "Starting container..."
  805. # Run container with port mapping and host gateway
  806. if podman run -d \
  807. --name "$container_name" \
  808. --add-host host.containers.internal:host-gateway \
  809. -p 5777:5777 \
  810. "$image_name"; then
  811. echo ""
  812. echo "Container started successfully!"
  813. echo "Container name: $container_name"
  814. echo "API listening on: http://0.0.0.0:5777"
  815. echo ""
  816. echo "Useful commands:"
  817. echo " podman logs $container_name - View logs"
  818. echo " podman stop $container_name - Stop container"
  819. echo " podman start $container_name - Start container"
  820. echo " podman restart $container_name - Restart container"
  821. read -p "Press Enter to continue..."
  822. else
  823. echo ""
  824. echo "Failed to start container!"
  825. read -p "Press Enter to continue..."
  826. return 1
  827. fi
  828. fi
  829. else
  830. echo ""
  831. echo "Container build failed! Check the output above for errors."
  832. read -p "Press Enter to continue..."
  833. return 1
  834. fi
  835. fi
  836. }
  837. setup_seckelapi_native() {
  838. if ! $HAS_CARGO; then
  839. "$DIALOG" --msgbox "Cargo not found in PATH.\n\nInstall Rust toolchain (rustup) to use native SeckelAPI setup." 10 70
  840. return
  841. fi
  842. # 1. Check Pre-requisites
  843. $DIALOG --msgbox "Pre-requisites for Native SeckelAPI Setup:\n\n1. Rust toolchain (rustup, cargo)\n2. build-essential (gcc, etc.)\n3. pkg-config\n4. libssl-dev\n5. Sudo privileges (for systemd service)\n\nEnsure these are met before proceeding." 14 60
  844. local sources_dir="$WORK_DIR/backend/seckelapi/sources"
  845. local config_dir="$WORK_DIR/backend/seckelapi/config"
  846. local sources_basics_config="$sources_dir/config/basics.toml"
  847. # 2. Clone sources if missing
  848. if [[ ! -d "$sources_dir/.git" ]]; then
  849. mkdir -p "$sources_dir"
  850. clone_if_missing "$SECKELAPI_REPO" "$sources_dir" || return
  851. fi
  852. # 3. Configure basics.toml
  853. # Ensure sources config directory exists and copy all template configs
  854. mkdir -p "$sources_dir/config"
  855. for conf_file in "$config_dir"/*.toml; do
  856. conf_name=$(basename "$conf_file")
  857. cp "$conf_file" "$sources_dir/config/$conf_name" 2>>"$LOG_FILE"
  858. done
  859. if [[ -f "$sources_basics_config" ]]; then
  860. # Update with DB settings from .env (localhost since it's native)
  861. sed -i.bak \
  862. -e '/^\[database\]/,/^\[/ {
  863. /^host = /s|= .*|= "'"$DB_HOST"'"|
  864. /^port = /s|= .*|= '"$DB_PORT"'|
  865. /^database = /s|= .*|= "'"$DB_NAME"'"|
  866. /^username = /s|= .*|= "'"$DB_USER"'"|
  867. /^password = /s|= .*|= "'"$DB_PASS"'"|
  868. }' \
  869. "$sources_basics_config"
  870. else
  871. $DIALOG --msgbox "Error: basics.toml not found at $sources_basics_config" 8 60
  872. return 1
  873. fi
  874. # 4. Build Release
  875. clear
  876. echo "Building SeckelAPI (Native)..."
  877. echo "============================"
  878. echo ""
  879. if (cd "$sources_dir" && cargo build --release); then
  880. # Copy config files to the build output directory
  881. local target_dir="$sources_dir/target/release"
  882. mkdir -p "$target_dir/config"
  883. cp "$sources_dir/config"/*.toml "$target_dir/config/" 2>>"$LOG_FILE"
  884. echo ""
  885. echo "Build completed successfully!"
  886. else
  887. echo ""
  888. echo "Build failed! Check the output above for errors."
  889. read -p "Press Enter to continue..."
  890. return 1
  891. fi
  892. # 5. Ask for installation directory
  893. local install_dir default_install_dir
  894. default_install_dir="$HOME/seckelapi"
  895. $DIALOG --inputbox "Where should SeckelAPI be installed?\n\n(Binary and config will be copied here)" 12 70 "$default_install_dir" 2>"$TMP_FILE" || return
  896. install_dir=$(<"$TMP_FILE")
  897. if [[ -z "$install_dir" ]]; then
  898. install_dir="$default_install_dir"
  899. fi
  900. # Create installation directory
  901. mkdir -p "$install_dir/config" || {
  902. $DIALOG --msgbox "Failed to create installation directory:\n$install_dir" 10 70
  903. return 1
  904. }
  905. # Copy binary and config files
  906. local target_dir="$sources_dir/target/release"
  907. cp "$target_dir/seckelapi" "$install_dir/" 2>>"$LOG_FILE" || {
  908. $DIALOG --msgbox "Failed to copy binary to $install_dir" 10 70
  909. return 1
  910. }
  911. cp "$sources_dir/config"/*.toml "$install_dir/config/" 2>>"$LOG_FILE" || {
  912. $DIALOG --msgbox "Failed to copy config files to $install_dir/config" 10 70
  913. return 1
  914. }
  915. # Make binary executable
  916. chmod +x "$install_dir/seckelapi"
  917. $DIALOG --msgbox "SeckelAPI installed to:\n$install_dir\n\nBinary: $install_dir/seckelapi\nConfig: $install_dir/config/" 12 70
  918. # 6. Install Systemd Service
  919. if $DIALOG --yesno "Do you want to install SeckelAPI as a systemd service?" 8 60; then
  920. local service_name="beepzone-seckelapi"
  921. local service_file="/tmp/${service_name}.service"
  922. local current_user
  923. current_user=$(whoami)
  924. # Working directory for the binary
  925. local working_dir="$install_dir"
  926. local exec_path="$install_dir/seckelapi"
  927. cat > "$service_file" <<EOF
  928. [Unit]
  929. Description=BeepZone SeckelAPI Service
  930. After=network.target mariadb.service
  931. Requires=mariadb.service
  932. [Service]
  933. Type=simple
  934. User=${current_user}
  935. WorkingDirectory=${working_dir}
  936. ExecStart=${exec_path}
  937. Restart=always
  938. RestartSec=10
  939. Environment=RUST_LOG=info
  940. [Install]
  941. WantedBy=multi-user.target
  942. EOF
  943. echo "Installing systemd service..."
  944. sudo mv "$service_file" "/etc/systemd/system/${service_name}.service"
  945. sudo systemctl daemon-reload
  946. sudo systemctl enable "${service_name}"
  947. if sudo systemctl start "${service_name}"; then
  948. $DIALOG --msgbox "Service '${service_name}' installed and started successfully!\n\nInstallation: $install_dir\nService: $service_name" 10 70
  949. else
  950. $DIALOG --msgbox "Failed to start service '${service_name}'. Check 'sudo systemctl status ${service_name}'" 10 70
  951. fi
  952. fi
  953. }
  954. update_from_git_masters() {
  955. local seckelapi_sources="$WORK_DIR/backend/seckelapi/sources"
  956. local client_sources="$WORK_DIR/frontend/desktop-client/sources"
  957. if $DIALOG --yesno "This will run 'git pull' in both backend and frontend source directories.\n\nContinue?" 10 60; then
  958. clear
  959. echo "Updating sources from git masters..."
  960. echo "===================================="
  961. echo ""
  962. # Update SeckelAPI
  963. if [[ -d "$seckelapi_sources/.git" ]]; then
  964. echo "Updating SeckelAPI..."
  965. if (cd "$seckelapi_sources" && git pull); then
  966. echo "SeckelAPI updated successfully."
  967. else
  968. echo "Failed to update SeckelAPI."
  969. fi
  970. else
  971. echo "SeckelAPI sources not found or not a git repository. Skipping."
  972. fi
  973. echo ""
  974. # Update Desktop Client
  975. if [[ -d "$client_sources/.git" ]]; then
  976. echo "Updating Desktop Client..."
  977. if (cd "$client_sources" && git pull); then
  978. echo "Desktop Client updated successfully."
  979. else
  980. echo "Failed to update Desktop Client."
  981. fi
  982. else
  983. echo "Desktop Client sources not found or not a git repository. Skipping."
  984. fi
  985. echo ""
  986. read -p "Press Enter to continue..."
  987. fi
  988. }
  989. clean_sources() {
  990. $DIALOG --yesno "This will delete all sources directories including hidden files:\n\n- backend/seckelapi/sources\n- frontend/desktop-client/sources\n\nAre you sure?" 12 70 || return
  991. local seckelapi_sources="$WORK_DIR/backend/seckelapi/sources"
  992. local client_sources="$WORK_DIR/frontend/desktop-client/sources"
  993. # Clean SeckelAPI sources
  994. if [[ -d "$seckelapi_sources" ]]; then
  995. find "$seckelapi_sources" -mindepth 1 -delete 2>>"$LOG_FILE"
  996. if [[ $? -eq 0 ]]; then
  997. echo "SeckelAPI sources contents removed" >>"$LOG_FILE"
  998. fi
  999. fi
  1000. # Clean desktop client sources
  1001. if [[ -d "$client_sources" ]]; then
  1002. find "$client_sources" -mindepth 1 -delete 2>>"$LOG_FILE"
  1003. if [[ $? -eq 0 ]]; then
  1004. echo "Desktop client sources contents removed" >>"$LOG_FILE"
  1005. fi
  1006. fi
  1007. $DIALOG --msgbox "All sources cleaned!" 7 50
  1008. # Ask to re-clone
  1009. if $DIALOG --yesno "Do you want to pull fresh sources now?" 7 50; then
  1010. # Clone SeckelAPI
  1011. mkdir -p "$(dirname "$seckelapi_sources")"
  1012. if clone_if_missing "$SECKELAPI_REPO" "$seckelapi_sources"; then
  1013. $DIALOG --msgbox "SeckelAPI sources cloned successfully!" 7 50
  1014. else
  1015. $DIALOG --msgbox "Failed to clone SeckelAPI sources. Check log." 7 50
  1016. return 1
  1017. fi
  1018. # Clone desktop client
  1019. mkdir -p "$(dirname "$client_sources")"
  1020. if clone_if_missing "$CLIENT_REPO" "$client_sources"; then
  1021. $DIALOG --msgbox "Desktop client sources cloned successfully!" 7 50
  1022. else
  1023. $DIALOG --msgbox "Failed to clone desktop client sources. Check log." 7 50
  1024. return 1
  1025. fi
  1026. $DIALOG --msgbox "All sources pulled fresh!" 7 50
  1027. fi
  1028. }
  1029. build_desktop_client() {
  1030. local sources_dir="$WORK_DIR/frontend/desktop-client/sources"
  1031. if ! $HAS_CARGO; then
  1032. "$DIALOG" --msgbox "Cargo not found in PATH.\n\nInstall Rust toolchain (rustup) to build the desktop client." 10 70
  1033. return
  1034. fi
  1035. # Check if sources are already cloned
  1036. if [[ ! -d "$sources_dir/.git" ]]; then
  1037. mkdir -p "$sources_dir"
  1038. clone_if_missing "$CLIENT_REPO" "$sources_dir" || return
  1039. fi
  1040. if $DIALOG --yesno "Build BeepZone desktop client in release mode now?" 8 70; then
  1041. echo "Building BeepZone Desktop Client..."
  1042. echo "===================================="
  1043. echo ""
  1044. if (cd "$sources_dir" && cargo build --release); then
  1045. echo ""
  1046. echo "Build completed successfully!"
  1047. echo "Binary location: $sources_dir/target/release/"
  1048. read -p "Press Enter to continue..."
  1049. else
  1050. echo ""
  1051. echo "Build failed! Check the output above for errors."
  1052. read -p "Press Enter to continue..."
  1053. return 1
  1054. fi
  1055. fi
  1056. }
  1057. show_capabilities_screen
  1058. ask_main_menu