1
0

beepzone-helper.sh 26 KB

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