-
Notifications
You must be signed in to change notification settings - Fork 6.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding metadata sample #270
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Compute Engine Metadata Samples | ||
|
||
These samples demonstrate interacting with the Compute Engine metadata service. These samples must be run on Compute Engine. | ||
|
||
<!-- auto-doc-link --> | ||
<!-- end-auto-doc-link --> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#!/usr/bin/env python | ||
|
||
# Copyright 2016 Google Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Example of using the Compute Engine API to watch for maintenance notices. | ||
|
||
For more information, see the README.md under /compute. | ||
""" | ||
|
||
# [START all] | ||
|
||
import time | ||
|
||
import requests | ||
|
||
|
||
METADATA_URL = "http://metadata.google.internal/computeMetadata/v1/" | ||
METADATA_HEADERS = {'Metadata-Flavor': 'Google'} | ||
|
||
|
||
def wait_for_maintenance(callback): | ||
url = METADATA_URL + 'instance/maintenance-event' | ||
last_in_maintenance = False | ||
# [START hanging_get] | ||
last_etag = 0 | ||
|
||
while True: | ||
r = requests.get( | ||
url, | ||
params={'last_etag': last_etag}, | ||
headers=METADATA_HEADERS) | ||
|
||
# During maintenance the service can return a 503, so these should | ||
# be retried. | ||
if r.status_code == 503: | ||
time.sleep(1) | ||
continue | ||
# [END hanging_get] | ||
|
||
last_etag = r.headers['etag'] | ||
|
||
if r.text == 'MIGRATE_ON_HOST_MAINTENANCE': | ||
in_maintenance = True | ||
else: | ||
in_maintenance = False | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in_maintenance = (r.text == 'MIGRATE_ON_HOST_MAINTENANCE') There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Meh, too clever. This a bit more clear. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well I disagree, it's not nested in any way, it's not clever as much as what I consider to be standard Pythonic syntax. BUT /bikeshed, don't want to block code purple work etc so whatever. |
||
|
||
if in_maintenance != last_in_maintenance: | ||
last_in_maintenance = in_maintenance | ||
callback(in_maintenance) | ||
|
||
|
||
def maintenance_callback(status): | ||
if status: | ||
print('Undergoing host maintenance') | ||
else: | ||
print('Finished host maintenance') | ||
|
||
|
||
def main(): | ||
wait_for_maintenance(maintenance_callback) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() | ||
# [END all] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Copyright 2016, Google, Inc. | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import main | ||
import mock | ||
|
||
|
||
@mock.patch('main.requests') | ||
def test_wait_for_maintenance(requests_mock): | ||
# Response 1 is a host maintenance event. | ||
response1_mock = mock.Mock() | ||
response1_mock.status_code = 200 | ||
response1_mock.text = 'MIGRATE_ON_HOST_MAINTENANCE' | ||
response1_mock.headers = {'etag': 1} | ||
# Response 2 is the end of the event. | ||
response2_mock = mock.Mock() | ||
response2_mock.status_code = 200 | ||
response2_mock.text = 'NONE' | ||
response2_mock.headers = {'etag': 2} | ||
# Response 3 is a 503 | ||
response3_mock = mock.Mock() | ||
response3_mock.status_code = 503 | ||
|
||
requests_mock.get.side_effect = [ | ||
response1_mock, response2_mock, response3_mock, response2_mock, | ||
StopIteration()] | ||
|
||
callback_mock = mock.Mock() | ||
|
||
try: | ||
main.wait_for_maintenance(callback_mock) | ||
except StopIteration: | ||
pass | ||
|
||
assert callback_mock.call_count == 2 | ||
assert callback_mock.call_args_list[0][0] == (True,) | ||
assert callback_mock.call_args_list[1][0] == (False,) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
requests==2.9.1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably missing something...looks like this code should terminate when status is false but it doesn't look like logic exists to do so?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It never terminates. It constantly watches for migration events and calls the callback. The callback can decide to terminate or whatever.