From 77937a0703510a2e18b03b9c57009e9482135985 Mon Sep 17 00:00:00 2001 From: aapatwa <161330416+aapatwa@users.noreply.github.com> Date: Wed, 28 Feb 2024 13:20:00 +0530 Subject: [PATCH 1/6] Update ucsgenutils.py Adding new definitions to read a file in chunks , creating URI and to upload firmware --- ucsmsdk/ucsgenutils.py | 70 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/ucsmsdk/ucsgenutils.py b/ucsmsdk/ucsgenutils.py index 4b92ef12..3cf69a58 100644 --- a/ucsmsdk/ucsgenutils.py +++ b/ucsmsdk/ucsgenutils.py @@ -226,6 +226,76 @@ def upload_file(driver, uri, file_dir, file_name, progress=Progress()): raise ValueError("File upload failed.") +def read_in_chunks(file_object, chunk_size=10*1024*1024): + """(generator) to read a file piece by piece. + Default chunk size: 10MB.""" + while True: + data = file_object.read(chunk_size) + if not data: + break + yield data + + +def generate_uri_for_chunks(uri,counter,flag): + """(generator) to generate uri for chunks + and return URI""" + index = uri.rfind("/") + if flag == 0 : + res = uri[ : index ] + "/" + str(counter) + uri[index : ] + return res + else : + res = uri[ : index ] + "/merge-" + str(counter) + uri[index : ] + return res + + +def upload_firmware(driver, uri, file_dir, file_name, progress=Progress()): + """ + Uploads the file on web server + + Args: + driver (UcsDriver) + uri (str): url to upload the file + file_dir (str): The directory to download to + file_name (str): The destination file name for the download + + Returns: + None + + Example: + driver = UcsDriver()\n + upload_firmware(driver=UcsDriver(), uri="http://fileurl", file_dir='/home/user/backup', file_name='my_config_backup.xml') + """ + + content_path = os.path.join(file_dir, file_name) + content_size = os.stat(content_path).st_size + + f = open(content_path,'rb') + CHUNK_SIZE = 10*1024*1024 + counter = 0 + flag = 0 + + for chunk in read_in_chunks(f,CHUNK_SIZE): + if len(chunk) > 0: + try: + counter += 1 + uri1 = generate_uri_for_chunks(uri,counter,flag) + response = driver.post(uri1, data = chunk) + progress.update(content_size,len(chunk)) + if not response: + raise ValueError("File upload failed.") + except Exception as e: + raise Exception(e) + try: + counter += 1 + flag = 1 + uri2 = generate_uri_for_chunks(uri,counter,flag) + response = driver.post(uri2) + if not response: + raise ValueError("File upload failed.") + except Exception as e: + raise Exception(e) + + def check_registry_key(java_key): """ Method checks for the java in the registry entries. """ From c138e3386c1f6db35fd5750b476a391be835ba79 Mon Sep 17 00:00:00 2001 From: aapatwa <161330416+aapatwa@users.noreply.github.com> Date: Wed, 28 Feb 2024 13:28:45 +0530 Subject: [PATCH 2/6] Update ucssession.py Adding a new definition to upload firmware for file size greater than 2 GB --- ucsmsdk/ucssession.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/ucsmsdk/ucssession.py b/ucsmsdk/ucssession.py index ac9dffff..121ad302 100644 --- a/ucsmsdk/ucssession.py +++ b/ucsmsdk/ucssession.py @@ -427,6 +427,49 @@ def file_upload( self.__driver.remove_header('Cookie') + + def firmware_upload( + self, + url_suffix, + file_dir, + file_name, + progress=Progress()): + """ + Uploads the file on UCSM server. + + Args: + url_suffix (str): suffix url to be appended to + http\https://host:port/ to locate the file on the server + source_dir (str): The directory to upload from + file_name (str): The destination file name for the download + progress (ucsgenutils.Progress): Class that has method to display progress + + Returns: + None + + Example: + source_dir = "/home/user/backup"\n + file_name = "config_backup.xml"\n + uri_suffix = "operations/file-%s/importconfig.txt" % file_name\n + firmware_upload(url_suffix=uri_suffix, source_dir=source_dir, file_name=file_name) + """ + + from .ucsgenutils import upload_firmware + + file_url = "%s/%s" % (self.__uri, url_suffix) + + self.__driver.add_header('Cookie', 'ucsm-cookie=%s' + % self.__cookie) + + upload_firmware(self.__driver, + uri=file_url, + file_dir=file_dir, + file_name=file_name, + progress=progress) + + self.__driver.remove_header('Cookie') + + def __start_refresh_timer(self): """ Internal method to support auto-refresh functionality. From db31fe6e51fb0020fc650feb208d4faaae9f06b8 Mon Sep 17 00:00:00 2001 From: aapatwa <161330416+aapatwa@users.noreply.github.com> Date: Wed, 28 Feb 2024 17:26:32 +0530 Subject: [PATCH 3/6] Update ucsmsdk/ucsgenutils.py Co-authored-by: Vikrant Balyan --- ucsmsdk/ucsgenutils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ucsmsdk/ucsgenutils.py b/ucsmsdk/ucsgenutils.py index 3cf69a58..410e2f40 100644 --- a/ucsmsdk/ucsgenutils.py +++ b/ucsmsdk/ucsgenutils.py @@ -267,7 +267,7 @@ def upload_firmware(driver, uri, file_dir, file_name, progress=Progress()): """ content_path = os.path.join(file_dir, file_name) - content_size = os.stat(content_path).st_size + content_size = os.path.getsize(content_path) f = open(content_path,'rb') CHUNK_SIZE = 10*1024*1024 From 51eca299033b40657363401bb0d56152ad3da5b0 Mon Sep 17 00:00:00 2001 From: aapatwa <161330416+aapatwa@users.noreply.github.com> Date: Fri, 1 Mar 2024 09:04:22 +0530 Subject: [PATCH 4/6] Update ucsgenutils.py --- ucsmsdk/ucsgenutils.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/ucsmsdk/ucsgenutils.py b/ucsmsdk/ucsgenutils.py index 410e2f40..8899f3af 100644 --- a/ucsmsdk/ucsgenutils.py +++ b/ucsmsdk/ucsgenutils.py @@ -236,7 +236,7 @@ def read_in_chunks(file_object, chunk_size=10*1024*1024): yield data -def generate_uri_for_chunks(uri,counter,flag): +def generate_uri_for_chunks(uri, counter, flag): """(generator) to generate uri for chunks and return URI""" index = uri.rfind("/") @@ -269,18 +269,21 @@ def upload_firmware(driver, uri, file_dir, file_name, progress=Progress()): content_path = os.path.join(file_dir, file_name) content_size = os.path.getsize(content_path) + if not os.path.exists(content_path): + raise IOError("File does not exist") + f = open(content_path,'rb') - CHUNK_SIZE = 10*1024*1024 + chunk_size = 10*1024*1024 counter = 0 flag = 0 - for chunk in read_in_chunks(f,CHUNK_SIZE): + for chunk in read_in_chunks(f, chunk_size): if len(chunk) > 0: try: counter += 1 - uri1 = generate_uri_for_chunks(uri,counter,flag) - response = driver.post(uri1, data = chunk) - progress.update(content_size,len(chunk)) + chunk_uri = generate_uri_for_chunks(uri, counter, flag) + response = driver.post(chunk_uri, data = chunk) + progress.update(content_size, len(chunk)) if not response: raise ValueError("File upload failed.") except Exception as e: @@ -288,8 +291,8 @@ def upload_firmware(driver, uri, file_dir, file_name, progress=Progress()): try: counter += 1 flag = 1 - uri2 = generate_uri_for_chunks(uri,counter,flag) - response = driver.post(uri2) + merge_uri = generate_uri_for_chunks(uri, counter, flag) + response = driver.post(merge_uri) if not response: raise ValueError("File upload failed.") except Exception as e: From d056cb6e6be15c8a1e7dbfb53caff4dfb607ee79 Mon Sep 17 00:00:00 2001 From: aapatwa <161330416+aapatwa@users.noreply.github.com> Date: Fri, 8 Mar 2024 15:15:21 +0530 Subject: [PATCH 5/6] Update ucsguilaunch.py - changed ucsm_launch_url --- ucsmsdk/utils/ucsguilaunch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ucsmsdk/utils/ucsguilaunch.py b/ucsmsdk/utils/ucsguilaunch.py index 1235dd9b..918245c3 100644 --- a/ucsmsdk/utils/ucsguilaunch.py +++ b/ucsmsdk/utils/ucsguilaunch.py @@ -56,7 +56,7 @@ def ucs_gui_launch(handle, need_url=False): jnlp_file = None try: - ucsm_gui_url = "%s/ucsm/ucsm.jnlp" % handle.uri + ucsm_gui_url = "%s/app/ucsm/index.html" % handle.uri if handle: auth_token = handle.get_auth_token() log.debug("AuthToken: <%s>" % auth_token) From a9ce0ebf476fb29737adab58ce1adcabd76c92d5 Mon Sep 17 00:00:00 2001 From: aapatwa <161330416+aapatwa@users.noreply.github.com> Date: Fri, 15 Mar 2024 12:46:21 +0530 Subject: [PATCH 6/6] Reverting ucsguilaunch.py --- ucsmsdk/utils/ucsguilaunch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ucsmsdk/utils/ucsguilaunch.py b/ucsmsdk/utils/ucsguilaunch.py index 918245c3..1235dd9b 100644 --- a/ucsmsdk/utils/ucsguilaunch.py +++ b/ucsmsdk/utils/ucsguilaunch.py @@ -56,7 +56,7 @@ def ucs_gui_launch(handle, need_url=False): jnlp_file = None try: - ucsm_gui_url = "%s/app/ucsm/index.html" % handle.uri + ucsm_gui_url = "%s/ucsm/ucsm.jnlp" % handle.uri if handle: auth_token = handle.get_auth_token() log.debug("AuthToken: <%s>" % auth_token)