beepzone-helper.sh 40 KB

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