Commit 28fa919f authored by John Tourtellott's avatar John Tourtellott
Browse files

Update for latest smtk API and Girder session on NERSC/Spin

Includes supporting newt session id for authentication
parent bed8ec40
......@@ -71,7 +71,10 @@ class CumulusClient():
# Getting mixed signals on what listFolder returns
# I *think* it is a generator
try:
self._private_folder_id = r.next()['_id']
if sys.version_info >= (3, 2):
self._private_folder_id = next(r)['_id']
else:
self._private_folder_id = r.next()['_id']
except Exception as ex:
# But just in case
self._private_folder_id = r[0]['_id']
......
......@@ -65,7 +65,12 @@ def submit_ace3p(scope, sim_item):
check_file(path, 'Cannot find simulation input file at %s')
# Start NERSC session
login_nersc(scope, sim_item)
scope.newt_sessionid = None
sessionid = login_nersc(scope, sim_item)
print('newt_sessionid', sessionid)
if sessionid is None or sessionid == "":
raise RuntimeError('ERROR: Unable to get valid session id')
scope.newt_sessionid = sessionid
# Initialize CumulusClient
scope.cumulus = create_cumulus_client(scope, sim_item)
......@@ -117,35 +122,48 @@ def release_resources(scope):
# ---------------------------------------------------------------------
def login_nersc(scope, sim_item):
'''Logs into NERSC and gets session id
'''Returns NEWT session id, logging into NERSC if specified
Note that this method must *also* initialize NewtClient instance
'''
scope.newt_sessionid = None
nersc_url = 'https://newt.nersc.gov/newt'
# Check if sesson id provided
credentials_item = sim_item.find('NERSCCredentials')
credentials_type = credentials_item.value()
if credentials_type == 'newt_sessionid':
session_item = credentials_item.find('NEWTSessionId')
session_id = session_item.value()
scope.nersc = NewtClient(nersc_url, session_id)
return session_id
elif credentials_type != 'login':
raise RuntimeError('ERROR - Unrecognized credentials_type', credentials_type)
# Check user inputs
username = get_string(sim_item, 'NERSCAccountName')
print('username', username)
if not username:
raise Exception('ERROR: NERSC account name not specified')
# if not username:
# raise Exception('ERROR: NERSC account name not specified')
password = get_string(sim_item, 'NERSCAccountPassword')
print('password length', len(password))
if not password:
raise Exception('ERROR: NERSC account password not specified')
# if not password:
# raise Exception('ERROR: NERSC account password not specified')
mfa = get_string(sim_item, 'NERSCMultfactorToken')
nersc_url = 'https://newt.nersc.gov/newt'
scope.nersc = NewtClient(nersc_url)
r = scope.nersc.login(username, password)
scope.newt_sessionid = scope.nersc.get_sessionid()
print('newt_sessionid', scope.newt_sessionid)
r = scope.nersc.login(username, password+mfa)
return scope.nersc.get_sessionid()
# ---------------------------------------------------------------------
def create_cumulus_client(scope, sim_item):
'''Instantiates Cumulus client
'''
item = sim_item.find('CumulusHost')
cumulus_item = smtk.attribute.to_concrete(item)
cumulus_item = sim_item.find('CumulusHost')
# cumulus_item = smtk.attribute.to_concrete(item)
cumulus_host = cumulus_item.value(0)
if not cumulus_host:
raise Exception('ERROR: Cumulus host not specified')
......@@ -362,12 +380,11 @@ def get_integer(group_item, name):
if not item.isEnabled():
return None
concrete_item = smtk.attribute.to_concrete(item)
if concrete_item.type() != smtk.attribute.Item.IntType:
if item.type() != smtk.attribute.Item.IntType:
print('WARNING: item \"%s\" not an integer item' % name)
return None
return concrete_item.value(0)
return item.value(0)
# ---------------------------------------------------------------------
def get_string(group_item, name):
......@@ -383,9 +400,8 @@ def get_string(group_item, name):
if not item.isEnabled():
return None
concrete_item = smtk.attribute.to_concrete(item)
if concrete_item.type() != smtk.attribute.Item.StringType:
if item.type() != smtk.attribute.Item.StringType:
print('WARNING: item \"%s\" not a string item' % name)
return None
return concrete_item.value(0)
return item.value(0)
......@@ -24,9 +24,9 @@ class NewtClient():
'''
# ---------------------------------------------------------------------
def __init__(self, base_url):
def __init__(self, base_url, session_id=None):
self._base_url = base_url
self._session_id = None
self._session_id = session_id
# ---------------------------------------------------------------------
def get_authentication_status(self):
......@@ -37,7 +37,7 @@ class NewtClient():
url = '%s/login/' % self._base_url
r = requests.get(url)
return r.json()
# ---------------------------------------------------------------------
def login(self, username, password):
'''Sends login command to server.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment