-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
1271 lines (1182 loc) · 47.8 KB
/
setup.sh
File metadata and controls
1271 lines (1182 loc) · 47.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
set -euo pipefail
set -E # enable ERR trap inheritance
# Preserve original script arguments for potential re-exec under elevated user
SCRIPT_ARGS=("$@")
# Non-interactive apt and sane locale defaults (quiet Perl/apt warnings on minimal systems)
export DEBIAN_FRONTEND=noninteractive
export LANG=C.UTF-8
export LC_ALL=C.UTF-8
export LANGUAGE=C
# FinX Debian/Ubuntu Installer (interactive + idempotent)
# - Installs Node.js LTS, PostgreSQL, Nginx (optional)
# - Creates database and app user with strong passwords
# - Generates .env, builds frontend
# - Configures systemd service and Nginx site
# - Starts everything and prints next steps
APP_NAME="finx"
APP_USER_DEFAULT="finx"
APP_DIR="$(cd "$(dirname "$0")" && pwd)"
INSTALL_DIR_DEFAULT="/opt/${APP_NAME}"
REPO_URL_DEFAULT="https://github.com/IT-BAER/finx.git"
# Optional versioning controls (can be provided via environment)
# FINX_REF: branch, tag, or commit to install (e.g., v1.2.3)
# FINX_ARCHIVE_URL: URL to a release archive (.tar.gz/.tgz/.zip) to install
ENV_DIR="/etc/${APP_NAME}"
ENV_FILE="${ENV_DIR}/${APP_NAME}.env"
SERVICE_FILE="/etc/systemd/system/${APP_NAME}.service"
NGINX_SITE_AVAILABLE="/etc/nginx/sites-available/${APP_NAME}.conf"
NGINX_SITE_ENABLED="/etc/nginx/sites-enabled/${APP_NAME}.conf"
FRONTEND_PORT=""
UPDATE_MODE="n"
# Creation flags for rollback
CREATED_SYSTEM_USER="n"
CREATED_INSTALL_DIR="n"
CREATED_ENV_FILE="n"
CREATED_SERVICE="n"
CREATED_NGINX_SITE="n"
CREATED_APACHE_SITE="n"
APPENDED_APACHE_PORT="n"
CREATED_DB_USER="n"
CREATED_DB="n"
STARTED_SERVICE="n"
COMPLETED="n"
APACHE_SITE_FILE="/etc/apache2/sites-available/${APP_NAME}.conf"
APACHE_PORTS_CONF="/etc/apache2/ports.conf"
BLUE="\033[1;34m"; GREEN="\033[1;32m"; YELLOW="\033[1;33m"; RED="\033[1;31m"; NC="\033[0m"
say() { echo -e "${BLUE}⇒${NC} $*"; }
ok() { echo -e "${GREEN}✔${NC} $*"; }
warn(){ echo -e "${YELLOW}⚠${NC} $*"; }
err() { echo -e "${RED}✖${NC} $*"; }
need_sudo() {
if [ "${EUID:-$(id -u)}" -ne 0 ]; then
if command -v sudo >/dev/null 2>&1; then
SUDO="sudo"
elif command -v doas >/dev/null 2>&1; then
# Support OpenBSD-style privilege escalation
SUDO="doas"
else
warn "Neither sudo nor doas is installed. Attempting to re-run as root using su."
# Resolve absolute path to this script
local SELF
if command -v realpath >/dev/null 2>&1; then
SELF=$(realpath "$0")
elif command -v readlink >/dev/null 2>&1; then
SELF=$(readlink -f "$0" 2>/dev/null || echo "$0")
else
SELF="$0"
fi
# Re-exec using su, preserving arguments; will prompt for root password
su -s /bin/sh -c "exec /usr/bin/env bash \"$SELF\" ${SCRIPT_ARGS[*]@Q}" root
# If su returns, it failed
err "Privilege escalation failed. Please run as root or install sudo/doas."
exit 1
fi
else
SUDO=""
fi
}
setup_traps() {
# Install traps after sudo detection so cleanup can use $SUDO
trap 'on_error' ERR
trap 'on_interrupt' INT TERM
}
on_error() {
err "Setup failed. Rolling back partial changes..."
cleanup "error"
exit 1
}
on_interrupt() {
warn "Setup interrupted. Rolling back partial changes..."
cleanup "interrupt"
exit 130
}
cleanup() {
local reason="${1:-unknown}"
# Stop service if we started it
if [ "$STARTED_SERVICE" = "y" ] && [ -f "$SERVICE_FILE" ]; then
$SUDO systemctl stop "${APP_NAME}.service" || true
fi
# Disable and remove systemd service if we created it this run
if [ "$CREATED_SERVICE" = "y" ] && [ -f "$SERVICE_FILE" ]; then
$SUDO systemctl disable "${APP_NAME}.service" >/dev/null 2>&1 || true
$SUDO rm -f "$SERVICE_FILE" || true
$SUDO systemctl daemon-reload || true
fi
# Remove nginx site if we created it
if [ "$CREATED_NGINX_SITE" = "y" ]; then
[ -L "$NGINX_SITE_ENABLED" ] && $SUDO rm -f "$NGINX_SITE_ENABLED" || true
[ -f "$NGINX_SITE_AVAILABLE" ] && $SUDO rm -f "$NGINX_SITE_AVAILABLE" || true
command -v nginx >/dev/null 2>&1 && $SUDO nginx -t >/dev/null 2>&1 && $SUDO systemctl reload nginx || true
fi
# Remove apache site and revert port if we created/appended
if [ "$CREATED_APACHE_SITE" = "y" ]; then
[ -f "$APACHE_SITE_FILE" ] && $SUDO a2dissite "${APP_NAME}.conf" >/dev/null 2>&1 || true
[ -f "$APACHE_SITE_FILE" ] && $SUDO rm -f "$APACHE_SITE_FILE" || true
if [ "$APPENDED_APACHE_PORT" = "y" ] && [ -f "$APACHE_PORTS_CONF" ]; then
$SUDO sed -i "/^Listen[[:space:]]\+${FRONTEND_PORT}$/d" "$APACHE_PORTS_CONF" || true
fi
command -v apache2ctl >/dev/null 2>&1 && $SUDO apache2ctl configtest >/dev/null 2>&1 && $SUDO systemctl reload apache2 || true
fi
# Remove env file if created
if [ "$CREATED_ENV_FILE" = "y" ] && [ -f "$ENV_FILE" ]; then
$SUDO rm -f "$ENV_FILE" || true
fi
# Drop database and role if we created them
if [ "$CREATED_DB" = "y" ]; then
run_as_user postgres psql -v ON_ERROR_STOP=1 -c "DROP DATABASE IF EXISTS ${DB_NAME};" || true
fi
if [ "$CREATED_DB_USER" = "y" ]; then
run_as_user postgres psql -v ON_ERROR_STOP=1 -c "DROP ROLE IF EXISTS ${DB_USER};" || true
fi
# Remove app directory if we created it
if [ "$CREATED_INSTALL_DIR" = "y" ] && [ -n "${INSTALL_DIR_DEFAULT}" ] && [ -d "${FINX_INSTALL_DIR:-${INSTALL_DIR_DEFAULT}}" ]; then
$SUDO rm -rf "${FINX_INSTALL_DIR:-${INSTALL_DIR_DEFAULT}}" || true
fi
# Remove system user if we created it
if [ "$CREATED_SYSTEM_USER" = "y" ]; then
$SUDO userdel -r "$APP_USER" >/dev/null 2>&1 || true
fi
warn "Rollback finished (${reason})."
}
# Run a command as a given system user, compatible with both sudo and pure-root contexts
run_as_user() {
local _user="$1"; shift
if [ -n "${SUDO}" ]; then
# sudo and doas both support -u
${SUDO} -u "${_user}" "$@"
else
# Use su for root context; preserve environment variables passed explicitly
su -s /bin/sh -c "cd / && $(printf '%q ' "$@")" "${_user}"
fi
}
# Run a command as a given user within a specific directory (preserve cwd for that command)
run_as_user_in_dir() {
local _user="$1"; shift
local _dir="$1"; shift
if [ -n "${SUDO}" ]; then
${SUDO} -u "${_user}" bash -lc "cd $(printf '%q' "${_dir}") && $(printf '%q ' "$@")"
else
su -s /bin/sh -c "cd $(printf '%q' "${_dir}") && $(printf '%q ' "$@")" "${_user}"
fi
}
detect_os() {
if [ -f /etc/debian_version ]; then
ok "Debian/Ubuntu detected"
else
warn "This installer is intended for Debian/Ubuntu. Continuing anyway."
fi
}
# Ensure the dedicated system user exists early if requested, so we can chown/clone as that user
ensure_system_user() {
[ "${CREATE_USER}" = "y" ] || return 0
if id -u "$APP_USER" >/dev/null 2>&1; then
ok "User $APP_USER exists"
return 0
fi
say "Creating system user $APP_USER"
$SUDO useradd -r -m -d "/home/${APP_USER}" -s /usr/sbin/nologin "$APP_USER"
CREATED_SYSTEM_USER="y"
}
generate_secret() {
# 40 chars alnum
# Suppress potential SIGPIPE stderr from tr when head closes early
tr -dc 'A-Za-z0-9' </dev/urandom 2>/dev/null | head -c 40 || true
}
ensure_pkg() {
local pkg="$1"
if ! dpkg -s "$pkg" >/dev/null 2>&1; then
say "Installing package: $pkg"
apt_update_once
$SUDO apt-get install -y "$pkg"
else
ok "$pkg already installed"
fi
}
ensure_tool() {
# Ensure a generic tool exists (curl, unzip, tar are typically present)
local tool="$1"
if ! command -v "$tool" >/dev/null 2>&1; then
case "$tool" in
curl) ensure_pkg curl ;;
unzip) ensure_pkg unzip ;;
tar) ensure_pkg tar ;;
git) ensure_pkg git ;;
*) ensure_pkg "$tool" ;;
esac
fi
}
# Load existing settings for non-interactive updates
load_existing_settings() {
say "Existing installation detected; running update with current settings"
# Load env if present
if [ -f "$ENV_FILE" ]; then
# shellcheck disable=SC1090
source "$ENV_FILE"
PORT=${PORT:-5000}
DB_NAME=${DB_NAME:-finx_db}
DB_USER=${DB_USER:-finx_user}
export DB_PASSWORD=${DB_PASSWORD:-}
fi
# Determine service user
if [ -f "$SERVICE_FILE" ]; then
local SVC_USER
SVC_USER=$(grep -E '^User=' "$SERVICE_FILE" | tail -n1 | cut -d'=' -f2 || true)
if [ -n "$SVC_USER" ]; then
CREATE_USER=y
APP_USER="$SVC_USER"
fi
fi
# Detect existing web server in use
if [ -f "/etc/nginx/sites-available/${APP_NAME}.conf" ]; then
WEB_SERVER=nginx
elif [ -f "/etc/apache2/sites-available/${APP_NAME}.conf" ]; then
WEB_SERVER=apache
else
WEB_SERVER=none
fi
}
install_node() {
if command -v node >/dev/null 2>&1; then
local v;
v=$(node -v | sed 's/v//')
say "Found Node.js ${v}"
else
say "Installing Node.js LTS (20.x) via NodeSource"
if [ -n "${SUDO}" ]; then
if [ "${SUDO}" = "sudo" ]; then
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
else
# doas (no -E support); pipe directly
curl -fsSL https://deb.nodesource.com/setup_20.x | ${SUDO} bash -
fi
else
curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
fi
ensure_pkg nodejs
fi
}
install_postgres() {
if ! command -v psql >/dev/null 2>&1; then
say "Installing PostgreSQL"
ensure_pkg postgresql
ensure_pkg postgresql-contrib
else
ok "PostgreSQL already installed"
fi
$SUDO systemctl enable postgresql
$SUDO systemctl start postgresql
}
install_redis() {
# Only install if user opted in
[ "${INSTALL_REDIS:-n}" = "y" ] || return 0
if ! command -v redis-server >/dev/null 2>&1; then
say "Installing Redis"
ensure_pkg redis-server
else
ok "Redis already installed"
fi
$SUDO systemctl enable redis-server
$SUDO systemctl start redis-server
ok "Redis is running on localhost:6379"
}
# Run apt-get update once per session to speed up repeated installs
APT_UPDATED_FLAG="/tmp/.finx-apt-updated.flag"
apt_update_once() {
if [ ! -f "$APT_UPDATED_FLAG" ]; then
$SUDO apt-get update -y
touch "$APT_UPDATED_FLAG"
fi
}
is_pkg_installed() { dpkg -s "$1" >/dev/null 2>&1; }
install_web_server() {
case "${WEB_SERVER}" in
nginx)
ensure_pkg nginx
$SUDO systemctl enable nginx
$SUDO systemctl start nginx
;;
apache)
ensure_pkg apache2
# Enable required Apache modules (include proxy_wstunnel for WebSockets)
$SUDO a2enmod proxy proxy_http proxy_wstunnel headers rewrite deflate mime expires >/dev/null 2>&1 || true
$SUDO systemctl enable apache2
$SUDO systemctl start apache2
;;
none|"")
warn "Skipping web server installation"
;;
esac
}
# Determine a free frontend port starting at 3000, avoiding conflicts in nginx/apache configs and active listeners
port_in_use_any() {
local p="$1"
# Check active listeners
if command -v ss >/dev/null 2>&1; then
ss -ltn 2>/dev/null | awk '{print $4}' | grep -E "(^|:)${p}$" -q && return 0
elif command -v netstat >/dev/null 2>&1; then
netstat -tln 2>/dev/null | awk '{print $4}' | grep -E "(^|:)${p}$" -q && return 0
elif command -v lsof >/dev/null 2>&1; then
lsof -i TCP:${p} -sTCP:LISTEN >/dev/null 2>&1 && return 0
fi
# Check nginx configs
if [ -d /etc/nginx ]; then
grep -RhoE "\\blisten\\s+${p}(;|\\s)" /etc/nginx/sites-enabled /etc/nginx/sites-available 2>/dev/null | grep -q . && return 0
fi
# Check apache configs
if [ -d /etc/apache2 ]; then
grep -RhoE "(VirtualHost|Listen)\\s+\*?:${p}\\b" /etc/apache2 2>/dev/null | grep -q . && return 0
fi
return 1
}
choose_frontend_port() {
local start=${1:-3000}
local max=$((start+200))
local p=${start}
while [ ${p} -le ${max} ]; do
if ! port_in_use_any "${p}"; then
FRONTEND_PORT="${p}"
ok "Selected frontend port ${FRONTEND_PORT}"
return 0
fi
p=$((p+1))
done
# Fallback
FRONTEND_PORT="${start}"
warn "Could not find a free port between ${start}-${max}; using ${FRONTEND_PORT}"
}
ensure_repo() {
# When executed inside a checked-out tree, use it as-is
if [ -f "${APP_DIR}/package.json" ] && [ -f "${APP_DIR}/server.js" ]; then
ok "Repository detected at ${APP_DIR}"
return 0
fi
local INSTALL_DIR REPO_URL ARCHIVE_URL REF TMP
INSTALL_DIR="${FINX_INSTALL_DIR:-${INSTALL_DIR_DEFAULT}}"
REPO_URL="${FINX_REPO_URL:-${REPO_URL_DEFAULT}}"
ARCHIVE_URL="${FINX_ARCHIVE_URL:-}"
REF="${FINX_REF:-}"
if [ ! -d "${INSTALL_DIR}" ]; then
$SUDO mkdir -p "${INSTALL_DIR}"
CREATED_INSTALL_DIR="y"
fi
if [ "${CREATE_USER}" = "y" ]; then
$SUDO chown -R "${APP_USER}:${APP_USER}" "${INSTALL_DIR}"
fi
# Path A: Install from a release/archive if provided
if [ -n "${ARCHIVE_URL}" ]; then
say "Fetching release archive to ${INSTALL_DIR}"
ensure_tool curl
ensure_tool file
# Determine filename
TMP=$(mktemp -t finx-archive-XXXX)
# Download as the target user if created, else as current user
if [ "${CREATE_USER}" = "y" ]; then
run_as_user "${APP_USER}" curl -fsSL "${ARCHIVE_URL}" -o "${TMP}"
else
curl -fsSL "${ARCHIVE_URL}" -o "${TMP}"
fi
# Detect archive type and extract
if file -b "${TMP}" | grep -qi 'gzip\|tar'; then
ensure_tool tar
if [ "${CREATE_USER}" = "y" ]; then
run_as_user "${APP_USER}" tar -xzf "${TMP}" -C "${INSTALL_DIR}"
else
tar -xzf "${TMP}" -C "${INSTALL_DIR}"
fi
else
ensure_tool unzip
if [ "${CREATE_USER}" = "y" ]; then
run_as_user "${APP_USER}" unzip -q "${TMP}" -d "${INSTALL_DIR}"
else
unzip -q "${TMP}" -d "${INSTALL_DIR}"
fi
fi
rm -f "${TMP}"
# If the archive extracted into a single top-level directory, flatten it
local entries count first
entries=$(ls -1A "${INSTALL_DIR}" | wc -l | tr -d ' ')
if [ "${entries}" = "1" ]; then
first=$(ls -1A "${INSTALL_DIR}")
if [ -d "${INSTALL_DIR}/${first}" ]; then
say "Flattening extracted directory ${first}"
# Use bash for dotglob to include hidden files when moving
$SUDO bash -lc "shopt -s dotglob; mv '${INSTALL_DIR}/${first}'/* '${INSTALL_DIR}/'"
$SUDO rm -rf "${INSTALL_DIR}/${first}"
fi
fi
APP_DIR="${INSTALL_DIR}"
ok "Using APP_DIR=${APP_DIR} (from archive)"
return 0
fi
# Path B: Git clone (prefer actual releases only, not arbitrary tags)
# If no explicit REF/ARCHIVE_URL provided, try to detect a real release via provider API; else fallback to main branch
if [ -z "${REF}" ] && [ -z "${ARCHIVE_URL}" ]; then
ensure_tool curl
# Parse REPO_URL to host/owner/name
local _url _host _path _owner _name _api_release_url _api_type
_url="${REPO_URL%.git}"
# Normalize ssh form git@host:owner/name to https://host/owner/name
if echo "${_url}" | grep -qE '^[^/]+@[^:]+:'; then
_host=$(printf "%s" "${_url}" | sed -E 's/^[^@]+@([^:]+):.*/\1/')
_path=$(printf "%s" "${_url}" | sed -E 's/^[^:]+:(.*)/\1/')
else
_host=$(printf "%s" "${_url}" | sed -E 's#^https?://([^/]+)/.*#\1#')
_path=$(printf "%s" "${_url}" | sed -E 's#^https?://[^/]+/(.*)$#\1#')
fi
_owner=$(printf "%s" "${_path}" | awk -F'/' '{print $1}')
_name=$(printf "%s" "${_path}" | awk -F'/' '{print $2}')
local NO_RELEASE="y"
if [ -n "${_host}" ] && [ -n "${_owner}" ] && [ -n "${_name}" ]; then
# Try GitHub
if [ "${_host}" = "github.com" ]; then
_api_release_url="https://api.github.com/repos/${_owner}/${_name}/releases/latest"
say "Checking GitHub releases for ${_owner}/${_name}"
local api
api=$(curl -fsSL "${_api_release_url}" 2>/dev/null || true)
if echo "$api" | grep -q '"tag_name"'; then
local tag tarball
tag=$(printf "%s" "$api" | sed -n 's/.*"tag_name"\s*:\s*"\([^"]\+\)".*/\1/p' | head -n1)
tarball=$(printf "%s" "$api" | sed -n 's/.*"tarball_url"\s*:\s*"\([^"]\+\)".*/\1/p' | head -n1)
if [ -n "$tag" ] && [ -n "$tarball" ]; then
REF="$tag"; ARCHIVE_URL="$tarball"; NO_RELEASE="n";
ok "Found latest GitHub release: ${REF}"
fi
fi
else
# Try Gitea API (common for self-hosted at ${_host})
_api_release_url="https://${_host}/api/v1/repos/${_owner}/${_name}/releases"
say "Checking releases via ${_host} API"
local api
api=$(curl -fsSL "${_api_release_url}" 2>/dev/null || true)
if echo "$api" | grep -q '\[{'; then
# Pick first release that's not draft/prerelease if possible
# Crude parse without jq: find first block with "draft":false and "prerelease":false
local block
block=$(printf "%s" "$api" | tr '\n' ' ' | sed 's/\],/\]\n/g' | sed -n '1,1p')
# Extract tag_name and (zip|tar)ball
local tag tarball zipball
tag=$(printf "%s" "$api" | sed -n 's/.*"draft"\s*:\s*false[^\}]*"prerelease"\s*:\s*false[^\}]*"tag_name"\s*:\s*"\([^"]\+\)".*/\1/p' | head -n1)
tarball=$(printf "%s" "$api" | sed -n 's/.*"tarball_url"\s*:\s*"\([^"]\+\)".*/\1/p' | head -n1)
zipball=$(printf "%s" "$api" | sed -n 's/.*"zipball_url"\s*:\s*"\([^"]\+\)".*/\1/p' | head -n1)
if [ -n "$tag" ]; then
REF="$tag"; ARCHIVE_URL="${tarball:-$zipball}"; NO_RELEASE="n";
ok "Found latest release: ${REF}"
fi
fi
fi
fi
if [ "${NO_RELEASE}" = "y" ]; then
warn "No actual releases found; will clone from 'main' branch"
fi
fi
# If release detection found an archive URL, use it instead of git clone
if [ -n "${ARCHIVE_URL}" ]; then
say "Using release archive: ${ARCHIVE_URL}"
ensure_tool curl
ensure_tool file
# For updates, remove existing git repo to avoid conflicts
if [ -d "${INSTALL_DIR}/.git" ]; then
say "Removing existing git repository for archive-based update"
$SUDO rm -rf "${INSTALL_DIR}/.git"
fi
TMP=$(mktemp -t finx-archive-XXXX)
curl -fsSL "${ARCHIVE_URL}" -o "${TMP}"
if file -b "${TMP}" | grep -qi 'gzip\|tar'; then
ensure_tool tar
tar -xzf "${TMP}" -C "${INSTALL_DIR}" --strip-components=1
else
ensure_tool unzip
unzip -q "${TMP}" -d "${INSTALL_DIR}"
# Flatten if needed
local entries first
entries=$(ls -1A "${INSTALL_DIR}" | wc -l | tr -d ' ')
if [ "${entries}" = "1" ]; then
first=$(ls -1A "${INSTALL_DIR}")
if [ -d "${INSTALL_DIR}/${first}" ]; then
say "Flattening extracted directory ${first}"
$SUDO bash -lc "shopt -s dotglob; mv '${INSTALL_DIR}/${first}'/* '${INSTALL_DIR}/'"
$SUDO rm -rf "${INSTALL_DIR}/${first}"
fi
fi
fi
rm -f "${TMP}"
APP_DIR="${INSTALL_DIR}"
ok "Using APP_DIR=${APP_DIR} (from release archive ${REF})"
return 0
fi
ensure_tool git
say "Preparing FinX repo in ${INSTALL_DIR}${REF:+ (ref: ${REF})}"
if [ -d "${INSTALL_DIR}/.git" ]; then
ok "Existing git repo found at ${INSTALL_DIR}"
APP_DIR="${INSTALL_DIR}"
# Update flow (no tag heuristics):
# - If a release REF was detected earlier, checkout that ref; else hard reset to origin/main
# Run all git commands as the app user to avoid 'dubious ownership'
if [ -n "${REF}" ] && [ -z "${ARCHIVE_URL}" ]; then
say "Updating repository to release ref: ${REF}"
if [ "${CREATE_USER}" = "y" ]; then
run_as_user_in_dir "${APP_USER}" "${INSTALL_DIR}" git fetch --all --prune || warn "git fetch failed in ${INSTALL_DIR}"
run_as_user_in_dir "${APP_USER}" "${INSTALL_DIR}" git checkout -q "${REF}" || warn "Checkout of ${REF} failed; repository remains unchanged"
else
(
cd "${INSTALL_DIR}" && \
git fetch --all --prune && \
git checkout -q "${REF}"
) || warn "Checkout of ${REF} failed; repository remains unchanged"
fi
else
say "Updating repository to main branch"
if [ "${CREATE_USER}" = "y" ]; then
# Try fetching specific branch first, fallback to generic fetch
run_as_user_in_dir "${APP_USER}" "${INSTALL_DIR}" git fetch origin main || run_as_user_in_dir "${APP_USER}" "${INSTALL_DIR}" git fetch origin || true
run_as_user_in_dir "${APP_USER}" "${INSTALL_DIR}" git checkout -q main || true
run_as_user_in_dir "${APP_USER}" "${INSTALL_DIR}" git reset --hard origin/main || true
else
(
cd "${INSTALL_DIR}" && \
git fetch origin main || git fetch origin && \
git checkout -q main || true && \
git reset --hard origin/main || true
) || warn "Update to main branch encountered issues; repository may be unchanged"
fi
fi
return 0
fi
if [ -n "${REF}" ] && [ -z "${ARCHIVE_URL}" ]; then
# Clone and checkout specific ref (branch or commit)
if [ "${CREATE_USER}" = "y" ]; then
run_as_user "${APP_USER}" git clone "${REPO_URL}" "${INSTALL_DIR}"
run_as_user_in_dir "${APP_USER}" "${INSTALL_DIR}" git checkout "${REF}"
else
git clone "${REPO_URL}" "${INSTALL_DIR}"
( cd "${INSTALL_DIR}" && git checkout "${REF}" )
fi
else
# Prefer cloning 'main'; if it fails, fall back to provider default branch
if [ "${CREATE_USER}" = "y" ]; then
if ! run_as_user "${APP_USER}" git clone --depth 1 -b main "${REPO_URL}" "${INSTALL_DIR}" 2>/dev/null; then
warn "Branch 'main' not found; cloning default branch"
run_as_user "${APP_USER}" git clone --depth 1 "${REPO_URL}" "${INSTALL_DIR}"
fi
else
if ! git clone --depth 1 -b main "${REPO_URL}" "${INSTALL_DIR}" 2>/dev/null; then
warn "Branch 'main' not found; cloning default branch"
git clone --depth 1 "${REPO_URL}" "${INSTALL_DIR}"
fi
fi
fi
APP_DIR="${INSTALL_DIR}"
ok "Using APP_DIR=${APP_DIR}"
}
prompt_inputs() {
say "Welcome to FinX installer"
read -rp "Domain to serve frontend (blank for localhost): " DOMAIN || true
read -rp "Backend port [5000]: " PORT || true; PORT=${PORT:-5000}
read -rp "Create a dedicated system user to run FinX? [Y/n]: " CREATE_USER || true
CREATE_USER=$(echo "${CREATE_USER:-Y}" | tr '[:upper:]' '[:lower:]')
if [ "${CREATE_USER}" = "y" ]; then
read -rp "System username [${APP_USER_DEFAULT}]: " APP_USER || true; APP_USER=${APP_USER:-$APP_USER_DEFAULT}
else
APP_USER=$(whoami)
fi
# Detect existing web servers
local has_nginx has_apache
if is_pkg_installed nginx; then has_nginx=y; else has_nginx=n; fi
if is_pkg_installed apache2; then has_apache=y; else has_apache=n; fi
say "Web server detection: Nginx=${has_nginx} Apache=${has_apache}"
if [ "$has_nginx" = "y" ] && [ "$has_apache" = "y" ]; then
echo "Choose web server to use:"
echo " 1) Nginx (recommended)"
echo " 2) Apache"
echo " 3) None"
read -rp "Selection [1]: " WS_CHOICE || true; WS_CHOICE=${WS_CHOICE:-1}
case "$WS_CHOICE" in
1) WEB_SERVER=nginx ;;
2) WEB_SERVER=apache ;;
*) WEB_SERVER=none ;;
esac
elif [ "$has_nginx" = "y" ]; then
read -rp "Use existing Nginx to serve the frontend? [Y/n]: " ANS || true; ANS=$(echo "${ANS:-Y}" | tr '[:upper:]' '[:lower:]')
WEB_SERVER=$([ "$ANS" = "n" ] && echo none || echo nginx)
elif [ "$has_apache" = "y" ]; then
read -rp "Use existing Apache to serve the frontend? [Y/n]: " ANS || true; ANS=$(echo "${ANS:-Y}" | tr '[:upper:]' '[:lower:]')
WEB_SERVER=$([ "$ANS" = "n" ] && echo none || echo apache)
else
echo "No web server found. Install one?"
echo " 1) Install Apache (recommended)"
echo " 2) Install Nginx"
echo " 3) None (I'll serve the build myself)"
read -rp "Selection [1]: " WS_CHOICE || true; WS_CHOICE=${WS_CHOICE:-1}
case "$WS_CHOICE" in
1) WEB_SERVER=apache ;;
2) WEB_SERVER=nginx ;;
*) WEB_SERVER=none ;;
esac
fi
read -rp "Database name [finx_db]: " DB_NAME || true; DB_NAME=${DB_NAME:-finx_db}
read -rp "Database user [finx_user]: " DB_USER || true; DB_USER=${DB_USER:-finx_user}
# Redis caching (optional - improves dashboard performance)
echo ""
say "Redis Cache (Optional - improves dashboard performance)"
read -rp "Install Redis for caching? [y/N]: " INSTALL_REDIS || true
INSTALL_REDIS=$(echo "${INSTALL_REDIS:-N}" | tr '[:upper:]' '[:lower:]')
}
create_user_if_needed() {
if [ "${CREATE_USER}" = "y" ]; then
if id -u "$APP_USER" >/dev/null 2>&1; then
ok "User $APP_USER exists"
else
say "Creating system user $APP_USER"
$SUDO useradd -r -m -d "/home/${APP_USER}" -s /usr/sbin/nologin "$APP_USER"
fi
$SUDO chown -R "$APP_USER":"$APP_USER" "$APP_DIR"
fi
}
create_db() {
say "Ensuring database and user exist"
local DB_PASS
if run_as_user postgres psql -Atqc "SELECT 1 FROM pg_roles WHERE rolname='${DB_USER}'" | grep -q 1; then
ok "DB user ${DB_USER} exists"
else
DB_PASS="$(generate_secret)"
run_as_user postgres psql -v ON_ERROR_STOP=1 <<SQL
CREATE USER ${DB_USER} WITH ENCRYPTED PASSWORD '${DB_PASS}';
SQL
ok "Created DB user ${DB_USER}"
CREATED_DB_USER="y"
fi
if run_as_user postgres psql -Atqc "SELECT 1 FROM pg_database WHERE datname='${DB_NAME}'" | grep -q 1; then
ok "DB ${DB_NAME} exists"
else
run_as_user postgres psql -v ON_ERROR_STOP=1 <<SQL
CREATE DATABASE ${DB_NAME} OWNER ${DB_USER};
GRANT ALL PRIVILEGES ON DATABASE ${DB_NAME} TO ${DB_USER};
SQL
ok "Created DB ${DB_NAME}"
CREATED_DB="y"
fi
# Fetch password for existing user if needed
if [ -z "${DB_PASS:-}" ]; then
# Can't query pg to retrieve password; if ENV exists, we'll use that; else prompt
if [ -f "$ENV_FILE" ]; then
# shellcheck disable=SC1090
source "$ENV_FILE"
DB_PASS="$DB_PASSWORD"
fi
if [ -z "${DB_PASS:-}" ]; then
read -rsp "Enter password for existing DB user ${DB_USER}: " DB_PASS; echo
fi
fi
export DB_PASSWORD="$DB_PASS"
}
create_env() {
# If env already exists in update mode, keep it
if [ "$UPDATE_MODE" = "y" ] && [ -f "$ENV_FILE" ]; then
ok "Keeping existing environment file at ${ENV_FILE}"
return 0
fi
say "Generating environment file at ${ENV_FILE}"
# Build CORS_ORIGIN list locally (avoid relying on outer scope with set -u)
local ORIGINS=""
if [ -n "${DOMAIN:-}" ]; then
# Use provided domain (both schemes), plus port-qualified HTTP if non-standard port
ORIGINS="https://${DOMAIN},http://${DOMAIN}"
if [ -n "${FRONTEND_PORT:-}" ] && [ "${FRONTEND_PORT}" != "80" ] && [ "${FRONTEND_PORT}" != "443" ]; then
ORIGINS="${ORIGINS},http://${DOMAIN}:${FRONTEND_PORT}"
fi
else
# No domain provided: default to localhost/loopback with the chosen port
if [ -n "${FRONTEND_PORT:-}" ]; then
if [ "${FRONTEND_PORT}" = "80" ]; then
ORIGINS="http://localhost,http://127.0.0.1"
elif [ "${FRONTEND_PORT}" = "443" ]; then
ORIGINS="https://localhost,https://127.0.0.1"
else
ORIGINS="http://localhost:${FRONTEND_PORT},http://127.0.0.1:${FRONTEND_PORT}"
fi
else
# Fallback if port is not set for some reason
ORIGINS="http://localhost,http://127.0.0.1"
fi
fi
local JWT_SECRET
JWT_SECRET=$(generate_secret)
$SUDO mkdir -p "$ENV_DIR"
$SUDO bash -c "cat > '${ENV_FILE}'" <<EOF
# FinX environment
NODE_ENV=production
PORT=${PORT}
# Database
DB_HOST=127.0.0.1
DB_PORT=5432
DB_NAME=${DB_NAME}
DB_USER=${DB_USER}
DB_PASSWORD=${DB_PASSWORD}
# Security
JWT_SECRET=${JWT_SECRET}
# CORS
CORS_ORIGIN=${ORIGINS}
# Feature flags
DISABLE_REGISTRATION=true
EOF
# Add Redis URL if Redis was installed
if [ "${INSTALL_REDIS:-n}" = "y" ]; then
$SUDO bash -c "echo '' >> '${ENV_FILE}'"
$SUDO bash -c "echo '# Redis Cache (optional - falls back to in-memory if unavailable)' >> '${ENV_FILE}'"
$SUDO bash -c "echo 'REDIS_URL=redis://127.0.0.1:6379' >> '${ENV_FILE}'"
fi
$SUDO chmod 640 "$ENV_FILE"
if [ "${CREATE_USER}" = "y" ]; then
$SUDO chown ${APP_USER}:root "$ENV_FILE"
fi
ok "Wrote ${ENV_FILE}"
CREATED_ENV_FILE="y"
}
install_dependencies() {
say "Installing backend dependencies"
if [ "${CREATE_USER}" = "y" ]; then
(
if [ -f "$APP_DIR/package-lock.json" ] || [ -f "$APP_DIR/npm-shrinkwrap.json" ]; then
run_as_user_in_dir "$APP_USER" "$APP_DIR" npm ci --omit=dev
else
run_as_user_in_dir "$APP_USER" "$APP_DIR" npm install --omit=dev
fi
)
else
(
if [ -f "$APP_DIR/package-lock.json" ] || [ -f "$APP_DIR/npm-shrinkwrap.json" ]; then
( cd "$APP_DIR" && npm ci --omit=dev )
else
( cd "$APP_DIR" && npm install --omit=dev )
fi
)
fi
say "Installing frontend dependencies and building"
if [ "${CREATE_USER}" = "y" ]; then
(
if [ -f "$APP_DIR/frontend/package-lock.json" ] || [ -f "$APP_DIR/frontend/npm-shrinkwrap.json" ]; then
run_as_user_in_dir "$APP_USER" "$APP_DIR/frontend" npm ci
else
run_as_user_in_dir "$APP_USER" "$APP_DIR/frontend" npm install
fi && \
# Ensure previous build output is removed to avoid stale precache entries
run_as_user_in_dir "$APP_USER" "$APP_DIR/frontend" bash -lc 'rm -rf build' && \
run_as_user_in_dir "$APP_USER" "$APP_DIR/frontend" npm run build && \
# Ensure icons and logos exist in build output (robust against toolchain changes)
run_as_user_in_dir "$APP_USER" "$APP_DIR/frontend" bash -lc '[ -d public/icons ] && mkdir -p build/icons && cp -rn public/icons/* build/icons/ || true; [ -d public/logos ] && mkdir -p build/logos && cp -rn public/logos/* build/logos/ || true; [ -f public/icons/favicon.ico ] && cp -n public/icons/favicon.ico build/icons/favicon.ico || true'
)
else
(
cd "$APP_DIR/frontend" && \
if [ -f package-lock.json ] || [ -f npm-shrinkwrap.json ]; then
npm ci
else
npm install
fi && \
# Ensure previous build output is removed to avoid stale precache entries
bash -lc 'rm -rf build' && \
npm run build && \
# Ensure icons and logos exist in build output (robust against toolchain changes)
bash -lc '[ -d public/icons ] && mkdir -p build/icons && cp -rn public/icons/* build/icons/ || true; [ -d public/logos ] && mkdir -p build/logos && cp -rn public/logos/* build/logos/ || true; [ -f public/icons/favicon.ico ] && cp -n public/icons/favicon.ico build/icons/favicon.ico || true'
)
fi
ok "Dependencies installed and frontend built"
}
run_migrations() {
say "Running database migrations"
# shellcheck disable=SC1090
set -a; source "$ENV_FILE"; set +a
# If repo has no migrations directory or contains no .sql files, skip gracefully
local MIG_DIR
MIG_DIR="${APP_DIR}/database/migrations"
if [ ! -d "$MIG_DIR" ] || ! ls -1 "$MIG_DIR"/*.sql >/dev/null 2>&1; then
echo "No migrations directory or SQL files found at $MIG_DIR; skipping."
return 0
fi
if [ "${CREATE_USER}" = "y" ]; then
(
run_as_user_in_dir "$APP_USER" "$APP_DIR" env $(grep -E '^[A-Z_]+=' "$ENV_FILE" | xargs) npm run migrate-db
)
else
(
cd "$APP_DIR" && \
env $(grep -E '^[A-Z_]+=' "$ENV_FILE" | xargs) npm run migrate-db
)
fi
ok "Migrations applied"
}
create_systemd_service() {
say "Creating systemd service ${SERVICE_FILE}"
local RUN_USER RUN_GROUP
if [ "${CREATE_USER}" = "y" ]; then
RUN_USER="$APP_USER"; RUN_GROUP="$APP_USER"
else
RUN_USER="$(whoami)"; RUN_GROUP="$(id -gn)"
fi
$SUDO bash -c "cat > '${SERVICE_FILE}'" <<EOF
[Unit]
Description=FinX API Service
After=network.target postgresql.service
[Service]
Type=simple
WorkingDirectory=${APP_DIR}
EnvironmentFile=${ENV_FILE}
ExecStart=$(command -v node) ${APP_DIR}/server.js
Restart=always
RestartSec=3
User=${RUN_USER}
Group=${RUN_GROUP}
AmbientCapabilities=CAP_NET_BIND_SERVICE
[Install]
WantedBy=multi-user.target
EOF
$SUDO systemctl daemon-reload
$SUDO systemctl enable "${APP_NAME}.service"
ok "Systemd service installed"
CREATED_SERVICE="y"
}
create_nginx_site() {
[ "${WEB_SERVER}" = "nginx" ] || return 0
# Skip if site already exists (update mode)
if [ -f "${NGINX_SITE_AVAILABLE}" ]; then
ok "Nginx site already present; skipping reconfiguration"
return 0
fi
say "Configuring Nginx site"
local SERVER_NAME
SERVER_NAME=${DOMAIN:-_}
# Ensure a frontend port is chosen
if [ -z "${FRONTEND_PORT}" ]; then
choose_frontend_port 3000
fi
$SUDO bash -c "cat > '${NGINX_SITE_AVAILABLE}'" <<'EOF'
server {
listen __WEB_PORT__;
server_name __SERVER_NAME__;
# Serve frontend build
root __APP_DIR__/frontend/build;
index index.html;
charset utf-8;
# Gzip + cache static assets
gzip on;
gzip_types text/plain text/css application/javascript application/json image/svg+xml application/font-woff2;
gzip_min_length 1024;
location /assets/ {
expires 30d;
add_header Cache-Control "public, max-age=2592000, immutable";
try_files $uri =404;
}
# Icon and logo directories
location /icons/ {
expires 365d;
add_header Cache-Control "public, max-age=31536000, immutable";
try_files $uri =404;
}
location /logos/ {
expires 365d;
add_header Cache-Control "public, max-age=31536000, immutable";
try_files $uri =404;
}
# Favicon root path
location = /favicon.ico {
expires 365d;
add_header Cache-Control "public, max-age=31536000, immutable";
try_files $uri /icons/favicon.ico =404;
}
# PWA files
location = /manifest.webmanifest {
# Scope MIME override to this location only
types { }
default_type application/manifest+json;
add_header Cache-Control "no-cache, no-store, must-revalidate";
expires -1;
try_files $uri /manifest.webmanifest =404;
}
# Hashed manifest emitted by VitePWA lives under /assets
location ~ ^/assets/.*\.webmanifest$ {
types { }
default_type application/manifest+json;
add_header Cache-Control "no-cache, no-store, must-revalidate";
expires -1;
try_files $uri =404;
}
# Service worker should never be cached aggressively to allow instant updates
location = /sw.js {
add_header Cache-Control "no-cache, no-store, must-revalidate";
expires -1;
try_files $uri /sw.js =404;
}
location ~ ^/workbox-.*\.js$ {
add_header Cache-Control "no-cache, no-store, must-revalidate";
expires -1;
try_files $uri =404;
}
# API proxy
location /api/ {
proxy_pass http://127.0.0.1:__PORT__;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 60s;
}
# WebSockets (Socket.IO)
location /socket.io/ {
proxy_pass http://127.0.0.1:__PORT__;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_read_timeout 60s;
}