forked from gcloudrig/gcloudrig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobals.sh
executable file
·850 lines (710 loc) · 24.9 KB
/
globals.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
#!/usr/bin/env bash
# what gpu and how many? see https://cloud.google.com/compute/docs/gpus
ACCELERATORTYPE="nvidia-tesla-p4-vws"
ACCELERATORCOUNT="1"
# instance and boot disk type?
INSTANCETYPE="n1-standard-8"
BOOTTYPE="pd-ssd"
# base image?
IMAGEBASEFAMILY="windows-2019"
IMAGEBASEPROJECT="windows-cloud"
# various resource and label names
GCRLABEL="gcloudrig" # also set in gcloudrig-startup.ps1
GAMESDISK="gcloudrig-games" # also set in gcloudrig-startup.ps1
IMAGEFAMILY="gcloudrig"
INSTANCEGROUP="gcloudrig-group"
INSTANCENAME="gcloudrig"
SETUPTEMPLATE="gcloudrig-setup-template"
CONFIGURATION="gcloudrig"
WINDOWSUSER="gcloudrig"
# other globals; overrides may be ignored
REGION=""
PROJECT_ID=""
ZONES=""
GCSBUCKET=""
declare -A SETUPOPTIONS
SETUPOPTIONS[ZeroTierNetwork]=""
SETUPOPTIONS[VideoMode]="1920x1080"
SETUPOPTIONS[DisplayScaling]=""
SETUPOPTIONS[InstallSteam]="false"
SETUPOPTIONS[InstallBattlenet]="false"
SETUPOPTIONS[InstallSSH]="false"
########
# INIT #
########
function init_gcloudrig {
DIR="$( cd "$( dirname -- "${BASH_SOURCE[0]}" )" >/dev/null && pwd)"
if [ -z "$PROJECT_ID" ]; then
PROJECT_ID="$(gcloud config get-value core/project --quiet 2> /dev/null)"
fi
if [ -z "$REGION" ]; then
REGION="$(gcloud config get-value compute/region --quiet 2> /dev/null)"
# Cloud Shell doesn't persist configurations, so look at project_ID metadata instead
if [ -z "$REGION" ]; then
REGION="$(gcloud compute project-info describe --project=$PROJECT_ID --format 'value(commonInstanceMetadata.items[google-compute-default-region])' --quiet 2> /dev/null)"
fi
fi
# if we still don't have a project id or region, bail and ask user to run setup
if [ -z "$PROJECT_ID" ] || [ -z "$REGION" ]; then
[ -z "$PROJECT_ID" ] && echo "Missing config 'core/project'" >&2
[ -z "$REGION" ] && echo "Missing config 'compute/region'" >&2
echo "Please run './setup.sh'" >&2
exit 1
fi
init_common
}
function init_setup {
DIR="$( cd "$( dirname -- "${BASH_SOURCE[0]}" )" >/dev/null && pwd)"
if [ -n "$REGION" -a -n "$PROJECT_ID" ] ; then
# using settings at the top of this file
enable_required_gcloud_apis
else
# use gcloud config configurations
gcloudrig_config_setup
fi
# not all accounts seem to have a GPUS_ALL_REGIONS quota
# but if they do it must be manually increased
gcloudrig_check_quota_gpus_all_regions
# create a GCS bucket to store Powershell module
gcloudrig_create_gcs_bucket
# now set the zones
init_common;
}
function init_common {
GCSBUCKET="gs://$PROJECT_ID"
local groupsize=0
gcloudrig_update_powershell_module
# get a comma separated list of zones with accelerators in the current region
ZONES="$(gcloudrig_get_accelerator_zones "$REGION")"
ZONES="${ZONES//[[:space:]]/,}"
if [ -z "$ZONES" ] ; then
gcloud config unset compute/zone --quiet
gcloud config unset compute/region --quiet
echo >&2
echo "#################################################################" >&2
echo "ERROR: There are no zones in $REGION with accelerator type \"$ACCELERATORTYPE\"" >&2
echo "Re-run ./setup.sh and choose a region from this list:" >&2
echo "$(gcloudrig_get_accelerator_zones)" >&2
exit 1
fi
# get the number of instances currently running
groupsize=$(gcloud compute instance-groups list --filter "name=$INSTANCEGROUP region:($REGION)" --format "value(size)" --quiet || echo "0")
# if an instance is running, expose some more vars
if ! [ -z "$groupsize" ] && [ "$groupsize" -gt "0" ]; then
INSTANCE="$(gcloudrig_get_instance_from_group "$INSTANCEGROUP")"
ZONE="$(gcloudrig_get_instance_zone_from_group "$INSTANCEGROUP")"
BOOTDISK="$(gcloudrig_get_bootdisk_from_instance "$ZONE" "$INSTANCE")"
gcloud config set compute/zone "$ZONE" --quiet;
fi
# the image
IMAGE=$(gcloudrig_get_bootimage)
if [ -z "$IMAGE" ]; then
IMAGE="$IMAGEFAMILY"
fi
}
####################
# Setup functions #
####################
function gcloudrig_config_setup {
# setup a gcloud config manually so we can make sure the user only chooses a
# region that has GPUs
# activate the default config, or create if not found
if [ -z "$(gcloud config configurations list --filter "name=($CONFIGURATION)" --format "value(name)" --quiet)" ]; then
# create and activate
gcloud config configurations create "$CONFIGURATION" --quiet
else
gcloud config configurations activate "$CONFIGURATION" --quiet
fi
# setup auth
if [ -z "$(gcloud config get-value account 2>/dev/null)" ] ; then
ACCOUNTS="$(gcloud auth list --format "value(account)")"
if [ -n "$ACCOUNTS" ] ; then
echo
echo "Select account to use:"
select acct in $ACCOUNTS "new account"; do
if [ -n "$acct" ] ; then
if [ "$acct" == "new account" ] ; then
gcloud auth login --no-launch-browser && break
else
gcloud config set account $acct && break
fi
fi
done
else
gcloud auth login --no-launch-browser
fi
fi
# check if default project_ID is set, if not select/create one
PROJECT_ID="$(gcloud config get-value project 2>/dev/null)"
if [ -z "$PROJECT_ID" ] ; then
declare -A PROJECTS
for line in $(gcloud projects list --format="csv[no-heading](name,project_id)") ; do
PROJECTS[${line%%,*}]=${line##*,}
done
if [ "${#PROJECTS[@]}" -eq 0 ] ; then
# no existing projects, create one
PROJECT_ID="gcloudrig-${RANDOM}${RANDOM}"
gcloud_projects_create "$PROJECT_ID"
else
echo
echo "Select project to use:"
select project in ${!PROJECTS[@]} "new project" ; do
if [ -n "$project" ] ; then
if [ "$project" == "new project" ] ; then
# user requested to use a new project
PROJECT_ID="gcloudrig-${RANDOM}${RANDOM}"
gcloud_projects_create "$PROJECT_ID"
else
PROJECT_ID="${PROJECTS[$project]}"
gcloud config set project "$PROJECT_ID"
fi
break
fi
done
fi
fi
# this is required before we can check for regions with GPUs
enable_required_gcloud_apis
# check default region is set in configuration, if not select one from regions with accelerators
REGION="$(gcloud config get-value compute/region 2>/dev/null)"
if [ -z "$REGION" ] ; then
gcloudrig_select_region
fi
}
function gcloudrig_select_software_options {
local installerOptions
local keys="$(echo ${!SETUPOPTIONS[@]} | fmt -1 | sort)"
while true ; do
installerOptions=""
for key in $keys ; do
installerOptions="$installerOptions $key=${SETUPOPTIONS[$key]}"
done
echo
select option in $installerOptions Done ; do
case "$option" in
ZeroTier*)
gcloudrig_select_zerotier_network
break
;;
VideoMode*)
gcloudrig_select_videomode
break
;;
DisplayScaling*)
gcloudrig_select_displayscaling
break
;;
Install*)
gcloudrig_select_software_install "$option"
break
;;
Done)
break 2
;;
esac
done
done
}
function gcloudrig_select_software_install {
local package="${1%%=*}"
local state="${1##*=}"
case "$state" in
true)
SETUPOPTIONS[$package]=false
;;
false)
SETUPOPTIONS[$package]=true
;;
esac
}
function gcloudrig_select_videomode {
local videoModes="960x720 1920x1080 2560x1440 other"
echo "Select a default videomode:"
select mode in $videoModes ; do
if [ -n "$mode" ] ; then
[ "$mode" == "other" ] && break
SETUPOPTIONS[VideoMode]="$mode"
break
fi
done
}
function gcloudrig_select_displayscaling {
local DPI_modes="96 144 other"
echo "Select DPI Display Scaling:"
select mode in $DPI_modes ; do
if [ -n "$mode" ] ; then
[ "$mode" == "other" ] && break
SETUPOPTIONS[DisplayScaling]="$mode"
break
fi
done
}
function gcloudrig_select_zerotier_network {
local prompt="Gcloudrig ZeroTier network id [or quit]: "
cat <<EOF
We strongly recommend you create a new ZeroTier network for Gcloudrig
https://my.zerotier.com/network
EOF
while read -e -p "$prompt" ; do
if echo "$REPLY" | grep -Eiq '^[0-9a-f]{16}$' ; then
SETUPOPTIONS[ZeroTierNetwork]="$REPLY"
return
fi
case "$REPLY" in
q|quit|exit|cancel)
break
;;
*)
echo "ZeroTier Network IDs are 16 hexadecimal numbers" >&2
esac
done
}
function gcloudrig_select_region {
local ACCELERATORREGIONS="$(gcloudrig_get_accelerator_zones | sed -ne 's/-[a-z]$//p' | sort -u)"
if [ -n "$ACCELERATORREGIONS" ] ; then
echo
echo "You can use http://gcping.com to find the closest region"
echo
echo "Select a region to use:"
select REGION in $ACCELERATORREGIONS ; do
[ -n "$REGION" ] && gcloud config set compute/region $REGION && gcloud compute project-info add-metadata \
--metadata google-compute-default-region=$REGION && break
done
else
echo >&2
echo "#################################################################" >&2
echo "ERROR: no regions with accelerator type \"$ACCELERATORTYPE\" found " >&2
exit 1
fi
}
function gcloud_projects_create {
local PROJECT_ID="$1"
gcloud projects create "$PROJECT_ID" --name gcloudrig --set-as-default
echo "You need to enable billing, then re-run setup.sh"
echo "https://console.developers.google.com/project/${PROJECT_ID}/settings"
exit 1
}
function enable_required_gcloud_apis {
# check if compute api is enabled, if not enable it
local COMPUTEAPI=$(gcloud services list --format "value(config.name)" --filter "config.name=compute.googleapis.com" --quiet)
if [ "$COMPUTEAPI" != "compute.googleapis.com" ]; then
echo "Enabling Compute API..."
gcloud services enable compute.googleapis.com
fi
# check if logging api is enabled, if not enable it
local LOGGINGAPI=$(gcloud services list --format "value(config.name)" --filter "config.name=logging.googleapis.com" --quiet)
if [ "$LOGGINGAPI" != "logging.googleapis.com" ]; then
echo "Enabling Logging API..."
gcloud services enable logging.googleapis.com
fi
}
####################
# GETTERS (stdout) #
####################
# Get instance name from instance group
function gcloudrig_get_instance_from_group {
local instance_group="$1"
gcloud compute instance-groups list-instances "$instance_group" \
--format "value(instance)" \
--region "$REGION" \
--quiet
}
# Get instance zone from instance group
function gcloudrig_get_instance_zone_from_group {
local instance_group="$1"
gcloud compute instance-groups list-instances "$instance_group" \
--format "value(instance.scope().segment(0))" \
--region "$REGION" \
--quiet
}
# Get bootdisk from instance
function gcloudrig_get_bootdisk_from_instance {
local zone="$1"
local instance="$2"
gcloud compute instances describe "$instance" \
--zone "$zone" \
--format "value(disks[0].source.basename())" \
--quiet
}
# Get boot image
function gcloudrig_get_bootimage {
gcloud compute images list \
--format "value(name)" \
--filter "labels.gcloudrig=true labels.latest=true"
}
# Get zones with accelerators in region $1
# if $1 is not specified, all regions
function gcloudrig_get_accelerator_zones {
local region="${1:-*}"
gcloud compute accelerator-types list \
--filter "zone:$region AND name=$ACCELERATORTYPE" \
--format "value(zone)"
}
##################
# INSTANCE GROUP #
##################
function gcloudrig_create_instance_template {
local templateName="$1" # required
local imageFlags
local bootImage=$(gcloudrig_get_bootimage)
# if the templateName is SETUPTEMPLATE or we still don't have a custom boot image, assume we're in setup
if [ "$templateName" == "$SETUPTEMPLATE" ] || [ -z "$bootImage" ]; then
imageFlags="--image-family $IMAGEBASEFAMILY --image-project $IMAGEBASEPROJECT"
else
imageFlags="--image $(gcloudrig_get_bootimage)"
fi
echo "Creating instance template '$templateName'..."
gcloud compute instance-templates create "$templateName" \
--accelerator "type=$ACCELERATORTYPE,count=$ACCELERATORCOUNT" \
--boot-disk-type "$BOOTTYPE" \
$imageFlags \
--labels "$GCRLABEL=true" \
--machine-type "$INSTANCETYPE" \
--maintenance-policy "TERMINATE" \
--scopes "default,compute-rw" \
--no-boot-disk-auto-delete \
--no-restart-on-failure \
--format "value(name)" \
--preemptible \
--metadata serial-port-logging-enable=true \
--metadata-from-file windows-startup-script-ps1=<(cat "$DIR/gcloudrig-boot.ps1") \
--quiet || echo
}
# creates regional managed instance group and gives it the base instance template
function gcloudrig_create_instance_group {
local templateName
local bootImage="$(gcloudrig_get_bootimage)"
# if we don't have a custom boot image, assume we're in setup
if [ -z "$bootImage" ] ; then
templateName="$SETUPTEMPLATE"
else
templateName="gcloudrig-template-$(date +"%Y%m%d%H%M%S")"
fi
gcloudrig_create_instance_template "$templateName"
echo "Creating managed instance group '$INSTANCEGROUP'..."
gcloud compute instance-groups managed create "$INSTANCEGROUP" \
--base-instance-name "$INSTANCENAME" \
--region "$REGION" \
--size "0" \
--template "$templateName" \
--zones "$ZONES" \
--format "value(name)" \
--quiet
}
# updates existing instance group with a new template that uses the latest image
function gcloudrig_update_instance_group {
# new template's name
local newtemplate="gcloudrig-template-$(date +"%Y%m%d%H%M%S")"
# create new template
gcloudrig_create_instance_template "$newtemplate"
# update instance group with the new template
gcloud compute instance-groups managed set-instance-template "$INSTANCEGROUP" --region "$REGION" --template "$newtemplate" --format "value(name)" --quiet
# tidy up - delete all other templates
local templates=()
mapfile -t templates < <(gcloud compute instance-templates list \
--format "value(name)" \
--filter "properties.labels.gcloudrig=true")
for template in "${templates[@]}"; do
if ! [ "$newtemplate" == "$template" ]; then
gcloud compute instance-templates delete "$template" --format "value(name)" --quiet || echo -n
fi
done
}
# deletes existing instance group and all templates
function gcloudrig_delete_instance_group {
if [ -n "$(gcloud compute instance-groups list --filter "name=$INSTANCEGROUP region:($REGION)" --format "value(name)" --quiet)" ]; then
gcloud compute instance-groups managed delete "$INSTANCEGROUP" \
--region "$REGION" \
--quiet
fi
# tidy up - delete all other templates
local templates=()
mapfile -t templates < <(gcloud compute instance-templates list \
--format "value(name)" \
--filter "properties.labels.gcloudrig=true")
for template in "${templates[@]}"; do
gcloud compute instance-templates delete "$template" --quiet || echo -n
done
}
##################
# QUOTA COMMANDS #
##################
function gcloudrig_get_project_quota_limits {
declare -gA QUOTAS
for line in $(gcloud compute project-info describe \
--project="$PROJECT_ID" \
--flatten="quotas[]" \
--format="csv[no-heading](quotas.metric,quotas.limit)") ; do
QUOTAS[${line%%,*}]="${line##*,}"
done
}
function gcloudrig_check_quota_gpus_all_regions {
if [ ! -v QUOTAS ] ; then
gcloudrig_get_project_quota_limits
fi
# if key exists in array
if [ -v "QUOTAS[GPUS_ALL_REGIONS]" ] ; then
# gcloud --format option sometimes outputs nothing if the value is 0.0
if [ -z "${QUOTAS[GPUS_ALL_REGIONS]}" -o "${QUOTAS[GPUS_ALL_REGIONS]}" == "0.0" ] ; then
cat <<EOF >&2
=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=
You have to manually request a quota increase for GPUS_ALL_REGIONS
https://console.cloud.google.com/iam-admin/quotas?project=${PROJECT_ID}&metric=GPUs%20(all%20regions)
See this page for more info on requesting quota:
https://cloud.google.com/compute/quotas#requesting_additional_quota
=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=
EOF
exit 1
else
echo "GPUS_ALL_REGIONS quota is ${QUOTAS["GPUS_ALL_REGIONS"]}"
fi
else
echo "no GPUS_ALL_REGIONS quota, good to go."
fi
}
##################
# OTHER COMMANDS #
##################
function gcloudrig_enable_software_setup {
# set project level metadata that gcloudrig-boot.ps1 will check to start a
# software install
echo "Enabling Gcloudrig software installer..."
gcloud compute project-info add-metadata --quiet \
--metadata "gcloudrig-setup-state=new" \
--metadata-from-file gcloudrig-setup-options=<(gcloudrig_setup_options_to_json)
}
function gcloudrig_setup_options_to_json {
# all this to avoid asking ppl to install jq...
local jsonTemplate='{%s}'
local optionTemplate='"%s":"%s"'
local booleanTemplate='"%s":%s'
local optionString=""
local key value
if [ "${#SETUPOPTIONS[@]}" -eq 0 ] ; then
return
fi
for key in "${!SETUPOPTIONS[@]}" ; do
value="${SETUPOPTIONS[$key]}"
value="${value//\'}" # nuke single quotes
value="${value//\"}" # nuke double quotes
if [ "$value" == "true" -o "$value" == "false" ] ; then
optionString="$(printf "$booleanTemplate" "$key" "$value"),$optionString"
else
optionString="$(printf "$optionTemplate" "$key" "$value"),$optionString"
fi
done
# trim any trailing ','
optionString="${optionString%,}"
printf "$jsonTemplate" "$optionString"
}
# TODO store the password somewhere safer
function gcloudrig_get_password_from_logs {
gcloud logging read "logName=projects/$PROJECT_ID/logs/gcloudrig-install AND textPayload:password" --format="value(textPayload)" --limit=1
}
# create GCS bucket, don't fail if it already exists
function gcloudrig_create_gcs_bucket {
local err result
GCSBUCKET="${GCSBUCKET:-gs://$PROJECT_ID}"
set +e
result="$(gsutil -q mb -p "$PROJECT_ID" -c regional \
-l "$REGION" "$GCSBUCKET/" 2>&1)"
err="$?"
if [ "$err" -gt 0 ] ; then
# catch errors, ignore "already exists"
if ! echo "$result" | grep -q "already exists" ; then
echo "$result" >&2
return "$err"
fi
fi
set -e
# announce script's gcs url via project metadata
gcloud compute project-info add-metadata --metadata "gcloudrig-setup-script-gcs-url=$GCSBUCKET/gcloudrig.psm1" --quiet
}
function gcloudrig_update_powershell_module {
# TODO only update if newer
local quiet=""
[ -n "$GCLOUDRIG_DEBUG" ] && quiet="-q"
gsutil $quiet cp "$DIR/gcloudrig.psm1" "$GCSBUCKET/"
}
function wait_until_instance_group_is_stable {
set +e
timeout 300s gcloud compute instance-groups managed wait-until --stable "$INSTANCEGROUP" \
--region "$REGION" \
--quiet
err=$?
set -e
if [ "$err" -gt "0" ]; then
gcloud logging read 'severity>=WARNING' \
--freshness 10m \
--format "table[box,title='-- Recent logs (10m) --'](timestamp,protoPayload.status.message)"
return $err
fi
}
# scale to 1 and wait, with retries every 5 minutes
function gcloudrig_start {
echo "Starting gcloudrig..."
# scale to 1
gcloud compute instance-groups managed resize "$INSTANCEGROUP" \
--size "1" \
--format "value(currentActions)" \
--region "$REGION" \
--quiet &>/dev/null
# if it doesn't start in 5 minutes
while ! wait_until_instance_group_is_stable; do
# scale it back down
gcloud compute instance-groups managed resize "$INSTANCEGROUP" \
--size "0" \
--format "value(currentActions)" \
--region "$REGION" \
--quiet &>/dev/null
# wait
wait_until_instance_group_is_stable
# ensure it's using the latest template/image
gcloudrig_update_instance_group
# and scale it back up again (chance of being spawned in a different zone)
gcloud compute instance-groups managed resize "$INSTANCEGROUP" \
--size "1" \
--format "value(currentActions)" \
--region "$REGION" \
--quiet &>/dev/null
done
# we have an instance!
INSTANCE="$(gcloudrig_get_instance_from_group "$INSTANCEGROUP")"
ZONE="$(gcloudrig_get_instance_zone_from_group "$INSTANCEGROUP")"
BOOTDISK="$(gcloudrig_get_bootdisk_from_instance "$ZONE" "$INSTANCE")"
echo "To watch boot/setup progress, visit https://console.cloud.google.com/logs/viewer?project=$PROJECT_ID&advancedFilter=logName:%22projects%2F$PROJECT_ID%2Flogs%22"
}
# scale to 0 and wait
function gcloudrig_stop {
echo "Stopping gcloudrig..."
gcloud compute instance-groups managed resize "$INSTANCEGROUP" \
--size "0" \
--format "value(currentActions)" \
--region "$REGION" \
--quiet &>/dev/null
wait_until_instance_group_is_stable
}
# turn boot disk into an image; delete all but latest on success
function gcloudrig_boot_disk_to_image {
local images=()
local newimage=""
echo "Creating boot image, this may take some time..."
# save boot image, but don't label it yet
newimage="$BOOTDISK-$(date +"%Y%m%d%H%M%S")"
gcloud compute images create "$newimage" \
--source-disk "$BOOTDISK" \
--source-disk-zone "$ZONE" \
--guest-os-features "WINDOWS" \
--family "$IMAGEFAMILY" \
--storage-location "$REGION" \
--labels "$GCRLABEL=true" \
--quiet
# find existing images
mapfile -t images < <(gcloud compute images list \
--format "value(name)" \
--filter "labels.$GCRLABEL=true")
# remove the "latest=true" label from all old images
for image in "${images[@]}"; do
LATEST="$(gcloud compute images describe "$image" \
--format "value(labels.latest)")"
if [ "$LATEST" = "true" ]; then
gcloud compute images remove-labels "$image" \
--labels "latest"
fi
done
# add labels to the latest image
gcloud compute images add-labels "$newimage" \
--labels "latest=true,$GCRLABEL=true"
# update the instance group (in the background)
gcloudrig_update_instance_group &
# delete boot disk
gcloud compute disks delete "$BOOTDISK" \
--zone "$ZONE" \
--quiet
# get all the images again, *except* the one labelled latest
mapfile -t images < <(gcloud compute images list \
--format "value(name)" \
--filter "labels.$GCRLABEL=true NOT labels.latest=true")
# delete them
for image in "${images[@]}"; do
gcloud compute images delete "$image" --quiet &
done
# wait for things to finish
wait
}
# turn games disk into a snapshot; delete all but latest on success
function gcloudrig_games_disk_to_snapshot {
local snapshots=()
local newsnapshot=""
echo "Snapshotting games disk..."
# save games snapshot, but don't label it yet
newsnapshot="$GAMESDISK-$(date +"%Y%m%d%H%M%S")"
gcloud compute disks snapshot "$GAMESDISK" \
--snapshot-names "$newsnapshot" \
--zone "$ZONE" \
--storage-location "$REGION" \
--guest-flush \
--quiet &>/dev/null
# find existing snapshots
mapfile -t snapshots < <(gcloud compute snapshots list \
--format "value(name)" \
--filter "labels.$GCRLABEL=true")
# remove the "latest=true" label from all existing gcloudrig snapshots
for snapshot in "${snapshots[@]}"; do
LATEST="$(gcloud compute snapshots describe "$snapshot" \
--format "value(labels.latest)")"
if [ "$LATEST" = "true" ]; then
gcloud compute snapshots remove-labels "$snapshot" \
--labels "latest"
fi
done
# add labels to the latest snapshot
gcloud compute snapshots add-labels "$newsnapshot" \
--labels "latest=true,$GCRLABEL=true"
# delete games disk
gcloud compute disks delete "$GAMESDISK" \
--zone "$ZONE" \
--quiet
# get all the snapshots again, *except* the one labelled latest
mapfile -t snapshots < <(gcloud compute snapshots list \
--format "value(name)" \
--filter "labels.$GCRLABEL=true NOT labels.latest=true")
# delete them
for snapshot in "${snapshots[@]}"; do
gcloud compute snapshots delete "$snapshot" --quiet
done
}
# mounts games disk, restoring from latest snapshot or creating a new one if nessessary
# no size/disk type specifified; gcloud will default to 500GB pd-standard when creating
function gcloudrig_mount_games_disk {
local snapshot
local existingdisk
echo "Restoring/creating games disk..."
# get latest games snapshot
snapshot="$(gcloud compute snapshots list \
--format "value(name)" \
--filter "labels.gcloudrig=true labels.latest=true")"
existingdisk="$(gcloud compute disks list \
--filter "name=$GAMESDISK zone:($ZONE)" \
--format "value(name)")"
# create a blank games disk
if [ -z "$snapshot" ] && [ -z "$existingdisk" ]; then
gcloud compute disks create "$GAMESDISK" \
--zone "$ZONE" \
--quiet \
--labels "$GCRLABEL=true"
# or restore it from the latest snapshot
elif [ -z "$existingdisk" ]; then
gcloud compute disks create "$GAMESDISK" \
--zone "$ZONE" \
--source-snapshot "$snapshot" \
--quiet \
--labels "$GCRLABEL=true"
fi
# disk exists, attach it
echo "Mounting games disk..."
gcloud compute instances attach-disk "$INSTANCE" \
--disk "$GAMESDISK" \
--zone "$ZONE" \
--quiet &>/dev/null
}