-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy patheaManager
More file actions
executable file
·886 lines (778 loc) · 27.1 KB
/
eaManager
File metadata and controls
executable file
·886 lines (778 loc) · 27.1 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
#!/bin/bash
########################################################
# Formatting Variables #
########################################################
b=$(tput bold)
n=$(tput sgr0)
un=$(tput smul)
nun=$(tput rmul)
########################################################
# Variables #
########################################################
IP_ADDRESS=$(ip route get 1.2.3.4 | awk '{print $7}')
VERSION="0.1.0"
# Grab API keys from flat file
source api_keys
# Grab Container names from flat file
source ~/.profile
# $1 --> option (--deploy)
# $2 --> external adapter name (--coingekco)
# $3 --> specific version for external adapter (0.1.4)
########################################################
# EA CONFIG & URL RESOLUTION #
########################################################
# Figure out repo root based on script location (follow symlinks)
SCRIPT_PATH="$(readlink -f "${BASH_SOURCE[0]}" 2>/dev/null || realpath "${BASH_SOURCE[0]}" 2>/dev/null || echo "${BASH_SOURCE[0]}")"
SCRIPT_DIR="$(cd "$(dirname "$SCRIPT_PATH")" && pwd)"
REPO_ROOT="$SCRIPT_DIR"
EA_CONFIG="${EA_CONFIG:-$REPO_ROOT/ea_endpoints}"
EXTERNAL_DIR="${EXTERNAL_DIR:-$REPO_ROOT/externalAdapters}"
########################################################
# Regenerate ea_endpoints from externalAdapters comments
########################################################
sync_ea_config_from_external() {
# If externalAdapters doesn't exist, just create an empty config and bail
if [[ ! -d "$EXTERNAL_DIR" ]]; then
{
echo "# Auto-generated EA endpoints config"
echo "# externalAdapters directory not found: $EXTERNAL_DIR"
echo "# This file will be rebuilt whenever externalAdapters exists."
echo
} > "$EA_CONFIG"
return 0
fi
# Rebuild ea_endpoints from scratch each time
{
echo "# Auto-generated EA endpoints config"
echo "# Source of truth: comment headers in scripts under externalAdapters/"
echo "# Format: <ea-name> <url>"
echo
} > "$EA_CONFIG"
local f ea_name_found endpoint_found
for f in "$EXTERNAL_DIR"/*; do
[[ -f "$f" ]] || continue
ea_name_found="$(
grep -E '^# *ea-name:' "$f" 2>/dev/null \
| head -n1 \
| sed -E 's/^# *ea-name:[[:space:]]*//'
)" || true
endpoint_found="$(
grep -E '^# *endpoint:' "$f" 2>/dev/null \
| head -n1 \
| sed -E 's/^# *endpoint:[[:space:]]*//'
)" || true
if [[ -n "$ea_name_found" && -n "$endpoint_found" ]]; then
echo "$ea_name_found $endpoint_found" >> "$EA_CONFIG"
fi
done
}
########################################################
# Extract Docker image repository from adapter script
########################################################
get_adapter_image_repo() {
local adapter_name="$1"
local script_path="$EXTERNAL_DIR/$adapter_name"
if [[ ! -f "$script_path" ]]; then
echo "Error: Adapter script not found: $script_path" >&2
exit 1
fi
# Extract the image repo (without tag) from the docker run command
local repo=$(grep -o 'public.ecr.aws/chainlink/adapters/[^:]*' "$script_path" 2>/dev/null | head -1)
if [[ -z "$repo" ]]; then
echo "Error: Could not find Docker image in adapter script: $script_path" >&2
exit 1
fi
printf '%s\n' "$repo"
}
########################################################
# Look up EA URL (with -redis fallback)
########################################################
resolve_ea_url_from_config() {
local name="$1"
sync_ea_config_from_external
local url=""
if [[ -f "$EA_CONFIG" ]]; then
# 1) exact match
url="$(
awk -v n="$name" '
NF && $1 !~ /^#/ && $1==n {print $2}
' "$EA_CONFIG" | head -n1
)" || true
# 2) if not found, try "<name>-redis" (to handle coinpaprika-state vs coinpaprika-state-redis)
if [[ -z "$url" ]]; then
local alt="${name}-redis"
url="$(
awk -v n="$alt" '
NF && $1 !~ /^#/ && $1==n {print $2}
' "$EA_CONFIG" | head -n1
)" || true
fi
fi
if [[ -n "$url" ]]; then
printf '%s\n' "$url"
return 0
fi
# Still not found: fall back to interactive one-off URL
echo "Warning: EA '$name' not found in externalAdapters comments / $(basename "$EA_CONFIG")." >&2
echo "Add lines like these to the appropriate script under externalAdapters/:" >&2
echo " # ea-name: $name" >&2
echo " # endpoint: http://HOST:PORT" >&2
echo >&2
echo "For now, enter a URL to use just for this run (or leave blank to abort):" >&2
local input_url=""
if ! read -r input_url </dev/tty; then
echo "Error: unable to read from /dev/tty." >&2
exit 1
fi
if [[ -z "$input_url" ]]; then
echo "No URL entered. Cannot continue for '$name'." >&2
exit 1
fi
printf '%s\n' "$input_url"
}
########################################################
# BASH COMPLETION FUNCTIONS #
########################################################
install_completion() {
local completion_dir="$HOME/.local/share/bash-completion/completions"
local completion_file="$completion_dir/eaManager"
echo "${b}Installing bash completion...${n}"
# Create directory if it doesn't exist
mkdir -p "$completion_dir"
# Write the completion script
cat > "$completion_file" << 'EOF'
#!/usr/bin/env bash
# Bash completion for eaManager
_eaManager() {
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
# Find the eaManager script directory
local script_dir=""
if [[ -d "$HOME/Chainlink-EA-Manager" ]]; then
script_dir="$HOME/Chainlink-EA-Manager"
elif [[ -d "/usr/local/Chainlink-EA-Manager" ]]; then
script_dir="/usr/local/Chainlink-EA-Manager"
elif command -v eaManager >/dev/null 2>&1; then
script_dir="$(dirname "$(readlink -f "$(command -v eaManager)")")"
fi
# Main options
local main_opts="-i --initialize --init -d --deploy -u --upgrade --update -t --test -v --version -l --list -h --help --install-completion"
# Test sub-options
local test_opts="--ea --url --file --help -e -u -f -h"
# Check if we're in test mode
local in_test_mode=0
for ((i=1; i<${#COMP_WORDS[@]}-1; i++)); do
if [[ "${COMP_WORDS[i]}" == "-t" || "${COMP_WORDS[i]}" == "--test" ]]; then
in_test_mode=1
break
fi
done
# Handle completions based on context
case "${prev}" in
-d|--deploy|-u|--upgrade|--update)
# Complete with adapter names from externalAdapters/
if [[ -d "$script_dir/externalAdapters" ]]; then
local adapters=$(cd "$script_dir/externalAdapters" && ls -1 2>/dev/null)
COMPREPLY=( $(compgen -W "${adapters}" -- ${cur}) )
fi
return 0
;;
-t|--test)
# After -t, offer test sub-options
COMPREPLY=( $(compgen -W "${test_opts}" -- ${cur}) )
return 0
;;
--ea|-e)
# Complete with EA names
if [[ $in_test_mode -eq 1 ]]; then
local ea_names=""
if [[ -f "$script_dir/ea_endpoints" ]]; then
ea_names=$(grep -v '^#' "$script_dir/ea_endpoints" 2>/dev/null | awk '{print $1}')
fi
if [[ -z "$ea_names" ]] && [[ -d "$script_dir/externalAdapters" ]]; then
ea_names=$(cd "$script_dir/externalAdapters" && ls -1 2>/dev/null)
fi
COMPREPLY=( $(compgen -W "${ea_names}" -- ${cur}) )
fi
return 0
;;
--file|-f)
# Complete with files from requests/ directory
if [[ -d "$script_dir/requests" ]]; then
local request_files=$(cd "$script_dir/requests" && ls -1 2>/dev/null)
local prefixed_files=$(cd "$script_dir/requests" && ls -1 2>/dev/null | sed 's|^|requests/|')
COMPREPLY=( $(compgen -W "${request_files} ${prefixed_files}" -- ${cur}) )
fi
COMPREPLY+=( $(compgen -f -- ${cur}) )
return 0
;;
--url)
return 0
;;
esac
# If in test mode, offer test options
if [[ $in_test_mode -eq 1 ]] && [[ ! "${prev}" =~ ^- ]]; then
COMPREPLY=( $(compgen -W "${test_opts}" -- ${cur}) )
return 0
fi
# Default: complete with main options
if [[ ${cur} == -* ]]; then
if [[ $in_test_mode -eq 1 ]]; then
COMPREPLY=( $(compgen -W "${test_opts}" -- ${cur}) )
else
COMPREPLY=( $(compgen -W "${main_opts}" -- ${cur}) )
fi
return 0
fi
# First argument: offer main options
if [[ ${COMP_CWORD} -eq 1 ]]; then
COMPREPLY=( $(compgen -W "${main_opts}" -- ${cur}) )
return 0
fi
}
complete -F _eaManager eaManager
complete -F _eaManager ./eaManager
EOF
chmod +x "$completion_file"
echo "${b}✓ Completion installed to: ${n}$completion_file"
echo ""
echo "To activate completion, run one of:"
echo " ${b}source $completion_file${n}"
echo " ${b}exec bash${n} (restart shell)"
echo ""
echo "Or add this to your ~/.bashrc to make it permanent:"
echo " ${b}source $completion_file${n}"
}
install_completion_quiet() {
local completion_dir="$HOME/.local/share/bash-completion/completions"
local completion_file="$completion_dir/eaManager"
# Create directory if it doesn't exist
mkdir -p "$completion_dir" 2>/dev/null
# Write the completion script (same as above, but silent)
cat > "$completion_file" 2>/dev/null << 'EOF'
#!/usr/bin/env bash
# Bash completion for eaManager
_eaManager() {
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
# Find the eaManager script directory
local script_dir=""
if [[ -d "$HOME/Chainlink-EA-Manager" ]]; then
script_dir="$HOME/Chainlink-EA-Manager"
elif [[ -d "/usr/local/Chainlink-EA-Manager" ]]; then
script_dir="/usr/local/Chainlink-EA-Manager"
elif command -v eaManager >/dev/null 2>&1; then
script_dir="$(dirname "$(readlink -f "$(command -v eaManager)")")"
fi
# Main options
local main_opts="-i --initialize --init -d --deploy -u --upgrade --update -t --test -v --version -l --list -h --help --install-completion"
# Test sub-options
local test_opts="--ea --url --file --help -e -u -f -h"
# Check if we're in test mode
local in_test_mode=0
for ((i=1; i<${#COMP_WORDS[@]}-1; i++)); do
if [[ "${COMP_WORDS[i]}" == "-t" || "${COMP_WORDS[i]}" == "--test" ]]; then
in_test_mode=1
break
fi
done
# Handle completions based on context
case "${prev}" in
-d|--deploy|-u|--upgrade|--update)
# Complete with adapter names from externalAdapters/
if [[ -d "$script_dir/externalAdapters" ]]; then
local adapters=$(cd "$script_dir/externalAdapters" && ls -1 2>/dev/null)
COMPREPLY=( $(compgen -W "${adapters}" -- ${cur}) )
fi
return 0
;;
-t|--test)
# After -t, offer test sub-options
COMPREPLY=( $(compgen -W "${test_opts}" -- ${cur}) )
return 0
;;
--ea|-e)
# Complete with EA names
if [[ $in_test_mode -eq 1 ]]; then
local ea_names=""
if [[ -f "$script_dir/ea_endpoints" ]]; then
ea_names=$(grep -v '^#' "$script_dir/ea_endpoints" 2>/dev/null | awk '{print $1}')
fi
if [[ -z "$ea_names" ]] && [[ -d "$script_dir/externalAdapters" ]]; then
ea_names=$(cd "$script_dir/externalAdapters" && ls -1 2>/dev/null)
fi
COMPREPLY=( $(compgen -W "${ea_names}" -- ${cur}) )
fi
return 0
;;
--file|-f)
# Complete with files from requests/ directory
if [[ -d "$script_dir/requests" ]]; then
local request_files=$(cd "$script_dir/requests" && ls -1 2>/dev/null)
local prefixed_files=$(cd "$script_dir/requests" && ls -1 2>/dev/null | sed 's|^|requests/|')
COMPREPLY=( $(compgen -W "${request_files} ${prefixed_files}" -- ${cur}) )
fi
COMPREPLY+=( $(compgen -f -- ${cur}) )
return 0
;;
--url)
return 0
;;
esac
# If in test mode, offer test options
if [[ $in_test_mode -eq 1 ]] && [[ ! "${prev}" =~ ^- ]]; then
COMPREPLY=( $(compgen -W "${test_opts}" -- ${cur}) )
return 0
fi
# Default: complete with main options
if [[ ${cur} == -* ]]; then
if [[ $in_test_mode -eq 1 ]]; then
COMPREPLY=( $(compgen -W "${test_opts}" -- ${cur}) )
else
COMPREPLY=( $(compgen -W "${main_opts}" -- ${cur}) )
fi
return 0
fi
# First argument: offer main options
if [[ ${COMP_CWORD} -eq 1 ]]; then
COMPREPLY=( $(compgen -W "${main_opts}" -- ${cur}) )
return 0
fi
}
complete -F _eaManager eaManager
complete -F _eaManager ./eaManager
EOF
chmod +x "$completion_file" 2>/dev/null
}
ensure_completion_installed() {
local completion_file="$HOME/.local/share/bash-completion/completions/eaManager"
if [[ ! -f "$completion_file" ]]; then
install_completion_quiet
fi
}
########################################################
# INITIALIZE FUNCTION #
########################################################
Initialize() {
# Initializes a new Docker environment
echo "${b}Initializing new Docker environment...${n}"
# Intall Docker if not already installed
if [ $(dpkg-query -W -f='${Status}' docker-ce 2>/dev/null | grep -c "install ok installed") -eq 0 ];
then
# Check if running as root
if [ "$EUID" -ne 0 ]
then echo "${b}Run as elevated user (sudo)${n}"
exit
fi
read -p "${b} Enter your non-root username:${n} " USERNAME
aptInstall () {
echo "${b}Updating apt & Installing Docker-CE${n}"
apt-get -y update >>/dev/null 2>&1
apt-get install -y apt-transport-https ca-certificates curl gnupg2 software-properties-common >>/dev/null 2>&1
curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add - >>/dev/null 2>&1
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable" >>/dev/null 2>&1
apt-get -y update >>/dev/null 2>&1
apt-get -y install docker-ce >>/dev/null 2>&1
groupadd docker >>/dev/null 2>&1
sudo usermod -aG docker $USERNAME >>/dev/null 2>&1
}
yumInstall (){
echo "Updating yum"
echo "Installing dependencies & Docker-CE"
sudo yum remove docker* >>/dev/null 2>&1
sudo yum -y update >>/dev/null 2>&1
sudo yum -y remove docker* >>/dev/null 2>&1
sudo yum -y install yum-utils >>/dev/null 2>&1
sudo yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo >>/dev/null 2>&1
sudo yum -y install docker-ce docker-ce-cli containerd.io >>/dev/null 2>&1
sudo systemctl start docker >>/dev/null 2>&1
groupadd docker >>/dev/null 2>&1
sudo usermod -aG docker $USERNAME >>/dev/null 2>&1
sudo curl -L "https://github.com/docker/compose/releases/download/1.26.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose >>/dev/null 2>&1
sudo chmod +x /usr/local/bin/docker-compose
docker-compose --version
sudo dnf install git -y
}
apt=`command -v apt-get`
yum=`command -v yum`
if [ -n "$apt" ]; then
echo "apt detected"
aptInstall
elif [ -n "$yum" ]; then
echo "yum detected"
yumInstall
else
echo "Err: apt or yum not detected.";
exit 1;
fi
fi
# create docker network
echo "${b}Creating Docker network...${n}"
docker network create \
--driver=bridge \
--subnet=192.168.0.0/16 \
--gateway=192.168.0.1 \
eas-net
# create directory to use for redis volume
if [ ! -d /home/mlzboy/b2c2/shared/db ]; then
echo "${b}Creating Redis container...${n}"
mkdir ~/.redis;
fi
# deploy redis container
echo ""
echo "${b}Redis volume located at ~/.redis${n}"
docker run \
--name redis-cache \
--restart=unless-stopped \
--net eas-net \
--ip=192.168.1.1 \
-p 6379:6379 \
-v ~/.redis:/data \
-d redis redis-server --maxclients 2500
# Provide Redis container information
echo ""
echo "${b}Redis container info:${n}"
echo "containerName: redis-cache"
echo "IPaddress: 192.168.1.1"
echo "listnPort: 6379"
# Symlink eaManager script to PATH
echo ""
echo "Adding eaManager to PATH"
local script_path="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/eaManager"
sudo ln -sf "$script_path" /usr/bin/eaManager
echo "Created symlink: /usr/bin/eaManager -> $script_path"
# Install bash completion
echo ""
install_completion
}
########################################################
# DEPLOY FUNCTION #
########################################################
Deploy() {
# Check if skopeo is installed, if not, install it
if ! command -v skopeo &> /dev/null; then
echo "skopeo could not be found, installing..."
sudo apt-get update && sudo apt-get install -y skopeo
fi
# Get the actual image repository from the adapter script
local repo=$(get_adapter_image_repo "$1")
local adapter_name=$(basename "$repo" | sed 's/-adapter$//')
local tags
local tag_list
local selected_tag
echo "Fetching available tags for $adapter_name-adapter..."
tags=$(skopeo list-tags "docker://$repo" | jq -r '.Tags[]' | sort -Vr | head -n 10)
if [[ -z "$tags" ]]; then
echo "No tags found for repository: $repo"
exit 1
fi
# Display the tags and prompt the user for selection
echo "Available tags for $repo:"
tag_list=($tags) # Convert tags into an array
for i in "${!tag_list[@]}"; do
echo "$i) ${tag_list[$i]}"
done
read -p "Select a tag (0-${#tag_list[@]}): " selected_index
# Validate input and assign the selected tag
if [[ "$selected_index" =~ ^[0-9]+$ ]] && [[ "$selected_index" -ge 0 ]] && [[ "$selected_index" -lt "${#tag_list[@]}" ]]; then
selected_tag="${tag_list[$selected_index]}"
echo "Selected tag: $selected_tag"
else
echo "Invalid selection."
exit 1
fi
# Ensure selected_tag is not empty
if [ -z "$selected_tag" ]; then
echo "No tag selected, exiting."
exit 1
fi
# Deploy the external adapter with the selected version using the corresponding bash script
echo "Deploying $1 at version $selected_tag"
# Ensure the script exists before attempting to execute it
local deploy_script="externalAdapters/$1"
if [[ ! -f "$deploy_script" ]]; then
echo "Deployment script $deploy_script not found."
exit 1
fi
# Run the deployment script with the selected tag
bash "$deploy_script" "$selected_tag"
}
########################################################
# UPGRADE FUNCTION #
########################################################
Upgrade() {
# Check if skopeo is installed, if not, install it
if ! command -v skopeo &> /dev/null; then
echo "skopeo could not be found, installing..."
sudo apt-get update && sudo apt-get install -y skopeo
fi
# Get the actual image repository from the adapter script
local repo=$(get_adapter_image_repo "$1")
local adapter_name=$(basename "$repo" | sed 's/-adapter$//')
local tags
local tag_list
local selected_tag
echo "Fetching available tags for $adapter_name-adapter..."
tags=$(skopeo list-tags "docker://$repo" | jq -r '.Tags[]' | sort -Vr | head -n 10)
if [[ -z "$tags" ]]; then
echo "No tags found for repository: $repo"
exit 1
fi
# Display the tags and prompt the user for selection
echo "Available tags for $repo:"
tag_list=($tags) # Convert tags into an array
for i in "${!tag_list[@]}"; do
echo "$i) ${tag_list[$i]}"
done
read -p "Select a tag (0-${#tag_list[@]}): " selected_index
# Validate input and assign the selected tag
if [[ "$selected_index" =~ ^[0-9]+$ ]] && [[ "$selected_index" -ge 0 ]] && [[ "$selected_index" -lt "${#tag_list[@]}" ]]; then
selected_tag="${tag_list[$selected_index]}"
echo "Selected tag: $selected_tag"
else
echo "Invalid selection."
exit 1
fi
# Ensure selected_tag is not empty
if [ -z "$selected_tag" ]; then
echo "No tag selected, exiting."
exit 1
fi
# Stop and remove the existing container
echo "Stopping, removing, and redeploying $1 at version $selected_tag"
docker stop "$1-redis"
docker rm "$1-redis"
# Ensure the script exists before attempting to execute it
local upgrade_script="externalAdapters/$1"
if [[ ! -f "$upgrade_script" ]]; then
echo "Upgrade script $upgrade_script not found."
exit 1
fi
# Run the upgrade script with the selected tag
bash "$upgrade_script" "$selected_tag"
}
########################################################
# TEST FUNCTION #
########################################################
Test() {
# Enhanced EA testing with flexible options
local ea_name=""
local ea_url=""
local payload_file=""
# Parse test-specific arguments
while [[ $# -gt 0 ]]; do
case "$1" in
-e|--ea)
ea_name="$2"
shift 2
;;
-u|--url)
ea_url="$2"
shift 2
;;
-f|--file)
payload_file="$2"
shift 2
;;
-h|--help)
echo "Test Usage:"
echo " eaManager -t --ea <ea-name> [--file payload.json]"
echo " eaManager -t --url <EA_URL> [--file payload.json]"
echo ""
echo "If --file is not provided, JSON is read from stdin (paste and Ctrl-D)."
echo ""
echo "Options:"
echo " -e, --ea EA name (looked up via comments in externalAdapters and cached in ea_endpoints)"
echo " -u, --url Full EA URL (bypass config / parsing)"
echo " -f, --file JSON payload file"
echo " -h, --help Show this help"
echo ""
echo "Examples:"
echo " eaManager -t --ea coinpaprika-state"
echo " eaManager -t --ea allium-state --file req.json"
echo " eaManager -t --url http://localhost:8010 --file req.json"
exit 0
;;
*)
echo "Unknown argument for test: $1" >&2
echo "Use 'eaManager -t --help' for test usage" >&2
exit 1
;;
esac
done
# Resolve URL
if [[ -z "${ea_url:-}" ]]; then
if [[ -z "${ea_name:-}" ]]; then
echo "Error: you must provide either --ea <name> or --url <URL>" >&2
echo "Use 'eaManager -t --help' for test usage" >&2
exit 1
fi
ea_url="$(resolve_ea_url_from_config "$ea_name")"
fi
# Read payload
local payload=""
if [[ -n "${payload_file:-}" ]]; then
if [[ ! -f "$payload_file" ]]; then
echo "Error: payload file '$payload_file' not found." >&2
exit 1
fi
payload="$(<"$payload_file")"
else
echo "Paste JSON payload, then press Ctrl-D:" >&2
payload="$(cat)"
fi
if [[ -z "$payload" ]]; then
echo "Error: empty payload." >&2
exit 1
fi
# Validate JSON if jq exists
if command -v jq >/dev/null 2>&1; then
if ! echo "$payload" | jq empty >/dev/null 2>&1; then
echo "Error: payload is not valid JSON (jq failed)." >&2
exit 1
fi
fi
# Fire request
echo "⇒ POST $ea_url" >&2
if command -v jq >/dev/null 2>&1; then
curl -sS -X POST "$ea_url" \
-H "Content-Type: application/json" \
-d "$payload" | jq .
else
curl -sS -X POST "$ea_url" \
-H "Content-Type: application/json" \
-d "$payload"
fi
echo "⇐ Done" >&2
}
########################################################
# VERSION FUNCTION #
########################################################
Version() {
# Print Version
echo "eaManager version: ${b}$VERSION${n}"
}
########################################################
# LIST FUNCTION #
########################################################
List() {
# List EA options
echo "${b}Listing Supported EAs:${n}"
echo ""
tree externalAdapters
}
########################################################
# HELP FUNCTION #
########################################################
Help() {
# Display Help
echo "This script helps you deploy, manage, and upgrade your Chainlink external adapters."
echo
echo "Usage: ${b}eaManager [option] [arguments]${n}"
echo ""
echo "Options:"
echo ""
echo "${b}-i, --initialize, --init${n}"
echo " Initializes a generic EA environment."
echo " Creates Docker network, deploys redis container,"
echo " use this if you want a quick turnkey deployment."
echo ""
echo "${b}-d, --deploy${n} <adapter-name>"
echo " Deploys new external adapter."
echo " ex: eaManager --deploy coingecko"
echo ""
echo "${b}-u, --upgrade, --update${n} <adapter-name>"
echo " Upgrades external adapter."
echo " ex: eaManager --upgrade coingecko"
echo ""
echo "${b}-t, --test${n} [options]"
echo " Send a test request to external adapter."
echo " ex: eaManager --test --ea coinpaprika-state"
echo " ex: eaManager --test --ea allium-state --file req.json"
echo " ex: eaManager --test --url http://localhost:8010 --file req.json"
echo " Use 'eaManager --test --help' for detailed test options"
echo ""
echo "${b}-v, --version${n}"
echo " Print script version and exit."
echo ""
echo "${b}-l, --list${n}"
echo " Print all supported EA names and exit."
echo ""
echo "${b}-h, --help${n}"
echo " Print this Help function and exit."
echo ""
echo "${b}--install-completion${n}"
echo " Install bash tab completion for eaManager."
echo " Enables auto-complete for all commands and adapter names."
echo " Also installed automatically during -i (Initialize)."
echo
}
########################################################
# MAIN #
########################################################
# Silently ensure completion is installed
ensure_completion_installed
# Show help if no arguments
if [[ $# -eq 0 ]]; then
Help
exit 0
fi
# Parse arguments
while [[ $# -gt 0 ]]; do
case "$1" in
-i|--initialize|--init)
Initialize
exit 0
;;
-d|--deploy)
shift
if [[ -z "$1" ]]; then
echo "Error: --deploy requires an adapter name" >&2
echo "Usage: eaManager --deploy <adapter-name>" >&2
exit 1
fi
Deploy "$@"
exit 0
;;
-u|--upgrade|--update)
shift
if [[ -z "$1" ]]; then
echo "Error: --upgrade requires an adapter name" >&2
echo "Usage: eaManager --upgrade <adapter-name>" >&2
exit 1
fi
Upgrade "$@"
exit 0
;;
-t|--test)
shift
Test "$@"
exit 0
;;
-v|--version)
Version
exit 0
;;
-l|--list)
List
exit 0
;;
-h|--help)
Help
exit 0
;;
--install-completion)
install_completion
exit 0
;;
*)
echo "Unknown option: $1" >&2
echo "Run 'eaManager --help' for usage information" >&2
exit 1
;;
esac
done