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

Added a new Definition to upload firmware images greater than 2GB in size #243

Merged
merged 6 commits into from
Apr 10, 2024
Merged
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
73 changes: 73 additions & 0 deletions ucsmsdk/ucsgenutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,79 @@ 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()):
vvb marked this conversation as resolved.
Show resolved Hide resolved
"""
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.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
counter = 0
flag = 0

for chunk in read_in_chunks(f, chunk_size):
if len(chunk) > 0:
try:
counter += 1
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:
raise Exception(e)
try:
counter += 1
flag = 1
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:
raise Exception(e)


def check_registry_key(java_key):
""" Method checks for the java in the registry entries. """

Expand Down
43 changes: 43 additions & 0 deletions ucsmsdk/ucssession.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down