Last Updated: July 12, 2018
·
168
· nafg

Script to edit google compute engine instance metadata entry as a file

#!/usr/bin/fish

set instance $argv[1]
set key $argv[2]

set tmpdir (mktemp -d)
set file $tmpdir/(string replace _ . $key)

gcloud compute instances describe $instance --format="value(metadata.$key)" > $file

set hash (md5sum $file)

echo $hash

eval $EDITOR $file

if test -s $file
  set hash2 (md5sum $file)
  echo $hash2
  if test "$hash2" != "$hash"
    echo File changed, updating
    gcloud compute instances add-metadata $instance --metadata-from-file=$key=$file
  else
    echo File not changed, not updating
  end
 else
   echo File is empty, not updating
end

rm -rf $tmpdir