forked from dbarj/oci-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oci_compute_clone_xregion.sh
1379 lines (1153 loc) · 48.7 KB
/
oci_compute_clone_xregion.sh
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
#************************************************************************
#
# oci_compute_clone_xregion.sh - Clone a Compute Instance across Regions
#
# Copyright 2018 Rodrigo Jorge <http://www.dbarj.com.br/>
#
# 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.
#
#************************************************************************
# Available at: https://github.com/dbarj/oci-scripts
# Created on: Nov/2018 by Rodrigo Jorge
# Version 1.12
#************************************************************************
set -eo pipefail
# Define paths for oci-cli and jq or put them on $PATH. Don't use relative PATHs in the variables below.
v_oci="oci"
v_jq="jq"
# Don't change it.
v_min_ocicli="2.6.9"
v_oci_image_clone_script="oci_image_clone_xregion.sh"
# Add any desired oci argument exporting OCI_CLI_ARGS. Keep default to avoid oci_cli_rc usage.
[ -n "${OCI_CLI_ARGS}" ] && v_oci_args="${OCI_CLI_ARGS}"
[ -z "${OCI_CLI_ARGS}" ] && v_oci_args="--cli-rc-file /dev/null"
# If DEBUG variable is undefined, change to 1. Note that [-q] parameter will override this option to 0.
[[ "${DEBUG}" == "" ]] && DEBUG=1
# 0 = Only basic echo.
# 1 = Show OCI-CLI steps commands.
# 2 = Show ALL OCI-CLI commands. (TODO)
####
#### INTERNAL - IF NOT PROVIDED HERE OR AS PARAMETERS, WILL BE ASKED DURING CODE EXECUTION.
####
v_orig_region=""
v_target_region=""
v_clone_subnetID=""
v_target_subnetID=""
v_target_IP=""
v_target_shape=""
v_os_bucketName=""
v_sedrep_rule_target_name=""
####
if [ -z "${BASH_VERSION}" ]
then
>&2 echo "Script must be executed in BASH shell."
exit 1
fi
v_this_script="$(basename -- "$0")"
read -r -d '' v_all_steps << EOM || true
01 - Create Volume Group with instance Boot-Volume and Volumes.
02 - Backup Volume Group.
03 - Remove Volume Group.
04 - Create a New Boot-Volume from the Backup.
05 - Create a Temporary cloned Compute Instance from the Boot Volume.
06 - Stop the Temporary cloned Compute Instance.
07 - Create an Image from it.
08 - Remove the Temporary cloned Compute Instance and Boot-Volume.
09-13 - Move the image from source to target (${v_oci_image_clone_script}).
14 - Remove Image on source region.
15 - Create a Compute Instance from Image on target region.
16 - Remove Image on target region.
17 - Move Volumes Backups from Source Region to Target Region.
18 - Remove Volumes Backups in Source Region.
19 - Create Volumes from backups in Target Region.
20 - Remove Volumes Backups in Target Region.
21 - Attach Volumes in target.
22 - Run iscsiadm commands on target (if Linux).
EOM
function echoError ()
{
[ -z "$2" ] && (>&2 echo "$1") || (>&2 echoStatus "$1" "$2")
}
function echoStatus ()
{
local RED='\033[0;31m'
local GREEN='\033[0;32m'
local BOLD='\033[0;1m'
local NC='\033[0m' # No Color
local TYPE="$GREEN"
[ "$2" == "GREEN" ] && TYPE="$GREEN"
[ "$2" == "BOLD" ] && TYPE="$BOLD"
[ "$2" == "RED" ] && TYPE="$RED"
printf "${TYPE}${1}${NC}\n"
}
function exitError ()
{
local v_filename="${v_this_script%.*}.log"
echoError "$1"
( set -o posix ; set ) > "${v_filename}"
exit 1
}
function checkError ()
{
# If 2 params given:
# - If 1st is NULL, abort script printing 2nd.
# If 3 params given:
# - If 1st is NULL, abort script printing 3rd.
# - If 2nd is not 0, abort script printing 3rd.
local v_arg1 v_arg2 v_arg3
v_arg1="$1"
v_arg2="$2"
v_arg3="$3"
[ "$#" -ne 2 -a "$#" -ne 3 ] && exitError "checkError wrong usage."
[ "$#" -eq 2 -a -z "${v_arg2}" ] && exitError "checkError wrong usage."
[ "$#" -eq 3 -a -z "${v_arg3}" ] && exitError "checkError wrong usage."
[ "$#" -eq 2 ] && [ -z "${v_arg1}" ] && echoError "${v_arg2}" "RED" && exit 1
[ "$#" -eq 3 ] && [ -z "${v_arg1}" ] && echoError "${v_arg3}" "RED" && exit 1
[ "$#" -eq 3 ] && [ "${v_arg2}" != "0" ] && echoError "${v_arg3}" "RED" && exit 1
return 0
}
# trap
trap 'exitError "Code Interrupted."' INT SIGINT SIGTERM
function printUsage ()
{
echoError "Usage: ${v_this_script} -i <value> [-n <value>] [-p <value>] [-s <value>] [-a <value>] [-t <value>] [-r <value>] [-b <value>] [-c <value>] [-q] [-x]"
echoError ""
echoError "Only \"-i\" is mandatory. All other parameters if omitted will be asked during execution."
echoError ""
echoError "-i : Source Compute Instance Name or OCID"
echoError "-n : Target Compute Instance Name"
echoError "-p : Target Compute Shape"
echoError "-s : Target Subnet ID"
echoError "-a : Target IP Address"
echoError "-t : Target Region"
echoError "-r : Source Region"
echoError "-b : Object Storage Bucket Name"
echoError "-c : Clone Subnet ID"
echoError "-h : Show this output."
echoError "-q : Quiet mode. Will suppress the spool of executed OCI-CLI commands."
echoError "-x : Will try to run step 22 automatically on new target. Default is off. Note you need a password-less SSH connection to the target with opc."
echoError ""
echoError "Steps: "
echoError ""
echoError "${v_all_steps}"
exit 1
}
while getopts ":i:n:p:s:a:t:r:b:c:qx" opt
do
case "${opt}" in
i)
v_orig_instName=${OPTARG}
;;
n)
v_target_instName=${OPTARG}
;;
p)
v_target_shape=${OPTARG}
;;
s)
v_target_subnetID=${OPTARG}
;;
a)
v_target_IP=${OPTARG}
;;
t)
v_target_region=${OPTARG}
;;
r)
v_orig_region=${OPTARG}
;;
b)
v_os_bucketName=${OPTARG}
;;
c)
v_clone_subnetID=${OPTARG}
;;
x)
v_skip_ssh=0
;;
q)
DEBUG=0
;;
*)
printUsage
;;
esac
done
shift $((OPTIND-1))
[ -z "$v_orig_instName" ] && printUsage
if ! $(which ${v_oci} >&- 2>&-)
then
echoError "Could not find oci-cli binary. Please adapt the path in the script if not in \$PATH."
exitError "Dowload page: https://github.com/oracle/oci-cli"
fi
if ! $(which ${v_jq} >&- 2>&-)
then
echoError "Could not find jq binary. Please adapt the path in the script if not in \$PATH."
exitError "Download page: https://github.com/stedolan/jq/releases"
fi
v_workdir=$(cd -P -- "$(dirname -- "$(command -v -- "$0")")" && pwd -P) # Folder of this script
if ! $(which "${v_workdir}"/${v_oci_image_clone_script} >&- 2>&-)
then
echoError "This shells also use \"${v_oci_image_clone_script}\" to handle the image migration task."
echoError "Could not find \"${v_oci_image_clone_script}\". Please keep it in same folder of ${v_this_script}."
exitError "Download page: https://github.com/dbarj/oci-scripts"
fi
v_cur_ocicli=$(${v_oci} -v)
if [ "${v_min_ocicli}" != "`echo -e "${v_min_ocicli}\n${v_cur_ocicli}" | sort -V | head -n1`" ]
then
exitError "Minimal oci version required is ${v_min_ocicli}. Found: ${v_cur_ocicli}"
fi
v_ocicli_timeout=3600
[ -z "${v_oci_args}" ] || v_oci="${v_oci} ${v_oci_args}"
v_oci_orig="${v_oci}"
#### FUNCTIONS
function getOrigRegion ()
{
local v_file v_region
v_file=~/.oci/config
if $(echo "$v_oci_args" | grep -q -i -- '--config-file')
then
exitError "Please specify Source Region parameter."
fi
if $(echo "$v_oci_args" | grep -q -i -- '--profile')
then
exitError "Please specify Source Region parameter."
fi
v_region=$(awk '/DEFAULT/{x=1}x&&/region/{print;exit}' "${v_file}" | sed 's/region[ ]*=[ ]*//')
[ ! -r "${v_file}" ] && exitError "Could not read OCI config file."
if [ -n "${v_region}" ]
then
echo ${v_region}
else
exitError "Could not get Source Region."
fi
}
function setRetion ()
{
# Receive region argument and set as oci-cli parameter
v_oci="$v_oci_orig"
[ -n "$1" ] && v_oci="${v_oci} --region $1"
return 0
}
function funcReadVarValidate ()
{
local v_arg1 v_arg2 v_arg3 v_arg4 v_arg5 v_arg6 v_var1 v_check v_loop
# 1 to 4 parameters must be provided
[ "$#" -ge 1 ] || return 2
v_arg1="$1" # Question
v_arg2="$2" # Suggestion
v_arg3="$3" # Options Title
v_arg4="$4" # Options
v_arg5="$5" # Error
v_arg6="$6" # ID / Description Separator
[ -n "${v_arg1}" ] || return 2
[ -n "${v_arg3}" -a -n "${v_arg4}" ] && echo "${v_arg3}:"
[ -z "${v_arg4}" ] || funcPrintRange "${v_arg4}"
v_loop=1
while [ ${v_loop} -eq 1 ]
do
echo -n "${v_arg1}"
[ -n "${v_arg2}" ] && echo -n " [${v_arg2}]"
echo -n ": "
read v_var1
[ -z "${v_var1}" -a -n "${v_arg2}" ] && v_var1="${v_arg2}"
v_loop=0
if [ -n "${v_arg4}" ]
then
v_check=$(funcCheckValueInRange "${v_var1}" "${v_arg4}" "${v_arg6}") || true
if [ -z "${v_check}" -o "${v_check}" == "N" ]
then
[ -z "${v_arg5}" ] || echoError "${v_arg5}"
v_loop=1
else
[ -z "${v_arg6}" ] || v_var1=$(echo "${v_var1}" | sed "s/${v_arg6}.*//")
fi
fi
[ -z "${v_var1}" ] && v_loop=1
done
v_return="${v_var1}"
return 0
}
function funcCheckValueInRange ()
{
local v_arg1 v_arg2 v_arg3 v_list v_opt IFS
v_arg1="$1" # Value
v_arg2="$2" # Range
v_arg3="$3" # ID / Description Separator
[ "$#" -ge 2 -a "$#" -le 3 ] || return 1
[ -n "${v_arg1}" ] || return 1
[ -n "${v_arg2}" ] || return 1
v_list=$(echo "${v_arg2}" | tr "," "\n")
IFS=$'\n'
for v_opt in ${v_list}
do
[ -z "${v_arg3}" ] || v_opt=$(echo "${v_opt}" | sed "s/${v_arg3}.*//")
if [ "$v_opt" == "${v_arg1}" ]
then
echo "Y"
return 0
fi
done
echo "N"
return 1
}
function funcPrintRange ()
{
local v_arg1 v_list v_opt IFS
v_arg1="$1" # Range
[ "$#" -eq 1 ] || return 1
[ -n "${v_arg1}" ] || return 1
v_list=$(echo "${v_arg1}" | tr "," "\n")
IFS=$'\n'
for v_opt in ${v_list}
do
echo "- ${v_opt}"
done
}
function in_subnet ()
{
# Doug R. in https://unix.stackexchange.com/questions/274330/check-ip-is-in-range-of-whitelist-array
# Determine whether IP address is in the specified subnet.
#
# Args:
# sub: Subnet, in CIDR notation.
# ip: IP address to check.
#
# Returns:
# 1|0
#
local ip ip_a mask netmask sub sub_ip rval start end
# Define bitmask.
local readonly BITMASK=0xFFFFFFFF
# Set DEBUG status if not already defined in the script.
[[ "${DEBUG}" == "" ]] && DEBUG=0
# Read arguments.
IFS=/ read sub mask <<< "${1}"
IFS=. read -a sub_ip <<< "${sub}"
IFS=. read -a ip_a <<< "${2}"
# Calculate netmask.
netmask=$(($BITMASK<<$((32-$mask)) & $BITMASK))
# Determine address range.
start=0
for o in "${sub_ip[@]}"
do
start=$(($start<<8 | $o))
done
start=$(($start & $netmask))
end=$(($start | ~$netmask & $BITMASK))
# Convert IP address to 32-bit number.
ip=0
for o in "${ip_a[@]}"
do
ip=$(($ip<<8 | $o))
done
# Removing Network, Gateway and Broadcast:
((start+=2))
((end-=1))
# Determine if IP in range.
(( $ip >= $start )) && (( $ip <= $end )) && rval=1 || rval=0
[ $DEBUG -ge 2 ] && printf "ip=0x%08X; start=0x%08X; end=0x%08X; in_subnet=%u\n" $ip $start $end $rval 1>&2
echo "${rval}"
}
function lineToComma ()
{
echo "${1}" | sort | tr "\n" "," | sed 's/,$//'
}
#### BEGIN
[ -z "${v_orig_region}" ] && v_orig_region=$(getOrigRegion)
setRetion "${v_orig_region}"
#### Validade OCI-CLI and PARAMETER
v_all_compJson=$(${v_oci} iam compartment list --all 2>&1) && v_ret=$? || v_ret=$?
if [ $v_ret -ne 0 ]
then
echoError "oci-cli not able to run \"${v_oci} iam compartment list --all\". Please check error:"
exitError "$v_all_compJson"
fi
v_all_compJson=$(echo ${v_all_compJson} | ${v_jq} -rc '.data[] | select(."lifecycle-state" != "DELETED")')
checkError "$v_all_compJson" "Could not get Json for compartments."
function getInstanceID ()
{
# Receives a parameter that can be either the Compute OCID or Display Name. Returns the Intance OCID and Display Name.
# If Display Name is duplicated on the region, returns an error.
local v_instID v_instName v_comp v_list_comps v_ret v_out
v_instName="$1"
if [ "${v_instName:0:18}" == "ocid1.instance.oc1" ]
then
v_instID=$(${v_oci} compute instance get --instance-id "${v_instName}" | ${v_jq} -rc '.data | select(."lifecycle-state" != "TERMINATED") | ."id"') && v_ret=$? || v_ret=$?
checkError "$v_instID" "$v_ret" "Could not find a compute with the provided OCID."
v_instName=$(${v_oci} compute instance get --instance-id "${v_instID}" | ${v_jq} -rc '.data."display-name"') && v_ret=$? || v_ret=$?
checkError "$v_instName" "$v_ret" "Could not get Display Name of compute ${v_instID}"
else
v_list_comps=$(echo ${v_all_compJson} | ${v_jq} -rc '."id"') && v_ret=$? || v_ret=$?
checkError "$v_list_comps" "$v_ret" "Could not list Compartments."
for v_comp in $v_list_comps
do
v_out=$(${v_oci} compute instance list --compartment-id "$v_comp" --all | ${v_jq} -rc '.data[] | select(."display-name" == "'"${v_instName}"'" and ."lifecycle-state" != "TERMINATED") | ."id"') && v_ret=$? || v_ret=$?
checkError "x" "$v_ret" "Could not search the OCID of compute ${v_instName} in compartment ${v_comp}. Use OCID instead."
if [ -n "$v_out" ]
then
[ -z "$v_instID" ] || exitError "More than 1 compute named \"${v_instName}\" found in this Tenancy. Use OCID instead."
[ -n "$v_instID" ] || v_instID="$v_out"
fi
done
if [ -z "$v_instID" ]
then
exitError "Could not get OCID of compute ${v_instName}"
elif [ $(echo "$v_instID" | wc -l) -ne 1 ]
then
exitError "More than 1 compute named \"${v_instName}\" found in one Compartment. Use OCID instead."
fi
fi
echo "${v_instID}|${v_instName}"
}
echo "Checking Compute Name and OCID."
v_out=$(getInstanceID "${v_orig_instName}")
read v_orig_instID v_orig_instName <<< $(echo "${v_out}" | awk -F'|' '{print $1, $2}')
echoStatus "Compute Name: ${v_orig_instName}"
echoStatus "Compute OCID: ${v_orig_instID}"
#### Collect Origin Information
echo "Getting OCI Compute information.."
v_orig_instJson=$(${v_oci} compute instance get --instance-id "${v_orig_instID}" | ${v_jq} -rc '.data') && v_ret=$? || v_ret=$?
checkError "$v_orig_instJson" "$v_ret" "Could not get Json for compute ${v_orig_instName}"
v_orig_compID=$(echo "$v_orig_instJson" | ${v_jq} -rc '."compartment-id"')
checkError "$v_orig_compID" "Could not get Instance Compartment ID."
v_orig_compArg="--compartment-id ${v_orig_compID}"
v_orig_vnicsJson=$(${v_oci} compute instance list-vnics --all --instance-id "${v_orig_instID}" | ${v_jq} -rc '.data[]') && v_ret=$? || v_ret=$?
checkError "$v_orig_vnicsJson" "$v_ret" "Could not get Json for vnics of ${v_orig_instName}"
v_orig_vnicPriJson=$(echo "$v_orig_vnicsJson" | ${v_jq} -rc 'select (."is-primary" == true)')
v_orig_vnicSecJson=$(echo "$v_orig_vnicsJson" | ${v_jq} -rc 'select (."is-primary" != true)')
v_orig_pubIPJson=$(${v_oci} network public-ip list ${v_orig_compArg} --scope REGION --all | ${v_jq} -rc '.data[]') && v_ret=$? || v_ret=$?
checkError "$v_orig_pubIPJson" "$v_ret" "Could not get Json for Public IPs of ${v_orig_instName}"
v_orig_pubIPs=$(echo "$v_orig_pubIPJson" | ${v_jq} -rc '."ip-address"')
v_orig_AD=$(echo "$v_orig_instJson" | ${v_jq} -rc '."availability-domain"')
checkError "$v_orig_AD" "Could not get Instance Availability Domain."
v_orig_shape=$(echo "$v_orig_instJson" | ${v_jq} -rc '."shape"')
checkError "$v_orig_shape" "Could not get Instance Shape."
v_orig_IP=$(echo "$v_orig_vnicPriJson" | ${v_jq} -rc '."private-ip"')
checkError "$v_orig_IP" "Could not get Instance Primary Private IP Address."
v_orig_subnetID=$(echo "$v_orig_vnicPriJson" | ${v_jq} -rc '."subnet-id"')
checkError "$v_orig_subnetID" "Could not get Instance Primary Subnet ID."
v_orig_subnetName=$(${v_oci} network subnet get --subnet-id "${v_orig_subnetID}" | ${v_jq} -rc '.data."display-name"')
checkError "$v_orig_subnetName" "Could not get Instance Primary Subnet Name."
v_orig_BVID=$(${v_oci} compute boot-volume-attachment list ${v_orig_compArg} --availability-domain "${v_orig_AD}" --instance-id "${v_orig_instID}" | ${v_jq} -rc '.data[] | ."boot-volume-id"')
checkError "$v_orig_BVID" "Could not get Instance Boot Volume ID."
v_orig_attachVolsJson=$(${v_oci} compute volume-attachment list ${v_orig_compArg} --all --instance-id "${v_orig_instID}") && v_ret=$? || v_ret=$?
checkError "x" "$v_ret" "Could not get Json for Attached Volumes of ${v_orig_instName}"
v_orig_imageID=$(echo "$v_orig_instJson" | ${v_jq} -rc '."image-id"')
checkError "$v_orig_imageID" "Could not get Instance Image ID."
v_orig_imageJson=$(${v_oci} compute image get --image-id ${v_orig_imageID}) && v_ret=$? || v_ret=$?
checkError "$v_orig_imageJson" "$v_ret" "Could not get Image Json."
v_orig_OS=$(echo "$v_orig_imageJson" | ${v_jq} -rc '.data."operating-system"')
checkError "$v_orig_OS" "Could not get Image OS."
[ "${v_orig_OS}" == "Windows" ] && exitError "Cloning Oracle Windows based compute instances is not yet supported by OCI-CLI."
#### Collect Clone Information
if [ -z "${v_clone_subnetID}" ]
then
echo "The Source machine will be temporarily cloned within the same region, so an image can be created without stopping it."
funcReadVarValidate "Create temporary Clone Compute machine in same subnet as the source" "YES" "Options" "YES,NO" "Invalid Option."
if [ "${v_return}" == "YES" ]
then
v_clone_subnetID="${v_orig_subnetID}"
v_clone_compArg="${v_orig_compArg}"
else
## Compartment
v_comps_list=$(echo "${v_all_compJson}" | ${v_jq} -rc '."name"')
v_orig_compName=$(echo "${v_all_compJson}" | ${v_jq} -rc 'select(."id" == "'${v_orig_compID}'") | ."name"')
funcReadVarValidate "Choose the container where the temporary Clone Compute will be placed" "${v_orig_compName}" "Available Containers" "$(lineToComma "${v_comps_list}")" "Invalid Container."
v_clone_compName="${v_return}"
v_clone_compID=$(echo "${v_all_compJson}" | ${v_jq} -rc 'select(."name" == "'"${v_clone_compName}"'") | ."id"')
v_clone_compArg="--compartment-id ${v_clone_compID}"
## VCN
v_all_vcnJson=$(${v_oci} network vcn list --all ${v_clone_compArg} | ${v_jq} -rc '.data[]') && v_ret=$? || v_ret=$?
checkError "$v_all_vcnJson" "$v_ret" "Could not get Json for VCNs."
if [ "${v_orig_compName}" == "${v_clone_compName}" ]
then
v_orig_VCNID=$(${v_oci} network subnet get --subnet-id $v_orig_subnetID | ${v_jq} -rc '.data."vcn-id"')
v_orig_VCNName=$(echo "${v_all_vcnJson}" | ${v_jq} -rc 'select(."id" == "'${v_orig_VCNID}'") | ."display-name"')
else
v_orig_VCNName=""
fi
v_vcns_list=$(echo "${v_all_vcnJson}" | ${v_jq} -rc '."display-name" + " - " + ."cidr-block"')
funcReadVarValidate "Choose a clone VCN" "${v_orig_VCNName}" "Available VCNs" "$(lineToComma "${v_vcns_list}")" "Invalid VCN." ' - '
v_clone_VCNName="${v_return}"
v_clone_VCNID=$(echo "${v_all_vcnJson}" | ${v_jq} -rc 'select(."display-name" == "'"${v_clone_VCNName}"'") | ."id"')
## SubNet
v_all_subnetJson=$(${v_oci} network subnet list --all ${v_clone_compArg} --vcn-id ${v_clone_VCNID} | ${v_jq} -rc '.data[]') && v_ret=$? || v_ret=$?
checkError "$v_all_subnetJson" "$v_ret" "Could not get Json for Subnets."
if [ "${v_orig_compName}" == "${v_clone_compName}" -a "${v_orig_VCNName}" == "${v_clone_VCNName}" ]
then
v_orig_subnetName=$(echo "${v_all_subnetJson}" | ${v_jq} -rc 'select(."id" == "'${v_orig_subnetID}'") | ."display-name"')
else
v_orig_subnetName=""
fi
v_subnets_list=$(echo "${v_all_subnetJson}" | ${v_jq} -rc '."display-name" + " - " + ."cidr-block"')
funcReadVarValidate "Choose a clone Subnet" "${v_orig_subnetName}" "Available Subnets" "$(lineToComma "${v_subnets_list}")" "Invalid Subnet." ' - '
v_clone_subnetName="${v_return}"
v_clone_subnetID=$(echo "${v_all_subnetJson}" | ${v_jq} -rc 'select(."display-name" == "'"${v_clone_subnetName}"'") | ."id"')
echoStatus "Clone Subnet ID: ${v_clone_subnetID}"
fi
fi
v_clone_subnetJson=$(${v_oci} network subnet get --subnet-id ${v_clone_subnetID} | ${v_jq} -rc '.data') && v_ret=$? || v_ret=$?
checkError "$v_clone_subnetJson" "$v_ret" "Can't find Clone Subnet."
v_clone_AD=$(echo "${v_clone_subnetJson}" | ${v_jq} -rc '."availability-domain" // empty')
checkError "x" "${v_ret}" "Can't find Clone AD."
[ -z "${v_clone_AD}" ] && v_clone_AD="${v_orig_AD}" # If regional Subnet
v_clone_compID=$(echo "${v_clone_subnetJson}" | ${v_jq} -rc '."compartment-id"') && v_ret=$? || v_ret=$?
checkError "${v_clone_compID}" "${v_ret}" "Could not get the clone Compartment ID."
v_clone_compArg="--compartment-id ${v_clone_compID}"
#### Collect Target Information
if [ -z "${v_target_instName}" ]
then
funcReadVarValidate "Target Instance Name" "$(echo "${v_orig_instName}" | sed "${v_sedrep_rule_target_name}")"
v_target_instName="${v_return}"
fi
[ -n "${v_sedrep_rule_target_name}" ] || v_sedrep_rule_target_name="s|${v_orig_instName}|${v_target_instName}|g"
if [ -z "${v_target_region}" ]
then
v_all_regionJson=$(${v_oci} iam region-subscription list | ${v_jq} -rc '.data[]') && v_ret=$? || v_ret=$?
checkError "$v_all_regionJson" "$v_ret" "Could not get Json for regions."
v_regions_list=$(echo "${v_all_regionJson}" | ${v_jq} -rc '."region-name"')
[ -n "${v_orig_region}" ] && v_regions_list=$(echo "${v_regions_list}" | grep -v "${v_orig_region}")
funcReadVarValidate "Choose a target region" '' "Available Regions" "$(lineToComma "${v_regions_list}")" "Invalid Region."
v_target_region="${v_return}"
fi
[ "${v_orig_region}" == "${v_target_region}" ] && exitError "Source and Target regions can't be the same. Use \"oci_compute_clone.sh\" instead."
setRetion "${v_target_region}"
if [ -z "${v_target_subnetID}" ]
then
## Compartment
v_comps_list=$(echo "${v_all_compJson}" | ${v_jq} -rc '."name"')
v_orig_compName=$(echo "${v_all_compJson}" | ${v_jq} -rc 'select(."id" == "'${v_orig_compID}'") | ."name"')
funcReadVarValidate "Choose a target container" "${v_orig_compName}" "Available Containers" "$(lineToComma "${v_comps_list}")" "Invalid Container."
v_target_compName="${v_return}"
v_target_compID=$(echo "${v_all_compJson}" | ${v_jq} -rc 'select(."name" == "'"${v_target_compName}"'") | ."id"')
v_target_compArg="--compartment-id ${v_target_compID}"
## VCN
v_all_vcnJson=$(${v_oci} network vcn list --all ${v_target_compArg} | ${v_jq} -rc '.data[]') && v_ret=$? || v_ret=$?
checkError "$v_all_vcnJson" "$v_ret" "Could not get Json for VCNs."
if [ "${v_orig_compName}" == "${v_target_compName}" ]
then
setRetion "${v_orig_region}"
v_orig_VCNID=$(${v_oci} network subnet get --subnet-id $v_orig_subnetID | ${v_jq} -rc '.data."vcn-id"')
v_orig_VCNName=$(echo "${v_all_vcnJson}" | ${v_jq} -rc 'select(."id" == "'${v_orig_VCNID}'") | ."display-name"')
setRetion "${v_target_region}"
else
v_orig_VCNName=""
fi
v_vcns_list=$(echo "${v_all_vcnJson}" | ${v_jq} -rc '."display-name" + " - " + ."cidr-block"')
funcReadVarValidate "Choose a target VCN" "${v_orig_VCNName}" "Available VCNs" "$(lineToComma "${v_vcns_list}")" "Invalid VCN." ' - '
v_target_VCNName="${v_return}"
v_target_VCNID=$(echo "${v_all_vcnJson}" | ${v_jq} -rc 'select(."display-name" == "'"${v_target_VCNName}"'") | ."id"')
## SubNet
v_all_subnetJson=$(${v_oci} network subnet list --all ${v_target_compArg} --vcn-id ${v_target_VCNID} | ${v_jq} -rc '.data[]') && v_ret=$? || v_ret=$?
checkError "$v_all_subnetJson" "$v_ret" "Could not get Json for Subnets."
if [ "${v_orig_compName}" == "${v_target_compName}" -a "${v_orig_VCNName}" == "${v_target_VCNName}" ]
then
v_orig_subnetName=$(echo "${v_all_subnetJson}" | ${v_jq} -rc 'select(."id" == "'${v_orig_subnetID}'") | ."display-name"')
else
v_orig_subnetName=""
fi
v_subnets_list=$(echo "${v_all_subnetJson}" | ${v_jq} -rc '."display-name" + " - " + ."cidr-block"')
funcReadVarValidate "Choose a target Subnet" "${v_orig_subnetName}" "Available Subnets" "$(lineToComma "${v_subnets_list}")" "Invalid Subnet." ' - '
v_target_subnetName="${v_return}"
v_target_subnetID=$(echo "${v_all_subnetJson}" | ${v_jq} -rc 'select(."display-name" == "'"${v_target_subnetName}"'") | ."id"')
echoStatus "Target Subnet ID: ${v_target_subnetID}"
fi
v_target_subnetJson=$(${v_oci} network subnet get --subnet-id ${v_target_subnetID} | ${v_jq} -rc '.data') && v_ret=$? || v_ret=$?
checkError "${v_target_subnetJson}" "${v_ret}" "Can't find Target Subnet."
v_all_IPs=$(${v_oci} network private-ip list --all --subnet-id ${v_target_subnetID} | ${v_jq} -rc '.data[]."ip-address"')
v_target_CIDR=$(echo "${v_target_subnetJson}" | ${v_jq} -rc '."cidr-block"')
v_target_compID=$(echo "${v_target_subnetJson}" | ${v_jq} -rc '."compartment-id"') && v_ret=$? || v_ret=$?
checkError "${v_target_compID}" "${v_ret}" "Could not get the target Compartment ID."
v_target_compArg="--compartment-id ${v_target_compID}"
v_target_AD=$(echo "${v_target_subnetJson}" | ${v_jq} -rc '."availability-domain" // empty') && v_ret=$? || v_ret=$?
checkError "x" "${v_ret}" "Can't find Target AD."
# When target subnet is regional, will use first AD for the instance.
if [ -z "${v_target_AD}" ]
then
v_target_AD=$(${v_oci} iam availability-domain list | ${v_jq} -rc '.data[0]."name"') && v_ret=$? || v_ret=$?
checkError "${v_target_AD}" "${v_ret}" "Can't define Target AD."
fi
v_target_allowPub=$(echo "${v_target_subnetJson}" | ${v_jq} -rc '."prohibit-public-ip-on-vnic"') && v_ret=$? || v_ret=$?
checkError "${v_target_allowPub}" "${v_ret}" "Can't get target IP allowance."
if [ -z "${v_target_IP}" ]
then
if [ -n "${v_all_IPs}" ]
then
echo "Following IP's are already in use:"
funcPrintRange "$(echo "${v_all_IPs}" | tr "\n" "," | sed 's/,$//')"
fi
v_loop=1
while [ ${v_loop} -eq 1 ]
do
v_return=""
funcReadVarValidate "Choose an IP in \"${v_target_CIDR}\""
v_check=$(funcCheckValueInRange "${v_return}" "$(lineToComma "${v_all_IPs}")") || true
if [ "${v_check}" == "Y" -a -n "${v_all_IPs}" ]
then
echoError "IP \"$v_return\" already in use."
else
v_loop=0
fi
(( $(in_subnet "${v_target_CIDR}" "${v_return}") )) || echoError "IP \"$v_return\" not in $v_target_CIDR block."
(( $(in_subnet "${v_target_CIDR}" "${v_return}") )) || v_loop=1
done
v_target_IP="${v_return}"
else
v_check=$(funcCheckValueInRange "${v_target_IP}" "$(echo "${v_all_IPs}" | tr "\n" "," | sed 's/,$//')") || true
if [ "${v_check}" == "Y" -a -n "${v_all_IPs}" ]
then
exitError "IP \"${v_target_IP}\" already in use."
fi
(( $(in_subnet "${v_target_CIDR}" "${v_target_IP}") )) || exitError "IP \"${v_target_IP}\" not in $v_target_CIDR block."
fi
v_all_shapes=$(${v_oci} compute shape list ${v_target_compArg} --all | jq -r '.data[].shape' | sort -u)
if [ -z "${v_target_shape}" ]
then
echo "Source Instance Shape: ${v_orig_shape}"
funcReadVarValidate "Target Instance Shape" "${v_orig_shape}" "Available Shapes" "$(lineToComma "${v_all_shapes}")" "Invalid Shape."
v_target_shape="${v_return}"
else
v_check=$(funcCheckValueInRange "${v_target_shape}" "$(echo "${v_all_shapes}" | tr "\n" "," | sed 's/,$//')") || true
if [ -z "${v_check}" -o "${v_check}" == "N" ]
then
exitError "Shape does not exist or not available."
fi
fi
#### Collect Bucket Information
setRetion "${v_orig_region}"
if [ -z "${v_os_bucketName}" ]
then
## Compartment
v_comps_list=$(echo "${v_all_compJson}" | ${v_jq} -rc '."name"')
v_clone_compName=$(echo "${v_all_compJson}" | ${v_jq} -rc 'select(."id" == "'${v_clone_compID}'") | ."name"')
funcReadVarValidate "Choose the container where the OS bucket used for migration is placed" "${v_clone_compName}" "Available Containers" "$(lineToComma "${v_comps_list}")" "Invalid Container."
v_os_compName="${v_return}"
v_os_compID=$(echo "${v_all_compJson}" | ${v_jq} -rc 'select(."name" == "'"${v_os_compName}"'") | ."id"')
v_os_compArg="--compartment-id ${v_os_compID}"
## VCN
v_all_bucketJson=$(${v_oci} os bucket list --all ${v_os_compArg} | ${v_jq} -rc '.data[]') && v_ret=$? || v_ret=$?
checkError "$v_all_bucketJson" "$v_ret" "Could not get Json for Buckets."
v_buckets_list=$(echo "${v_all_bucketJson}" | ${v_jq} -rc '."name"')
funcReadVarValidate "Choose a bucket for image migration" "" "Available Buckets" "$(lineToComma "${v_buckets_list}")" "Invalid Bucket."
v_os_bucketName="${v_return}"
fi
v_os_bucketJson=$(${v_oci} os bucket get --bucket-name ${v_os_bucketName} | ${v_jq} -rc '.data') && v_ret=$? || v_ret=$?
checkError "${v_os_bucketJson}" "${v_ret}" "Can't find OS Bucket."
# New version is not using public OS Buckets to move anymore.
# v_os_bucketPublic=$(echo "${v_os_bucketJson}" | ${v_jq} -rc '."public-access-type"')
# checkError "${v_os_bucketPublic}" "Can't get Bucket public attribute."
# [ "${v_os_bucketPublic}" == "NoPublicAccess" ] && exitError "OS Bucket must have Public ObjectRead Access enabled."
#####
#####
#####
export v_step=1
function printStep ()
{
echoStatus "Executing Step $v_step"
((v_step++))
}
echoStatus "Starting execution."
echo "Steps:"
echo "$v_all_steps"
######
### 01
######
printStep
setRetion "${v_orig_region}"
v_orig_VGName="${v_orig_instName}_VG"
v_volList='{'
v_volList+='"type":"volumeIds",'
v_volList+='"volumeIds": ['
v_volList+='"'${v_orig_BVID}'"'
# Not using "raw" as jq param to include quotes.
v_instvolsID_list=$(echo "$v_orig_attachVolsJson" | ${v_jq} -c '.data[] | select(."lifecycle-state" == "ATTACHED") | ."volume-id"')
for v_instvolsID in $v_instvolsID_list
do
v_volList+=",${v_instvolsID}"
done
v_volList+=']'
v_volList+='}'
v_params=()
v_params+=(${v_orig_compArg})
v_params+=(--availability-domain ${v_orig_AD})
v_params+=(--display-name "${v_orig_VGName}")
v_params+=(--max-wait-seconds $v_ocicli_timeout)
v_params+=(--wait-for-state AVAILABLE)
v_params+=(--source-details "${v_volList}")
(( $DEBUG )) && set -x
v_orig_VGJson=$(${v_oci} bv volume-group create "${v_params[@]}") && v_ret=$? || v_ret=$?
(( $DEBUG )) && set +x
checkError "$v_orig_VGJson" "$v_ret" "Could not create Volume Group."
v_orig_VGID=$(echo "$v_orig_VGJson"| ${v_jq} -rc '.data."id"')
checkError "$v_orig_VGID" "Could not get Volume ID."
######
### 02
######
printStep
v_orig_VGBackupName="${v_orig_instName}_VG_BKP"
v_params=()
v_params+=(${v_clone_compArg})
v_params+=(--volume-group-id ${v_orig_VGID})
v_params+=(--display-name "${v_orig_VGBackupName}")
v_params+=(--type INCREMENTAL)
v_params+=(--wait-for-state AVAILABLE)
v_params+=(--max-wait-seconds $v_ocicli_timeout)
(( $DEBUG )) && set -x
v_orig_VGBackupJson=$(${v_oci} bv volume-group-backup create "${v_params[@]}") && v_ret=$? || v_ret=$?
(( $DEBUG )) && set +x
checkError "$v_orig_VGBackupJson" "$v_ret" "Could not create Volume Group Backup."
v_orig_VGBackupID=$(echo "$v_orig_VGBackupJson"| ${v_jq} -rc '.data."id"')
checkError "$v_orig_VGBackupID" "Could not get Volume Group Backup ID."
######
### 03
######
printStep
v_params=()
v_params+=(--volume-group-id ${v_orig_VGID})
v_params+=(--force)
v_params+=(--wait-for-state TERMINATED)
v_params+=(--max-wait-seconds $v_ocicli_timeout)
(( $DEBUG )) && set -x
${v_oci} bv volume-group delete "${v_params[@]}" && v_ret=$? || v_ret=$?
(( $DEBUG )) && set +x
checkError "x" "$v_ret" "Could not remove Volume Group."
######
### 04
######
printStep
v_volBkpID_list=$(echo "$v_orig_VGBackupJson" | ${v_jq} -rc '.data."volume-backup-ids"[]')
for v_volBkpID in $v_volBkpID_list
do
if [ "${v_volBkpID:0:26}" == "ocid1.bootvolumebackup.oc1" ]
then
v_clone_BVBackupID="${v_volBkpID}"
fi
done
v_clone_BVName="${v_orig_instName}_CLONE_BV"
v_params=()
v_params+=(--boot-volume-backup-id ${v_clone_BVBackupID})
v_params+=(--display-name "${v_clone_BVName}")
v_params+=(--availability-domain ${v_clone_AD})
v_params+=(--wait-for-state AVAILABLE)
v_params+=(--max-wait-seconds $v_ocicli_timeout)
(( $DEBUG )) && set -x
v_clone_BVJson=$(${v_oci} bv boot-volume create "${v_params[@]}") && v_ret=$? || v_ret=$?
(( $DEBUG )) && set +x
checkError "$v_clone_BVJson" "$v_ret" "Could not create Boot Volume from Backup."
v_clone_BVID=$(echo "$v_clone_BVJson" | ${v_jq} -rc '.data."id"')
checkError "$v_clone_BVID" "Could not get Boot Volume ID."
######
### 05
######
printStep
v_clone_instName="${v_orig_instName}_CLONE_INST"
v_clone_instShape="VM.Standard2.1"
v_params=()
v_params+=(${v_clone_compArg})
v_params+=(--availability-domain ${v_clone_AD})
v_params+=(--shape ${v_clone_instShape})
v_params+=(--wait-for-state RUNNING)
v_params+=(--max-wait-seconds $v_ocicli_timeout)
v_params+=(--display-name "${v_clone_instName}")
v_params+=(--source-boot-volume-id ${v_clone_BVID})
v_params+=(--subnet-id ${v_clone_subnetID})
v_params+=(--assign-public-ip false)
(( $DEBUG )) && set -x
v_clone_instJson=$(${v_oci} compute instance launch "${v_params[@]}") && v_ret=$? || v_ret=$?
(( $DEBUG )) && set +x
checkError "$v_clone_instJson" "$v_ret" "Could not create Clone Instance."
v_clone_instID=$(echo "$v_clone_instJson" | ${v_jq} -rc '.data."id"')
checkError "$v_clone_instID" "Could not get Clone Instance ID."
######
### 06
######
printStep
v_params=()
v_params+=(--instance-id ${v_clone_instID})
v_params+=(--action STOP)
v_params+=(--wait-for-state STOPPED)
v_params+=(--max-wait-seconds $v_ocicli_timeout)
(( $DEBUG )) && set -x
${v_oci} compute instance action "${v_params[@]}" >&- && v_ret=$? || v_ret=$?
(( $DEBUG )) && set +x
checkError "x" "$v_ret" "Could not stop Clone Instance."
######
### 07
######
printStep
v_clone_imageName="${v_orig_instName}_IMAGE"
v_params=()
v_params+=(${v_clone_compArg})
v_params+=(--display-name "${v_clone_imageName}")
v_params+=(--instance-id ${v_clone_instID})
v_params+=(--wait-for-state AVAILABLE)
v_params+=(--max-wait-seconds $v_ocicli_timeout)
(( $DEBUG )) && set -x
v_clone_imageJson=$(${v_oci} compute image create "${v_params[@]}") && v_ret=$? || v_ret=$?
(( $DEBUG )) && set +x
checkError "$v_clone_imageJson" "$v_ret" "Could not create Image from Clone Instance."
v_clone_imageID=$(echo "$v_clone_imageJson" | ${v_jq} -rc '.data."id"')
checkError "$v_clone_imageID" "Could not get Clone Instance Image ID."
######
### 08
######
printStep
v_params=()
v_params+=(--instance-id ${v_clone_instID})
v_params+=(--preserve-boot-volume false)
v_params+=(--force)
v_params+=(--wait-for-state TERMINATED)
v_params+=(--max-wait-seconds $v_ocicli_timeout)
(( $DEBUG )) && set -x
${v_oci} compute instance terminate "${v_params[@]}" && v_ret=$? || v_ret=$?
(( $DEBUG )) && set +x
checkError "x" "$v_ret" "Could not terminate Clone Instance."
######
### 09
######
"${v_workdir}"/${v_oci_image_clone_script} -i "${v_clone_imageID}" -b "${v_os_bucketName}" -t "${v_target_region}" -s "${v_orig_region}"
setRetion "${v_target_region}"
v_params=()
v_params+=(${v_clone_compArg})
(( $DEBUG )) && set -x
v_target_imageJson=$(${v_oci} compute image list "${v_params[@]}" | ${v_jq} -rc '.data[] | select (."freeform-tags"."Source_OCID"=="'${v_clone_imageID}'")') && v_ret=$? || v_ret=$?
(( $DEBUG )) && set +x
checkError "$v_target_imageJson" "$v_ret" "Could not get Json of imported Image."
v_target_imageID=$(echo "$v_target_imageJson" | ${v_jq} -rc '."id"')
checkError "$v_target_imageID" "Could not get OCID of imported Image."
######
### 14
######
v_step=$((v_step+5))
printStep
setRetion "${v_orig_region}"
v_params=()
v_params+=(--image-id ${v_clone_imageID})
v_params+=(--force)
v_params+=(--wait-for-state DELETED)
v_params+=(--max-wait-seconds $v_ocicli_timeout)
(( $DEBUG )) && set -x
${v_oci} compute image delete "${v_params[@]}" && v_ret=$? || v_ret=$?
(( $DEBUG )) && set +x
checkError "x" "$v_ret" "Could not delete Source Image."