Created
May 28, 2021 06:51
-
-
Save akkornel/1940186251faa5e8a765b3fd2bea4b8d to your computer and use it in GitHub Desktop.
Given a GCSv5 (Globus Connect Server version 5) endpoint UUID, look up the IP addresses of its DTNs.
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
#!/bin/env python3 | |
# -*- coding: utf-8 -*- | |
# vim: ts=4 sw=4 et | |
# Given a GCSv5 endpoint UUID, look up the IP addresses of its DTNs. | |
# WARNING: Although this is more than bare-bones code, in that it | |
# does have a fair amount of error checking, you should not consider | |
# it production-ready. In particular because it relies GCSv5 | |
# endpoint APIs that aren't documented. | |
# © 2021 A. Karl Kornel | |
# SET THESE: | |
# Enter a Globus Auth client ID & secret. | |
# Register a new Globus App. Do not make a native app. | |
# Use rediect URL "http://localhost" as a placeholder. | |
# All other fields should be left blank. | |
client_id='...' | |
client_secret='...' | |
import globus_sdk | |
import sys | |
import uuid | |
# Get endpoint UUID | |
endpoint_uuid=str(uuid.UUID( | |
hex=input('Enter endpoint UUID: ') | |
)) | |
# Do auth. This will fail if it's not GCSv5. | |
endpoint_scope='urn:globus:auth:scope:{}:manage_collections'.format(endpoint_uuid) | |
transfer_scope='urn:globus:auth:scope:transfer.api.globus.org:all' | |
auth_client=globus_sdk.ConfidentialAppAuthClient( | |
client_id=client_id, | |
client_secret=client_secret, | |
) | |
try: | |
tokens=auth_client.oauth2_client_credentials_tokens( | |
requested_scopes=[ | |
transfer_scope, | |
endpoint_scope, | |
] | |
) | |
except globus_sdk.exc.AuthAPIError as e: | |
if e.code == 'UNKNOWN_SCOPE_ERROR': | |
print('Endpoint is not a GCSv5 endpoint.') | |
sys.exit(1) | |
else: | |
raise e | |
endpoint_token=tokens.by_scopes[endpoint_scope]['access_token'] | |
transfer_token=tokens.by_scopes['urn:globus:auth:scope:transfer.api.globus.org:all']['access_token'] | |
transfer_authorizer=globus_sdk.AccessTokenAuthorizer(transfer_token) | |
endpoint_authorizer=globus_sdk.AccessTokenAuthorizer(endpoint_token) | |
# Get endpoint info. This will fail if it's private and we don't have access. | |
# It will also fail if the endpoint hasn't been set up yet. | |
transfer_client=globus_sdk.TransferClient(transfer_authorizer) | |
try: | |
endpoint_info=transfer_client.get_endpoint(endpoint_uuid) | |
except globus_sdk.exc.TransferAPIError as e: | |
auth_client.oauth2_revoke_token(endpoint_token) | |
auth_client.oauth2_revoke_token(transfer_token) | |
if e.http_status == 403: | |
print('Endpoint is private.') | |
print('Add {}@clients.auth.globus.org as an Activity Monitor.'.format(client_id)) | |
sys.exit(1) | |
elif e.http_status == 404: | |
print('Endpoint is not set up yet. Try again later.') | |
sys.exit(1) | |
else: | |
raise e | |
# Get endpoint DTNs | |
endpoint_url='https://{}'.format( | |
endpoint_info['DATA'][0]['hostname'] | |
) | |
endpoint_client=globus_sdk.base.BaseClient( | |
service='endpoint', | |
base_url='{}/api'.format(endpoint_url), | |
authorizer=endpoint_authorizer, | |
http_timeout=5, | |
) | |
try: | |
for node in endpoint_client.get('/nodes')['data']: | |
print('DTN {} has IPs {}'.format( | |
node['id'], | |
','.join(node['ip_addresses']), | |
)) | |
except globus_sdk.exc.GlobusConnectionTimeoutError as e: | |
print('Timeout on endpoint query. All DTNs may be down.') | |
# Don't exit now, since all that's left is cleanup. | |
# Clean up | |
auth_client.oauth2_revoke_token(endpoint_token) | |
auth_client.oauth2_revoke_token(transfer_token) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment