beepzone-helper.sh 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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-get install dialog" >&2
  12. exit 1
  13. fi
  14. if ! command -v podman >/dev/null 2>&1; then
  15. "$DIALOG" --title "BeepZone Setup" --msgbox "podman is required but not found in PATH.\nPlease install and configure podman (podman-desktop recommended)." 10 60
  16. exit 1
  17. fi
  18. # Check for MariaDB/MySQL client - try common brew locations on macOS
  19. MYSQL_CLIENT=""
  20. if command -v mariadb >/dev/null 2>&1; then
  21. MYSQL_CLIENT="mariadb"
  22. elif command -v mysql >/dev/null 2>&1; then
  23. MYSQL_CLIENT="mysql"
  24. elif [[ -x /opt/homebrew/opt/mysql-client/bin/mysql ]]; then
  25. MYSQL_CLIENT="/opt/homebrew/opt/mysql-client/bin/mysql"
  26. export PATH="/opt/homebrew/opt/mysql-client/bin:$PATH"
  27. elif [[ -x /opt/homebrew/opt/mariadb/bin/mariadb ]]; then
  28. MYSQL_CLIENT="/opt/homebrew/opt/mariadb/bin/mariadb"
  29. export PATH="/opt/homebrew/opt/mariadb/bin:$PATH"
  30. elif [[ -x /opt/homebrew/bin/mariadb ]]; then
  31. MYSQL_CLIENT="/opt/homebrew/bin/mariadb"
  32. export PATH="/opt/homebrew/bin:$PATH"
  33. elif [[ -x /opt/homebrew/bin/mysql ]]; then
  34. MYSQL_CLIENT="/opt/homebrew/bin/mysql"
  35. export PATH="/opt/homebrew/bin:$PATH"
  36. elif [[ -x /usr/local/opt/mysql-client/bin/mysql ]]; then
  37. MYSQL_CLIENT="/usr/local/opt/mysql-client/bin/mysql"
  38. export PATH="/usr/local/opt/mysql-client/bin:$PATH"
  39. elif [[ -x /usr/local/bin/mariadb ]]; then
  40. MYSQL_CLIENT="/usr/local/bin/mariadb"
  41. export PATH="/usr/local/bin:$PATH"
  42. elif [[ -x /usr/local/bin/mysql ]]; then
  43. MYSQL_CLIENT="/usr/local/bin/mysql"
  44. export PATH="/usr/local/bin:$PATH"
  45. else
  46. "$DIALOG" --title "BeepZone Setup" --msgbox "MariaDB/MySQL client is required but not found.\n\nSearched locations:\n- System PATH\n- /opt/homebrew/opt/mysql-client/bin/\n- /opt/homebrew/opt/mariadb/bin/\n- /opt/homebrew/bin/\n- /usr/local/opt/mysql-client/bin/\n- /usr/local/bin/\n\nOn macOS: brew install mysql-client\nOn Debian: sudo apt-get install mariadb-client" 18 70
  47. exit 1
  48. fi
  49. TMP_FILE="$(mktemp)"
  50. CHOICE_FILE="$(mktemp)"
  51. trap 'rm -f "$TMP_FILE" "$CHOICE_FILE"' EXIT
  52. LOG_FILE="/tmp/beepzone-helper.log"
  53. > "$LOG_FILE"
  54. ENV_FILE="$SCRIPT_DIR/.env"
  55. # Load existing settings from .env if present
  56. if [[ -f "$ENV_FILE" ]]; then
  57. source "$ENV_FILE"
  58. fi
  59. # Set defaults if not loaded from .env
  60. : "${BEEPZONE_DB_CONTAINER_NAME:=beepzone-mariadb}"
  61. : "${BEEPZONE_DB_IMAGE:=mariadb:12}"
  62. : "${DB_HOST:=127.0.0.1}"
  63. : "${DB_PORT:=3306}"
  64. : "${DB_NAME:=beepzone}"
  65. : "${DB_USER:=beepzone}"
  66. : "${DB_PASS:=beepzone}"
  67. : "${DB_ROOT_PASSWORD:=root}"
  68. : "${SECKELAPI_REPO:=https://git.teleco.ch/crt/seckelapi.git}"
  69. : "${CLIENT_REPO:=https://git.teleco.ch/crt/beepzone-client-egui-emo.git}"
  70. : "${DEPLOYMENT_TYPE:=clean}"
  71. save_env() {
  72. cat > "$ENV_FILE" << EOF
  73. # BeepZone Setup Configuration
  74. # Auto-generated by beepzone-helper.sh
  75. BEEPZONE_DB_CONTAINER_NAME="$BEEPZONE_DB_CONTAINER_NAME"
  76. BEEPZONE_DB_IMAGE="$BEEPZONE_DB_IMAGE"
  77. DB_HOST="$DB_HOST"
  78. DB_PORT="$DB_PORT"
  79. DB_NAME="$DB_NAME"
  80. DB_USER="$DB_USER"
  81. DB_PASS="$DB_PASS"
  82. DB_ROOT_PASSWORD="$DB_ROOT_PASSWORD"
  83. SECKELAPI_REPO="$SECKELAPI_REPO"
  84. CLIENT_REPO="$CLIENT_REPO"
  85. DEPLOYMENT_TYPE="$DEPLOYMENT_TYPE"
  86. EOF
  87. }
  88. ask_main_menu() {
  89. while true; do
  90. $DIALOG --clear \
  91. --title "BeepZone Setup" \
  92. --menu "Choose an action" 16 76 6 \
  93. 1 "Configure & run MariaDB (podman)" \
  94. 2 "Import DB schema & data" \
  95. 3 "Manage users & roles" \
  96. 4 "Configure & setup SeckelAPI" \
  97. 5 "Build desktop client" \
  98. 6 "Quit" 2>"$CHOICE_FILE"
  99. choice=$(<"$CHOICE_FILE")
  100. case $choice in
  101. 1) configure_and_run_db ;;
  102. 2) import_schema_and_seed ;;
  103. 3) manage_users_and_roles ;;
  104. 4) setup_seckelapi ;;
  105. 5) build_desktop_client ;;
  106. 6) exit 0 ;;
  107. esac
  108. done
  109. }
  110. configure_and_run_db() {
  111. $DIALOG --form "MariaDB container configuration" 18 70 8 \
  112. "DB Host" 1 1 "$DB_HOST" 1 18 30 30 \
  113. "DB Port" 2 1 "$DB_PORT" 2 18 30 30 \
  114. "DB Name" 3 1 "$DB_NAME" 3 18 30 30 \
  115. "DB User" 4 1 "$DB_USER" 4 18 30 30 \
  116. "DB Password" 5 1 "$DB_PASS" 5 18 30 30 \
  117. "Root Password" 6 1 "$DB_ROOT_PASSWORD" 6 18 30 30 \
  118. "Container Name" 7 1 "$BEEPZONE_DB_CONTAINER_NAME" 7 18 30 30 \
  119. "Image" 8 1 "$BEEPZONE_DB_IMAGE" 8 18 30 30 2>"$TMP_FILE" || return
  120. # Parse dialog output (8 lines, one per field)
  121. {
  122. read -r DB_HOST
  123. read -r DB_PORT
  124. read -r DB_NAME
  125. read -r DB_USER
  126. read -r DB_PASS
  127. read -r DB_ROOT_PASSWORD
  128. read -r BEEPZONE_DB_CONTAINER_NAME
  129. read -r BEEPZONE_DB_IMAGE
  130. } < "$TMP_FILE"
  131. save_env
  132. if podman ps -a --format '{{.Names}}' | grep -q "^${BEEPZONE_DB_CONTAINER_NAME}$"; then
  133. $DIALOG --yesno "Container '$BEEPZONE_DB_CONTAINER_NAME' already exists.\n\nStart (or restart) it now?" 10 60
  134. if [[ $? -eq 0 ]]; then
  135. podman start "$BEEPZONE_DB_CONTAINER_NAME" >/dev/null 2>&1 || podman restart "$BEEPZONE_DB_CONTAINER_NAME" >/dev/null 2>&1 || true
  136. $DIALOG --msgbox "Container '$BEEPZONE_DB_CONTAINER_NAME' started (or already running)." 7 60
  137. fi
  138. return
  139. fi
  140. run_cmd=(
  141. podman run -d
  142. --name "${BEEPZONE_DB_CONTAINER_NAME}"
  143. -e "MARIADB_ROOT_PASSWORD=${DB_ROOT_PASSWORD}"
  144. -e "MARIADB_DATABASE=${DB_NAME}"
  145. -e "MARIADB_USER=${DB_USER}"
  146. -e "MARIADB_PASSWORD=${DB_PASS}"
  147. -p "${DB_PORT}:3306"
  148. "${BEEPZONE_DB_IMAGE}"
  149. )
  150. pretty_cmd="podman run -d \\
  151. --name ${BEEPZONE_DB_CONTAINER_NAME} \\
  152. -e MARIADB_ROOT_PASSWORD=${DB_ROOT_PASSWORD} \\
  153. -e MARIADB_DATABASE=${DB_NAME} \\
  154. -e MARIADB_USER=${DB_USER} \\
  155. -e MARIADB_PASSWORD=${DB_PASS} \\
  156. -p ${DB_PORT}:3306 ${BEEPZONE_DB_IMAGE}"
  157. if $DIALOG --yesno "About to run:\n\n${pretty_cmd}\n\nProceed?" 17 76; then
  158. if "${run_cmd[@]}" >>"$LOG_FILE" 2>&1; then
  159. $DIALOG --msgbox "MariaDB container '${BEEPZONE_DB_CONTAINER_NAME}' started successfully." 8 70
  160. else
  161. error_log=$(tail -20 "$LOG_FILE")
  162. $DIALOG --title "Error" --msgbox "Failed to start MariaDB container.\n\nError:\n${error_log}" 20 76
  163. fi
  164. fi
  165. }
  166. import_schema_and_seed() {
  167. local import_type schema_file full_dump_file
  168. schema_file="$WORK_DIR/backend/database/schema/beepzone-schema-dump.sql"
  169. full_dump_file="$WORK_DIR/backend/database/dev/beepzone-full-dump.sql"
  170. # Ask what type of import
  171. $DIALOG --clear \
  172. --title "Database Import" \
  173. --menu "Select import type" 12 70 2 \
  174. 1 "Full dump (schema + data in one file)" \
  175. 2 "Clean schema only (no data)" 2>"$CHOICE_FILE" || return
  176. import_type=$(<"$CHOICE_FILE")
  177. if [[ "$import_type" == "1" ]]; then
  178. # Full dump import
  179. if [[ ! -f "$full_dump_file" ]]; then
  180. $DIALOG --msgbox "full dump file not found at:\n$full_dump_file" 10 70
  181. return
  182. fi
  183. $DIALOG --yesno "import full database dump from:\n$full_dump_file\n\nThis will DROP and recreate the database.\n\nProceed?" 12 70 || return
  184. {
  185. echo "DROP DATABASE IF EXISTS \`$DB_NAME\`;"
  186. echo "CREATE DATABASE \`$DB_NAME\` CHARACTER SET utf8mb4 COLLATE utf8mb4_uca1400_ai_ci;"
  187. echo "USE \`$DB_NAME\`;"
  188. echo "SET FOREIGN_KEY_CHECKS=0;"
  189. cat "$full_dump_file"
  190. echo "SET FOREIGN_KEY_CHECKS=1;"
  191. } | podman exec -i "$BEEPZONE_DB_CONTAINER_NAME" \
  192. mariadb -h"$DB_HOST" -P"$DB_PORT" -uroot -p"$DB_ROOT_PASSWORD" >>"$LOG_FILE" 2>&1 || {
  193. error_log=$(tail -30 "$LOG_FILE")
  194. $DIALOG --title "Error" --msgbox "full dump import failed.\n\nError:\n${error_log}" 22 76
  195. return
  196. }
  197. DEPLOYMENT_TYPE="dev"
  198. save_env
  199. $DIALOG --msgbox "full database dump imported successfully!" 8 70
  200. else
  201. # Clean schema import
  202. if [[ ! -f "$schema_file" ]]; then
  203. $DIALOG --msgbox "schema file not found at:\n$schema_file" 10 60
  204. return
  205. fi
  206. $DIALOG --yesno "import clean schema from:\n$schema_file\n\nThis will DROP and recreate the database.\n\nProceed?" 12 70 || return
  207. {
  208. echo "DROP DATABASE IF EXISTS \`$DB_NAME\`;"
  209. echo "CREATE DATABASE \`$DB_NAME\` CHARACTER SET utf8mb4 COLLATE utf8mb4_uca1400_ai_ci;"
  210. echo "USE \`$DB_NAME\`;"
  211. echo "SET FOREIGN_KEY_CHECKS=0;"
  212. cat "$schema_file"
  213. echo "SET FOREIGN_KEY_CHECKS=1;"
  214. } | podman exec -i "$BEEPZONE_DB_CONTAINER_NAME" \
  215. mariadb -h"$DB_HOST" -P"$DB_PORT" -uroot -p"$DB_ROOT_PASSWORD" >>"$LOG_FILE" 2>&1 || {
  216. error_log=$(tail -30 "$LOG_FILE")
  217. $DIALOG --title "Error" --msgbox "schema import failed.\n\nError:\n${error_log}" 22 76
  218. return
  219. }
  220. DEPLOYMENT_TYPE="clean"
  221. save_env
  222. $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
  223. fi
  224. }
  225. manage_users_and_roles() {
  226. while true; do
  227. $DIALOG --clear \
  228. --title "Manage Users & Roles" \
  229. --menu "Choose an action:" 17 60 6 \
  230. 1 "Create a role" \
  231. 2 "Create a user" \
  232. 3 "Delete a role" \
  233. 4 "Delete a user" \
  234. 5 "Back to main menu" 2>"$CHOICE_FILE"
  235. [[ ! -s "$CHOICE_FILE" ]] && return 0
  236. choice=$(<"$CHOICE_FILE")
  237. case $choice in
  238. 1) create_role ;;
  239. 2) create_user ;;
  240. 3) delete_role ;;
  241. 4) delete_user ;;
  242. 5) return 0 ;;
  243. esac
  244. done
  245. }
  246. create_role() {
  247. $DIALOG --title "Create Role" \
  248. --form "Enter role details:" 12 60 2 \
  249. "Role name:" 1 1 "" 1 20 30 0 \
  250. "Power (1-100):" 2 1 "" 2 20 10 0 \
  251. 2>"$TMP_FILE"
  252. [[ ! -s "$TMP_FILE" ]] && return 1
  253. local name power
  254. {
  255. read -r name
  256. read -r power
  257. } < "$TMP_FILE"
  258. [[ -z "$name" || -z "$power" ]] && {
  259. $DIALOG --msgbox "Role name and power are required." 8 50
  260. return 1
  261. }
  262. # Validate power is a number between 1-100
  263. if ! [[ "$power" =~ ^[0-9]+$ ]] || [[ "$power" -lt 1 || "$power" -gt 100 ]]; then
  264. $DIALOG --msgbox "Power must be a number between 1 and 100." 8 50
  265. return 1
  266. fi
  267. local sql="INSERT INTO roles (name, power) VALUES ('$name', $power);"
  268. echo "$sql" | podman exec -i "$BEEPZONE_DB_CONTAINER_NAME" \
  269. mariadb -h"$DB_HOST" -P"$DB_PORT" -uroot -p"$DB_ROOT_PASSWORD" "$DB_NAME" >>"$LOG_FILE" 2>&1 || {
  270. error_log=$(tail -20 "$LOG_FILE")
  271. $DIALOG --title "Error" --msgbox "Failed to create role.\n\nError:\n${error_log}" 20 76
  272. return 1
  273. }
  274. $DIALOG --msgbox "Role '$name' created successfully!" 8 50
  275. }
  276. delete_role() {
  277. # Fetch roles from the database
  278. local roles_list
  279. roles_list=$(podman exec -i "$BEEPZONE_DB_CONTAINER_NAME" \
  280. mariadb -h"$DB_HOST" -P"$DB_PORT" -uroot -p"$DB_ROOT_PASSWORD" "$DB_NAME" \
  281. -e "SELECT id, name, power FROM roles ORDER BY power DESC;" -s -N 2>>"$LOG_FILE") || {
  282. $DIALOG --msgbox "Failed to fetch roles from database." 10 60
  283. return
  284. }
  285. # Build role selection menu
  286. local role_options=()
  287. while IFS=$'\t' read -r role_id role_name role_power; do
  288. role_options+=("$role_id" "$role_name (power: $role_power)")
  289. done <<< "$roles_list"
  290. if [[ ${#role_options[@]} -eq 0 ]]; then
  291. $DIALOG --msgbox "No roles found in database." 10 60
  292. return
  293. fi
  294. # Select role to delete
  295. $DIALOG --menu "Select role to delete:" 20 70 10 "${role_options[@]}" 2>"$TMP_FILE" || return
  296. local selected_role_id
  297. selected_role_id=$(<"$TMP_FILE")
  298. # Get role name for confirmation
  299. local selected_role_name
  300. selected_role_name=$(echo "$roles_list" | awk -v id="$selected_role_id" -F'\t' '$1 == id {print $2}')
  301. # Check if any users are using this role
  302. local user_count
  303. user_count=$(podman exec -i "$BEEPZONE_DB_CONTAINER_NAME" \
  304. mariadb -h"$DB_HOST" -P"$DB_PORT" -uroot -p"$DB_ROOT_PASSWORD" "$DB_NAME" \
  305. -e "SELECT COUNT(*) FROM users WHERE role_id = $selected_role_id;" -s -N 2>>"$LOG_FILE")
  306. if [[ "$user_count" -gt 0 ]]; then
  307. $DIALOG --msgbox "Cannot delete role '$selected_role_name'.\n$user_count user(s) are currently assigned this role." 10 60
  308. return
  309. fi
  310. # Confirm deletion
  311. $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
  312. # Delete role
  313. local sql="DELETE FROM roles WHERE id = $selected_role_id;"
  314. echo "$sql" | podman exec -i "$BEEPZONE_DB_CONTAINER_NAME" \
  315. mariadb -h"$DB_HOST" -P"$DB_PORT" -uroot -p"$DB_ROOT_PASSWORD" "$DB_NAME" >>"$LOG_FILE" 2>&1 || {
  316. error_log=$(tail -20 "$LOG_FILE")
  317. $DIALOG --title "Error" --msgbox "Failed to delete role.\n\nError:\n${error_log}" 20 76
  318. return
  319. }
  320. $DIALOG --msgbox "Role '$selected_role_name' deleted successfully!" 8 60
  321. }
  322. create_user() {
  323. # Get available roles
  324. local roles_list
  325. roles_list=$(podman exec -i "$BEEPZONE_DB_CONTAINER_NAME" \
  326. mariadb -h"$DB_HOST" -P"$DB_PORT" -uroot -p"$DB_ROOT_PASSWORD" "$DB_NAME" \
  327. -e "SELECT id, name, power FROM roles ORDER BY power DESC;" -s -N 2>>"$LOG_FILE") || {
  328. $DIALOG --msgbox "Failed to fetch roles from database.\nEnsure schema is imported and roles exist." 10 60
  329. return
  330. }
  331. # Build role selection menu
  332. local role_options=()
  333. while IFS=$'\t' read -r role_id role_name role_power; do
  334. role_options+=("$role_id" "$role_name (power: $role_power)")
  335. done <<< "$roles_list"
  336. if [[ ${#role_options[@]} -eq 0 ]]; then
  337. $DIALOG --msgbox "No roles found in database.\nPlease create a role first." 10 60
  338. return
  339. fi
  340. # Select role
  341. $DIALOG --menu "Select user role" 15 60 5 "${role_options[@]}" 2>"$TMP_FILE" || return
  342. local selected_role_id
  343. selected_role_id=$(<"$TMP_FILE")
  344. # Get user details
  345. $DIALOG --form "Create BeepZone user" 14 70 5 \
  346. "Name (full name)" 1 1 "" 1 20 40 40 \
  347. "Username" 2 1 "" 2 20 40 40 \
  348. "Password" 3 1 "" 3 20 40 40 \
  349. "Email" 4 1 "" 4 20 40 40 \
  350. "Phone" 5 1 "" 5 20 40 40 2>"$TMP_FILE" || return
  351. local name username password email phone
  352. {
  353. read -r name
  354. read -r username
  355. read -r password
  356. read -r email
  357. read -r phone
  358. } < "$TMP_FILE"
  359. if [[ -z "$name" || -z "$username" || -z "$password" ]]; then
  360. $DIALOG --msgbox "Name, username, and password are required." 8 60
  361. return
  362. fi
  363. # Hash password with bcrypt (cost 12)
  364. local password_hash
  365. # Try htpasswd first (native Unix tool)
  366. if command -v htpasswd >/dev/null 2>&1; then
  367. password_hash=$(htpasswd -nbB -C 12 "$username" "$password" 2>>"$LOG_FILE" | cut -d: -f2)
  368. # Fall back to Python bcrypt
  369. elif command -v python3 >/dev/null 2>&1; then
  370. password_hash=$(python3 -c "import bcrypt; print(bcrypt.hashpw('$password'.encode('utf-8'), bcrypt.gensalt(12)).decode('utf-8'))" 2>>"$LOG_FILE")
  371. fi
  372. if [[ -z "$password_hash" ]]; then
  373. $DIALOG --msgbox "Error: Failed to hash password. No bcrypt tool found.\n\nInstall options:\n- htpasswd: brew install httpd (macOS) or apt install apache2-utils (Linux)\n- Python bcrypt: pip3 install bcrypt" 12 70
  374. return
  375. fi
  376. # Insert user with hashed password
  377. local sql
  378. sql="INSERT INTO users (name, username, password, role_id, email, phone, active) VALUES
  379. ('$name', '$username', '$password_hash', $selected_role_id, "
  380. [[ -n "$email" ]] && sql+="'$email'" || sql+="NULL"
  381. sql+=", "
  382. [[ -n "$phone" ]] && sql+="'$phone'" || sql+="NULL"
  383. sql+=", 1);"
  384. echo "$sql" | podman exec -i "$BEEPZONE_DB_CONTAINER_NAME" \
  385. mariadb -h"$DB_HOST" -P"$DB_PORT" -uroot -p"$DB_ROOT_PASSWORD" "$DB_NAME" >>"$LOG_FILE" 2>&1 || {
  386. error_log=$(tail -20 "$LOG_FILE")
  387. $DIALOG --title "Error" --msgbox "Failed to create user.\n\nError:\n${error_log}" 20 76
  388. return
  389. }
  390. $DIALOG --msgbox "User '$username' created successfully!" 8 60
  391. }
  392. delete_user() {
  393. # Fetch users from the database
  394. local users_list
  395. users_list=$(podman exec -i "$BEEPZONE_DB_CONTAINER_NAME" \
  396. mariadb -h"$DB_HOST" -P"$DB_PORT" -uroot -p"$DB_ROOT_PASSWORD" "$DB_NAME" \
  397. -e "SELECT id, username, name FROM users ORDER BY id;" -s -N 2>>"$LOG_FILE") || {
  398. $DIALOG --msgbox "Failed to fetch users from database." 10 60
  399. return
  400. }
  401. # Build user selection menu
  402. local user_options=()
  403. while IFS=$'\t' read -r user_id username name; do
  404. user_options+=("$user_id" "$username - $name")
  405. done <<< "$users_list"
  406. if [[ ${#user_options[@]} -eq 0 ]]; then
  407. $DIALOG --msgbox "No users found in database." 10 60
  408. return
  409. fi
  410. # Select user to delete
  411. $DIALOG --menu "Select user to delete:" 20 70 10 "${user_options[@]}" 2>"$TMP_FILE" || return
  412. local selected_user_id
  413. selected_user_id=$(<"$TMP_FILE")
  414. # Get username for confirmation
  415. local selected_username
  416. selected_username=$(echo "$users_list" | awk -v id="$selected_user_id" -F'\t' '$1 == id {print $2}')
  417. # Confirm deletion
  418. $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
  419. # Delete user
  420. local sql="DELETE FROM users WHERE id = $selected_user_id;"
  421. echo "$sql" | podman exec -i "$BEEPZONE_DB_CONTAINER_NAME" \
  422. mariadb -h"$DB_HOST" -P"$DB_PORT" -uroot -p"$DB_ROOT_PASSWORD" "$DB_NAME" >>"$LOG_FILE" 2>&1 || {
  423. error_log=$(tail -20 "$LOG_FILE")
  424. $DIALOG --title "Error" --msgbox "Failed to delete user.\n\nError:\n${error_log}" 20 76
  425. return
  426. }
  427. $DIALOG --msgbox "User '$selected_username' deleted successfully!" 8 60
  428. }
  429. clone_if_missing() {
  430. local repo_url="$1" dest_dir="$2"
  431. if [[ -d "$dest_dir/.git" ]]; then
  432. return
  433. fi
  434. git clone "$repo_url" "$dest_dir" >>"$LOG_FILE" 2>&1 || {
  435. error_log=$(tail -20 "$LOG_FILE")
  436. $DIALOG --title "Error" --msgbox "Failed to clone $repo_url.\n\nError:\n${error_log}" 20 76
  437. return 1
  438. }
  439. }
  440. setup_seckelapi() {
  441. local sources_dir="$WORK_DIR/backend/seckelapi/sources"
  442. local config_dir="$WORK_DIR/backend/seckelapi/config"
  443. local sources_basics_config="$sources_dir/config/basics.toml"
  444. # Check if sources are already cloned
  445. if [[ ! -d "$sources_dir/.git" ]]; then
  446. mkdir -p "$sources_dir"
  447. clone_if_missing "$SECKELAPI_REPO" "$sources_dir" || return
  448. fi
  449. # Ask about config update
  450. $DIALOG --clear \
  451. --title "SeckelAPI Configuration" \
  452. --menu "How do you want to handle the config?" 12 70 2 \
  453. 1 "Auto-update from database settings" \
  454. 2 "Manually edit config file" 2>"$CHOICE_FILE"
  455. [[ ! -s "$CHOICE_FILE" ]] && return 0
  456. config_choice=$(<"$CHOICE_FILE")
  457. # Ensure sources config directory exists and copy all template configs
  458. mkdir -p "$sources_dir/config"
  459. # Always copy all template configs from config/ to sources/config/
  460. for conf_file in "$config_dir"/*.toml; do
  461. conf_name=$(basename "$conf_file")
  462. cp "$conf_file" "$sources_dir/config/$conf_name" 2>>"$LOG_FILE"
  463. done
  464. if [[ "$config_choice" == "1" ]]; then
  465. # Auto-update basics.toml in sources with database settings (only [database] section)
  466. if [[ -f "$sources_basics_config" ]]; then
  467. # Determine database host - use host.containers.internal for container deployments
  468. local db_host_value="$DB_HOST"
  469. sed -i.bak \
  470. -e '/^\[database\]/,/^\[/ {
  471. /^host = /s|= .*|= "'"$db_host_value"'"|
  472. /^port = /s|= .*|= '"$DB_PORT"'|
  473. /^database = /s|= .*|= "'"$DB_NAME"'"|
  474. /^username = /s|= .*|= "'"$DB_USER"'"|
  475. /^password = /s|= .*|= "'"$DB_PASS"'"|
  476. }' \
  477. "$sources_basics_config"
  478. $DIALOG --msgbox "Config updated with database settings from .env" 8 60
  479. else
  480. $DIALOG --msgbox "Error: basics.toml not found at $sources_basics_config" 8 60
  481. return 1
  482. fi
  483. else
  484. # Open config file for manual editing (in sources/config/)
  485. if [[ -f "$sources_basics_config" ]]; then
  486. ${EDITOR:-nano} "$sources_basics_config"
  487. else
  488. $DIALOG --msgbox "Error: basics.toml not found at $sources_basics_config" 8 60
  489. return 1
  490. fi
  491. fi
  492. # Ask about build and deployment
  493. $DIALOG --clear \
  494. --title "Build & Deploy SeckelAPI" \
  495. --menu "Choose an action:" 12 70 2 \
  496. 1 "Build and deploy to podman container" \
  497. 2 "Build natively on host" 2>"$CHOICE_FILE"
  498. [[ ! -s "$CHOICE_FILE" ]] && return 0
  499. build_choice=$(<"$CHOICE_FILE")
  500. if [[ "$build_choice" == "2" ]]; then
  501. # Native host build with live output
  502. clear
  503. echo "Building SeckelAPI..."
  504. echo "===================="
  505. echo ""
  506. if (cd "$sources_dir" && cargo build --release); then
  507. # Copy config files to the build output directory
  508. local target_dir="$sources_dir/target/release"
  509. mkdir -p "$target_dir/config"
  510. cp "$sources_dir/config"/*.toml "$target_dir/config/" 2>>"$LOG_FILE"
  511. echo ""
  512. echo "Build completed successfully!"
  513. echo "Binary location: $target_dir/seckelapi"
  514. echo "Config location: $target_dir/config/"
  515. read -p "Press Enter to continue..."
  516. else
  517. echo ""
  518. echo "Build failed! Check the output above for errors."
  519. read -p "Press Enter to continue..."
  520. return 1
  521. fi
  522. elif [[ "$build_choice" == "1" ]]; then
  523. # Build and deploy to podman container
  524. clear
  525. echo "Building SeckelAPI container..."
  526. echo "================================"
  527. echo ""
  528. # Get the host gateway IP for container to access host services
  529. # For Podman, we'll use the gateway IP from the default bridge network
  530. local host_gateway="host.containers.internal"
  531. # Update database host for container networking
  532. if [[ -f "$sources_basics_config" ]]; then
  533. echo "Updating database host to: $host_gateway"
  534. sed -i.container-bak \
  535. -e '/^\[database\]/,/^\[/ {
  536. /^host = /s|= .*|= "'"$host_gateway"'"|
  537. }' \
  538. "$sources_basics_config"
  539. fi
  540. local container_name="beepzone-seckelapi"
  541. local image_name="beepzone-seckelapi:latest"
  542. local containerfile="$WORK_DIR/backend/seckelapi/Containerfile"
  543. # Stop and remove existing container if running
  544. if podman ps -a --format "{{.Names}}" | grep -q "^${container_name}$"; then
  545. echo "Stopping and removing existing container..."
  546. podman stop "$container_name" 2>/dev/null || true
  547. podman rm "$container_name" 2>/dev/null || true
  548. fi
  549. # Build container image
  550. echo "Building container image..."
  551. if podman build -t "$image_name" -f "$containerfile" "$WORK_DIR/backend/seckelapi"; then
  552. echo ""
  553. echo "Container image built successfully!"
  554. echo ""
  555. # Ask to run the container
  556. if $DIALOG --yesno "Start the SeckelAPI container now?" 8 50; then
  557. echo "Starting container..."
  558. # Run container with port mapping and host gateway
  559. if podman run -d \
  560. --name "$container_name" \
  561. --add-host host.containers.internal:host-gateway \
  562. -p 5777:5777 \
  563. "$image_name"; then
  564. echo ""
  565. echo "Container started successfully!"
  566. echo "Container name: $container_name"
  567. echo "API listening on: http://0.0.0.0:5777"
  568. echo ""
  569. echo "Useful commands:"
  570. echo " podman logs $container_name - View logs"
  571. echo " podman stop $container_name - Stop container"
  572. echo " podman start $container_name - Start container"
  573. echo " podman restart $container_name - Restart container"
  574. read -p "Press Enter to continue..."
  575. else
  576. echo ""
  577. echo "Failed to start container!"
  578. read -p "Press Enter to continue..."
  579. return 1
  580. fi
  581. fi
  582. else
  583. echo ""
  584. echo "Container build failed! Check the output above for errors."
  585. read -p "Press Enter to continue..."
  586. return 1
  587. fi
  588. fi
  589. }
  590. build_desktop_client() {
  591. local sources_dir="$WORK_DIR/frontend/desktop-client/sources"
  592. # Check if sources are already cloned
  593. if [[ ! -d "$sources_dir/.git" ]]; then
  594. mkdir -p "$sources_dir"
  595. clone_if_missing "$CLIENT_REPO" "$sources_dir" || return
  596. fi
  597. if $DIALOG --yesno "Build BeepZone desktop client in release mode now?" 8 70; then
  598. clear
  599. echo "Building BeepZone Desktop Client..."
  600. echo "===================================="
  601. echo ""
  602. if (cd "$sources_dir" && cargo build --release); then
  603. echo ""
  604. echo "Build completed successfully!"
  605. echo "Binary location: $sources_dir/target/release/"
  606. read -p "Press Enter to continue..."
  607. else
  608. echo ""
  609. echo "Build failed! Check the output above for errors."
  610. read -p "Press Enter to continue..."
  611. return 1
  612. fi
  613. fi
  614. }
  615. ask_main_menu