@@ -17,6 +17,7 @@ BUILD_ID_SHORT="${BUILD_ID:0:6}"
1717PROVISIONING_MODEL=" SPOT"
1818TERMINATION_ACTION=" DELETE"
1919FULL_INSTANCE_PREFIX=" ${INSTANCE_PREFIX} -${BUILD_ID_SHORT} -"
20+ MIN_NODES=2 # Define minimum number of nodes required
2021
2122generate_instance_names () {
2223 local prefix=$1
@@ -71,28 +72,55 @@ SUCCESS=false
7172# Loop through all zones to find capacity
7273for ZONE in " ${ZONES_ARRAY[@]} " ; do
7374 readarray -t INSTANCE_NAMES_ARRAY < <( generate_instance_names " ${FULL_INSTANCE_PREFIX} " " ${NUM_NODES} " )
75+ # Join instance names with commas for the --predefined-names flag
76+ instance_names_str=$(
77+ IFS=,
78+ echo " ${INSTANCE_NAMES_ARRAY[*]} "
79+ )
7480
7581 declare -a GCLOUD_CMD
7682 GCLOUD_CMD=(
77- gcloud compute instances create " ${INSTANCE_NAMES_ARRAY[@]} "
83+ gcloud compute instances bulk create
84+ --predefined-names=" ${instance_names_str} "
7885 --project=" ${PROJECT_ID} "
7986 --zone=" ${ZONE} "
8087 --machine-type=" ${MACHINE_TYPE} "
8188 --provisioning-model=" ${PROVISIONING_MODEL} "
8289 --instance-termination-action=" ${TERMINATION_ACTION} "
8390 --no-address
8491 --quiet
92+ --min-count=" ${MIN_NODES} "
8593 )
8694 if CREATE_OUTPUT=$( " ${GCLOUD_CMD[@]} " 2>&1 ) ; then
95+ instance_list_output=$( gcloud compute instances list --project=" ${PROJECT_ID} " --zones=" ${ZONE} " \
96+ --filter=" name ~ ^${FULL_INSTANCE_PREFIX} " --format=' value(name)' )
97+
98+ if [[ $? -eq 0 && -n " ${instance_list_output} " ]]; then
99+ readarray -t created_instances < <( echo " ${instance_list_output} " )
100+ NUM_CREATED=$(( ${# created_instances[@]} ))
101+ echo " ${ZONE} ${NUM_CREATED} "
102+ cleanup_instances " ${PROJECT_ID} " " ${ZONE} " " ${FULL_INSTANCE_PREFIX} "
103+
104+ if [[ " ${NUM_CREATED} " -ge " ${MIN_NODES} " ]]; then
105+ SELECTED_ZONE=" ${ZONE} "
106+ SUCCESS=true
107+ break
108+ else
109+ echo " ERROR: Bulk create succeeded but only ${NUM_CREATED} instances found, less than min_count ${MIN_NODES} in ${ZONE} ." >&2
110+ fi
111+ else
112+ echo " ERROR: Bulk create command succeeded in ${ZONE} , but failed to list the created instances or none found." >&2
113+ cleanup_instances " ${PROJECT_ID} " " ${ZONE} " " ${FULL_INSTANCE_PREFIX} "
114+ fi
115+ else
116+ # Command failed
117+ if [[ " ${CREATE_OUTPUT} " != * " INSUFFICIENT_CAPACITY" * &&
118+ " ${CREATE_OUTPUT} " != * " ZONE_RESOURCE_POOL_EXHAUSTED" * ]]; then
119+ echo " ERROR: Unexpected error during bulk create in ${ZONE} : ${CREATE_OUTPUT} " >&2
120+ fi
121+ # bulk create with --min-count should handle rollback on failure, but cleanup just in case.
87122 cleanup_instances " ${PROJECT_ID} " " ${ZONE} " " ${FULL_INSTANCE_PREFIX} "
88- SELECTED_ZONE=" ${ZONE} "
89- SUCCESS=true
90- break
91- elif [[ " ${CREATE_OUTPUT} " != * " INSUFFICIENT_CAPACITY" * &&
92- " ${CREATE_OUTPUT} " != * " ZONE_RESOURCE_POOL_EXHAUSTED" * ]]; then
93- echo " ERROR: Unexpected error ${CREATE_OUTPUT} " >&2
94123 fi
95- cleanup_instances " ${PROJECT_ID} " " ${ZONE} " " ${FULL_INSTANCE_PREFIX} "
96124done
97125
98126if [[ " ${SUCCESS} " == " true" ]]; then
0 commit comments