This repository was archived by the owner on Sep 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 247
Support datalab connect() to connect a GCE instance created from Deep… #2112
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
support datalab connect() to connect a GCE instance created from Deep…
…learning images
- Loading branch information
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,7 +68,8 @@ class InvalidInstanceException(Exception): | |
|
|
||
| _MESSAGE = ( | ||
| 'The specified instance, {}, does not appear ' | ||
| 'to have been created by the `datalab` tool, and ' | ||
| 'to have been created by the `datalab` tool, or ' | ||
| 'from any GCE Deeplearning images, and ' | ||
| 'so cannot be managed by it.') | ||
|
|
||
| def __init__(self, instance_name): | ||
|
|
@@ -240,22 +241,35 @@ def flatten_metadata(metadata): | |
| return result | ||
|
|
||
|
|
||
| def _check_datalab_tag(instance, tags): | ||
| def _check_datalab_tag(instance, status_tags_and_metadata): | ||
|
||
| """Check that the given "tags" object contains `datalab`. | ||
|
|
||
| This is used to verify that a VM was created by the `datalab create` | ||
| command. | ||
| command or was from GCE Deeplearning images, by checking if the VM | ||
| description contains a tag of 'datalab' or includes a c2d-tensorflow | ||
| licence string. | ||
|
|
||
| Args: | ||
| instance: The name of the instance to check | ||
| tags: An object with an 'items' field that is a list of tags. | ||
| status_tags_and_metadata: An object containing the result of GCE | ||
| VM instance description. | ||
| Raises: | ||
| InvalidInstanceException: If the check fails. | ||
| """ | ||
| tags = status_tags_and_metadata.get('tags', {}) | ||
| items = tags.get('items', []) | ||
| if 'datalab' not in items: | ||
| raise InvalidInstanceException(instance) | ||
| return | ||
|
|
||
| if 'datalab' in items: | ||
| return | ||
| else: | ||
| _license = ('https://www.googleapis.com/compute/v1/projects/' | ||
| 'click-to-deploy-images/global/licenses/c2d-tensorflow') | ||
| disks = status_tags_and_metadata.get('disks', []) | ||
| for disk in disks: | ||
| if _license in disk.get('licenses', []): | ||
| return | ||
|
|
||
| raise InvalidInstanceException(instance) | ||
|
|
||
|
|
||
| def describe_instance(args, gcloud_compute, instance): | ||
|
|
@@ -282,16 +296,15 @@ def describe_instance(args, gcloud_compute, instance): | |
| if args.zone: | ||
| get_cmd.extend(['--zone', args.zone]) | ||
| get_cmd.extend( | ||
| ['--format', 'json(status,tags.items,metadata.items)', instance]) | ||
| ['--format', 'json(status,tags.items,metadata.items,disks[].licenses)', instance]) | ||
| with tempfile.TemporaryFile() as stdout, \ | ||
| tempfile.TemporaryFile() as stderr: | ||
| try: | ||
| gcloud_compute(args, get_cmd, stdout=stdout, stderr=stderr) | ||
| stdout.seek(0) | ||
| json_result = stdout.read().decode('utf-8').strip() | ||
| status_tags_and_metadata = json.loads(json_result) | ||
| tags = status_tags_and_metadata.get('tags', {}) | ||
| _check_datalab_tag(instance, tags) | ||
| _check_datalab_tag(instance, status_tags_and_metadata) | ||
|
|
||
| status = status_tags_and_metadata.get('status', 'UNKNOWN') | ||
| metadata = status_tags_and_metadata.get('metadata', {}) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Do we need the "and"?
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.
I rewrote it.