From e7d9bd82621ede294f510e736f2fad20f1fae949 Mon Sep 17 00:00:00 2001 From: Ivo Reznicek Date: Tue, 20 Apr 2021 17:09:57 +0200 Subject: [PATCH] chore: better hcp error handling, syntax --- vcenter/vcenter.py | 45 ++++++++++++++++++++++++++------------------- web/settings.py | 4 ++-- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/vcenter/vcenter.py b/vcenter/vcenter.py index 8af6cc5..3b4ee5f 100644 --- a/vcenter/vcenter.py +++ b/vcenter/vcenter.py @@ -17,6 +17,7 @@ import uuid import urllib.request + class VCenter: def __init__(self): @@ -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 diff --git a/web/settings.py b/web/settings.py index 9a2c3bd..6f208d8 100644 --- a/web/settings.py +++ b/web/settings.py @@ -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': {