-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·1240 lines (1169 loc) · 48.3 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·1240 lines (1169 loc) · 48.3 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 -Eeuo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SOURCE_ROOT="${GOST_MANAGER_SOURCE_ROOT:-${SCRIPT_DIR}}"
GOST_MANAGER_ROOT="${GOST_MANAGER_ROOT:-}"
GOST_MANAGER_TESTING="${GOST_MANAGER_TESTING:-0}"
GOST_MANAGER_INSTALL_DEPS="${GOST_MANAGER_INSTALL_DEPS:-0}"
GOST_MANAGER_ALLOW_TEST_PACKAGE_STUB="${GOST_MANAGER_ALLOW_TEST_PACKAGE_STUB:-0}"
GOST_MANAGER_TEST_MISSING_COMMANDS="${GOST_MANAGER_TEST_MISSING_COMMANDS:-}"
GOST_MANAGER_TEST_DEP_BIN="${GOST_MANAGER_TEST_DEP_BIN:-}"
GOST_MANAGER_FAIL_PHASE="${GOST_MANAGER_FAIL_PHASE:-}"
GOST_MANAGER_INSTALLED_VERSION_OVERRIDE_TEST="${GOST_MANAGER_INSTALLED_VERSION_OVERRIDE_TEST:-}"
SYSTEMCTL_BIN="${SYSTEMCTL_BIN:-systemctl}"
SYSTEMD_ANALYZE_BIN="${SYSTEMD_ANALYZE_BIN:-systemd-analyze}"
PYTHON_BIN="${PYTHON_BIN:-python3}"
INSTALL_BIN="${INSTALL_BIN:-install}"
CP_BIN="${CP_BIN:-cp}"
MV_BIN="${MV_BIN:-mv}"
RM_BIN="${RM_BIN:-rm}"
CHMOD_BIN="${CHMOD_BIN:-chmod}"
CHOWN_BIN="${CHOWN_BIN:-chown}"
SYNC_BIN="${SYNC_BIN:-sync}"
CMP_BIN="${CMP_BIN:-cmp}"
APT_GET_BIN="${APT_GET_BIN:-apt-get}"
MONITOR_SERVICE="gost-monitor-collector.service"
WATCHDOG_SERVICE="gost-upstream-watchdog.service"
STAGE_DIR=""
ACTIVE_CONFIG_PATH=""
CONFIGURED_DB_PRODUCTION=""
CONFIGURED_DB_ACTUAL=""
CONFIGURED_ENV_PRODUCTION=""
CONFIGURED_ENV_ACTUAL=""
SOURCE_VERSION=""
INSTALL_COMMITTED=0
ROLLBACK_VERIFIED=0
SERVICE_STATE_RECORDED=0
UNIT_CHANGED=0
PREVIOUS_UNIT_EXISTED=0
PREVIOUS_ENABLED=0
PREVIOUS_ACTIVE=0
FILES_CHANGED=0
DATABASE_CREATED=0
SERVICE_ENABLE_ATTEMPTED=0
SERVICE_START_ATTEMPTED=0
WATCHDOG_UNIT_CHANGED=0
WATCHDOG_FILES_CHANGED=0
PREVIOUS_WATCHDOG_UNIT_EXISTED=0
PREVIOUS_WATCHDOG_ENABLED=0
PREVIOUS_WATCHDOG_ACTIVE=0
WATCHDOG_SERVICE_STATE_RECORDED=0
WATCHDOG_SERVICE_ENABLE_ATTEMPTED=0
WATCHDOG_SERVICE_START_ATTEMPTED=0
WATCHDOG_DATABASE_CREATED=0
declare -a RUNTIME_MANIFEST_ENTRIES=()
declare -a STAGED_MODULES=()
declare -a WATCHDOG_RUNTIME_MANIFEST_ENTRIES=()
declare -a STAGED_WATCHDOG_MODULES=()
declare -a CHANGED_DESTINATIONS=()
declare -a BACKUP_PATHS=()
declare -a BACKUP_CONTAINERS=()
declare -a CREATED_DIRECTORIES=()
declare -a PENDING_PATHS=()
declare -a METADATA_PATHS=()
declare -a METADATA_MODES=()
declare -a METADATA_UIDS=()
declare -a METADATA_GIDS=()
die() {
printf 'Error: %s\n' "$*" >&2
return 1
}
info() {
printf '%s\n' "$*"
}
path_for() {
local absolute="$1"
[[ "${absolute}" == /* ]] || die "managed path must be absolute: ${absolute}"
if [[ -n "${GOST_MANAGER_ROOT}" ]]; then
printf '%s%s\n' "${GOST_MANAGER_ROOT}" "${absolute}"
else
printf '%s\n' "${absolute}"
fi
}
inject_failure() {
local phase="$1"
if [[ "${GOST_MANAGER_FAIL_PHASE}" == "${phase}" ]]; then
die "injected installer failure at phase: ${phase}"
fi
}
require_safe_root() {
local resolved
if [[ "${GOST_MANAGER_TESTING}" == "1" ]]; then
[[ -n "${GOST_MANAGER_ROOT}" ]] || die "testing mode requires GOST_MANAGER_ROOT."
[[ "${GOST_MANAGER_ROOT}" == /* && "${GOST_MANAGER_ROOT}" != "/" ]] || die "testing root must be an absolute non-root path."
[[ -d "${GOST_MANAGER_ROOT}" && ! -L "${GOST_MANAGER_ROOT}" ]] || die "testing root must be an existing real directory."
resolved="$(cd "${GOST_MANAGER_ROOT}" && pwd -P)"
[[ "${resolved}" != "/" ]] || die "testing root may not resolve to /."
GOST_MANAGER_ROOT="${resolved}"
else
[[ -z "${GOST_MANAGER_ROOT}" ]] || die "GOST_MANAGER_ROOT is allowed only in testing mode."
[[ "${SOURCE_ROOT}" == "${SCRIPT_DIR}" ]] || die "GOST_MANAGER_SOURCE_ROOT is allowed only in testing mode."
[[ -z "${GOST_MANAGER_INSTALLED_VERSION_OVERRIDE_TEST}" ]] || die "GOST_MANAGER_INSTALLED_VERSION_OVERRIDE_TEST is allowed only in testing mode."
[[ "${EUID}" -eq 0 ]] || die "install.sh must be run as root. Try: sudo bash install.sh"
fi
[[ "${SOURCE_ROOT}" == /* && -d "${SOURCE_ROOT}" && ! -L "${SOURCE_ROOT}" ]] || die "installer source root must be a real absolute directory."
SOURCE_ROOT="$(cd "${SOURCE_ROOT}" && pwd -P)"
}
reject_symlink_path() {
local path="$1"
local boundary current relative part
local -a parts=()
boundary="${GOST_MANAGER_ROOT:-/}"
current="${boundary}"
relative="${path#"${boundary}"}"
IFS='/' read -r -a parts <<< "${relative#/}"
for part in "${parts[@]+"${parts[@]}"}"; do
[[ -n "${part}" ]] || continue
current="${current%/}/${part}"
[[ ! -L "${current}" ]] || die "managed path may not traverse a symlink: ${current}"
done
if [[ -n "${GOST_MANAGER_ROOT}" ]]; then
[[ "${path}" == "${GOST_MANAGER_ROOT}"/* ]] || die "managed path escaped the testing root: ${path}"
fi
}
parse_arguments() {
while [[ "$#" -gt 0 ]]; do
case "$1" in
--install-dependencies) GOST_MANAGER_INSTALL_DEPS=1 ;;
*) die "unknown installer option: $1" ;;
esac
shift
done
}
package_for_command() {
case "$1" in
bash) printf 'bash\n' ;;
python3) printf 'python3\n' ;;
systemctl|systemd-analyze) printf 'systemd\n' ;;
ss) printf 'iproute2\n' ;;
ping) printf 'iputils-ping\n' ;;
cmp) printf 'diffutils\n' ;;
grep) printf 'grep\n' ;;
install|cp|mv|rm|chmod|chown|sync|mktemp|stat) printf 'coreutils\n' ;;
*) die "no package mapping for required command: $1" ;;
esac
}
command_available() {
local command_name="$1"
local forced=",${GOST_MANAGER_TEST_MISSING_COMMANDS// /,},"
if [[ "${GOST_MANAGER_TESTING}" == "1" && "${forced}" == *",${command_name},"* ]]; then
[[ -n "${GOST_MANAGER_TEST_DEP_BIN}" && -x "${GOST_MANAGER_TEST_DEP_BIN}/${command_name}" ]]
return
fi
command -v "${command_name}" >/dev/null 2>&1
}
validate_dependencies() {
local command_name package existing
local -a required=(bash python3 systemctl systemd-analyze ss ping grep install cp mv rm chmod chown sync cmp mktemp stat)
local -a missing=()
local -a packages=()
for command_name in "${required[@]}"; do
if ! command_available "${command_name}"; then
missing+=("${command_name}")
package="$(package_for_command "${command_name}")"
existing=0
for candidate in "${packages[@]+"${packages[@]}"}"; do
if [[ "${candidate}" == "${package}" ]]; then
existing=1
break
fi
done
[[ "${existing}" == "1" ]] || packages+=("${package}")
fi
done
if [[ "${#missing[@]}" -eq 0 ]]; then
return 0
fi
info "Missing required commands: ${missing[*]}"
info "Suggested packages: ${packages[*]}"
[[ "${GOST_MANAGER_INSTALL_DEPS}" == "1" ]] || die "dependencies are missing; rerun with --install-dependencies to opt in."
if [[ "${GOST_MANAGER_TESTING}" == "1" && "${GOST_MANAGER_ALLOW_TEST_PACKAGE_STUB}" != "1" ]]; then
die "package installation is disabled in testing mode."
fi
command -v "${APT_GET_BIN}" >/dev/null 2>&1 || die "apt-get is required for dependency installation."
info "Installing only: ${packages[*]}"
"${APT_GET_BIN}" update
"${APT_GET_BIN}" install -y "${packages[@]}"
for command_name in "${missing[@]}"; do
command_available "${command_name}" || die "dependency installation did not provide: ${command_name}"
done
}
validate_ping_capability() {
local ping_binary
ping_binary="$(command -v ping)" || die "ping is required after dependency validation."
if ! "${ping_binary}" -n -c 1 -W 1 -- 127.0.0.1 >/dev/null 2>&1; then
die "installed ping cannot execute the supported local capability probe."
fi
}
manifest_contains() {
local expected="$1"
local entry
for entry in "${RUNTIME_MANIFEST_ENTRIES[@]+"${RUNTIME_MANIFEST_ENTRIES[@]}"}"; do
[[ "${entry}" == "${expected}" ]] && return 0
done
return 1
}
validate_source_manifest() {
local path entry existing
local manifest="${SOURCE_ROOT}/packaging/monitoring-runtime-manifest.txt"
local watchdog_manifest="${SOURCE_ROOT}/packaging/watchdog-runtime-manifest.txt"
local -a fixed=(
"VERSION"
"gost-manager.sh"
"lib/gost-run-iran.sh"
"lib/gost-run-kharej.sh"
"packaging/gost-monitor"
"packaging/gost-monitor-admin"
"packaging/gost-monitor-collector"
"packaging/gost-monitor-collector.service"
"packaging/monitoring.env"
"packaging/monitoring-runtime-manifest.txt"
"packaging/gost-upstream-watchdog"
"packaging/gost-watchdog-admin"
"packaging/gost-upstream-watchdog.service"
"packaging/watchdog.conf"
"packaging/watchdog-runtime-manifest.txt"
)
for path in "${fixed[@]}"; do
[[ -f "${SOURCE_ROOT}/${path}" && ! -L "${SOURCE_ROOT}/${path}" ]] || die "required source file is missing or unsafe: ${path}"
done
SOURCE_VERSION="$(< "${SOURCE_ROOT}/VERSION")"
[[ "${SOURCE_VERSION}" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] || die "VERSION must contain a semantic version such as 2.0.0."
[[ -f "${manifest}" && ! -L "${manifest}" ]] || die "runtime manifest must be a regular non-symlink file."
RUNTIME_MANIFEST_ENTRIES=()
while IFS= read -r entry || [[ -n "${entry}" ]]; do
[[ -n "${entry}" ]] || die "runtime manifest contains a blank record."
[[ "${entry}" != /* && "${entry}" != *".."* ]] || die "runtime manifest contains an unsafe path: ${entry}"
[[ "${entry}" =~ ^monitoring/[A-Za-z_][A-Za-z0-9_]*\.py$ ]] || die "runtime manifest contains an invalid module: ${entry}"
existing=0
for path in "${RUNTIME_MANIFEST_ENTRIES[@]+"${RUNTIME_MANIFEST_ENTRIES[@]}"}"; do
if [[ "${path}" == "${entry}" ]]; then
existing=1
break
fi
done
[[ "${existing}" == "0" ]] || die "runtime manifest contains a duplicate module: ${entry}"
[[ -f "${SOURCE_ROOT}/${entry}" && ! -L "${SOURCE_ROOT}/${entry}" ]] || die "runtime module is missing, non-regular, or symlinked: ${entry}"
RUNTIME_MANIFEST_ENTRIES+=("${entry}")
done < "${manifest}"
[[ "${#RUNTIME_MANIFEST_ENTRIES[@]}" -gt 1 ]] || die "runtime manifest is incomplete."
for path in monitoring/__init__.py monitoring/admin_cli.py monitoring/gost_monitoring.py monitoring/query_cli.py monitoring/runtime_lock.py; do
manifest_contains "${path}" || die "runtime manifest omits required module: ${path}"
done
[[ -f "${watchdog_manifest}" && ! -L "${watchdog_manifest}" ]] || die "Watchdog runtime manifest must be a regular non-symlink file."
WATCHDOG_RUNTIME_MANIFEST_ENTRIES=()
while IFS= read -r entry || [[ -n "${entry}" ]]; do
[[ -n "${entry}" ]] || die "Watchdog runtime manifest contains a blank record."
[[ "${entry}" != /* && "${entry}" != *".."* ]] || die "Watchdog runtime manifest contains an unsafe path: ${entry}"
[[ "${entry}" =~ ^gost_watchdog/[A-Za-z_][A-Za-z0-9_]*\.py$ ]] || die "Watchdog runtime manifest contains an invalid module: ${entry}"
existing=0
for path in "${WATCHDOG_RUNTIME_MANIFEST_ENTRIES[@]+"${WATCHDOG_RUNTIME_MANIFEST_ENTRIES[@]}"}"; do
[[ "${path}" != "${entry}" ]] || existing=1
done
[[ "${existing}" == "0" ]] || die "Watchdog runtime manifest contains a duplicate module: ${entry}"
[[ -f "${SOURCE_ROOT}/${entry}" && ! -L "${SOURCE_ROOT}/${entry}" ]] || die "Watchdog runtime module is missing, non-regular, or symlinked: ${entry}"
WATCHDOG_RUNTIME_MANIFEST_ENTRIES+=("${entry}")
done < "${watchdog_manifest}"
[[ "${#WATCHDOG_RUNTIME_MANIFEST_ENTRIES[@]}" -gt 1 ]] || die "Watchdog runtime manifest is incomplete."
for path in gost_watchdog/__init__.py gost_watchdog/admin_cli.py gost_watchdog/daemon.py gost_watchdog/engine.py gost_watchdog/storage.py; do
existing=0
for entry in "${WATCHDOG_RUNTIME_MANIFEST_ENTRIES[@]}"; do
[[ "${entry}" != "${path}" ]] || existing=1
done
[[ "${existing}" == "1" ]] || die "Watchdog runtime manifest omits required module: ${path}"
done
}
stage_sources() {
local entry destination
STAGE_DIR="$(mktemp -d "${TMPDIR:-/tmp}/gost-manager-install.XXXXXX")"
"${CHMOD_BIN}" 700 "${STAGE_DIR}"
"${INSTALL_BIN}" -d -m 755 "${STAGE_DIR}/lib" "${STAGE_DIR}/monitoring" "${STAGE_DIR}/gost_watchdog" "${STAGE_DIR}/sbin"
"${CP_BIN}" "${SOURCE_ROOT}/VERSION" "${STAGE_DIR}/VERSION"
"${CP_BIN}" "${SOURCE_ROOT}/gost-manager.sh" "${STAGE_DIR}/gost-manager"
"${CP_BIN}" "${SOURCE_ROOT}/lib/gost-run-iran.sh" "${STAGE_DIR}/lib/gost-run-iran.sh"
"${CP_BIN}" "${SOURCE_ROOT}/lib/gost-run-kharej.sh" "${STAGE_DIR}/lib/gost-run-kharej.sh"
STAGED_MODULES=()
for entry in "${RUNTIME_MANIFEST_ENTRIES[@]}"; do
destination="${STAGE_DIR}/monitoring/${entry##*/}"
"${CP_BIN}" "${SOURCE_ROOT}/${entry}" "${destination}"
STAGED_MODULES+=("${destination}")
done
STAGED_WATCHDOG_MODULES=()
for entry in "${WATCHDOG_RUNTIME_MANIFEST_ENTRIES[@]}"; do
destination="${STAGE_DIR}/gost_watchdog/${entry##*/}"
"${CP_BIN}" "${SOURCE_ROOT}/${entry}" "${destination}"
STAGED_WATCHDOG_MODULES+=("${destination}")
done
"${CP_BIN}" "${SOURCE_ROOT}/packaging/gost-monitor" "${STAGE_DIR}/sbin/gost-monitor"
"${CP_BIN}" "${SOURCE_ROOT}/packaging/gost-monitor-admin" "${STAGE_DIR}/sbin/gost-monitor-admin"
"${CP_BIN}" "${SOURCE_ROOT}/packaging/gost-monitor-collector" "${STAGE_DIR}/sbin/gost-monitor-collector"
"${CP_BIN}" "${SOURCE_ROOT}/packaging/gost-monitor-collector.service" "${STAGE_DIR}/gost-monitor-collector.service"
"${CP_BIN}" "${SOURCE_ROOT}/packaging/monitoring.env" "${STAGE_DIR}/monitoring.env"
"${CP_BIN}" "${SOURCE_ROOT}/packaging/gost-upstream-watchdog" "${STAGE_DIR}/sbin/gost-upstream-watchdog"
"${CP_BIN}" "${SOURCE_ROOT}/packaging/gost-watchdog-admin" "${STAGE_DIR}/sbin/gost-watchdog-admin"
"${CP_BIN}" "${SOURCE_ROOT}/packaging/gost-upstream-watchdog.service" "${STAGE_DIR}/gost-upstream-watchdog.service"
"${CP_BIN}" "${SOURCE_ROOT}/packaging/watchdog.conf" "${STAGE_DIR}/watchdog.conf"
}
validate_staged_bash() {
bash -n \
"${STAGE_DIR}/gost-manager" \
"${STAGE_DIR}/lib/gost-run-iran.sh" \
"${STAGE_DIR}/lib/gost-run-kharej.sh" \
"${STAGE_DIR}/sbin/gost-monitor" \
"${STAGE_DIR}/sbin/gost-monitor-admin" \
"${STAGE_DIR}/sbin/gost-monitor-collector" \
"${STAGE_DIR}/sbin/gost-upstream-watchdog" \
"${STAGE_DIR}/sbin/gost-watchdog-admin"
}
validate_staged_python() {
PYTHONPYCACHEPREFIX="${STAGE_DIR}/pycache" "${PYTHON_BIN}" -m py_compile "${STAGED_MODULES[@]}" "${STAGED_WATCHDOG_MODULES[@]}"
}
admin_config_command() {
local config_path="$1"
shift
local -a root_args=()
if [[ -n "${GOST_MANAGER_ROOT}" ]]; then
root_args=(--path-root "${GOST_MANAGER_ROOT}")
fi
if [[ "${#root_args[@]}" -gt 0 ]]; then
PYTHONPATH="${STAGE_DIR}" "${PYTHON_BIN}" -m monitoring.admin_cli \
--policy installed "${root_args[@]}" "$@" --config "${config_path}"
else
PYTHONPATH="${STAGE_DIR}" "${PYTHON_BIN}" -m monitoring.admin_cli \
--policy installed "$@" --config "${config_path}"
fi
}
validate_config_file() {
local config_path="$1"
[[ ! -L "${config_path}" ]] || die "monitoring config may not be a symlink."
admin_config_command "${config_path}" validate-config >/dev/null
}
config_value() {
local config_path="$1"
local field="$2"
admin_config_command "${config_path}" config --format value --field "${field}"
}
validate_staged_config() {
local installed_config
validate_config_file "${STAGE_DIR}/monitoring.env"
installed_config="$(path_for /etc/gost-manager/monitoring.env)"
ACTIVE_CONFIG_PATH="${STAGE_DIR}/monitoring.env"
if [[ -e "${installed_config}" || -L "${installed_config}" ]]; then
reject_symlink_path "${installed_config}"
if ! validate_config_file "${installed_config}"; then
die "existing monitoring config is incompatible with the installed policy; move its database below /var/lib/gost-manager and env directory below /etc/gost before retrying."
fi
ACTIVE_CONFIG_PATH="${installed_config}"
fi
CONFIGURED_DB_PRODUCTION="$(config_value "${ACTIVE_CONFIG_PATH}" database_path)"
CONFIGURED_ENV_PRODUCTION="$(config_value "${ACTIVE_CONFIG_PATH}" env_directory)"
CONFIGURED_DB_ACTUAL="$(path_for "${CONFIGURED_DB_PRODUCTION}")"
CONFIGURED_ENV_ACTUAL="$(path_for "${CONFIGURED_ENV_PRODUCTION}")"
reject_symlink_path "${CONFIGURED_DB_ACTUAL}"
reject_symlink_path "${CONFIGURED_ENV_ACTUAL}"
}
watchdog_admin_command() {
local -a root_args=()
if [[ -n "${GOST_MANAGER_ROOT}" ]]; then
root_args=(--path-root "${GOST_MANAGER_ROOT}")
fi
PYTHONPATH="${STAGE_DIR}" "${PYTHON_BIN}" -m gost_watchdog.admin_cli \
--policy installed "${root_args[@]+"${root_args[@]}"}" "$@"
}
validate_staged_watchdog_config() {
local installed_config
[[ -f "${STAGE_DIR}/watchdog.conf" && ! -L "${STAGE_DIR}/watchdog.conf" ]] || die "staged Watchdog config is missing or unsafe."
PYTHONPATH="${STAGE_DIR}" "${PYTHON_BIN}" -c \
'import sys; from pathlib import Path; from gost_watchdog.config import parse_global_config; parse_global_config(Path(sys.argv[1]).read_text(encoding="ascii"))' \
"${STAGE_DIR}/watchdog.conf"
installed_config="$(path_for /etc/gost-manager/watchdog.conf)"
if [[ -e "${installed_config}" || -L "${installed_config}" ]]; then
reject_symlink_path "${installed_config}"
watchdog_admin_command validate-config >/dev/null || die "existing Watchdog global config is invalid or unsafe."
fi
}
validate_unit_content() {
local unit="${STAGE_DIR}/gost-monitor-collector.service"
local forbidden required verification_dir verification_unit output
for forbidden in 'Requires=' 'PartOf=' 'BindsTo=' 'PrivateNetwork=' 'ProtectProc=' 'ProcSubset=' 'InaccessiblePaths=/proc' 'gost-iran-' 'gost-kharej-'; do
if grep -Fq "${forbidden}" "${unit}"; then
die "monitoring unit contains forbidden setting: ${forbidden}"
fi
done
for required in \
'EnvironmentFile=/etc/gost-manager/monitoring.env' \
'ExecStartPre=/usr/local/sbin/gost-monitor-admin validate-config --config /etc/gost-manager/monitoring.env' \
'ExecStart=/usr/local/sbin/gost-monitor-collector --daemon' \
'Restart=on-failure' 'UMask=0077' 'StateDirectoryMode=0700' \
'RuntimeDirectory=gost-manager' 'RuntimeDirectoryMode=0700' \
'ReadWritePaths=/var/lib/gost-manager'; do
grep -Fq "${required}" "${unit}" || die "monitoring unit lacks required production setting: ${required}"
done
if ! command -v "${SYSTEMD_ANALYZE_BIN}" >/dev/null 2>&1; then
info "systemd-analyze unavailable; deterministic unit validation passed."
return 0
fi
verification_dir="$(mktemp -d "${STAGE_DIR}/systemd-verify.XXXXXX")"
verification_unit="${verification_dir}/${MONITOR_SERVICE}"
"${CP_BIN}" "${unit}" "${verification_unit}"
"${CP_BIN}" "${STAGE_DIR}/monitoring.env" "${verification_dir}/monitoring.env"
printf '#!/usr/bin/env bash\nexit 0\n' > "${verification_dir}/gost-monitor-admin"
printf '#!/usr/bin/env bash\nexit 0\n' > "${verification_dir}/gost-monitor-collector"
"${CHMOD_BIN}" 755 "${verification_dir}/gost-monitor-admin" "${verification_dir}/gost-monitor-collector"
"${PYTHON_BIN}" -c \
'import sys; from pathlib import Path; p=Path(sys.argv[1]); s=p.read_text(); s=s.replace("/usr/local/sbin/gost-monitor-admin", sys.argv[2]).replace("/usr/local/sbin/gost-monitor-collector", sys.argv[3]).replace("/etc/gost-manager/monitoring.env", sys.argv[4]); p.write_text(s)' \
"${verification_unit}" \
"${verification_dir}/gost-monitor-admin" \
"${verification_dir}/gost-monitor-collector" \
"${verification_dir}/monitoring.env"
if ! output="$("${SYSTEMD_ANALYZE_BIN}" verify "${verification_unit}" 2>&1)"; then
[[ -z "${output}" ]] || printf '%s\n' "${output}" >&2
die "real host systemd unit verification failed."
fi
if [[ -n "${output}" ]]; then
case "${output}" in
*"${MONITOR_SERVICE}"*|*"${verification_dir}"*)
printf '%s\n' "${output}" >&2
die "real host systemd unit verification emitted candidate warnings."
;;
*)
info "systemd-analyze reported unrelated host-unit diagnostics; the staged monitoring unit passed."
;;
esac
fi
}
validate_watchdog_unit_content() {
local unit="${STAGE_DIR}/gost-upstream-watchdog.service"
local forbidden required verification_dir verification_unit output
for forbidden in 'Requires=' 'PartOf=' 'BindsTo=' 'gost-iran-' 'gost-kharej-' 'PrivateNetwork='; do
if grep -Fq "${forbidden}" "${unit}"; then
die "Watchdog unit contains forbidden setting: ${forbidden}"
fi
done
for required in \
'Wants=network-online.target' \
'After=network-online.target' \
'ExecStartPre=/usr/local/sbin/gost-watchdog-admin validate-config' \
'ExecStart=/usr/local/sbin/gost-upstream-watchdog' \
'Restart=on-failure' 'RestartSec=5' 'UMask=0077' \
'WorkingDirectory=/var/lib/gost-manager/watchdog' \
'ReadWritePaths=/var/lib/gost-manager/watchdog' \
'LimitNOFILE=4096' 'ProtectSystem=strict'; do
grep -Fq "${required}" "${unit}" || die "Watchdog unit lacks required production setting: ${required}"
done
if ! command -v "${SYSTEMD_ANALYZE_BIN}" >/dev/null 2>&1; then
info "systemd-analyze unavailable; deterministic Watchdog unit validation passed."
return 0
fi
verification_dir="$(mktemp -d "${STAGE_DIR}/watchdog-systemd-verify.XXXXXX")"
verification_unit="${verification_dir}/${WATCHDOG_SERVICE}"
"${CP_BIN}" "${unit}" "${verification_unit}"
"${INSTALL_BIN}" -d -m 700 \
"${verification_dir}/state" "${verification_dir}/watchdog.d" \
"${verification_dir}/gost" "${verification_dir}/systemd"
"${CP_BIN}" "${STAGE_DIR}/watchdog.conf" "${verification_dir}/watchdog.conf"
printf '#!/usr/bin/env bash\nexit 0\n' > "${verification_dir}/gost-watchdog-admin"
printf '#!/usr/bin/env bash\nexit 0\n' > "${verification_dir}/gost-upstream-watchdog"
"${CHMOD_BIN}" 755 "${verification_dir}/gost-watchdog-admin" "${verification_dir}/gost-upstream-watchdog"
"${PYTHON_BIN}" -c \
'import functools, sys; from pathlib import Path; p=Path(sys.argv[1]); s=p.read_text(); pairs=zip(sys.argv[2::2],sys.argv[3::2]); p.write_text(functools.reduce(lambda value, pair: value.replace(*pair), pairs, s))' \
"${verification_unit}" \
/usr/local/sbin/gost-watchdog-admin "${verification_dir}/gost-watchdog-admin" \
/usr/local/sbin/gost-upstream-watchdog "${verification_dir}/gost-upstream-watchdog" \
/var/lib/gost-manager/watchdog "${verification_dir}/state" \
/etc/gost-manager/watchdog.conf "${verification_dir}/watchdog.conf" \
/etc/gost-manager/watchdog.d "${verification_dir}/watchdog.d" \
/etc/gost "${verification_dir}/gost" \
/etc/systemd/system "${verification_dir}/systemd"
if ! output="$("${SYSTEMD_ANALYZE_BIN}" verify "${verification_unit}" 2>&1)"; then
[[ -z "${output}" ]] || printf '%s\n' "${output}" >&2
die "real host Watchdog systemd unit verification failed."
fi
if [[ -n "${output}" ]]; then
case "${output}" in
*"${WATCHDOG_SERVICE}"*|*"${verification_dir}"*)
printf '%s\n' "${output}" >&2
die "real host Watchdog unit verification emitted candidate warnings."
;;
*) info "systemd-analyze reported unrelated host-unit diagnostics; the staged Watchdog unit passed." ;;
esac
fi
}
record_service_state() {
local unit_path
unit_path="$(path_for /etc/systemd/system/${MONITOR_SERVICE})"
[[ -e "${unit_path}" ]] && PREVIOUS_UNIT_EXISTED=1
if "${SYSTEMCTL_BIN}" is-enabled --quiet "${MONITOR_SERVICE}" >/dev/null 2>&1; then
PREVIOUS_ENABLED=1
fi
if "${SYSTEMCTL_BIN}" is-active --quiet "${MONITOR_SERVICE}" >/dev/null 2>&1; then
PREVIOUS_ACTIVE=1
fi
SERVICE_STATE_RECORDED=1
}
record_watchdog_service_state() {
local unit_path
unit_path="$(path_for /etc/systemd/system/${WATCHDOG_SERVICE})"
[[ -e "${unit_path}" ]] && PREVIOUS_WATCHDOG_UNIT_EXISTED=1
if "${SYSTEMCTL_BIN}" is-enabled --quiet "${WATCHDOG_SERVICE}" >/dev/null 2>&1; then
PREVIOUS_WATCHDOG_ENABLED=1
fi
if "${SYSTEMCTL_BIN}" is-active --quiet "${WATCHDOG_SERVICE}" >/dev/null 2>&1; then
PREVIOUS_WATCHDOG_ACTIVE=1
fi
WATCHDOG_SERVICE_STATE_RECORDED=1
}
record_metadata() {
local path="$1"
local existing metadata mode uid gid
[[ -e "${path}" && ! -L "${path}" ]] || return 0
for existing in "${METADATA_PATHS[@]+"${METADATA_PATHS[@]}"}"; do
[[ "${existing}" == "${path}" ]] && return 0
done
metadata="$(stat -c '%a %u %g' "${path}" 2>/dev/null || stat -f '%Lp %u %g' "${path}")"
read -r mode uid gid <<< "${metadata}"
METADATA_PATHS+=("${path}")
METADATA_MODES+=("${mode}")
METADATA_UIDS+=("${uid}")
METADATA_GIDS+=("${gid}")
}
ensure_shared_directory() {
local path="$1"
local mode="$2"
reject_symlink_path "${path}"
if [[ -e "${path}" && ! -d "${path}" ]]; then
die "shared path is not a directory: ${path}"
fi
if [[ ! -d "${path}" ]]; then
"${INSTALL_BIN}" -d -m "${mode}" "${path}"
CREATED_DIRECTORIES+=("${path}")
"${CHOWN_BIN}" root:root "${path}"
fi
}
ensure_legacy_directory() {
local path="$1"
reject_symlink_path "${path}"
if [[ -e "${path}" && ! -d "${path}" ]]; then
die "legacy path is not a directory: ${path}"
fi
if [[ ! -d "${path}" ]]; then
"${INSTALL_BIN}" -d -m 700 "${path}"
CREATED_DIRECTORIES+=("${path}")
"${CHOWN_BIN}" root:root "${path}"
fi
}
ensure_private_directory() {
local path="$1"
local mode="$2"
reject_symlink_path "${path}"
if [[ -e "${path}" && ! -d "${path}" ]]; then
die "private managed path is not a directory: ${path}"
fi
if [[ ! -d "${path}" ]]; then
"${INSTALL_BIN}" -d -m "${mode}" "${path}"
CREATED_DIRECTORIES+=("${path}")
"${CHOWN_BIN}" root:root "${path}"
else
record_metadata "${path}"
"${CHMOD_BIN}" "${mode}" "${path}"
"${CHOWN_BIN}" root:root "${path}"
fi
}
ensure_configured_db_parent() {
local state_root parent relative part current
local -a parts=()
state_root="$(path_for /var/lib/gost-manager)"
parent="${CONFIGURED_DB_ACTUAL%/*}"
[[ "${parent}" == "${state_root}" || "${parent}" == "${state_root}"/* ]] || die "configured database parent escaped the managed state tree."
relative="${parent#"${state_root}"}"
current="${state_root}"
IFS='/' read -r -a parts <<< "${relative#/}"
for part in "${parts[@]+"${parts[@]}"}"; do
[[ -n "${part}" ]] || continue
current="${current}/${part}"
ensure_private_directory "${current}" 700
done
}
install_managed_file() {
local source="$1"
local destination="$2"
local mode="$3"
local candidate backup backup_container
reject_symlink_path "${destination}"
if [[ -e "${destination}" && ! -f "${destination}" ]]; then
die "managed file destination is not a regular file: ${destination}"
fi
if [[ -f "${destination}" ]]; then
record_metadata "${destination}"
if "${CMP_BIN}" -s "${source}" "${destination}"; then
"${CHMOD_BIN}" "${mode}" "${destination}"
"${CHOWN_BIN}" root:root "${destination}"
return 0
fi
fi
candidate="$(mktemp "${destination}.gost-manager-new.XXXXXX")"
PENDING_PATHS+=("${candidate}")
"${INSTALL_BIN}" -m "${mode}" "${source}" "${candidate}"
"${CHOWN_BIN}" root:root "${candidate}"
if [[ -e "${destination}" ]]; then
backup_container="$(mktemp -d "${destination}.gost-manager-backup.XXXXXX")"
BACKUP_CONTAINERS+=("${backup_container}")
"${CHMOD_BIN}" 700 "${backup_container}"
"${CHOWN_BIN}" root:root "${backup_container}"
backup="${backup_container}/original"
"${CP_BIN}" -p "${destination}" "${backup}"
else
backup=""
backup_container=""
fi
CHANGED_DESTINATIONS+=("${destination}")
BACKUP_PATHS+=("${backup}")
inject_failure backup
"${MV_BIN}" "${candidate}" "${destination}"
FILES_CHANGED=1
}
install_monitoring_package() {
local destination candidate backup backup_container entry
destination="$(path_for /usr/local/lib/gost-manager/monitoring)"
reject_symlink_path "${destination}"
if [[ -e "${destination}" && ! -d "${destination}" ]]; then
die "monitoring package destination is not a directory."
fi
[[ ! -d "${destination}" ]] || record_metadata "${destination}"
candidate="$(mktemp -d "${destination}.gost-manager-new.XXXXXX")"
PENDING_PATHS+=("${candidate}")
"${CHMOD_BIN}" 755 "${candidate}"
for entry in "${RUNTIME_MANIFEST_ENTRIES[@]}"; do
"${INSTALL_BIN}" -m 644 "${STAGE_DIR}/monitoring/${entry##*/}" "${candidate}/${entry##*/}"
done
"${CHOWN_BIN}" -R root:root "${candidate}"
if [[ -d "${destination}" ]]; then
backup_container="$(mktemp -d "${destination}.gost-manager-backup.XXXXXX")"
BACKUP_CONTAINERS+=("${backup_container}")
"${CHMOD_BIN}" 700 "${backup_container}"
"${CHOWN_BIN}" root:root "${backup_container}"
backup="${backup_container}/original"
"${MV_BIN}" "${destination}" "${backup}"
else
backup=""
backup_container=""
fi
CHANGED_DESTINATIONS+=("${destination}")
BACKUP_PATHS+=("${backup}")
"${MV_BIN}" "${candidate}" "${destination}"
FILES_CHANGED=1
}
install_watchdog_package() {
local destination candidate backup backup_container entry
destination="$(path_for /usr/local/lib/gost-manager/gost_watchdog)"
reject_symlink_path "${destination}"
if [[ -e "${destination}" && ! -d "${destination}" ]]; then
die "Watchdog package destination is not a directory."
fi
[[ ! -d "${destination}" ]] || record_metadata "${destination}"
candidate="$(mktemp -d "${destination}.gost-manager-new.XXXXXX")"
PENDING_PATHS+=("${candidate}")
"${CHMOD_BIN}" 755 "${candidate}"
for entry in "${WATCHDOG_RUNTIME_MANIFEST_ENTRIES[@]}"; do
"${INSTALL_BIN}" -m 644 "${STAGE_DIR}/gost_watchdog/${entry##*/}" "${candidate}/${entry##*/}"
done
"${CHOWN_BIN}" -R root:root "${candidate}"
if [[ -d "${destination}" ]]; then
backup_container="$(mktemp -d "${destination}.gost-manager-backup.XXXXXX")"
BACKUP_CONTAINERS+=("${backup_container}")
"${CHMOD_BIN}" 700 "${backup_container}"
"${CHOWN_BIN}" root:root "${backup_container}"
backup="${backup_container}/original"
"${MV_BIN}" "${destination}" "${backup}"
else
backup=""
fi
CHANGED_DESTINATIONS+=("${destination}")
BACKUP_PATHS+=("${backup}")
"${MV_BIN}" "${candidate}" "${destination}"
FILES_CHANGED=1
WATCHDOG_FILES_CHANGED=1
}
install_files() {
local config_path unit_path before_count watchdog_config watchdog_unit
ensure_shared_directory "$(path_for /usr/local/sbin)" 755
ensure_private_directory "$(path_for /usr/local/lib/gost-manager)" 755
ensure_legacy_directory "$(path_for /etc/gost)"
ensure_private_directory "$(path_for /etc/gost-manager)" 700
ensure_shared_directory "$(path_for /etc/systemd/system)" 755
ensure_private_directory "$(path_for /var/lib/gost-manager)" 700
ensure_private_directory "$(path_for /etc/gost-manager/watchdog.d)" 700
ensure_private_directory "$(path_for /var/lib/gost-manager/watchdog)" 700
ensure_configured_db_parent
install_managed_file "${STAGE_DIR}/gost-manager" "$(path_for /usr/local/sbin/gost-manager)" 755
install_managed_file "${STAGE_DIR}/VERSION" "$(path_for /usr/local/lib/gost-manager/VERSION)" 644
inject_failure partial_file_replacement
install_managed_file "${STAGE_DIR}/lib/gost-run-iran.sh" "$(path_for /usr/local/lib/gost-manager/gost-run-iran.sh)" 755
install_managed_file "${STAGE_DIR}/lib/gost-run-kharej.sh" "$(path_for /usr/local/lib/gost-manager/gost-run-kharej.sh)" 755
install_monitoring_package
install_managed_file "${STAGE_DIR}/sbin/gost-monitor" "$(path_for /usr/local/sbin/gost-monitor)" 755
install_managed_file "${STAGE_DIR}/sbin/gost-monitor-admin" "$(path_for /usr/local/sbin/gost-monitor-admin)" 755
install_managed_file "${STAGE_DIR}/sbin/gost-monitor-collector" "$(path_for /usr/local/sbin/gost-monitor-collector)" 755
before_count="${#CHANGED_DESTINATIONS[@]}"
install_watchdog_package
install_managed_file "${STAGE_DIR}/sbin/gost-upstream-watchdog" "$(path_for /usr/local/sbin/gost-upstream-watchdog)" 755
install_managed_file "${STAGE_DIR}/sbin/gost-watchdog-admin" "$(path_for /usr/local/sbin/gost-watchdog-admin)" 755
if [[ "${#CHANGED_DESTINATIONS[@]}" -gt "${before_count}" ]]; then
WATCHDOG_FILES_CHANGED=1
fi
config_path="$(path_for /etc/gost-manager/monitoring.env)"
if [[ ! -e "${config_path}" ]]; then
install_managed_file "${STAGE_DIR}/monitoring.env" "${config_path}" 600
else
record_metadata "${config_path}"
"${CHMOD_BIN}" 600 "${config_path}"
"${CHOWN_BIN}" root:root "${config_path}"
fi
ACTIVE_CONFIG_PATH="${config_path}"
unit_path="$(path_for /etc/systemd/system/${MONITOR_SERVICE})"
before_count="${#CHANGED_DESTINATIONS[@]}"
install_managed_file "${STAGE_DIR}/gost-monitor-collector.service" "${unit_path}" 644
if [[ "${#CHANGED_DESTINATIONS[@]}" -gt "${before_count}" ]]; then
UNIT_CHANGED=1
fi
watchdog_config="$(path_for /etc/gost-manager/watchdog.conf)"
if [[ ! -e "${watchdog_config}" ]]; then
install_managed_file "${STAGE_DIR}/watchdog.conf" "${watchdog_config}" 600
WATCHDOG_FILES_CHANGED=1
else
record_metadata "${watchdog_config}"
"${CHMOD_BIN}" 600 "${watchdog_config}"
"${CHOWN_BIN}" root:root "${watchdog_config}"
fi
watchdog_unit="$(path_for /etc/systemd/system/${WATCHDOG_SERVICE})"
before_count="${#CHANGED_DESTINATIONS[@]}"
install_managed_file "${STAGE_DIR}/gost-upstream-watchdog.service" "${watchdog_unit}" 644
if [[ "${#CHANGED_DESTINATIONS[@]}" -gt "${before_count}" ]]; then
WATCHDOG_UNIT_CHANGED=1
WATCHDOG_FILES_CHANGED=1
fi
}
migrate_configured_database() {
local library
local -a root_args=()
library="$(path_for /usr/local/lib/gost-manager)"
[[ -e "${CONFIGURED_DB_ACTUAL}" ]] || DATABASE_CREATED=1
[[ ! -e "${CONFIGURED_DB_ACTUAL}" ]] || record_metadata "${CONFIGURED_DB_ACTUAL}"
if [[ -n "${GOST_MANAGER_ROOT}" ]]; then
root_args=(--path-root "${GOST_MANAGER_ROOT}")
fi
if [[ "${#root_args[@]}" -gt 0 ]]; then
PYTHONPATH="${library}" "${PYTHON_BIN}" -m monitoring.admin_cli \
--policy installed "${root_args[@]}" migrate --config "${ACTIVE_CONFIG_PATH}" >/dev/null
else
PYTHONPATH="${library}" "${PYTHON_BIN}" -m monitoring.admin_cli \
--policy installed migrate --config "${ACTIVE_CONFIG_PATH}" >/dev/null
fi
reject_symlink_path "${CONFIGURED_DB_ACTUAL}"
"${CHMOD_BIN}" 600 "${CONFIGURED_DB_ACTUAL}"
"${CHOWN_BIN}" root:root "${CONFIGURED_DB_ACTUAL}"
}
migrate_watchdog_database() {
local database
local -a root_args=()
database="$(path_for /var/lib/gost-manager/watchdog/watchdog.sqlite3)"
reject_symlink_path "${database}"
[[ -e "${database}" ]] || WATCHDOG_DATABASE_CREATED=1
[[ ! -e "${database}" ]] || record_metadata "${database}"
if [[ -n "${GOST_MANAGER_ROOT}" ]]; then
root_args=(--path-root "${GOST_MANAGER_ROOT}")
fi
PYTHONPATH="$(path_for /usr/local/lib/gost-manager)" "${PYTHON_BIN}" \
-m gost_watchdog.admin_cli --policy installed \
"${root_args[@]+"${root_args[@]}"}" migrate >/dev/null
"${CHMOD_BIN}" 600 "${database}"
"${CHOWN_BIN}" root:root "${database}"
}
activate_collector() {
if [[ "${UNIT_CHANGED}" == "1" || "${WATCHDOG_UNIT_CHANGED}" == "1" ]]; then
"${SYSTEMCTL_BIN}" daemon-reload
fi
inject_failure daemon_reload
migrate_configured_database
inject_failure migration
if [[ "${PREVIOUS_UNIT_EXISTED}" == "0" ]]; then
SERVICE_ENABLE_ATTEMPTED=1
"${SYSTEMCTL_BIN}" enable "${MONITOR_SERVICE}"
SERVICE_START_ATTEMPTED=1
"${SYSTEMCTL_BIN}" start "${MONITOR_SERVICE}"
elif [[ "${PREVIOUS_ACTIVE}" == "1" && "${FILES_CHANGED}" == "1" ]]; then
"${SYSTEMCTL_BIN}" restart "${MONITOR_SERVICE}"
fi
inject_failure collector_start
migrate_watchdog_database
inject_failure watchdog_migration
if [[ "${PREVIOUS_WATCHDOG_UNIT_EXISTED}" == "0" ]]; then
WATCHDOG_SERVICE_ENABLE_ATTEMPTED=1
"${SYSTEMCTL_BIN}" enable "${WATCHDOG_SERVICE}"
WATCHDOG_SERVICE_START_ATTEMPTED=1
"${SYSTEMCTL_BIN}" start "${WATCHDOG_SERVICE}"
elif [[ "${PREVIOUS_WATCHDOG_ACTIVE}" == "1" && "${WATCHDOG_FILES_CHANGED}" == "1" ]]; then
"${SYSTEMCTL_BIN}" restart "${WATCHDOG_SERVICE}"
fi
inject_failure watchdog_start
}
verify_installed_runtime() {
local version_path manager_path watchdog_path watchdog_admin watchdog_config watchdog_unit installed_version
version_path="$(path_for /usr/local/lib/gost-manager/VERSION)"
manager_path="$(path_for /usr/local/sbin/gost-manager)"
reject_symlink_path "${version_path}"
reject_symlink_path "${manager_path}"
[[ -f "${version_path}" && ! -L "${version_path}" ]] || die "installed VERSION must be a regular non-symlink file."
[[ -f "${manager_path}" && ! -L "${manager_path}" && -x "${manager_path}" ]] || die "installed manager must be a safe executable file."
watchdog_path="$(path_for /usr/local/sbin/gost-upstream-watchdog)"
watchdog_admin="$(path_for /usr/local/sbin/gost-watchdog-admin)"
watchdog_config="$(path_for /etc/gost-manager/watchdog.conf)"
watchdog_unit="$(path_for /etc/systemd/system/${WATCHDOG_SERVICE})"
[[ -x "${watchdog_path}" && ! -L "${watchdog_path}" ]] || die "installed Watchdog daemon launcher is missing or unsafe."
[[ -x "${watchdog_admin}" && ! -L "${watchdog_admin}" ]] || die "installed Watchdog admin launcher is missing or unsafe."
[[ -f "${watchdog_config}" && ! -L "${watchdog_config}" ]] || die "installed Watchdog config is missing or unsafe."
[[ -f "${watchdog_unit}" && ! -L "${watchdog_unit}" ]] || die "installed Watchdog unit is missing or unsafe."
if [[ "${GOST_MANAGER_TESTING}" == "1" && -n "${GOST_MANAGER_INSTALLED_VERSION_OVERRIDE_TEST}" ]]; then
printf '%s\n' "${GOST_MANAGER_INSTALLED_VERSION_OVERRIDE_TEST}" > "${version_path}"
fi
installed_version="$(< "${version_path}")"
[[ "${installed_version}" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] || die "installed VERSION must contain a semantic version."
[[ "${installed_version}" == "${SOURCE_VERSION}" ]] || die "installed VERSION ${installed_version} does not match source VERSION ${SOURCE_VERSION}."
}
rollback_files() {
local index destination backup failed=0
for ((index=${#CHANGED_DESTINATIONS[@]}-1; index>=0; index--)); do
destination="${CHANGED_DESTINATIONS[index]}"
backup="${BACKUP_PATHS[index]}"
"${RM_BIN}" -rf "${destination}" || failed=1
if [[ -n "${backup}" && -e "${backup}" ]]; then
"${CP_BIN}" -Rp "${backup}" "${destination}" || failed=1
fi
done
return "${failed}"
}
restore_metadata() {
local index path failed=0
for ((index=${#METADATA_PATHS[@]}-1; index>=0; index--)); do
path="${METADATA_PATHS[index]}"
if [[ -e "${path}" && ! -L "${path}" ]]; then
"${CHMOD_BIN}" "${METADATA_MODES[index]}" "${path}" || failed=1
"${CHOWN_BIN}" "${METADATA_UIDS[index]}:${METADATA_GIDS[index]}" "${path}" || failed=1
fi
done
return "${failed}"
}
collector_is_active() {
"${SYSTEMCTL_BIN}" is-active --quiet "${MONITOR_SERVICE}" >/dev/null 2>&1
}
collector_is_enabled() {
"${SYSTEMCTL_BIN}" is-enabled --quiet "${MONITOR_SERVICE}" >/dev/null 2>&1
}
verify_collector_loaded() {
local state
state="$("${SYSTEMCTL_BIN}" show "${MONITOR_SERVICE}" --property=LoadState --value 2>/dev/null)" || return 1
[[ -n "${state}" && "${state}" != "not-found" ]]
}
verify_collector_not_loaded() {
local state
state="$("${SYSTEMCTL_BIN}" show "${MONITOR_SERVICE}" --property=LoadState --value 2>/dev/null)" || return 1
[[ "${state}" == "not-found" ]]
}
watchdog_is_active() {
"${SYSTEMCTL_BIN}" is-active --quiet "${WATCHDOG_SERVICE}" >/dev/null 2>&1
}
watchdog_is_enabled() {
"${SYSTEMCTL_BIN}" is-enabled --quiet "${WATCHDOG_SERVICE}" >/dev/null 2>&1
}
verify_watchdog_loaded() {
local state
state="$("${SYSTEMCTL_BIN}" show "${WATCHDOG_SERVICE}" --property=LoadState --value 2>/dev/null)" || return 1
[[ -n "${state}" && "${state}" != "not-found" ]]
}
verify_watchdog_not_loaded() {
local state
state="$("${SYSTEMCTL_BIN}" show "${WATCHDOG_SERVICE}" --property=LoadState --value 2>/dev/null)" || return 1
[[ "${state}" == "not-found" ]]
}
prepare_service_rollback() {
if [[ "${PREVIOUS_UNIT_EXISTED}" == "0" ]]; then
if [[ "${SERVICE_START_ATTEMPTED}" == "1" ]] || collector_is_active; then
"${SYSTEMCTL_BIN}" stop "${MONITOR_SERVICE}" >/dev/null 2>&1 || true
fi
if [[ "${SERVICE_ENABLE_ATTEMPTED}" == "1" ]] || collector_is_enabled; then
"${SYSTEMCTL_BIN}" disable "${MONITOR_SERVICE}" >/dev/null 2>&1 || true
fi
if collector_is_active || collector_is_enabled; then
info "Collector cleanup could not be verified before fresh-install rollback."
return 1
fi
return 0
fi
if [[ "${FILES_CHANGED}" == "1" && "${PREVIOUS_ACTIVE}" == "1" ]]; then
"${SYSTEMCTL_BIN}" stop "${MONITOR_SERVICE}" >/dev/null 2>&1 || true
if collector_is_active; then
info "Existing collector could not be stopped for file restoration."
return 1
fi
fi
}
prepare_watchdog_service_rollback() {
if [[ "${PREVIOUS_WATCHDOG_UNIT_EXISTED}" == "0" ]]; then
if [[ "${WATCHDOG_SERVICE_START_ATTEMPTED}" == "1" ]] || watchdog_is_active; then
"${SYSTEMCTL_BIN}" stop "${WATCHDOG_SERVICE}" >/dev/null 2>&1 || true
fi
if [[ "${WATCHDOG_SERVICE_ENABLE_ATTEMPTED}" == "1" ]] || watchdog_is_enabled; then
"${SYSTEMCTL_BIN}" disable "${WATCHDOG_SERVICE}" >/dev/null 2>&1 || true
fi
if watchdog_is_active || watchdog_is_enabled; then
info "Watchdog cleanup could not be verified before fresh-install rollback."
return 1
fi
return 0
fi
if [[ "${WATCHDOG_FILES_CHANGED}" == "1" && "${PREVIOUS_WATCHDOG_ACTIVE}" == "1" ]]; then
"${SYSTEMCTL_BIN}" stop "${WATCHDOG_SERVICE}" >/dev/null 2>&1 || true
if watchdog_is_active; then
info "Existing Watchdog could not be stopped for file restoration."
return 1
fi
fi
}
restore_recorded_service_state() {
if [[ "${PREVIOUS_ENABLED}" == "1" ]]; then
"${SYSTEMCTL_BIN}" enable "${MONITOR_SERVICE}" >/dev/null 2>&1 || true
else
"${SYSTEMCTL_BIN}" disable "${MONITOR_SERVICE}" >/dev/null 2>&1 || true
fi
if [[ "${PREVIOUS_ACTIVE}" == "1" ]]; then