-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinstall-legacy-from-source.sh
More file actions
executable file
·964 lines (796 loc) · 29.8 KB
/
install-legacy-from-source.sh
File metadata and controls
executable file
·964 lines (796 loc) · 29.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
#!/bin/bash
set -euo pipefail # Exit on error, undefined variables, pipe failures
# =============================================================================
# TROLE INSTALLATION SCRIPT - IMPROVED VERSION
# =============================================================================
# This script installs and configures the Trole ecosystem including:
# - IPFS (InterPlanetary File System)
# - SPK Network Node (HoneyComb)
# - Caddy web server with reverse proxy
# - Proof of Access (PoA) services
# - Go development environment
# =============================================================================
# Configuration
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly LOG_FILE="${SCRIPT_DIR}/install.log"
readonly ENV_FILE="${SCRIPT_DIR}/.env"
readonly BACKUP_DIR="${HOME}/.trole_backups"
# Color codes for output formatting
readonly RED='\033[0;31m'
readonly GREEN='\033[0;32m'
readonly YELLOW='\033[0;33m'
readonly BLUE='\033[0;34m'
readonly NC='\033[0m' # No Color
# Version constraints
readonly MIN_NODE_VERSION=14
readonly REQUIRED_IPFS_VERSION="v0.26.0"
readonly REQUIRED_GO_VERSION="1.19"
# =============================================================================
# UTILITY FUNCTIONS
# =============================================================================
log() {
local level="$1"
shift
local message="$*"
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
case "$level" in
INFO) echo -e "${GREEN}[INFO]${NC} $message" | tee -a "$LOG_FILE" ;;
WARN) echo -e "${YELLOW}[WARN]${NC} $message" | tee -a "$LOG_FILE" ;;
ERROR) echo -e "${RED}[ERROR]${NC} $message" | tee -a "$LOG_FILE" ;;
DEBUG) echo -e "${BLUE}[DEBUG]${NC} $message" | tee -a "$LOG_FILE" ;;
esac
echo "[$timestamp] [$level] $message" >> "$LOG_FILE"
}
error_exit() {
log ERROR "$1"
exit "${2:-1}"
}
command_exists() {
command -v "$1" >/dev/null 2>&1
}
validate_environment() {
local whoami_user
whoami_user=$(whoami)
if [[ "$whoami_user" == "root" ]]; then
error_exit "Cannot install as root user. Please run as a regular user with sudo privileges."
fi
if ! groups | grep -q "$whoami_user"; then
error_exit "User $whoami_user is not part of group $whoami_user. Please add user to group and retry."
fi
# Check OS compatibility
if [[ ! -f /etc/os-release ]]; then
error_exit "Cannot determine OS version. /etc/os-release not found."
fi
source /etc/os-release
if [[ "${ID_LIKE:-}" != "debian" ]]; then
error_exit "This script requires Ubuntu/Debian. Detected: ${PRETTY_NAME:-Unknown}"
fi
log INFO "Environment validation passed for user: $whoami_user on ${PRETTY_NAME:-Unknown OS}"
}
backup_existing_config() {
if [[ -f "$ENV_FILE" ]]; then
mkdir -p "$BACKUP_DIR"
local backup_file="${BACKUP_DIR}/.env.backup.$(date +%Y%m%d_%H%M%S)"
cp "$ENV_FILE" "$backup_file"
log INFO "Backed up existing .env to $backup_file"
fi
}
create_directories() {
local dirs=("${HOME}/trole/db" "${HOME}/trole/uploads" "$BACKUP_DIR")
for dir in "${dirs[@]}"; do
if [[ ! -d "$dir" ]]; then
mkdir -p "$dir"
log INFO "Created directory: $dir"
fi
done
}
# =============================================================================
# INPUT VALIDATION AND COLLECTION
# =============================================================================
validate_domain() {
local domain="$1"
if [[ ! "$domain" =~ ^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]?\.[a-zA-Z]{2,}$ ]]; then
return 1
fi
return 0
}
validate_hive_account() {
local account="$1"
if [[ ! "$account" =~ ^[a-z][a-z0-9.-]{2,15}$ ]]; then
return 1
fi
return 0
}
validate_hive_key() {
local key="$1"
# Basic validation - Hive keys start with 5 and are 51-52 characters
if [[ ! "$key" =~ ^5[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{50,51}$ ]]; then
return 1
fi
return 0
}
prompt_user_input() {
local var_name="$1"
local prompt_text="$2"
local validator_func="${3:-}"
local hide_input="${4:-false}"
local input=""
while true; do
if [[ "$hide_input" == "true" ]]; then
read -s -p "$prompt_text: " input
echo
else
read -p "$prompt_text: " input
fi
if [[ -n "$input" ]]; then
if [[ -n "$validator_func" ]] && ! "$validator_func" "$input"; then
log WARN "Invalid input format. Please try again."
continue
fi
eval "$var_name='$input'"
break
else
log WARN "Input cannot be empty. Please try again."
fi
done
}
collect_configuration() {
log INFO "Collecting configuration parameters..."
# Load existing environment if available
if [[ -f "$ENV_FILE" ]]; then
source "$ENV_FILE"
log INFO "Loaded existing configuration from .env"
fi
# Domain configuration
if [[ -z "${DOMAIN:-}" ]]; then
prompt_user_input DOMAIN "Enter your domain name (e.g., example.com)" validate_domain
echo "DOMAIN=${DOMAIN}" >> "$ENV_FILE"
fi
log INFO "Domain: $DOMAIN"
# Hive account configuration
if [[ -z "${ACCOUNT:-}" ]]; then
prompt_user_input ACCOUNT "Enter your HIVE account name" validate_hive_account
echo "ACCOUNT=${ACCOUNT}" >> "$ENV_FILE"
fi
log INFO "Account: $ACCOUNT"
# Hive active key
if [[ -z "${ACTIVE:-}" ]]; then
prompt_user_input ACTIVE "Enter the ACTIVE key for $ACCOUNT" validate_hive_key true
echo "ACTIVE=${ACTIVE}" >> "$ENV_FILE"
fi
log INFO "Active key configured (hidden)"
# Service configuration prompts
if [[ -z "${BUILDSPK:-}" ]]; then
while true; do
read -p "Install SPK Node and Validator? (y/n): " -n 1 -r
echo
case $REPLY in
[Yy]*) BUILDSPK=true; break;;
[Nn]*) BUILDSPK=false; break;;
*) log WARN "Please answer y or n.";;
esac
done
echo "BUILDSPK=${BUILDSPK}" >> "$ENV_FILE"
fi
# Ask for Honeygraph URL if installing SPK node
if [[ "${BUILDSPK}" == "true" ]] && [[ -z "${HONEYGRAPH_URL:-}" ]]; then
log INFO "Honeygraph provides read replication for SPK Network nodes"
read -p "Enter Honeygraph URL (optional, e.g., https://graph.spk.network): " honeygraph_input
if [[ -n "$honeygraph_input" ]]; then
HONEYGRAPH_URL="$honeygraph_input"
echo "HONEYGRAPH_URL=${HONEYGRAPH_URL}" >> "$ENV_FILE"
log INFO "Honeygraph URL configured: ${HONEYGRAPH_URL}"
log INFO "Your SPK node will authenticate to Honeygraph using account: ${ACCOUNT}"
else
log INFO "No Honeygraph URL provided, continuing without read replication"
fi
fi
if [[ -z "${BUILDVAL:-}" ]]; then
while true; do
read -p "Register as Validator? (y/n): " -n 1 -r
echo
case $REPLY in
[Yy]*) BUILDVAL=true; break;;
[Nn]*) BUILDVAL=false; break;;
*) log WARN "Please answer y or n.";;
esac
done
echo "BUILDVAL=${BUILDVAL}" >> "$ENV_FILE"
fi
# Set default values for other configuration
local whoami_user
whoami_user=$(whoami)
{
echo "domain=${DOMAIN}"
echo "account=${ACCOUNT}"
echo "active=${ACTIVE}"
echo "mirrorNet=true"
echo "API_PORT=${API_PORT:-5050}"
echo "ENDPOINT=${ENDPOINT:-127.0.0.1}"
echo "ENDPORT=${ENDPORT:-5001}"
echo "POA_URL=${POA_URL:-ws://localhost:8001}"
echo "VALIDATOR=${BUILDVAL}"
echo "ipfshost=127.0.0.1"
echo "ipfsprotocol=http"
echo "ipfsport=5001"
echo "STARTURL=https://rpc.ecency.com/"
echo "APIURL=https://rpc.ecency.com/"
echo "UPLOAD_DIR=/home/${whoami_user}/trole/uploads"
} >> "$ENV_FILE"
# Handle SPK keys
handle_spk_keys
log INFO "Configuration collection completed"
}
handle_spk_keys() {
if [[ -z "${SPKPRIV:-}" ]]; then
while true; do
read -p "Do you have existing SPK keypair? (y/n): " -n 1 -r
echo
case $REPLY in
[Yy]*)
prompt_user_input SPKPRIV "Enter your SPK private key" "" true
prompt_user_input SPKPUB "Enter your SPK public key"
break;;
[Nn]*)
log INFO "Generating new SPK keypair..."
if [[ -f "generate_key_pair.js" ]]; then
local key_pair
key_pair=$(node generate_key_pair.js)
SPKPRIV=$(echo "$key_pair" | cut -d " " -f1)
SPKPUB=$(echo "$key_pair" | cut -d " " -f2)
log INFO "Generated new SPK keypair"
else
error_exit "generate_key_pair.js not found. Cannot generate SPK keys."
fi
break;;
*) log WARN "Please answer y or n.";;
esac
done
{
echo "SPKPRIV=${SPKPRIV}"
echo "SPKPUB=${SPKPUB}"
echo "msowner=${SPKPRIV}"
echo "mspublic=${SPKPUB}"
} >> "$ENV_FILE"
fi
}
# =============================================================================
# SYSTEM DEPENDENCIES INSTALLATION
# =============================================================================
update_system() {
log INFO "Updating system packages..."
if ! sudo apt update >/dev/null; then
error_exit "Failed to update package lists"
fi
log INFO "System packages updated successfully"
}
install_nodejs() {
if command_exists node; then
local node_version
node_version=$(node -v | cut -f1 -d. | sed 's/v//')
if [[ "$node_version" -ge "$MIN_NODE_VERSION" ]]; then
log INFO "Node.js $(node -v) is already installed and meets requirements"
return 0
else
log WARN "Node.js version $node_version is below minimum required version $MIN_NODE_VERSION"
fi
fi
log INFO "Installing Node.js..."
if ! curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -; then
error_exit "Failed to add Node.js repository"
fi
if ! sudo apt install -y nodejs; then
error_exit "Failed to install Node.js"
fi
# Verify installation
if ! command_exists node || ! command_exists npm; then
error_exit "Node.js installation verification failed"
fi
log INFO "Node.js $(node -v) and npm $(npm -v) installed successfully"
}
install_trole_dependencies() {
log INFO "Installing Trole Node.js dependencies..."
if [[ ! -f "package.json" ]]; then
error_exit "package.json not found. Please run this script from the Trole directory."
fi
if [[ -d "node_modules" ]]; then
log INFO "Node modules already exist, updating..."
if ! npm update; then
log WARN "npm update failed, trying fresh install..."
rm -rf node_modules package-lock.json
fi
fi
if [[ ! -d "node_modules" ]]; then
if ! npm install; then
error_exit "Failed to install Node.js dependencies"
fi
fi
log INFO "Trole dependencies installed successfully"
}
install_ipfs() {
if command_exists ipfs; then
local ipfs_version
ipfs_version=$(ipfs version --number)
log INFO "IPFS $ipfs_version is already installed"
return 0
fi
log INFO "Installing IPFS (Kubo)..."
local temp_dir
temp_dir=$(mktemp -d)
pushd "$temp_dir" >/dev/null || error_exit "Failed to change to temporary directory"
if ! wget -q "https://github.com/ipfs/kubo/releases/download/${REQUIRED_IPFS_VERSION}/kubo_${REQUIRED_IPFS_VERSION}_linux-amd64.tar.gz"; then
error_exit "Failed to download IPFS"
fi
if ! tar -xzf "kubo_${REQUIRED_IPFS_VERSION}_linux-amd64.tar.gz"; then
error_exit "Failed to extract IPFS archive"
fi
if ! sudo bash kubo/install.sh >/dev/null; then
error_exit "Failed to install IPFS"
fi
popd >/dev/null
rm -rf "$temp_dir"
if ! command_exists ipfs; then
error_exit "IPFS installation verification failed"
fi
log INFO "IPFS $(ipfs version --number) installed successfully"
}
configure_ipfs() {
local ipfs_config_file="${HOME}/.ipfs/config"
if [[ ! -f "$ipfs_config_file" ]]; then
log INFO "Initializing IPFS..."
if ! ipfs init --profile server >/dev/null; then
error_exit "Failed to initialize IPFS"
fi
else
log INFO "IPFS configuration already exists"
fi
log INFO "Configuring IPFS CORS settings..."
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["*"]'
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods '["GET", "POST"]'
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Headers '["Authorization"]'
ipfs config --json API.HTTPHeaders.Access-Control-Expose-Headers '["Location"]'
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Credentials '["true"]'
# Get and store IPFS ID
local ipfs_id
ipfs_id=$(ipfs id --format="<id>")
if [[ -z "${IPFSID:-}" ]]; then
echo "IPFSID=${ipfs_id}" >> "$ENV_FILE"
log INFO "IPFS ID stored: $ipfs_id"
elif [[ "$ipfs_id" != "$IPFSID" ]]; then
log WARN "IPFS ID has changed from $IPFSID to $ipfs_id"
while true; do
read -p "Update IPFSID in .env? (y/n): " -n 1 -r
echo
case $REPLY in
[Yy]*)
sed -i "s/IPFSID=.*/IPFSID=${ipfs_id}/" "$ENV_FILE"
log INFO "IPFS ID updated in .env"
break;;
[Nn]*)
log INFO "IPFS ID not updated"
break;;
*) log WARN "Please answer y or n.";;
esac
done
fi
}
install_go() {
if command_exists go; then
log INFO "Go $(go version | awk '{print $3}') is already installed"
return 0
fi
log INFO "Installing Go via Snap..."
if ! command_exists snap; then
log INFO "Installing Snap package manager..."
if ! sudo apt install -y snapd; then
error_exit "Failed to install Snap"
fi
fi
if ! sudo snap install go --classic; then
error_exit "Failed to install Go"
fi
# Add snap bin to PATH if not already there
if [[ ":$PATH:" != *":/snap/bin:"* ]]; then
echo 'export PATH="/snap/bin:$PATH"' >> "${HOME}/.bashrc"
export PATH="/snap/bin:$PATH"
fi
if ! command_exists go; then
error_exit "Go installation verification failed"
fi
log INFO "Go $(go version | awk '{print $3}') installed successfully"
}
install_caddy() {
if command_exists caddy; then
log INFO "Caddy is already installed"
return 0
fi
log INFO "Installing Caddy web server..."
if ! sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https; then
error_exit "Failed to install Caddy prerequisites"
fi
if ! curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg; then
error_exit "Failed to add Caddy GPG key"
fi
if ! curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list >/dev/null; then
error_exit "Failed to add Caddy repository"
fi
if ! sudo apt update >/dev/null; then
error_exit "Failed to update package lists after adding Caddy repository"
fi
if ! sudo apt install -y caddy; then
error_exit "Failed to install Caddy"
fi
log INFO "Caddy installed successfully"
}
# =============================================================================
# SERVICE MANAGEMENT
# =============================================================================
create_systemd_service() {
local service_name="$1"
local service_file="/lib/systemd/system/${service_name}.service"
local service_content="$2"
if [[ -f "$service_file" ]]; then
log INFO "$service_name service already exists"
return 0
fi
log INFO "Creating $service_name systemd service..."
echo -e "$service_content" | sudo tee "$service_file" >/dev/null
sudo systemctl daemon-reload
log INFO "$service_name service created successfully"
}
manage_service() {
local service_name="$1"
local enable_service="${2:-true}"
# Start service if not active
if ! sudo systemctl is-active --quiet "$service_name"; then
log INFO "Starting $service_name service..."
if ! sudo systemctl start "$service_name"; then
error_exit "$service_name service failed to start"
fi
else
log INFO "$service_name service is already running"
fi
# Enable service for auto-start if requested
if [[ "$enable_service" == "true" ]]; then
if ! sudo systemctl is-enabled --quiet "$service_name"; then
log INFO "Enabling $service_name for auto-start..."
sudo systemctl enable "$service_name"
else
log INFO "$service_name is already enabled for auto-start"
fi
fi
# Verify service is running
if ! sudo systemctl is-active --quiet "$service_name"; then
error_exit "$service_name service is not running after start attempt"
fi
log INFO "$service_name service is running successfully"
}
setup_ipfs_service() {
local whoami_user
whoami_user=$(whoami)
local service_content="[Unit]
Description=IPFS daemon
After=network.target
[Service]
Type=notify
ExecStart=/usr/local/bin/ipfs daemon --enable-pubsub-experiment --enable-gc
Restart=on-failure
RestartSec=5
User=${whoami_user}
Group=${whoami_user}
Environment=\"IPFS_PATH=/home/${whoami_user}/.ipfs\"
[Install]
WantedBy=multi-user.target"
create_systemd_service "ipfs" "$service_content"
manage_service "ipfs"
}
setup_trole_service() {
local whoami_user
whoami_user=$(whoami)
# Ensure uploads directory has proper permissions
local uploads_dir="/home/${whoami_user}/trole/uploads"
if [[ -d "$uploads_dir" ]]; then
chmod 755 "$uploads_dir"
chown "${whoami_user}:${whoami_user}" "$uploads_dir"
log INFO "Set permissions for uploads directory: $uploads_dir"
fi
local service_content="[Unit]
Description=Trole Node
After=network.target ipfs.service
Requires=ipfs.service
[Service]
Type=simple
WorkingDirectory=/home/${whoami_user}/trole
ExecStart=/usr/bin/node /home/${whoami_user}/trole/index.js
Restart=on-failure
RestartSec=5
User=${whoami_user}
Group=${whoami_user}
Environment=NODE_ENV=production
Environment=UPLOAD_DIR=/home/${whoami_user}/trole/uploads
[Install]
WantedBy=multi-user.target"
create_systemd_service "trole" "$service_content"
manage_service "trole"
}
setup_spk_service() {
if [[ "${BUILDSPK:-false}" != "true" ]]; then
log INFO "Skipping SPK service setup (not requested)"
return 0
fi
local whoami_user
whoami_user=$(whoami)
local honeycomb_dir="${HOME}/honeycomb"
# Clone and setup honeycomb if not exists
if [[ ! -d "$honeycomb_dir" ]]; then
log INFO "Cloning HoneyComb SPK node..."
if ! git clone https://github.com/spknetwork/honeycomb-spkcc.git "$honeycomb_dir"; then
error_exit "Failed to clone HoneyComb repository"
fi
pushd "$honeycomb_dir" >/dev/null
if ! git checkout 1.2-poa; then
log WARN "Failed to checkout 1.2-poa branch, using default"
fi
if ! npm install; then
error_exit "Failed to install HoneyComb dependencies"
fi
# Copy environment file
cp "$ENV_FILE" "${honeycomb_dir}/.env"
echo "DOMAIN=spk.${DOMAIN}" >> "${honeycomb_dir}/.env"
# Configure Honeygraph authentication if endpoint is provided
if [[ -n "${HONEYGRAPH_URL:-}" ]]; then
log INFO "Configuring Honeygraph authentication..."
echo "" >> "${honeycomb_dir}/.env"
echo "# Honeygraph Configuration" >> "${honeycomb_dir}/.env"
echo "HONEYGRAPH_URL=${HONEYGRAPH_URL}" >> "${honeycomb_dir}/.env"
echo "HONEYGRAPH_AUTH_ENABLED=true" >> "${honeycomb_dir}/.env"
echo "# Honeygraph will authenticate using this node's Hive account: ${ACCOUNT}" >> "${honeycomb_dir}/.env"
log INFO "Honeygraph authentication configured for account: ${ACCOUNT}"
fi
popd >/dev/null
fi
local service_content="[Unit]
Description=SPK Network Node (HoneyComb)
After=network.target ipfs.service
Requires=ipfs.service
[Service]
Type=simple
WorkingDirectory=/home/${whoami_user}/honeycomb
ExecStart=/usr/bin/node /home/${whoami_user}/honeycomb/index.mjs
Restart=on-failure
RestartSec=5
User=${whoami_user}
Group=${whoami_user}
Environment=NODE_ENV=production
[Install]
WantedBy=multi-user.target"
create_systemd_service "spk" "$service_content"
manage_service "spk"
}
setup_poa_service() {
local whoami_user
whoami_user=$(whoami)
local poa_dir="${HOME}/proofofaccess"
# Clone and build PoA if not exists
if [[ ! -d "$poa_dir" ]]; then
log INFO "Cloning and building Proof of Access..."
if ! git clone https://github.com/spknetwork/proofofaccess.git "$poa_dir"; then
error_exit "Failed to clone Proof of Access repository"
fi
pushd "$poa_dir" >/dev/null
mkdir -p data
# Build the Go application
if ! /snap/bin/go build -o main main.go; then
error_exit "Failed to build Proof of Access"
fi
popd >/dev/null
fi
local service_content="[Unit]
Description=Proof of Access Node
After=network.target ipfs.service
Requires=ipfs.service
[Service]
Type=simple
WorkingDirectory=/home/${whoami_user}
ExecStart=/home/${whoami_user}/proofofaccess/main -node 2 -username ${ACCOUNT} -WS_PORT=8000 -useWS=true -honeycomb=true -IPFS_PORT=5001
Restart=on-failure
RestartSec=5
User=${whoami_user}
Group=${whoami_user}
[Install]
WantedBy=multi-user.target"
create_systemd_service "poa" "$service_content"
manage_service "poa"
# Setup validator service if requested
if [[ "${BUILDSPK:-false}" == "true" ]]; then
setup_poav_service
fi
}
setup_poav_service() {
local whoami_user
whoami_user=$(whoami)
local service_content="[Unit]
Description=Proof of Access Validator
After=network.target ipfs.service poa.service
Requires=ipfs.service
[Service]
Type=simple
WorkingDirectory=/home/${whoami_user}
ExecStart=/home/${whoami_user}/proofofaccess/main -node 1 -username validator1 -WS_PORT=8001 -useWS=true -honeycomb=true -IPFS_PORT=5001
Restart=on-failure
RestartSec=5
User=${whoami_user}
Group=${whoami_user}
[Install]
WantedBy=multi-user.target"
create_systemd_service "poav" "$service_content"
# Only enable if validator mode is requested
local enable_validator="${BUILDVAL:-false}"
manage_service "poav" "$enable_validator"
}
# =============================================================================
# CADDY CONFIGURATION
# =============================================================================
configure_caddy() {
local caddy_file="/etc/caddy/Caddyfile"
local caddy_pattern="ipfs.${DOMAIN}"
if grep -q "$caddy_pattern" "$caddy_file" 2>/dev/null; then
log INFO "Caddy configuration already exists for $DOMAIN"
log WARN "To update Caddy config, remove existing config from $caddy_file and run script again"
return 0
fi
if [[ ! -f "Caddyfile.template" ]]; then
error_exit "Caddyfile.template not found. Cannot configure Caddy."
fi
log INFO "Configuring Caddy with domain $DOMAIN..."
# Generate Caddyfile from template
local whoami_user
whoami_user=$(whoami)
local upload_dir="/home/${whoami_user}/trole/uploads"
if ! sed -e "s/{{DOMAIN}}/${DOMAIN}/g" \
-e "s/{{API_PORT}}/${API_PORT:-5050}/g" \
-e "s|{{UPLOAD_DIR}}|${upload_dir}|g" \
Caddyfile.template | sudo tee -a "$caddy_file" >/dev/null; then
error_exit "Failed to update Caddy configuration"
fi
# Test Caddy configuration
if ! sudo caddy validate --config "$caddy_file"; then
error_exit "Caddy configuration validation failed"
fi
# Restart Caddy to apply new configuration
if ! sudo systemctl restart caddy; then
error_exit "Failed to restart Caddy"
fi
log INFO "Caddy configured successfully"
}
# =============================================================================
# NODE REGISTRATION
# =============================================================================
register_node() {
if [[ ! -f "register_node.js" ]]; then
log WARN "register_node.js not found. Skipping node registration."
return 0
fi
log INFO "Registering node services..."
# Ensure all required services are running before registration
local required_services=("ipfs" "trole")
for service in "${required_services[@]}"; do
if ! sudo systemctl is-active --quiet "$service"; then
log WARN "$service is not running. Registration may fail."
fi
done
# Run registration with timeout
if timeout 60 node register_node.js; then
log INFO "Node registration completed successfully"
else
log WARN "Node registration failed or timed out"
fi
}
# =============================================================================
# CLEANUP AND FINALIZATION
# =============================================================================
cleanup_temp_files() {
log INFO "Cleaning up temporary files..."
# Remove any temporary archives
find "$SCRIPT_DIR" -name "*.tar.gz" -mtime +1 -delete 2>/dev/null || true
# Remove old log files (keep last 10)
find "$SCRIPT_DIR" -name "install.log.*" -type f | sort -r | tail -n +11 | xargs rm -f 2>/dev/null || true
log INFO "Cleanup completed"
}
display_final_status() {
log INFO "Installation completed successfully!"
echo
echo -e "${GREEN}=== TROLE INSTALLATION SUMMARY ===${NC}"
echo -e "${BLUE}Domain:${NC} $DOMAIN"
echo -e "${BLUE}Account:${NC} $ACCOUNT"
echo -e "${BLUE}Upload Directory:${NC} ${UPLOAD_DIR:-/home/$(whoami)/trole/uploads}"
echo -e "${BLUE}Services installed:${NC}"
local services=("ipfs" "trole")
if [[ "${BUILDSPK:-false}" == "true" ]]; then
services+=("spk")
fi
services+=("poa")
if [[ "${BUILDSPK:-false}" == "true" ]]; then
services+=("poav")
fi
services+=("caddy")
for service in "${services[@]}"; do
if sudo systemctl is-active --quiet "$service"; then
echo -e "${GREEN} ✓ $service${NC}"
else
echo -e "${RED} ✗ $service${NC}"
fi
done
echo
echo -e "${YELLOW}DNS Requirements:${NC}"
echo -e " - ipfs.$DOMAIN should point to this server"
if [[ "${BUILDSPK:-false}" == "true" ]]; then
echo -e " - spk.$DOMAIN should point to this server"
fi
if [[ "${BUILDVAL:-false}" == "true" ]]; then
echo -e " - poa.$DOMAIN should point to this server"
fi
echo
if [[ -n "${HONEYGRAPH_URL:-}" ]]; then
echo -e "${YELLOW}Honeygraph Configuration:${NC}"
echo -e " - URL: ${HONEYGRAPH_URL}"
echo -e " - Authentication: Using Hive account ${ACCOUNT}"
echo -e " - The SPK node will authenticate automatically"
echo
fi
echo -e "${YELLOW}Important:${NC}"
echo -e " - Your .env file contains sensitive keys"
echo -e " - A backup has been created in: $BACKUP_DIR"
echo -e " - Keep your .env file secure and backed up"
echo -e " - Installation log saved to: $LOG_FILE"
echo
}
# =============================================================================
# MAIN INSTALLATION FLOW
# =============================================================================
main() {
log INFO "Starting Trole installation script..."
log INFO "Log file: $LOG_FILE"
# Pre-installation checks
validate_environment
backup_existing_config
create_directories
# Collect configuration
collect_configuration
source "$ENV_FILE" # Reload updated environment
# System updates and dependencies
update_system
install_nodejs
install_trole_dependencies
install_ipfs
configure_ipfs
install_go
install_caddy
# Service setup
setup_ipfs_service
setup_trole_service
setup_spk_service
setup_poa_service
# Configuration
configure_caddy
# Registration and finalization
register_node
cleanup_temp_files
display_final_status
log INFO "Trole installation completed successfully!"
}
# =============================================================================
# SCRIPT EXECUTION
# =============================================================================
# Create log file and redirect stderr
exec 2> >(tee -a "$LOG_FILE")
# Check if running as root (should exit early)
if [[ $EUID -eq 0 ]]; then
error_exit "This script should not be run as root. Please run as a regular user with sudo privileges."
fi
# Handle script interruption
trap 'log ERROR "Installation interrupted by user"; exit 130' INT
trap 'log ERROR "Installation failed unexpectedly on line $LINENO"; exit 1' ERR
# Run main installation
main "$@"
exit 0