Last active
July 5, 2018 23:49
-
-
Save nafg/d10e3c6372f6d8039e0c5a01204d874e to your computer and use it in GitHub Desktop.
Script to edit google compute engine instance metadata entry as a file
This file contains hidden or 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
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
edit_md.fish <instance-name> <metadata-key>