Skip to content

Instantly share code, notes, and snippets.

@nafg
Last active July 5, 2018 23:49
Show Gist options
  • Save nafg/d10e3c6372f6d8039e0c5a01204d874e to your computer and use it in GitHub Desktop.
Save nafg/d10e3c6372f6d8039e0c5a01204d874e to your computer and use it in GitHub Desktop.
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
@nafg
Copy link
Author

nafg commented Jul 5, 2018

Usage: edit_md.fish <instance-name> <metadata-key>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment