forked from dell/omnia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathomnia.sh
More file actions
executable file
·2518 lines (2124 loc) · 97.8 KB
/
omnia.sh
File metadata and controls
executable file
·2518 lines (2124 loc) · 97.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# Copyright 2025 Dell Inc. or its subsidiaries. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# This script is used to generate the Omnia core docker image.
# The image is based on Fedora and uses systemd to start all of the necessary
# services.
#
# This script prompts the user for the Omnia shared path and the root
# password. It then checks if the Omnia shared path exists.
#
# The script checks if the ssh key file exists. If it does not exist, a new ssh
# Color Definitions
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
YELLOW='\033[0;33m'
# Function to get version from git tag
get_version_from_git_tag() {
local tag_version
local script_dir
local git_root
# First try to get script directory
if [ -L "${BASH_SOURCE[0]}" ]; then
# If script is a symlink, resolve it
script_dir="$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" && pwd)"
else
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
fi
# Find git repository by traversing up from script directory
git_root="$script_dir"
while [ "$git_root" != "/" ] && [ ! -d "$git_root/.git" ]; do
git_root="$(dirname "$git_root")"
done
# If we found a git repository, run git command
if [ "$git_root" != "/" ] && [ -d "$git_root/.git" ]; then
tag_version=$(cd "$git_root" && git tag --points-at HEAD 2>/dev/null | head -n 1)
else
tag_version=""
fi
if [ -z "$tag_version" ]; then
echo ""
return 1
fi
# If tag starts with 'v', strip it and return the rest
if [[ "$tag_version" =~ ^v(.+)$ ]]; then
echo "${BASH_REMATCH[1]}"
return 0
fi
# Tag doesn't start with 'v', return as-is
echo "$tag_version"
return 0
}
# Function to validate version string format
validate_version_string() {
local version="$1"
# Check if version is empty
if [ -z "$version" ]; then
return 1
fi
# Basic version format validation: X.Y.Z.W or X.Y.Z.W-rcN or X.Y.Z.W-suffix
if [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9]+)?$ ]]; then
return 0
fi
return 1
}
# Function to get version for metadata (from git tag or default)
get_metadata_version() {
local default_version="${1:-$omnia_release}"
local git_tag_version
git_tag_version=$(get_version_from_git_tag)
if [ -n "$git_tag_version" ] && validate_version_string "$git_tag_version"; then
echo "$git_tag_version"
else
echo "$default_version"
fi
}
omnia_release=2.1.0.0
core_container_status=false
omnia_path=""
hashed_passwd=""
domain_name=""
is_local_ip() {
local ip_to_check="$1"
# Get all local IP addresses (excluding loopback)
local local_ips
local_ips=$(hostname -I)
# Check if the IP matches any local IP
if echo "$local_ips" | grep -qw "$ip_to_check"; then
return 0 # IP is local
else
return 1 # IP is not local
fi
}
# Version configuration variables
OMNIA_CORE_CONTAINER_TAG="2.1" # Default container tag
OMNIA_VERSION="" # Will be read from metadata
TARGET_OMNIA_VERSION="" # Target version for upgrade
TARGET_CONTAINER_TAG="" # Target container tag for upgrade
# Centralized version list (in chronological order)
# Note: Include RC milestones so upgrades from RC to RC/GA appear
ALL_OMNIA_VERSIONS=(
"2.0.0.0"
"2.1.0.0-rc1"
"2.1.0.0-rc2"
"2.1.0.0"
)
# Container-side paths (used inside podman exec commands)
CONTAINER_INPUT_DIR="/opt/omnia/input"
CONTAINER_BACKUPS_DIR="/opt/omnia/backups"
CONTAINER_METADATA_FILE="/opt/omnia/.data/oim_metadata.yml"
# Function to get available upgrade versions (higher than current)
get_available_upgrade_versions() {
local current_version="$1"
local available_versions=()
local version_descriptions=()
# Find versions higher than current
local found_current=false
for version in "${ALL_OMNIA_VERSIONS[@]}"; do
if [ "$version" = "$current_version" ]; then
found_current=true
continue
fi
if [ "$found_current" = true ]; then
# Skip RC targets; only offer GA paths
if [[ "$version" == *-rc* ]]; then
continue
fi
available_versions+=("$version")
# Generate description based on upgrade type
local current_tag=$(get_container_tag_from_version "$current_version")
local target_tag=$(get_container_tag_from_version "$version")
if [ "$current_tag" = "$target_tag" ]; then
version_descriptions+=("Patch upgrade to $version (container restart only)")
else
version_descriptions+=("Major upgrade to $version (container swap required)")
fi
fi
done
# Return arrays
printf '%s\n' "${available_versions[@]}"
printf '%s\n' "${version_descriptions[@]}"
}
# Function to get available rollback versions (lower than current)
get_available_rollback_versions() {
local current_version="$1"
local normalized_current_version="${current_version%%-rc*}"
if [ -z "$normalized_current_version" ]; then
normalized_current_version="$current_version"
fi
local available_versions=()
# Find versions lower than current
for version in "${ALL_OMNIA_VERSIONS[@]}"; do
if [ "$version" = "$normalized_current_version" ]; then
break
fi
# Skip RC targets for rollback choices
if [[ "$version" == *-rc* ]]; then
continue
fi
available_versions+=("$version")
done
# Return array (reverse order for rollback - newest first)
local reversed_versions=()
for ((i=${#available_versions[@]}-1; i>=0; i--)); do
reversed_versions+=("${available_versions[$i]}")
done
printf '%s\n' "${reversed_versions[@]}"
}
# Function to perform same-tag rollback (container restart only)
rollback_same_tag() {
local target_version="$1"
local current_version="$2"
echo "[INFO] [ROLLBACK] Phase: Same-Tag Rollback"
echo "[INFO] [ROLLBACK] Rolling back to $target_version within same container tag"
# Verify container is running
if ! podman ps --format '{{.Names}}' | grep -qw "omnia_core"; then
echo "[ERROR] [ROLLBACK] Container is not running for same-tag rollback"
return 1
fi
# Get version from git tag or use target version
local metadata_version=$(get_metadata_version "$target_version")
echo "[INFO] [ROLLBACK] Updating metadata to version $metadata_version"
# Update version metadata
if ! podman exec -u root omnia_core bash -c "
set -e
if [ ! -f '$CONTAINER_METADATA_FILE' ]; then
echo '[ERROR] Metadata file not found inside container: $CONTAINER_METADATA_FILE' >&2
exit 1
fi
if grep -q '^omnia_version:' '$CONTAINER_METADATA_FILE'; then
sed -i 's/^omnia_version:.*/omnia_version: $metadata_version/' '$CONTAINER_METADATA_FILE'
else
echo 'omnia_version: $metadata_version' >> '$CONTAINER_METADATA_FILE'
fi
"; then
echo "[ERROR] [ROLLBACK] Failed to update metadata version"
echo "[ERROR] [ROLLBACK] Rollback failed: Could not update version metadata"
return 1
fi
echo "[INFO] [ROLLBACK] Restarting container to apply changes..."
# Restart container to apply changes
if ! systemctl restart omnia_core.service; then
echo "[ERROR] [ROLLBACK] Failed to restart container service"
echo "[ERROR] [ROLLBACK] Rollback failed: Container restart failed"
return 1
fi
# Wait for container to be healthy after restart
echo "[INFO] [ROLLBACK] Waiting for container health check after restart (30s)"
local health_timeout=30
local health_count=0
while [ $health_count -lt $health_timeout ]; do
if podman ps --format '{{.Names}} {{.Status}}' | grep -E "omnia_core.*Up" | grep -q "healthy\|Up"; then
echo "[INFO] [ROLLBACK] Container is healthy after restart"
break
fi
sleep 1
health_count=$((health_count + 1))
echo -n "."
done
if [ $health_count -ge $health_timeout ]; then
echo ""
echo "[ERROR] [ROLLBACK] Container failed to become healthy within 30 seconds after restart"
echo "[ERROR] [ROLLBACK] Rollback failed: Container health check failed"
return 1
fi
# Verify version update
local updated_version=$(get_current_omnia_version)
if [ "$updated_version" != "$metadata_version" ]; then
echo "[ERROR] [ROLLBACK] Version update verification failed"
echo "[ERROR] [ROLLBACK] Expected: $metadata_version, Found: $updated_version"
return 1
fi
echo "[INFO] [ROLLBACK] Same-tag rollback completed successfully"
echo "[INFO] [ROLLBACK] Version rolled back to: $metadata_version"
return 0
}
# Function to validate container image availability and show build instructions
validate_container_image() {
local target_version="$1"
local target_container_tag="$2"
local operation="${3:-upgrade}"
echo -e "${BLUE}Validating target container image: omnia_core:$target_container_tag${NC}"
if ! podman inspect "omnia_core:$target_container_tag" >/dev/null 2>&1; then
echo ""
echo -e "${RED}================================================================================${NC}"
echo -e "${RED}ERROR: Target container image not found locally${NC}"
echo -e "${RED}================================================================================${NC}"
echo -e "${YELLOW}Required image:${NC} omnia_core:$target_container_tag"
echo ""
echo -e "${YELLOW}Omnia does not pull images from Docker Hub.${NC}"
echo -e "${YELLOW}You must build or load the container image locally before proceeding.${NC}"
echo ""
echo -e "${BLUE}Build the required image using the following commands:${NC}"
echo ""
echo -e "git clone https://github.com/dell/omnia-artifactory.git -b omnia-container-<version>"
echo -e "${YELLOW}Note: Replace <version> with the target Omnia version (e.g., v2.1.0.0)${NC}"
echo ""
echo -e "cd omnia-artifactory"
echo ""
echo -e "./build_images.sh core core_tag=<tag> omnia_branch=<branch>"
echo -e "${YELLOW}Note: Replace <branch> with the target Omnia branch (e.g., v2.1.0.0)${NC}"
echo -e "${YELLOW}Note: core_tag <tag> will be the first 2 digits of the target Omnia version (e.g., 2.1 for v2.1.0.0)${NC}"
echo ""
echo -e "${BLUE}After the image is built successfully, re-run:${NC}"
echo -e "./omnia.sh --$operation"
echo ""
echo -e "${RED}================================================================================${NC}"
return 1
fi
echo -e "${GREEN}✓ Target image available locally: omnia_core:$target_container_tag${NC}"
return 0
}
# Function to get container tag from omnia version
get_container_tag_from_version() {
local version="$1"
# Explicit mapping: 2.1.0.0-rc1 stays on pre-GA tag 1.0
if [[ "$version" == "2.1.0.0-rc1" ]]; then
echo "1.0"
return
fi
case "$version" in
2.0.*)
echo "1.0"
;;
*)
# All other versions (including rc2/GA) use major.minor as tag
echo "$(echo "$version" | awk -F. '{print $1"."$2}')"
;;
esac
}
# Function to read current omnia version from metadata
get_current_omnia_version() {
if podman ps --format '{{.Names}}' | grep -qw "omnia_core"; then
podman exec omnia_core cat /opt/omnia/.data/oim_metadata.yml 2>/dev/null | grep "omnia_version:" | awk '{print $2}' | tr -d '"'
else
echo ""
fi
}
# Update metadata with git tag version from inside container
update_metadata_with_git_tag() {
local default_version="${1:-$omnia_release}"
podman exec -u root omnia_core bash -c '
set -e
cd /omnia || exit 0
git_tag_version=$(git tag --points-at HEAD 2>/dev/null | head -n 1 || true)
if [[ "$git_tag_version" =~ ^v(.+)$ ]]; then
git_tag_version="${BASH_REMATCH[1]}"
fi
if [[ "$git_tag_version" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9]+)?$ ]]; then
metadata_version="$git_tag_version"
else
metadata_version="'"$default_version"'"
fi
if [ -f '"'$CONTAINER_METADATA_FILE'"' ]; then
if grep -q "^omnia_version:" '"'$CONTAINER_METADATA_FILE'"'; then
sed -i "s/^omnia_version:.*/omnia_version: $metadata_version/" '"'$CONTAINER_METADATA_FILE'"'
else
echo "omnia_version: $metadata_version" >> '"'$CONTAINER_METADATA_FILE'"'
fi
echo "[INFO] Updated omnia_version to: $metadata_version"
fi
' || true
}
show_post_upgrade_instructions() {
local upgraded_version="$1"
echo ""
echo -e "${YELLOW}================================================================================${NC}"
echo -e "${YELLOW} IMPORTANT POST-UPGRADE STEP${NC}"
echo -e "${YELLOW}================================================================================${NC}"
echo ""
echo -e "${BLUE}NEXT REQUIRED ACTION:${NC}"
echo -e "${YELLOW}You must now run the upgrade playbook inside the omnia_core container:${NC}"
echo ""
echo -e "${GREEN}ansible-playbook /omnia/upgrade/upgrade_omnia.yml${NC}"
echo ""
echo -e "${BLUE}This playbook will:${NC}"
echo -e "• Update input files based on the previous version inputs"
echo -e "• Provide further steps to follow"
echo -e "• Provide user guidance for provisioning nodes"
echo ""
echo -e "${YELLOW}Note: Run the above command after the container is fully healthy and stable${NC}"
echo -e "${YELLOW}================================================================================${NC}"
echo ""
}
# Host-side paths (initialized dynamically after omnia_path is set)
OMNIA_INPUT_DIR=""
OMNIA_METADATA_DIR=""
OMNIA_METADATA_FILE=""
update_metadata_upgrade_backup_dir() {
local backup_dir="$1"
if ! podman ps --format '{{.Names}}' | grep -qw "omnia_core"; then
echo "[ERROR] [ORCHESTRATOR] omnia_core container is not running"
return 1
fi
podman exec -u root omnia_core bash -c "
set -e
if [ ! -f '$CONTAINER_METADATA_FILE' ]; then
echo '[ERROR] Metadata file not found inside container: $CONTAINER_METADATA_FILE' >&2
exit 1
fi
if grep -q '^upgrade_backup_dir:' '$CONTAINER_METADATA_FILE'; then
sed -i 's|^upgrade_backup_dir:.*|upgrade_backup_dir: ${backup_dir}|' '$CONTAINER_METADATA_FILE'
else
echo 'upgrade_backup_dir: ${backup_dir}' >> '$CONTAINER_METADATA_FILE'
fi
"
}
# Resolve the upgrade guard lock path (container or host shared path)
get_upgrade_guard_lock_path() {
local upgrade_guard_lock_container="/opt/omnia/.data/upgrade_in_progress.lock"
local upgrade_guard_lock_host
upgrade_guard_lock_host=$(podman exec -u root omnia_core grep '^oim_shared_path:' /opt/omnia/.data/oim_metadata.yml 2>/dev/null | cut -d':' -f2- | tr -d ' \t\n\r')
if [ -n "$upgrade_guard_lock_host" ]; then
upgrade_guard_lock_host="$upgrade_guard_lock_host/omnia/.data/upgrade_in_progress.lock"
else
upgrade_guard_lock_host="$upgrade_guard_lock_container"
fi
echo "$upgrade_guard_lock_host"
}
check_internal_nfs_export() {
local nfs_server_ip="$1" nfs_server_share_path="$2"
local exports line export_path share_path export_path_norm share_path_norm
if ! is_local_ip "$nfs_server_ip"; then
echo "The provided NFS server IP ($nfs_server_ip) is NOT the current system's IP."
exit 1
fi
echo "The provided NFS server IP ($nfs_server_ip) belongs to the current system."
if ! exports=$(showmount -e "$nfs_server_ip" 2>/dev/null); then
echo -e "${RED}ERROR: Unable to contact NFS server at $nfs_server_ip. Ensure NFS and rpcbind are running, and firewall allows access.${NC}"
exit 1
fi
# Normalize share path
share_path="${nfs_server_share_path#"${nfs_server_share_path%%[![:space:]]*}"}"
share_path="${share_path%"${share_path##*[![:space:]]}"}"
share_path_norm="${share_path%/}"
[[ -z "$share_path_norm" ]] && share_path_norm="/"
# Check showmount exports
while IFS= read -r line; do
line="${line#"${line%%[![:space:]]*}"}"
[[ -z "$line" || "$line" == \#* || "$line" == Export\ list\ for* ]] && continue
export_path="${line%%[[:space:]]*}"
[[ "$export_path" != /* ]] && continue
export_path_norm="${export_path%/}"
[[ -z "$export_path_norm" ]] && export_path_norm="/"
if [[ "$share_path_norm" == "$export_path_norm" || "$share_path_norm" == "$export_path_norm"/* ]]; then
echo -e "${GREEN}Path $nfs_server_share_path is covered by exported path $export_path_norm on $nfs_server_ip.${NC}"
return 0
fi
done <<< "$exports"
# Fallback: check /etc/exports if showmount didn't find a match
if [[ -f /etc/exports ]]; then
while IFS= read -r line; do
line="${line#"${line%%[![:space:]]*}"}"
[[ -z "$line" || "$line" == \#* ]] && continue
export_path="${line%%[[:space:]]*}"
[[ "$export_path" != /* ]] && continue
export_path_norm="${export_path%/}"
[[ -z "$export_path_norm" ]] && export_path_norm="/"
if [[ "$share_path_norm" == "$export_path_norm" || "$share_path_norm" == "$export_path_norm"/* ]]; then
echo -e "${GREEN}Path $nfs_server_share_path is covered by exported path $export_path_norm on $nfs_server_ip.${NC}"
return 0
fi
done < /etc/exports
fi
echo -e "${RED}ERROR: Path $nfs_server_share_path is NOT exported by $nfs_server_ip.${NC}"
exit 1
}
display_supported_use_cases() {
# Color definitions
BLUE='\033[1;34m'
YELLOW='\033[1;33m'
GREEN='\033[1;32m'
NC='\033[0m' # No Color
# Introductory Guidance
echo -e "${BLUE} ----------------- Omnia Shared Path Configuration ---------------- ${NC}"
echo -e "${BLUE} Please choose the type of Omnia shared path in Omnia Infrastructure Manager (OIM): ${NC}"
echo -e "${BLUE} It is recommended to use a external NFS share for the Omnia shared path. ${NC}"
echo -e "${BLUE} If you are not using NFS, make sure enough space is available on the disk. ${NC}"
echo -e "\nSupported Use Cases:\n"
# Table content
{
echo -e "Share Option\tType\tDescription\tAdditional Info"
echo -e "${GREEN}NFS\tExternal\tExternal NFS server(outside OIM) created by user\tUsed only for flat provisioning. Mounts on OIM. ${NC}"
echo -e "NFS\tInternal\tNFS server created by user in OIM\tUsed only for flat provisioning. No mount performed."
echo -e "Local\tDisk\tDisk storage in OIM\tUsed only for flat provisioning. Directory to be created by user."
} | column -t -s $'\t'
}
# This function is responsible for initializing the Omnia core container
# It prompts the user for the Omnia shared path and the root password.
# It checks if the Omnia shared path exists.
setup_omnia_core() {
# Validate the system environment
validate_oim
# Initialize the container configuration
init_container_config
# Setup the container
setup_container
# Post container setup configuration
post_setup_config
remove_container_omnia_sh
# Start the container
start_container_session
}
# This function is responsible for cleaning up the Omnia core container.
# It removes the container and performs the necessary cleanup steps.
cleanup_omnia_core() {
# Block if critical service containers exist
critical_running=$(podman ps --format '{{.Names}}' | grep -E '^pulp$|^omnia_auth$|^minio-server$|^registry$|^step-ca$|^postgres$|^hydra$|^opaal-idp$|^smd$|^opaal$|^bss$|^cloud-init-server$|^haproxy$|^coresmd$|^omnia_build_stream$|^omnia_postgres$')
if [ -n "$critical_running" ]; then
echo -e "${RED}Failed to intiatiate omnia_core container cleanup. There are other critical service containers still running:${NC}"
echo "$critical_running"
echo -e "${GREEN}Run oim_cleanup.yml first to cleanup all containers.${NC}"
exit 1
fi
echo -e "${RED} WARNING: This will remove Omnia core container and all files in Omnia Shared Path.${NC}"
echo -e "${GREEN} You can abort and take backup if you want.${NC}"
read -p " Are you sure you want to continue with the cleanup? (y/n): " confirm
if [ "$confirm" = "n" ] || [ "$confirm" = "N" ]; then
echo -e "${GREEN}Aborting.${NC}"
exit 0
elif [ "$confirm" = "y" ] || [ "$confirm" = "Y" ]; then
# Fetch the configuration from the Omnia core container.
fetch_config
# Clear upgrade guard lock if present (shared path visible to container and host)
local upgrade_guard_lock_path=$(get_upgrade_guard_lock_path)
rm -f "$upgrade_guard_lock_path" >/dev/null 2>&1 || true
echo "[INFO] [CLEANUP] Cleared upgrade guard lock (if present): $upgrade_guard_lock_path"
# Remove the container
remove_container
# Perform the necessary cleanup steps
cleanup_config
fi
}
# This function is responsible for cleaning up the Omnia core container configuration.
# It removes the public key from the authorized_keys file.
# It removes the private key.
# It removes the ssh key from the known_hosts file.
# It removes the Omnia core configuration.
#
cleanup_config(){
# Set the path to the ssh public key.
ssh_key_file="$HOME/.ssh/oim_rsa.pub"
# Remove the public key from the authorized_keys file.
if [ -f "$ssh_key_file" ]; then
# Remove the line from the authorized_keys file.
sed -i "\|^$(cat $ssh_key_file)$|d" $HOME/.ssh/authorized_keys
echo -e "${GREEN} Public key has been removed from authorized_keys.${NC}"
else
echo -e "${RED} Public key file not found.${NC}"
fi
# Remove the SSH key pair.
ssh_key_file="$HOME/.ssh/oim_rsa"
ssh_key_file_pub="${ssh_key_file}.pub"
if [ -f "$ssh_key_file" ] && [ -f "$ssh_key_file_pub" ]; then
rm -f "$ssh_key_file" "$ssh_key_file_pub"
echo -e "${GREEN} SSH key pair have been removed.${NC}"
else
echo -e "${RED} SSH key file not found.${NC}"
fi
# Remove the ssh key from the known_hosts file.
echo -e "${BLUE} Removing ssh key from known_hosts file.${NC}"
ssh-keygen -R "[localhost]:2222" >/dev/null 2>&1
# Remove the host entry from the config file in .ssh folder.
ssh_config_file="$HOME/.ssh/config"
if [ -f "$ssh_config_file" ]; then
sed -i '/Host omnia_core/,+5d' "$ssh_config_file"
echo -e "${GREEN} Host entry has been removed from config file.${NC}"
else
echo -e "${RED} Config file not found.${NC}"
fi
# Remove the Omnia core configuration.
echo -e "${BLUE} Removing Omnia core configuration.${NC}"
rm -rf $omnia_path/omnia/{hosts,input,log,pulp,provision,pcs,ssh_config,tmp,.data}
# Unmount the NFS shared path if the share option is NFS.
if [ "$share_option" = "NFS" ] && [ "$nfs_type" = "external" ]; then
umount "$omnia_path"
if [ $? -eq 0 ]; then
echo -e "${GREEN} NFS shared path has been unmounted.${NC}"
else
echo -e "${RED} Failed to unmount NFS shared path.${NC}"
fi
# Remove the entry from /etc/fstab
fstab_file="/etc/fstab"
if [ -f "$fstab_file" ]; then
# Create a backup of the fstab file.
cp "$fstab_file" "$fstab_file.bak"
# Remove the line from the fstab file.
sed -i "\#$omnia_path#d" "$fstab_file"
if [ $? -ne 0 ]; then
echo -e "${RED} Failed to remove the entry from /etc/fstab.${NC}"
fi
fi
fi
echo -e "${GREEN} Omnia core configuration has been cleaned up.${NC}"
}
# This function is responsible for removing the Omnia core container.
#
# It removes the container using the 'podman rm -f' command.
# If the container is removed successfully, it prints a success message.
# Otherwise, it prints an error message.
remove_container() {
# Block if critical service containers exist
critical_running=$(podman ps --format '{{.Names}}' | grep -E '^pulp$|^omnia_auth$|^minio-server$|^registry$|^step-ca$|^postgres$|^hydra$|^opaal-idp$|^smd$|^opaal$|^bss$|^cloud-init-server$|^haproxy$|^coresmd$|^omnia_build_stream$|^omnia_postgres$')
if [ -n "$critical_running" ]; then
echo -e "${RED}Failed to intiatiate omnia_core container cleanup. There are other critical service containers still running:${NC}"
echo "$critical_running"
echo -e "${GREEN}Run oim_cleanup.yml first to cleanup all containers.${NC}"
exit 1
fi
# Remove the container.
echo -e "${BLUE} Removing the Omnia core container.${NC}"
if systemctl stop omnia_core.service; then
echo -e "${GREEN} Omnia core container has been removed.${NC}"
# Remove the systemd generator symlinks.
echo -e "${GREEN} Cleaning up systemd generator symlinks.${NC}"
rm -f /run/systemd/generator/omnia_core.service
rm -f /run/systemd/generator/multi-user.target.wants/omnia_core.service
rm -f /run/systemd/generator/default.target.wants/omnia_core.service
echo -e "${GREEN} Cleaning up omnia_core.container.${NC}"
rm -f /etc/containers/systemd/omnia_core.container
# Remove the omnia_core.service file.
rm -f /etc/systemd/system/omnia_core.service
systemctl daemon-reload
systemctl reset-failed omnia_core.service
# check if service is removed
if systemctl status omnia_core.service >/dev/null 2>&1; then
echo -e "${RED} Failed to remove Omnia core service.${NC}"
else
echo -e "${GREEN} Omnia core service has been removed.${NC}"
fi
else
echo -e "${RED} Failed to remove Omnia core container.${NC}"
fi
# Remove the container image.
# if podman rmi omnia_core; then
# echo -e "${GREEN} Omnia core image has been removed.${NC}"
# else
# echo -e "${RED} Failed to remove Omnia core image.${NC}"
# fi
}
# This function is responsible for initializing the Omnia core container.
#
# It prompts the user for the Omnia shared path and the root
# password. It then checks if the Omnia shared path exists.
#
# The function generates the ssh key pair and copies the private
# key to the Omnia shared path.
#
# The function also copies the ssh public key to the
# authorized_keys file.
#
# The function creates the necessary log directories.
init_container_config() {
share_option=""
# Display the supported use cases
display_supported_use_cases
# Display the choices for the user
echo -e "${BLUE} Choose the type of Omnia shared path:${NC}"
options=( "NFS (recommended)" "Local" )
PS3="Select the option number: "
select opt in "${options[@]}"; do
case $opt in
"NFS (recommended)")
share_option="NFS"
break
;;
"Local")
share_option="Local"
break
;;
*)
echo -e "${RED} Invalid option.${NC}"
continue
esac
done
case $share_option in
"Local")
# Prompt the user for the Omnia shared path.
echo -e "${BLUE} Please provide Omnia shared path:${NC}"
read -p "Omnia shared path: " omnia_path
# Check if the Omnia shared path is absolute path and path exists.
if [[ "$omnia_path" != /* ]] || [ ! -d "$omnia_path" ]; then
echo -e "${RED} Omnia shared path is not an absolute path or does not exist! Please re-run omnia.sh --install with valid Omnia shared path.${NC}"
exit 1
fi
;;
"NFS")
echo -e "${BLUE} Select NFS type:${NC}"
select nfs_type in "External (Recommended)" "Internal"; do
case $nfs_type in
"External (Recommended)")
echo -e "${BLUE} Please provide the external NFS server IP:${NC}"
read -p "External NFS server IP: " nfs_server_ip
echo -e "${BLUE} Please provide the external NFS server share path:${NC}"
read -p "External NFS share path: " nfs_server_share_path
echo -e "${BLUE} Please provide the OIM client share path (mount target):${NC}"
read -p "Omnia shared path: " omnia_path
# Validate Omnia shared path is absolute
if [[ "$omnia_path" != /* ]]; then
echo -e "${RED}Omnia shared path must be an absolute path.${NC}"
exit 1
fi
nfs_type="external"
break
;;
"Internal")
echo -e "${BLUE} Please provide the OIM server IP:${NC}"
read -p "OIM server IP: " nfs_server_ip
echo -e "${BLUE} Please provide the OIM server share path:${NC}"
read -p "OIM server share path: " nfs_server_share_path
echo -e "${BLUE} Checking if the OIM server share path is mounted${NC}"
check_internal_nfs_export "$nfs_server_ip" "$nfs_server_share_path"
# Note: No mounting performed here
echo -e "${YELLOW}Note: Internal NFS does not support HA OIM or hierarchical cluster. Proceeding...${NC}"
nfs_type="internal"
omnia_path="$nfs_server_share_path"
break
;;
*)
echo -e "${RED}Invalid option. Please choose 1 or 2.${NC}"
;;
esac
done
;;
esac
# Prompt the user for the Omnia core root password.
echo -e "${BLUE} Please provide Omnia core root password for accessing container:${NC}"
read -p " Enter: " -s passwd
# Prompt the user for the Omnia core root password confirmation.
echo -e "\n${BLUE} Please confirm password:${NC}"
read -s -p " Enter: " cnf_passwd
# Check if the provided passwords match.
if [ "$passwd" != "$cnf_passwd" ]; then
echo -e "${RED} Invalid Omnia core root password, passwords do not match!${NC}"
exit 1
fi
# Check if the password contains any of the invalid characters
invalid_chars='[\\|&;`"><*?!$(){}[\]]'
if [[ "$passwd" =~ $invalid_chars ]]; then
echo -e "${RED} Invalid password, passwords must not contain any of these special characters: [\\|&;\`\"><*?!$(){}[\]]${NC}"
exit 1
fi
# Install NFS client package if option NFS is selected
if [[ "$share_option" == "NFS" ]]; then
# Install NFS client package
echo -e "${BLUE} Installing NFS client package.${NC}"
dnf install -y nfs-utils nfs4-acl-tools
# Create omnia_path directory if it does not exist
echo -e "${BLUE} Creating omnia shared path directory if it does not exist.${NC}"
mkdir -p $omnia_path
# Mount NFS server share path in Omnia share path
if [[ "$nfs_type" == "external" ]]; then
if is_local_ip "$nfs_server_ip"; then
echo -e "${RED} Error: NFS server $nfs_server_ip is a local IP.${NC}"
echo -e "${RED} Please provide an external NFS server IP or re-run omnia.sh --install with valid options.${NC}"
exit 1
fi
# Validate if NFS server is reachable
echo -e "${BLUE} Validating if NFS server is reachable.${NC}"
ping -c1 -W1 $nfs_server_ip > /dev/null
if [ $? -ne 0 ]; then
echo -e "${RED} NFS server $nfs_server_ip is not reachable.${NC}"
exit 1
fi
echo -e "${BLUE} Mounting NFS server share path in Omnia share path.${NC}"
mount -t nfs -o nosuid,rw,sync,hard,intr,timeo=30 "$nfs_server_ip:$nfs_server_share_path" "$omnia_path"
if [[ $? -ne 0 ]]; then
echo -e "${RED} Failed to mount NFS. Please check the IP and path.${NC}"
exit 1
fi
# Validate if NFS server share path is mounted
echo -e "${BLUE} Validating if NFS server share path is mounted.${NC}"
# strip the trailing slash from nfs_server_share_path
nfs_server_share_path="${nfs_server_share_path%/}"
if grep -qs "$nfs_server_ip:$nfs_server_share_path" /proc/mounts; then
echo -e "${GREEN} NFS server share path is mounted.${NC}"
else
echo -e "${RED} NFS server share path is not mounted. Provide valid NFS server details. ${NC}"
exit 1
fi
# Add NFS server share to /etc/fstab to mount on startup
echo "$nfs_server_ip:$nfs_server_share_path $omnia_path nfs nosuid,rw,sync,hard,intr" >> /etc/fstab
else
echo -e "${BLUE} Using internal NFS path without mounting.${NC}"
fi
fi
hashed_passwd=$(openssl passwd -1 $passwd)
ssh_key_file="/root/.ssh/oim_rsa"
ssh_port=2222
# Generate a new ssh key pair.
if [ -f "$ssh_key_file" ]; then
echo -e "\n${BLUE} Skipping generating new ssh key pair.${NC}"
else
echo -e "\n${GREEN} Generating a new ssh key pair.${NC}"
ssh-keygen -t rsa -b 4096 -C "omnia_oim" -q -N '' -f /root/.ssh/oim_rsa
{
echo "Host omnia_core"
echo " Hostname localhost"
echo " Port $ssh_port"
echo " User root"
echo " IdentityFile ~/.ssh/oim_rsa"
echo " IdentitiesOnly yes"
} >> $HOME/.ssh/config
fi
# Create the ssh configuration directory if it does not exist.
echo -e "${GREEN} Creating the ssh configuration directory if it does not exist.${NC}"
mkdir -p "$omnia_path/omnia/ssh_config/.ssh"
# Copy the omnia_core ssh config to the shared path.
echo -e "${GREEN} Copying the omnia_core ssh config to the omnia shared path.${NC}"
cp "$HOME/.ssh/config" "$omnia_path/omnia/ssh_config/.ssh/config"
# Copy the oim_rsa ssh key to the shared path.
echo -e "${GREEN} Copying the oim_rsa ssh key to the omnia shared path.${NC}"
cp "$HOME/.ssh/oim_rsa" "$omnia_path/omnia/ssh_config/.ssh/oim_rsa"
# Copy the ssh private key to the omnia shared path.
echo -e "${GREEN} Copying the ssh private key to the omnia shared path.${NC}"
cp $ssh_key_file "$omnia_path/omnia/ssh_config/.ssh/id_rsa"
# Copy the ssh public key to the omnia shared path.
echo -e "${GREEN} Copying the ssh public key to the omnia shared path.${NC}"
cp $ssh_key_file.pub "$omnia_path/omnia/ssh_config/.ssh/id_rsa.pub"
# Get the ssh public key.
ssh_public_key="$(cat /root/.ssh/oim_rsa.pub)"
validate_nfs_server
# Add ssh public key to the authorized_keys.
echo -e "${GREEN} Adding ssh public key to the authorized_keys.${NC}"
if grep -q "$ssh_public_key" $HOME/.ssh/authorized_keys; then
echo -e "${BLUE} Skipping adding ssh public key to the authorized_keys.${NC}"
else
echo "$ssh_public_key" >> $HOME/.ssh/authorized_keys
chmod 600 $HOME/.ssh/authorized_keys
fi
# Add ssh public key to the authorized_keys in the ssh_config directory.
echo -e "${GREEN} Adding ssh public key to the authorized_keys in the Omnia ssh_config directory.${NC}"
if [ -f "$omnia_path/omnia/ssh_config/.ssh/authorized_keys" ] && grep -q "$ssh_public_key" "$omnia_path/omnia/ssh_config/.ssh/authorized_keys"; then
echo -e "${BLUE} Skipping adding ssh public key to the authorized_keys in the Omnia ssh_config directory.${NC}"
else
echo "$ssh_public_key" >> "$omnia_path/omnia/ssh_config/.ssh/authorized_keys"
chmod 600 "$omnia_path/omnia/ssh_config/.ssh/authorized_keys"
fi
# Create the log directory if it does not exist.
echo -e "${GREEN} Creating the log directory if it does not exist.${NC}"
mkdir -p "$omnia_path/omnia/log/core/container"
mkdir -p "$omnia_path/omnia/log/core/playbooks"
# Create the hosts file for cluster in $omnia_path/omnia/hosts
echo -e "${GREEN} Creating the hosts file for cluster.${NC}"
touch "$omnia_path/omnia/hosts"
# Create the pulp_ha directory if it does not exist.
echo -e "${GREEN} Creating the pulp HA directory if it does not exist.${NC}"
mkdir -p "$omnia_path/omnia/pulp/pulp_ha"
# Initialize host-side path variables based on user-provided omnia_path
OMNIA_INPUT_DIR="$omnia_path/omnia/input"
OMNIA_METADATA_DIR="$omnia_path/omnia/.data"
OMNIA_METADATA_FILE="$omnia_path/omnia/.data/oim_metadata.yml"
}
# This function is responsible for fetching the configuration from the Omnia core.
# It uses podman exec to run a command in the Omnia core container.
# The command retrieves the metadata from the oim_metadata.yml file.
# The metadata is then parsed and the required configuration is extracted.
fetch_config() {
# Fetch the metadata from the oim_metadata.yml file.
echo -e "${GREEN} Fetching the metadata from the oim_metadata.yml file.${NC}"
core_config=$(podman exec -ti omnia_core /bin/bash -c 'cat /opt/omnia/.data/oim_metadata.yml')
# Split the metadata into separate lines.
IFS=$'\n' read -r -d '' -a config_lines <<<"$core_config"
# Loop through the lines and extract the required configuration.
for line in "${config_lines[@]}"; do
# Extract the key and value from the line.
key=$(echo "$line" | awk -F ':' '{print $1}')
value=$(echo "$line" | awk -F ':' '{print $2}')