Skip to content

Instantly share code, notes, and snippets.

@BadBastion
Last active February 8, 2019 19:31
Show Gist options
  • Save BadBastion/7a27c479b2110f09f3e865a56713a42d to your computer and use it in GitHub Desktop.
Save BadBastion/7a27c479b2110f09f3e865a56713a42d to your computer and use it in GitHub Desktop.
VPOD spawning snipits
# VPOD spawning utils file sinipits
# Spawning new VPODs
# Also checks the JSON responce for errors and throws if VPODs api errored
def __check_json_for_errors(json):
error_name = f"Error {json.get('error')}: " if json.get('error') else ''
error_msg = f"\"{json.get('message')}\"" if json.get('message') else ''
error_full = f"Error: {json.get('errorMsg')}" if json.get('errorMsg') else ''
error = f'{error_name}{error_msg}{error_full}'
if(error != ''):
log.error(f'JSON response included the errors "{error}"')
raise RuntimeError('JSON response included errors')
def spawn(usecase, oracle_data_id, release, changelist, branch, is_external, headers):
log.info('Sending spawn request')
if(branch == 'main'):
branch = ''
elif(branch == 'patch'):
branch = f'__patch'
else:
log.fatal(f'Unknown branch: {branch}')
url = f'{vpod_api_url}/V1/lab/spawn'
data = {
'usecase': usecase,
'releaseLabel': f'{release}{branch}',
'changelist': changelist,
'oracleDataId': oracle_data_id,
'isExternal': is_external
}
res_json = requests.post(url, headers=headers, data=data).json()
__check_json_for_errors(res_json)
return res_json.get('id')
# Poling VPODs for alive state
# Checks for both VPOD lab status & app server status
def __vip(lab_id, headers):
get_lab_ip = f'{vpod_api_url}/V1/lab/get/ip/{lab_id}'
res_json = requests.get(get_lab_ip, headers=headers).json()
url = res_json.get('coreVip')
def __app_servers_status(lab_id, headers):
url = __vip(lab_id, headers)
try:
status = requests.get(f'{url}/ping.jsp').text
except:
status = 'STARTING'
return status
def __status(usecase, lab_id, headers):
res_json = requests.get(f'https://facade.vpod.t.force.com:8443/V1/labState/{lab_id}', headers=headers).json()
lab_status = res_json.get('labStatus')
if(lab_status == 'DONE'):
lab_status = __app_servers_status(lab_id, headers)
elif(not lab_status): lab_status = 'SPAWNING'
def poll_status(usecase, lab_id, headers, max_tries=30):
log.info(f'Checking if lab: {lab_id} is alive.')
lab_status = None
for i in range(max_tries):
lab_status = __status(usecase, lab_id, headers)
if(lab_status in ['SPAWNING', 'STARTING']):
log.info(f'Poll {i}: Lab is {lab_status}')
time.sleep(240)
else: break
if(lab_status not in ['SPAWNING', 'STARTING', 'ALIVE']):
log.warn(f'Unexpected lab status: {lab_status}. Expected SPAWNING, STARTING, or ALIVE')
return lab_status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment