Created
October 14, 2023 01:21
-
-
Save takeru/505d039dabaa02d78470f6eca51afdfc to your computer and use it in GitHub Desktop.
GCEインスタンスのCPUとGPUを変更する.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set -xe | |
# INSTANCE=xxxxx | |
# ZONE=us-central1-a | |
# PROJECT=yyyyy | |
# #ACCELERATORTYPE="" | |
# ACCELERATORTYPE="nvidia-tesla-t4" | |
# #ACCELERATORTYPE="nvidia-tesla-v100" | |
# MACHINETYPE=n1-standard-2 | |
SCRIPT_DIR=$(cd $(dirname $0); pwd) | |
LOGDIR=$SCRIPT_DIR/update_gce_instance_logs | |
#LOGDIR=$TMPDIR/update_gce_instance_logs | |
mkdir -p $LOGDIR | |
YQ=$SCRIPT_DIR/.venv/bin/yq | |
TS=`LANG=C date +%Y%m%d_%H%M%S` | |
YAMLPATH_ORIG=$LOGDIR/${TS}_${INSTANCE}.orig.yaml | |
YAMLPATH_EDIT=$LOGDIR/${TS}_${INSTANCE}.edit.yaml | |
YAMLPATH_AFTR=$LOGDIR/${TS}_${INSTANCE}.aftr.yaml | |
gcloud --project=$PROJECT compute instances export --zone=$ZONE $INSTANCE > $YAMLPATH_ORIG | |
if [[ "$ACCELERATORTYPE" != "" ]] ; then | |
if [[ "$ACCELERATORTYPE" == "t4" ]] || [[ "$ACCELERATORTYPE" == "v100" ]] ; then | |
ACCELERATORTYPE="nvidia-tesla-$ACCELERATORTYPE" | |
fi | |
guestAccelerator="{\"acceleratorCount\":1,\"acceleratorType\":\"https://www.googleapis.com/compute/v1/projects/$PROJECT/zones/$ZONE/acceleratorTypes/$ACCELERATORTYPE\"}" | |
else | |
guestAccelerator="" | |
fi | |
machineType="https://www.googleapis.com/compute/v1/projects/$PROJECT/zones/$ZONE/machineTypes/$MACHINETYPE" | |
$YQ --yaml-output --indentless-lists ".guestAccelerators=[$guestAccelerator]|.machineType=\"$machineType\"" $YAMLPATH_ORIG > $YAMLPATH_EDIT | |
gcloud --project=$PROJECT compute instances update-from-file --source=$YAMLPATH_EDIT --zone=$ZONE $INSTANCE | |
gcloud --project=$PROJECT compute instances export --zone=$ZONE $INSTANCE > $YAMLPATH_AFTR | |
set +x | |
echo "[diff $YAMLPATH_ORIG $YAMLPATH_AFTR]" | |
echo "********************************" | |
diff $YAMLPATH_ORIG $YAMLPATH_AFTR | |
echo "********************************" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://cloud.google.com/sdk/gcloud/reference/compute/instances/export
https://cloud.google.com/sdk/gcloud/reference/compute/instances/update-from-file
https://cloud.google.com/compute/docs/instances/changing-machine-type-of-stopped-instance?hl=ja#changing_machine_type