-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmacos-installer.sh
More file actions
executable file
Β·972 lines (832 loc) Β· 30.8 KB
/
macos-installer.sh
File metadata and controls
executable file
Β·972 lines (832 loc) Β· 30.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
#!/bin/bash
set -e
# Script Configuration
SCRIPT_NAME="macos-installer.sh"
SCRIPT_VERSION="1.0.0"
# Default Configuration
NAMESPACE="self-hosted-operator"
CHART_NAME="self-hosted-operator"
IMAGE_NAME="self-hosted-operator"
DEFAULT_ENV="ea" # Default environment as per the release state. Change this value as release progress.
DEFAULT_OPERATION="install"
DEFAULT_USE_ACR="false" # Temporary backward compatibility for Azure ACR
DEFAULT_PEGASUS_ENABLED="false" # Enable Pegasus features
# Environment-specific settings
ECR_ALIAS_GA="j0s5s8b0/ga" # GA ECR alias
ECR_ALIAS_EA="g4u4y4x2/lab" # EA ECR alias #m5i8c6m7/ea
ECR_ALIAS_TEST="u4p0z5h7/test" # Test ECR alias
ECR_ALIAS_DEV="g4u4y4x2/lab" # Dev ECR alias
PUB_REGISTRY="public.ecr.aws"
# Global variables (set by parse_arguments)
SHO_VERSION=""
ENV="$DEFAULT_ENV"
OPERATION="$DEFAULT_OPERATION"
USE_ACR="$DEFAULT_USE_ACR"
PEGASUS_ENABLED="$DEFAULT_PEGASUS_ENABLED"
# Derived configuration (set by setup_environment)
ECR_ALIAS=""
CHART_REPOSITORY=""
IMAGE_REGISTRY=""
IMAGE_REPOSITORY=""
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Logging functions
log_info() {
echo -e "${BLUE}βΉοΈ $1${NC}"
}
log_success() {
echo -e "${GREEN}β
$1${NC}"
}
log_warning() {
echo -e "${YELLOW}β οΈ $1${NC}"
}
log_error() {
echo -e "${RED}β $1${NC}"
}
log_step() {
echo -e "${BLUE}π $1${NC}"
}
# Function to show usage
show_usage() {
cat << EOF
${SCRIPT_NAME} v${SCRIPT_VERSION} - OutSystems Self-Hosted Operator for macOS
USAGE:
${SCRIPT_NAME} [OPTIONS]
OPTIONS:
--version=VERSION SHO version to install/manage (default: latest)
--env=ENVIRONMENT Environment: test, ea, ga (default: ga)
--operation=OPERATION Operation: install, uninstall, get-console-url (default: install)
--use-acr=BOOLEAN Use ACR registry: true, false (default: false)
[TEMPORARY: Backward compatibility for Azure ACR]
--pegasus-enabled=BOOLEAN Enable Pegasus features: true, false (default: false)
Note: Only supported in test environment
--help, -h Show this help message
OPERATIONS:
install Install OutSystems Self-Hosted Operator
uninstall Uninstall OutSystems Self-Hosted Operator
get-console-url Get console URL for installed SHO
EXAMPLES:
# Install latest version in GA environment
${SCRIPT_NAME}
# Install specific version in EA environment
${SCRIPT_NAME} --operation=install --version=0.2.3 --env=ea
# Get console URL for test environment
${SCRIPT_NAME} --operation=get-console-url --env=test
# Uninstall from GA environment
${SCRIPT_NAME} --operation=uninstall --env=ga
EOF
}
logout_ecr_public() {
# Remove Helm and Docker credentials for the public registry if they exist
helm registry logout "$PUB_REGISTRY" 2>/dev/null || true
docker logout "$PUB_REGISTRY" 2>/dev/null || true
}
# Function to validate arguments
validate_arguments() {
log_step "Validating arguments..."
# Set ENV to default if not provided
if [[ -z "$ENV" ]]; then
log_info "No environment specified. Using default: $DEFAULT_ENV"
ENV="$DEFAULT_ENV"
fi
# Validate environment
case "$ENV" in
ga|ea|test|dev)
log_success "Environment '$ENV' is valid"
;;
*)
log_error "Invalid environment: '$ENV'. Must be one of: ga, ea, test, dev"
return 1
;;
esac
# Validate operation
case "$OPERATION" in
install|uninstall|get-console-url)
# Temporarily disable uninstall operation
if [[ "$OPERATION" == "uninstall" ]]; then
log_error "The uninstall operation is currently unavailable. Please contact support for assistance with uninstallation."
return 1
fi
log_success "Operation '$OPERATION' is valid"
;;
*)
log_error "Invalid operation: '$OPERATION'. Must be one of: install, uninstall, get-console-url"
return 1
;;
esac
# Validate version format if provided
if [[ -n "$SHO_VERSION" && "$SHO_VERSION" != "latest" ]]; then
if [[ ! "$SHO_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
log_error "Invalid version format: '$SHO_VERSION'. Expected format: x.y.z (e.g., 0.2.3)"
return 1
fi
log_success "Version '$SHO_VERSION' format is valid"
fi
# Validate Pegasus configuration
if [[ "$PEGASUS_ENABLED" == "true" && "$ENV" != "test" ]]; then
log_error "Pegasus features (--pegasus-enabled=true) are only supported in 'test' environment"
log_info "Current environment: $ENV"
return 1
fi
# Validate ACR configuration only for install operation
if [[ "$USE_ACR" == "true" ]]; then
if [[ "$OPERATION" == "install" ]]; then
log_step "Validating ACR configuration..."
local missing_vars=()
if [[ -z "${SP_ID}" ]]; then
missing_vars+=("SP_ID")
fi
if [[ -z "${SP_SECRET}" ]]; then
missing_vars+=("SP_SECRET")
fi
if [[ -z "${SH_REGISTRY}" ]]; then
missing_vars+=("SH_REGISTRY")
fi
if [[ ${#missing_vars[@]} -gt 0 ]]; then
log_error "Missing required environment variables for ACR: ${missing_vars[*]}"
log_info "Please set the following environment variables:"
for var in "${missing_vars[@]}"; do
log_info " export $var=<value>"
done
return 1
fi
log_success "ACR configuration is valid"
else
log_info "Skipping ACR configuration validation (not required for operation: $OPERATION)"
fi
fi
return 0
}
# Function to parse command line arguments
parse_arguments() {
while [[ $# -gt 0 ]]; do
case $1 in
--version=*)
SHO_VERSION="${1#*=}"
log_info "Version specified: $SHO_VERSION"
;;
--env=*)
ENV="${1#*=}"
log_info "Environment specified: $ENV"
;;
--operation=*)
OPERATION="${1#*=}"
log_info "Operation specified: $OPERATION"
;;
--use-acr=*)
USE_ACR="${1#*=}"
# Normalize boolean values
case "$(echo "${USE_ACR}" | tr '[:upper:]' '[:lower:]')" in
true|1|yes|on)
USE_ACR="true"
log_info "ACR registry mode enabled"
;;
false|0|no|off)
USE_ACR="false"
;;
*)
log_error "Invalid value for --use-acr: '$USE_ACR'. Must be true or false"
exit 1
;;
esac
;;
--use-acr)
# Support flag without value (defaults to true for backward compatibility)
USE_ACR="true"
log_info "ACR registry mode enabled"
;;
--pegasus-enabled=*)
PEGASUS_ENABLED="${1#*=}"
# Normalize boolean values
case "$(echo "${PEGASUS_ENABLED}" | tr '[:upper:]' '[:lower:]')" in
true|1|yes|on)
PEGASUS_ENABLED="true"
log_info "Pegasus features enabled"
;;
false|0|no|off)
PEGASUS_ENABLED="false"
;;
*)
log_error "Invalid value for --pegasus-enabled: '$PEGASUS_ENABLED'. Must be true or false"
exit 1
;;
esac
;;
--help|-h)
show_usage
exit 0
;;
*)
log_error "Unknown option: $1"
echo
show_usage
exit 1
;;
esac
shift
done
}
# Function to setup environment-specific configuration
setup_environment() {
log_step "Setting up environment configuration for: $ENV"
case "$ENV" in
ga)
ECR_ALIAS="$ECR_ALIAS_GA"
log_info "Using GA ECR alias: $ECR_ALIAS"
;;
ea)
ECR_ALIAS="$ECR_ALIAS_EA"
log_info "Using EA ECR alias: $ECR_ALIAS"
;;
test)
ECR_ALIAS="$ECR_ALIAS_TEST"
log_info "Using Test ECR alias: $ECR_ALIAS"
;;
dev)
ECR_ALIAS="$ECR_ALIAS_DEV"
log_info "Using Dev ECR alias: $ECR_ALIAS"
;;
*)
log_error "Invalid environment: '$ENV'. Must be one of: ga, ea, test, dev"
exit 1
;;
esac
# Set repository URLs
CHART_REPOSITORY="${ECR_ALIAS}/helm/self-hosted-operator"
IMAGE_REGISTRY="${ECR_ALIAS}"
IMAGE_REPOSITORY="${ECR_ALIAS}/$IMAGE_NAME"
log_info "Using ECR repository: $PUB_REGISTRY/$CHART_REPOSITORY"
log_success "Environment setup completed"
}
# Function to check if command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to install package using Homebrew
install_package_homebrew() {
local package="$1"
log_step "Installing $package via Homebrew..."
if ! command_exists brew; then
log_error "Homebrew not found. Please install Homebrew first: https://brew.sh"
return 1
fi
brew install "$package"
}
# Function to install kubectl
install_kubectl() {
log_step "Installing kubectl..."
if command_exists brew; then
log_info "Installing kubectl via Homebrew..."
brew install kubectl
else
log_info "Installing kubectl via direct download..."
# Get the latest stable version
local version
version=$(curl -L -s https://dl.k8s.io/release/stable.txt)
if [[ -z "$version" ]]; then
log_error "Failed to get kubectl version"
return 1
fi
log_info "Downloading kubectl $version..."
curl -LO "https://dl.k8s.io/release/$version/bin/darwin/amd64/kubectl"
chmod +x kubectl
# Try to move to system PATH
if sudo mv kubectl /usr/local/bin/ 2>/dev/null; then
log_success "kubectl installed to /usr/local/bin/"
elif mkdir -p ~/bin && mv kubectl ~/bin/ && export PATH="$HOME/bin:$PATH"; then
log_success "kubectl installed to ~/bin/"
log_info "Added ~/bin to PATH for this session"
log_info "Add 'export PATH=\"\$HOME/bin:\$PATH\"' to your shell profile for permanent access"
else
log_error "Failed to install kubectl to system PATH"
return 1
fi
fi
# Verify installation
if command_exists kubectl; then
log_success "kubectl installed successfully"
return 0
else
log_error "kubectl installation failed"
return 1
fi
}
# Function to install Helm
install_helm() {
log_step "Installing Helm..."
if command_exists brew; then
log_info "Installing Helm via Homebrew..."
brew install helm
else
log_info "Installing Helm via official script..."
# Use official Helm installation script
curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
chmod 700 get_helm.sh
./get_helm.sh
rm -f get_helm.sh
fi
if command_exists helm; then
log_success "Helm installed successfully"
helm version --short
return 0
else
log_error "Helm installation failed"
return 1
fi
}
# Function to check dependencies
check_dependencies() {
log_step "Checking dependencies for macOS..."
local all_deps_ok=true
# Check if Homebrew is available
if command_exists brew; then
log_success "Homebrew is available"
else
log_warning "Homebrew not found. Some installations may fall back to manual methods."
log_info "For best experience, install Homebrew from: https://brew.sh"
fi
# Check jq
if ! command_exists jq; then
log_warning "jq not found. Installing..."
if ! install_package_homebrew jq; then
all_deps_ok=false
fi
else
log_success "jq is installed"
fi
# Check curl (usually pre-installed on macOS)
if ! command_exists curl; then
log_warning "curl not found. Installing..."
if ! install_package_homebrew curl; then
all_deps_ok=false
fi
else
log_success "curl is installed"
fi
# Check kubectl
if ! command_exists kubectl; then
log_warning "kubectl not found. Installing..."
if ! install_kubectl; then
all_deps_ok=false
fi
else
log_success "kubectl is installed"
fi
# Check Helm
if ! command_exists helm; then
log_warning "Helm not found. Installing..."
if ! install_helm; then
all_deps_ok=false
fi
else
log_success "Helm is installed"
helm version --short
fi
# Check Kubernetes connectivity
log_step "Checking Kubernetes cluster connectivity..."
if kubectl cluster-info >/dev/null 2>&1; then
log_success "Connected to Kubernetes cluster"
else
log_error "Cannot connect to Kubernetes cluster"
log_info "Make sure you have a valid kubeconfig and cluster access"
all_deps_ok=false
fi
if [[ "$all_deps_ok" == true ]]; then
log_success "All dependencies are satisfied"
return 0
else
log_error "Some dependencies are missing or failed to install"
return 1
fi
}
# Function to get latest SHO version
get_latest_sho_version() {
log_step "Fetching latest SHO version..."
# Get public ECR token
local token_json
token_json=$(curl -sL "https://${PUB_REGISTRY}/token?scope=repository:${CHART_REPOSITORY}:pull")
if [[ $? -ne 0 || -z "$token_json" ]]; then
log_error "Failed to get ECR token"
return 1
fi
local token
token=$(echo "$token_json" | jq -r '.token')
if [[ -z "$token" || "$token" == "null" ]]; then
log_error "Failed to extract token from ECR response"
return 1
fi
# Get chart tags
local tags_json
tags_json=$(curl -s -H "Authorization: Bearer $token" "https://${PUB_REGISTRY}/v2/${CHART_REPOSITORY}/tags/list")
if [[ $? -ne 0 || -z "$tags_json" ]]; then
log_error "Failed to fetch chart tags"
return 1
fi
# Find latest version (assumes semantic versioning)
local latest_version
latest_version=$(echo "$tags_json" | jq -r '.tags[]' | grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' | sort -V | tail -n 1)
if [[ -z "$latest_version" ]]; then
log_error "Failed to find valid chart version"
echo "Available tags:"
echo "$tags_json" | jq -r '.tags[]'
return 1
fi
SHO_VERSION="$latest_version"
log_success "Latest version found: $SHO_VERSION"
return 0
}
# Function to install SHO
sho_install() {
log_step "Installing OutSystems Self-Hosted Operator..."
# Validate if self-hosted-operator namespace already exists and its not in terminating state
if kubectl get namespace "$NAMESPACE" >/dev/null 2>&1; then
local ns_status
ns_status=$(kubectl get namespace "$NAMESPACE" -o jsonpath='{.status.phase}' 2>/dev/null)
if [[ "$ns_status" == "Terminating" ]]; then
log_error "Namespace '$NAMESPACE' is in 'Terminating' state. Please resolve this before proceeding."
return 1
else
log_info "Namespace '$NAMESPACE' already exists."
fi
fi
log_info "Installing SHO version: $SHO_VERSION"
log_info "Environment: $ENV"
log_info "Namespace: $NAMESPACE"
# Enable OCI mode for Helm
export HELM_EXPERIMENTAL_OCI=1
# Logout from ECR public registry to avoid stale credentials
logout_ecr_public
# Pull chart to temp directory
local chart_oci="oci://${PUB_REGISTRY}/${CHART_REPOSITORY}"
local tmpdir
tmpdir="$(mktemp -d)"
trap 'rm -rf "${tmpdir}"' EXIT
log_step "Pulling chart from: $chart_oci"
if ! helm pull "${chart_oci}" --version "${SHO_VERSION}" -d "${tmpdir}"; then
log_error "Failed to pull Helm chart"
return 1
fi
# Find chart file
local chart_file="${tmpdir}/${CHART_NAME}-${SHO_VERSION}.tgz"
if [[ ! -f "$chart_file" ]]; then
chart_file="$(find "${tmpdir}" -maxdepth 1 -type f -name '*.tgz' | head -n1)"
fi
if [[ -z "$chart_file" || ! -f "$chart_file" ]]; then
log_error "Could not find pulled chart package in ${tmpdir}"
return 1
fi
log_success "Chart package ready: $chart_file"
# Install/upgrade chart
log_step "Installing/upgrading SHO in namespace $NAMESPACE..."
local timestamp
timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
local install_output
if [[ "$USE_ACR" == "true" ]]; then
log_info "Installing with ACR registry configuration"
install_output=$(helm upgrade --install "${CHART_NAME}" "${chart_file}" \
--namespace "$NAMESPACE" \
--create-namespace \
--set "image.registry=${PUB_REGISTRY}/${IMAGE_REGISTRY}" \
--set "image.repository=${IMAGE_NAME}" \
--set "image.tag=v${SHO_VERSION}" \
--set-string "podAnnotations.timestamp=$timestamp" \
--set "ring=$ENV" \
--set "registry.url=${SH_REGISTRY}" \
--set "registry.username=${SP_ID}" \
--set "registry.password=${SP_SECRET}" \
--set "enableECR.enabled=false" \
--set "pegasusEnabled=$PEGASUS_ENABLED" 2>&1)
else
install_output=$(helm upgrade --install "${CHART_NAME}" "${chart_file}" \
--namespace "$NAMESPACE" \
--create-namespace \
--set "image.registry=${PUB_REGISTRY}/${IMAGE_REGISTRY}" \
--set "image.repository=${IMAGE_NAME}" \
--set "image.tag=v${SHO_VERSION}" \
--set-string "podAnnotations.timestamp=$timestamp" \
--set "ring=$ENV" \
--set "pegasusEnabled=$PEGASUS_ENABLED" 2>&1)
fi
if [[ $? -eq 0 ]]; then
log_success "OutSystems Self-Hosted Operator installed successfully!"
log_info "Installation details:"
echo "$install_output"
# Wait for pods to be ready
if wait_for_pods_ready; then
log_success "SHO is running successfully!"
start_port_forwarding
else
log_warning "Installation completed but pods are not ready yet"
show_troubleshooting_commands
return 2 # Return special code to indicate warning
fi
return 0
else
log_error "Failed to install SHO"
log_info "Error details:"
echo "$install_output"
return 1
fi
}
# Function to wait for pods to be ready
wait_for_pods_ready() {
log_step "Waiting for SHO pods to be ready..."
# Disable debug output for this function to prevent variable assignments from being displayed
local previous_debug_state
if [[ $- == *x* ]]; then
previous_debug_state="x"
set +x
fi
local max_wait=300 # 5 minutes
local check_interval=10
local elapsed=0
while [[ $elapsed -lt $max_wait ]]; do
local pod_info
pod_info=$(kubectl get pods -n "$NAMESPACE" -l "app.kubernetes.io/instance=$CHART_NAME" \
-o custom-columns="NAME:.metadata.name,STATUS:.status.phase,READY:.status.containerStatuses[0].ready" \
--no-headers 2>/dev/null)
if [[ -n "$pod_info" ]]; then
local running_pods
running_pods=$(echo "$pod_info" | grep "Running" | grep "true" | wc -l | tr -d ' ')
local total_pods
total_pods=$(echo "$pod_info" | wc -l | tr -d ' ')
if [[ $running_pods -gt 0 && $running_pods -eq $total_pods ]]; then
log_success "All SHO pods are running and ready!"
# Restore debug state if it was enabled
[[ "$previous_debug_state" == "x" ]] && set -x
return 0
elif echo "$pod_info" | grep -q "Error\|CrashLoopBackOff\|ImagePullBackOff"; then
log_error "Pod(s) in error state detected!"
kubectl describe pods -n "$NAMESPACE" -l "app.kubernetes.io/instance=$CHART_NAME"
# Restore debug state if it was enabled
[[ "$previous_debug_state" == "x" ]] && set -x
return 1
else
log_info "Pods still starting... ($running_pods/$total_pods ready) - waiting ${check_interval}s..."
fi
else
log_info "No pods found yet... (${elapsed}s elapsed)"
fi
sleep $check_interval
elapsed=$((elapsed + check_interval))
done
log_warning "Timeout reached while waiting for pods to be ready"
# Restore debug state if it was enabled
[[ "$previous_debug_state" == "x" ]] && set -x
return 1
}
# Function to start port forwarding
start_port_forwarding() {
log_step "Setting up port forwarding..."
local service_name="$CHART_NAME"
local local_port=5050
local service_port=5050
if ! kubectl get svc "$service_name" -n "$NAMESPACE" >/dev/null 2>&1; then
log_error "Service $service_name does not exist in namespace $NAMESPACE"
return 1
fi
# Kill any existing port forwarding on the same port
log_info "Checking for existing port forwarding on port $local_port..."
local existing_pids
existing_pids=$(pgrep -f "kubectl.*port-forward.*:$local_port" 2>/dev/null || true)
if [[ -n "$existing_pids" ]]; then
log_info "Stopping existing port forwarding process(es)..."
echo "$existing_pids" | xargs kill 2>/dev/null || true
sleep 2
fi
# Start port forwarding in background
log_info "Starting port forwarding: localhost:$local_port -> $service_name:$service_port"
kubectl port-forward -n "$NAMESPACE" svc/"$service_name" "$local_port:$service_port" >/dev/null 2>&1 &
local pf_pid=$!
# Wait a moment for port forwarding to establish
log_info "Waiting for port forwarding to establish..."
sleep 5
local local_url="http://localhost:$local_port"
log_success "Port forwarding established!"
log_success "SHO Console URL: $local_url"
# Test URL accessibility
local max_attempts=12 # 60 seconds total
local attempts=0
local accessible=false
log_step "Testing console accessibility..."
while [[ $attempts -lt $max_attempts && "$accessible" == "false" ]]; do
if test_url_accessible "$local_url"; then
accessible=true
log_success "SHO console is responding!"
open_browser "$local_url"
log_success "Port forwarding is running in the background (PID: $pf_pid)"
log_info "To stop port forwarding, run: kill $pf_pid"
else
attempts=$((attempts + 1))
if [[ $attempts -lt $max_attempts ]]; then
log_info "Console not ready yet. Attempt $attempts/$max_attempts - waiting 5s..."
sleep 5
fi
fi
done
if [[ "$accessible" == "false" ]]; then
log_warning "SHO console is not yet responding"
log_info "Please wait a few minutes and access: $local_url"
log_info "Port forwarding is running in the background (PID: $pf_pid)"
fi
return 0
}
# Function to test URL accessibility
test_url_accessible() {
local url="$1"
local max_tries=5
local try=1
while [[ $try -le $max_tries ]]; do
if curl -s -f --connect-timeout 10 --max-time 10 --head "$url" >/dev/null 2>&1; then
return 0
fi
try=$((try + 1))
sleep 5
done
return 1
}
# Function to open browser (macOS)
open_browser() {
local url="$1"
if command_exists open; then
open "$url"
log_success "Browser opened"
else
log_info "Please open the URL manually: $url"
fi
}
# Function to uninstall SHO
sho_uninstall() {
log_step "Uninstalling OutSystems Self-Hosted Operator..."
local route_name="${CHART_NAME}-public"
echo
log_warning "WARNING: You are about to uninstall OutSystems Self-Hosted Operator"
log_info "This will remove the Helm release and stop any port forwarding"
log_info "Release: $CHART_NAME"
log_info "Namespace: $NAMESPACE"
echo
echo -n "Are you sure you want to proceed? (y/n): "
read -r confirm
if [[ ! "$confirm" =~ ^(yes|y)$ ]]; then
log_info "Uninstallation cancelled"
exit 0
fi
# Check if release exists
if ! helm status "$CHART_NAME" -n "$NAMESPACE" >/dev/null 2>&1; then
log_error "Release $CHART_NAME not found in namespace $NAMESPACE"
return 1
fi
# Stop any existing port forwarding processes
log_step "Stopping port forwarding processes..."
local pf_pids
pf_pids=$(pgrep -f "kubectl.*port-forward" 2>/dev/null || true)
if [[ -n "$pf_pids" ]]; then
echo "$pf_pids" | xargs kill 2>/dev/null || true
log_info "Port forwarding processes stopped"
else
log_info "No port forwarding processes found"
fi
# Clean up resources
log_step "Cleaning up resources..."
kubectl get selfhostedruntimes -o name 2>/dev/null | xargs -I{} kubectl patch {} --type merge -p '{"metadata":{"finalizers":null}}' || true
kubectl get selfhostedvaultoperators -o name 2>/dev/null | xargs -I{} kubectl patch {} --type merge -p '{"metadata":{"finalizers":null}}' || true
kubectl delete selfhostedruntime --ignore-not-found self-hosted-runtime || true
# Uninstall Helm release
log_step "Uninstalling Helm release..."
if helm uninstall "$CHART_NAME" -n "$NAMESPACE"; then
log_success "SHO release uninstalled successfully"
# Optional: Delete namespace
echo -n "Do you want to delete the namespace '$NAMESPACE'? (y/n): "
read -r delete_ns
if [[ "$delete_ns" =~ ^(yes|y)$ ]]; then
kubectl delete namespace "$NAMESPACE" --wait=false || true
log_info "Namespace deletion initiated"
fi
log_success "OutSystems Self-Hosted Operator uninstalled successfully!"
return 0
else
log_error "Failed to uninstall SHO release"
return 1
fi
}
# Function to get console URL
get_console_url() {
log_step "Getting console URL for OutSystems Self-Hosted Operator..."
# Check if SHO is installed
if ! helm status "$CHART_NAME" -n "$NAMESPACE" >/dev/null 2>&1; then
log_error "OutSystems Self-Hosted Operator is not installed"
log_info "Please install it first using: $SCRIPT_NAME --operation=install"
return 1
fi
# Check if pods are running
local pods_status
pods_status=$(kubectl get pods -n "$NAMESPACE" -l app.kubernetes.io/name="$CHART_NAME" -o jsonpath='{.items[*].status.phase}' 2>/dev/null)
if [[ "$pods_status" != *"Running"* ]]; then
log_error "SHO pods are not running"
log_info "Please ensure the SHO installation is healthy"
return 1
fi
log_success "SHO is installed and pods are running"
# Start new port forwarding
log_info "Starting port forwarding..."
start_port_forwarding
}
# Function to show troubleshooting commands
show_troubleshooting_commands() {
cat << EOF
π οΈ Troubleshooting Commands:
π Check pod status:
kubectl get pods -n $NAMESPACE -l app.kubernetes.io/instance=$CHART_NAME
π Describe pods:
kubectl describe pods -n $NAMESPACE -l app.kubernetes.io/instance=$CHART_NAME
π View pod logs:
kubectl logs -n $NAMESPACE -l app.kubernetes.io/instance=$CHART_NAME --tail=50
π Check events:
kubectl get events -n $NAMESPACE --sort-by=.metadata.creationTimestamp
β‘ Check helm status:
helm status $CHART_NAME -n $NAMESPACE
π Restart deployment:
kubectl rollout restart deployment -n $NAMESPACE -l app.kubernetes.io/instance=$CHART_NAME
EOF
}
# Function to show configuration summary
show_configuration() {
echo ""
echo "=== Configuration Summary ==="
echo "Script Version: $SCRIPT_VERSION"
echo "Platform: macOS"
echo "Operation: $OPERATION"
echo "Environment: $ENV"
if [[ "$OPERATION" == "install" ]]; then
echo "Version: ${SHO_VERSION:-latest}"
echo "Use ACR: $USE_ACR"
fi
echo "Namespace: $NAMESPACE"
echo "Chart Name: $CHART_NAME"
echo "Repository: $PUB_REGISTRY/$CHART_REPOSITORY"
echo "Image Registry: $PUB_REGISTRY/$IMAGE_REGISTRY"
echo ""
}
# Main function
main() {
echo "π OutSystems Self-Hosted Operator macOS Installer v${SCRIPT_VERSION}"
echo
# Parse command line arguments
parse_arguments "$@"
# Validate arguments
if ! validate_arguments; then
exit 1
fi
# Setup environment
setup_environment
# Check dependencies
if ! check_dependencies; then
log_error "Dependency check failed. Please resolve issues and try again."
exit 1
fi
# For install operation, get version if not specified before showing configuration
if [[ "$OPERATION" == "install" ]]; then
if [[ -z "$SHO_VERSION" || "$SHO_VERSION" == "latest" ]]; then
if ! get_latest_sho_version; then
log_error "Failed to fetch latest SHO version"
return 1
fi
fi
fi
# Show configuration
show_configuration
# Execute operation
case "$OPERATION" in
install)
sho_install
;;
uninstall)
sho_uninstall
;;
get-console-url)
get_console_url
;;
*)
log_error "Unknown operation: $OPERATION"
exit 1
;;
esac
local exit_code=$?
if [[ $exit_code -eq 0 ]]; then
log_success "Operation '$OPERATION' completed successfully!"
elif [[ $exit_code -eq 2 ]]; then
log_warning "Operation '$OPERATION' completed with warning!"
exit_code=0 # Still exit successfully for automation
else
log_error "Operation '$OPERATION' failed with exit code $exit_code"
fi
exit $exit_code
}
# Run main function if script is executed directly or piped to shell
if [[ "${BASH_SOURCE[0]}" == "${0}" ]] || [[ -z "${BASH_SOURCE[0]}" ]] || [[ "${BASH_SOURCE[0]}" == "-" ]]; then
main "$@"
fi