Skip to content

Instantly share code, notes, and snippets.

@takeru
Created October 14, 2023 01:21
Show Gist options
  • Save takeru/505d039dabaa02d78470f6eca51afdfc to your computer and use it in GitHub Desktop.
Save takeru/505d039dabaa02d78470f6eca51afdfc to your computer and use it in GitHub Desktop.
GCEインスタンスのCPUとGPUを変更する.sh
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 "********************************"