@@ -22,7 +22,6 @@ function cloud_init {
22
22
GOOGLE_APPLICATION_CREDENTIALS=${GOOGLE_APPLICATION_CREDENTIALS:- " " }
23
23
GCP_PROJECT=${GCP_PROJECT:- " " }
24
24
GCP_REGION=${GCP_REGION:- " us-east4" }
25
- GCP_ZONE=${GCP_ZONE:- " us-east4-a" }
26
25
GCP_MACHINE_MIN_CPU_PLATFORM=${GCP_MACHINE_MIN_CPU_PLATFORM:- " Intel Cascade Lake" }
27
26
GCP_NETWORK_NAME=${GCP_NETWORK_NAME:- " ${CLUSTER_NAME} -mynetwork" }
28
27
@@ -40,8 +39,7 @@ function cloud_init {
40
39
41
40
function init_infrastructure() {
42
41
if [[ ${GCP_NETWORK_NAME} != " default" ]]; then
43
- if ! gcloud compute networks describe " $GCP_NETWORK_NAME " --project " $GCP_PROJECT " > /dev/null;
44
- then
42
+ if ! gcloud compute networks describe " $GCP_NETWORK_NAME " --project " $GCP_PROJECT " > /dev/null; then
45
43
gcloud compute networks create --project " $GCP_PROJECT " " $GCP_NETWORK_NAME " --subnet-mode custom
46
44
gcloud compute networks subnets create " $GCP_NETWORK_NAME " --project " $GCP_PROJECT " \
47
45
--network=" $GCP_NETWORK_NAME " --range=" $PRIVATE_NETWORK_CIDR " --region " $GCP_REGION "
@@ -66,17 +64,15 @@ function init_infrastructure() {
66
64
gcloud compute networks list --project=" $GCP_PROJECT "
67
65
gcloud compute networks describe " $GCP_NETWORK_NAME " --project=" $GCP_PROJECT "
68
66
69
- if ! gcloud compute routers describe " ${CLUSTER_NAME} -myrouter" --project=" $GCP_PROJECT " --region=" $GCP_REGION " > /dev/null;
70
- then
67
+ if ! gcloud compute routers describe " ${CLUSTER_NAME} -myrouter" --project=" $GCP_PROJECT " --region=" $GCP_REGION " > /dev/null; then
71
68
gcloud compute routers create " ${CLUSTER_NAME} -myrouter" --project=" $GCP_PROJECT " \
72
- --region=" $GCP_REGION " --network=" $GCP_NETWORK_NAME "
69
+ --region=" $GCP_REGION " --network=" $GCP_NETWORK_NAME "
73
70
fi
74
71
if ! gcloud compute routers nats describe --router=" $CLUSTER_NAME -myrouter" " $CLUSTER_NAME -mynat" \
75
- --project=" $GCP_PROJECT " --region=" ${GCP_REGION} " > /dev/null;
76
- then
77
- gcloud compute routers nats create " ${CLUSTER_NAME} -mynat" --project=" $GCP_PROJECT " \
78
- --router-region=" $GCP_REGION " --router=" ${CLUSTER_NAME} -myrouter" \
79
- --nat-all-subnet-ip-ranges --auto-allocate-nat-external-ips
72
+ --project=" $GCP_PROJECT " --region=" ${GCP_REGION} " > /dev/null; then
73
+ gcloud compute routers nats create " ${CLUSTER_NAME} -mynat" --project=" $GCP_PROJECT " \
74
+ --router-region=" $GCP_REGION " --router=" ${CLUSTER_NAME} -myrouter" \
75
+ --nat-all-subnet-ip-ranges --auto-allocate-nat-external-ips
80
76
fi
81
77
}
82
78
@@ -92,49 +88,56 @@ function create_vm {
92
88
local diskname=" ${CLUSTER_NAME} -disk"
93
89
local imagename=" ${servername} -image"
94
90
95
- # Create the base disk image based on the public Ubuntu 20.04 LTS cloud image
96
- # Note that this has also been verified to work with CentOS 8 as of
97
- # 2021-01-12, but this is not tested regularly.
98
- # To use CentOS 8:
99
- # --image-project centos-cloud --image-family centos-stream-8
100
- if ! gcloud compute disks describe " $diskname " --project " $GCP_PROJECT " --zone " $GCP_ZONE " > /dev/null;
101
- then
102
- gcloud compute disks create " $diskname " \
103
- --project " $GCP_PROJECT " \
104
- --image-project ubuntu-os-cloud --image-family ubuntu-2004-lts \
105
- --zone " $GCP_ZONE "
106
- fi
107
-
108
- if ! gcloud compute images describe " $imagename " --project " $GCP_PROJECT " > /dev/null;
109
- then
110
- gcloud compute images create " $imagename " \
111
- --project " $GCP_PROJECT " \
112
- --source-disk " $diskname " --source-disk-zone " $GCP_ZONE " \
113
- --licenses " https://www.googleapis.com/compute/v1/projects/vm-options/global/licenses/enable-vmx"
114
- fi
91
+ # Loop over all zones in the GCP region to ignore a full zone.
92
+ # We are not able to use 'gcloud compute zones list' as the gcloud.compute.zones.list permission is missing.
93
+ for GCP_ZONE in " ${GCP_REGION} -a" " ${GCP_REGION} -b" " ${GCP_REGION} -c" ; do
94
+ # Check if image was already created.
95
+ # Images are not zone specific, but the disk is.
96
+ if ! gcloud compute images describe " $imagename " --project " $GCP_PROJECT " > /dev/null; then
97
+ # Create the base disk image based on the public Ubuntu 20.04 LTS cloud image
98
+ # Note that this has also been verified to work with CentOS 8 as of
99
+ # 2021-01-12, but this is not tested regularly.
100
+ # To use CentOS 8:
101
+ # --image-project centos-cloud --image-family centos-stream-8
102
+ if ! gcloud compute disks describe " $diskname " --project " $GCP_PROJECT " --zone " $GCP_ZONE " > /dev/null; then
103
+ gcloud compute disks create " $diskname " \
104
+ --project " $GCP_PROJECT " \
105
+ --image-project ubuntu-os-cloud --image-family ubuntu-2004-lts \
106
+ --zone " $GCP_ZONE "
107
+ fi
108
+ gcloud compute images create " $imagename " \
109
+ --project " $GCP_PROJECT " \
110
+ --source-disk " $diskname " --source-disk-zone " $GCP_ZONE " \
111
+ --licenses " https://www.googleapis.com/compute/v1/projects/vm-options/global/licenses/enable-vmx"
112
+ fi
115
113
116
- if ! gcloud compute instances describe " $servername " --project " $GCP_PROJECT " --zone " $GCP_ZONE " > /dev/null;
117
- then
118
- gcloud compute instances create " $servername " \
119
- --project " $GCP_PROJECT " \
120
- --zone " $GCP_ZONE " \
121
- --image " $imagename " \
122
- --boot-disk-size 200G \
123
- --boot-disk-type pd-ssd \
124
- --can-ip-forward \
125
- --tags http-server,https-server,novnc,openstack-apis \
126
- --min-cpu-platform " $GCP_MACHINE_MIN_CPU_PLATFORM " \
127
- --machine-type " $machine_type " \
128
- --network-interface=" private-network-ip=${ip} ,network=${CLUSTER_NAME} -mynetwork,subnet=${CLUSTER_NAME} -mynetwork" \
129
- --metadata-from-file user-data=" $userdata "
130
- fi
114
+ if ! gcloud compute instances describe " $servername " --project " $GCP_PROJECT " --zone " $GCP_ZONE " > /dev/null; then
115
+ if gcloud compute instances create " $servername " \
116
+ --project " $GCP_PROJECT " \
117
+ --zone " $GCP_ZONE " \
118
+ --image " $imagename " \
119
+ --boot-disk-size 200G \
120
+ --boot-disk-type pd-ssd \
121
+ --can-ip-forward \
122
+ --tags http-server,https-server,novnc,openstack-apis \
123
+ --min-cpu-platform " $GCP_MACHINE_MIN_CPU_PLATFORM " \
124
+ --machine-type " $machine_type " \
125
+ --network-interface=" private-network-ip=${ip} ,network=${CLUSTER_NAME} -mynetwork,subnet=${CLUSTER_NAME} -mynetwork" \
126
+ --metadata-from-file user-data=" $userdata " ; then
127
+ # return function create_vm if the instance have been created successfully.
128
+ return
129
+ fi
130
+ fi
131
+ done
132
+ echo " No free GCP zone could be found to create instance $servername ."
133
+ exit 1
131
134
}
132
135
133
136
function get_public_ip {
134
137
local ip
135
138
while ! ip=$( gcloud compute instances describe " ${CLUSTER_NAME} -controller" \
136
- --project " $GCP_PROJECT " --zone " $GCP_ZONE " \
137
- --format=' get(networkInterfaces[0].accessConfigs[0].natIP)' ) ; do
139
+ --project " $GCP_PROJECT " --zone " $GCP_ZONE " \
140
+ --format=' get(networkInterfaces[0].accessConfigs[0].natIP)' ) ; do
138
141
echo " Waiting for a public IP"
139
142
sleep 5
140
143
done
0 commit comments