summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeremy Edwards <jeremyedwards@google.com>2014-06-02 13:07:54 -0700
committerJeremy Edwards <jeremyedwards@google.com>2014-06-02 13:07:54 -0700
commit4e26e83d9d6b98ce122e03c301c42bfc9f98ae02 (patch)
tree8f35f757ed9a922c5661bf927c6fb12bf7cc9cc2
parentccdbd96708a03a73f1aac16f00b8c319ec9e98b5 (diff)
downloadgoogle-compute-image-packages-4e26e83d9d6b98ce122e03c301c42bfc9f98ae02.tar.gz
Update get_metadata_value to use v1 metadata server url for instance with fallback to project.
-rwxr-xr-xgoogle-startup-scripts/usr/share/google/get_metadata_value45
1 files changed, 36 insertions, 9 deletions
diff --git a/google-startup-scripts/usr/share/google/get_metadata_value b/google-startup-scripts/usr/share/google/get_metadata_value
index 005b44d..4df6186 100755
--- a/google-startup-scripts/usr/share/google/get_metadata_value
+++ b/google-startup-scripts/usr/share/google/get_metadata_value
@@ -16,20 +16,47 @@
# Get a metadata value from the metadata server.
declare -r VARNAME=$1
-declare -r MDS=http://169.254.169.254/computeMetadata/v1/instance/attributes
+declare -r MDS_PREFIX=http://169.254.169.254/computeMetadata/v1
+declare -r MDS_INSTANCE=${MDS_PREFIX}/instance/attributes
+declare -r MDS_PROJECT=${MDS_PREFIX}/project/attributes
declare -r MDS_TRIES=${MDS_TRIES:-100}
+function metadata_get_url_code() {
+ return $(curl "${1}" -H "Metadata-Flavor: Google" -s \
+ -w "%{http_code}" -o /dev/null)
+}
+
+function print_metadata_value() {
+ local readonly response=$(curl "${1}" -H "Metadata-Flavor: Google" -s)
+ local readonly return_code=$?
+ # If the command completed successfully, print the metadata value to stdout.
+ if [[ ${return_code} == 0 ]]; then
+ printf ${response}
+ fi
+ return ${return_code}
+}
+
+function print_metadata_value_if_exists() {
+ local readonly url=$1
+ metadata_get_url_code ${url}
+ http_code=$?
+ # Test the instance metadata value.
+ if test ${http_code} -eq 200; then
+ print_metadata_value ${url}
+ return_code=$?
+ fi
+}
+
function get_metadata_value() {
local readonly varname=$1
- local readonly tmpfile=$(mktemp)
- curl -f ${MDS}/${varname} -H "Metadata-Flavor: Google" > ${tmpfile} 2>/dev/null
- local return_code=$?
- if [[ ${return_code} == 0 ]]; then
- cat ${tmpfile}
- else
- echo "curl for ${varname} returned ${return_code}" > /dev/console
+ # Print the instance metadata value.
+ print_metadata_value_if_exists ${MDS_INSTANCE}/${varname}
+ return_code=$?
+ # If the instance doesn't have the value, try the project.
+ if [[ ${return_code} != 0 ]]; then
+ print_metadata_value_if_exists ${MDS_INSTANCE}/${varname}
+ return_code=$?
fi
- rm -f ${tmpfile}
return ${return_code}
}