Skip to content
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

chore: better hcp error handling, syntax #75

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 26 additions & 19 deletions vcenter/vcenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import uuid
import urllib.request


class VCenter:

def __init__(self):
Expand Down Expand Up @@ -515,29 +516,35 @@ def _store_screenshot_to_hcp(self, machine_uuid: str, screenshot_data) -> str:
hcp_auth = settings.app['hcp']['auth']
hcp_base_dir = settings.app['hcp']['base_dir']

hcp_filename= f'{machine_uuid}_{uuid.uuid4()}.png'
hcp_filename = f'{machine_uuid}_{uuid.uuid4()}.png'
upload_url = f'{hcp_server}/rest/{hcp_base_dir}/{hcp_filename}'
put_request = urllib.request.Request(
upload_url,
method='PUT',
data=screenshot_data,
)
put_request.add_header('Content-Length', str(len(screenshot_data)))
put_request.add_header('Content-Type', 'multipart/form-data')
put_request.add_header('Authorization', hcp_auth)
ssl_context = ssl._create_unverified_context()
response = urllib.request.urlopen(
put_request,
context=ssl_context,
timeout=settings.app['hcp'].get('timeout', 120)
)
if response.code != 201:
settings.raven.captureMessage(
f'problem uploading data to hcp: {hcp_server}, {upload_url} -> {response.code}'
try:
put_request = urllib.request.Request(
upload_url,
method='PUT',
data=screenshot_data,
)
put_request.add_header('Content-Length', str(len(screenshot_data)))
put_request.add_header('Content-Type', 'multipart/form-data')
put_request.add_header('Authorization', hcp_auth)
ssl_context = ssl._create_unverified_context()
response = urllib.request.urlopen(
put_request,
context=ssl_context,
timeout=settings.app['hcp'].get('timeout', 120)
)
if response.code != 201:
settings.raven.captureMessage(
f'problem uploading data to hcp: {hcp_server}, {upload_url} -> {response.code}'
)
except Exception as ex:
settings.raven.captureException(exc_info=True)
self.__logger.error(f"Exception while sending data to hcp: {ex}")
raise ex

return upload_url.replace('/rest/', '/hs3/')

def take_screenshot(self, uuid: str, store_to: str='db') -> str:
def take_screenshot(self, uuid: str, store_to: str = 'db') -> str:
"""
Takes screenshot of VM and returns it as base64 encoded string or hcp url
:param uuid: machine uuid
Expand Down
4 changes: 2 additions & 2 deletions web/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ class Settings:
'caching_period': 15, # in seconds
'caching_enabled_threshold': 90, # in percent
},
'screenshot_store': 'db', # hcp eventually
'screenshot_store': 'db', # hcp eventually
},
'hcp': {
'url': None,
'auth': None,
'base_dir':'ss',
'base_dir': 'ss',
'timeout': 120,
},
'retries': {
Expand Down