1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-12-26 16:58:28 +00:00

Do not cache to md5sum file in some situations

This commit is contained in:
grossmj 2022-04-18 17:13:52 +07:00
parent e6c8144210
commit 3106c8a6a2
5 changed files with 26 additions and 20 deletions

View File

@ -1057,10 +1057,10 @@ class QemuVM(BaseNode):
# In case user upload image manually we don't have md5 sums. # In case user upload image manually we don't have md5 sums.
# We need generate hashes at this point, otherwise they will be generated # We need generate hashes at this point, otherwise they will be generated
# at asdict but not on separate thread. # at asdict but not on separate thread.
await cancellable_wait_run_in_executor(md5sum, self._hda_disk_image) await cancellable_wait_run_in_executor(md5sum, self._hda_disk_image, self.working_dir)
await cancellable_wait_run_in_executor(md5sum, self._hdb_disk_image) await cancellable_wait_run_in_executor(md5sum, self._hdb_disk_image, self.working_dir)
await cancellable_wait_run_in_executor(md5sum, self._hdc_disk_image) await cancellable_wait_run_in_executor(md5sum, self._hdc_disk_image, self.working_dir)
await cancellable_wait_run_in_executor(md5sum, self._hdd_disk_image) await cancellable_wait_run_in_executor(md5sum, self._hdd_disk_image, self.working_dir)
super().create() super().create()

View File

@ -153,8 +153,14 @@ class ApplianceManager:
version_images[appliance_key] = image_in_db.filename version_images[appliance_key] = image_in_db.filename
else: else:
# check if the image is on disk # check if the image is on disk
# FIXME: still necessary? the image should have been discovered and saved in the db already
image_path = os.path.join(image_dir, appliance_file) image_path = os.path.join(image_dir, appliance_file)
if os.path.exists(image_path) and await wait_run_in_executor(md5sum, image_path) == image_checksum: if os.path.exists(image_path) and \
await wait_run_in_executor(
md5sum,
image_path,
cache_to_md5file=False
) == image_checksum:
async with aiofiles.open(image_path, "rb") as f: async with aiofiles.open(image_path, "rb") as f:
await write_image(appliance_file, image_path, f, images_repo) await write_image(appliance_file, image_path, f, images_repo)
else: else:

View File

@ -630,9 +630,6 @@ class Compute:
try: try:
if type in ["qemu", "dynamips", "iou"]: if type in ["qemu", "dynamips", "iou"]:
# for local_image in list_images(type):
# if local_image['filename'] not in [i['filename'] for i in images]:
# images.append(local_image)
images = sorted(images, key=itemgetter("filename")) images = sorted(images, key=itemgetter("filename"))
else: else:
images = sorted(images, key=itemgetter("image")) images = sorted(images, key=itemgetter("image"))

View File

@ -122,7 +122,7 @@ async def read_image_info(path: str, expected_image_type: str = None) -> dict:
"image_type": detected_image_type, "image_type": detected_image_type,
"image_size": os.stat(path).st_size, "image_size": os.stat(path).st_size,
"path": path, "path": path,
"checksum": await wait_run_in_executor(md5sum, path), "checksum": await wait_run_in_executor(md5sum, path, cache_to_md5file=False),
"checksum_algorithm": "md5", "checksum_algorithm": "md5",
} }
return image_info return image_info
@ -149,7 +149,7 @@ async def discover_images(image_type: str, skip_image_paths: list = None) -> Lis
try: try:
images.append(await read_image_info(path, image_type)) images.append(await read_image_info(path, image_type))
except InvalidImageError as e: except InvalidImageError as e:
#log.warning(f"{e}") log.debug(str(e))
continue continue
return images return images
@ -211,7 +211,7 @@ def images_directories(image_type):
return [force_unix_path(p) for p in paths if os.path.exists(p)] return [force_unix_path(p) for p in paths if os.path.exists(p)]
def md5sum(path, working_dir=None, stopped_event=None): def md5sum(path, working_dir=None, stopped_event=None, cache_to_md5file=True):
""" """
Return the md5sum of an image and cache it on disk Return the md5sum of an image and cache it on disk
@ -255,11 +255,12 @@ def md5sum(path, working_dir=None, stopped_event=None):
log.error("Can't create digest of %s: %s", path, str(e)) log.error("Can't create digest of %s: %s", path, str(e))
return None return None
try: if cache_to_md5file:
with open(md5sum_file, "w+") as f: try:
f.write(digest) with open(md5sum_file, "w+") as f:
except OSError as e: f.write(digest)
log.error("Can't write digest of %s: %s", path, str(e)) except OSError as e:
log.error("Can't write digest of %s: %s", path, str(e))
return digest return digest

View File

@ -18,6 +18,7 @@
import os import os
import sys import sys
import threading import threading
import pytest
from unittest.mock import patch from unittest.mock import patch
@ -110,7 +111,8 @@ def test_remove_checksum(tmpdir):
remove_checksum(str(tmpdir / 'not_exists')) remove_checksum(str(tmpdir / 'not_exists'))
def test_list_images(tmpdir, config): @pytest.mark.asyncio
async def test_list_images(tmpdir, config):
path1 = tmpdir / "images1" / "IOS" / "test1.image" path1 = tmpdir / "images1" / "IOS" / "test1.image"
path1.write(b'\x7fELF\x01\x02\x01', ensure=True) path1.write(b'\x7fELF\x01\x02\x01', ensure=True)
@ -140,7 +142,7 @@ def test_list_images(tmpdir, config):
config.settings.Server.images_path = str(tmpdir / "images1") config.settings.Server.images_path = str(tmpdir / "images1")
config.settings.Server.additional_images_paths = "/tmp/null24564;" + str(tmpdir / "images2") config.settings.Server.additional_images_paths = "/tmp/null24564;" + str(tmpdir / "images2")
assert list_images("dynamips") == [ assert await list_images("dynamips") == [
{ {
'filename': 'test1.image', 'filename': 'test1.image',
'filesize': 7, 'filesize': 7,
@ -156,7 +158,7 @@ def test_list_images(tmpdir, config):
] ]
if sys.platform.startswith("linux"): if sys.platform.startswith("linux"):
assert list_images("iou") == [ assert await list_images("iou") == [
{ {
'filename': 'test3.bin', 'filename': 'test3.bin',
'filesize': 7, 'filesize': 7,
@ -165,7 +167,7 @@ def test_list_images(tmpdir, config):
} }
] ]
assert list_images("qemu") == [ assert await list_images("qemu") == [
{ {
'filename': 'test4.qcow2', 'filename': 'test4.qcow2',
'filesize': 1, 'filesize': 1,