Compute a md5sum of images for futur purpose

Fix #234
This commit is contained in:
Julien Duponchelle committed 2015-06-17 17:11:25 +02:00
1 parent 0f15e4b56a
commit f041697311
15 files changed
+248 -16

No files matched your search

+4 -1
View File
@@ -21,6 +21,7 @@ import stat
from ..config import Config
from ..web.route import Route
from ..utils.images import remove_checksum, md5sum
class UploadHandler:
@@ -36,7 +37,7 @@ class UploadHandler:
try:
for root, _, files in os.walk(UploadHandler.image_directory()):
for filename in files:
if not filename.startswith("."):
if not filename.startswith(".") and not filename.endswith(".md5sum"):
image_file = os.path.join(root, filename)
uploaded_files.append(image_file)
except OSError:
@@ -70,12 +71,14 @@ class UploadHandler:
destination_path = os.path.join(destination_dir, data["file"].filename)
try:
os.makedirs(destination_dir, exist_ok=True)
remove_checksum(destination_path)
with open(destination_path, "wb+") as f:
while True:
chunk = data["file"].file.read(512)
if not chunk:
break
f.write(chunk)
md5sum(destination_path)
st = os.stat(destination_path)
os.chmod(destination_path, st.st_mode | stat.S_IXUSR)
except OSError as e:
+4 -1
View File
@@ -37,6 +37,7 @@ from .nios.nio_udp import NIOUDP
from .nios.nio_tap import NIOTAP
from .nios.nio_nat import NIONAT
from .nios.nio_generic_ethernet import NIOGenericEthernet
from ..utils.images import md5sum, remove_checksum
class BaseManager:
@@ -444,7 +445,7 @@ class BaseManager:
files.sort()
images = []
for filename in files:
if filename[0] != ".":
if filename[0] != "." and not filename.endswith(".md5sum"):
images.append({"filename": filename})
return images
@@ -461,6 +462,7 @@ class BaseManager:
path = os.path.join(directory, os.path.basename(filename))
log.info("Writting image file %s", path)
try:
remove_checksum(path)
os.makedirs(directory, exist_ok=True)
with open(path, 'wb+') as f:
while True:
@@ -469,5 +471,6 @@ class BaseManager:
break
f.write(packet)
os.chmod(path, stat.S_IWRITE | stat.S_IREAD | stat.S_IEXEC)
md5sum(path)
except OSError as e:
raise aiohttp.web.HTTPConflict(text="Could not write image: {} to {}".format(filename, e))
@@ -37,6 +37,7 @@ from ..nios.nio_udp import NIOUDP
from gns3server.config import Config
from gns3server.utils.asyncio import wait_run_in_executor, monitor_process
from gns3server.utils.images import md5sum
class Router(BaseVM):
@@ -134,6 +135,7 @@ class Router(BaseVM):
"dynamips_id": self._dynamips_id,
"platform": self._platform,
"image": self._image,
"image_md5sum": md5sum(self._image),
"startup_config": self._startup_config,
"private_config": self._private_config,
"ram": self._ram,
+3 -1
View File
@@ -46,6 +46,7 @@ from .utils.iou_import import nvram_import
from .utils.iou_export import nvram_export
from .ioucon import start_ioucon
import gns3server.utils.asyncio
import gns3server.utils.images
import logging
@@ -208,6 +209,7 @@ class IOUVM(BaseVM):
"console": self._console,
"project_id": self.project.id,
"path": self.path,
"md5sum": gns3server.utils.images.md5sum(self.path),
"ethernet_adapters": len(self._ethernet_adapters),
"serial_adapters": len(self._serial_adapters),
"ram": self._ram,
@@ -789,7 +791,7 @@ class IOUVM(BaseVM):
# do not let IOU create the NVRAM anymore
#startup_config_file = self.startup_config_file
#if startup_config_file:
# if startup_config_file:
# command.extend(["-c", os.path.basename(startup_config_file)])
if self._l1_keepalives:
+12 -1
View File
@@ -38,6 +38,7 @@ from ..nios.nio_nat import NIONAT
from ..base_vm import BaseVM
from ...schemas.qemu import QEMU_OBJECT_SCHEMA, QEMU_PLATFORMS
from ...utils.asyncio import monitor_process
from ...utils.images import md5sum
import logging
log = logging.getLogger(__name__)
@@ -1217,13 +1218,23 @@ class QemuVM(BaseVM):
# Qemu has a long list of options. The JSON schema is the single source of information
for field in QEMU_OBJECT_SCHEMA["required"]:
if field not in answer:
answer[field] = getattr(self, field)
try:
answer[field] = getattr(self, field)
except AttributeError:
pass
answer["hda_disk_image"] = self.manager.get_relative_image_path(self._hda_disk_image)
answer["hda_disk_image_md5sum"] = md5sum(self._hda_disk_image)
answer["hdb_disk_image"] = self.manager.get_relative_image_path(self._hdb_disk_image)
answer["hdb_disk_image_md5sum"] = md5sum(self._hdb_disk_image)
answer["hdc_disk_image"] = self.manager.get_relative_image_path(self._hdc_disk_image)
answer["hdc_disk_image_md5sum"] = md5sum(self._hdc_disk_image)
answer["hdd_disk_image"] = self.manager.get_relative_image_path(self._hdd_disk_image)
answer["hdd_disk_image_md5sum"] = md5sum(self._hdd_disk_image)
answer["initrd"] = self.manager.get_relative_image_path(self._initrd)
answer["initrd_md5sum"] = md5sum(self._initrd)
answer["kernel_image"] = self.manager.get_relative_image_path(self._kernel_image)
answer["kernel_image_md5sum"] = md5sum(self._kernel_image)
return answer
+5
View File
@@ -546,6 +546,11 @@ VM_OBJECT_SCHEMA = {
"type": "string",
"minLength": 1,
},
"image_md5sum": {
"description": "checksum of the IOS image",
"type": "string",
"minLength": 1,
},
"startup_config": {
"description": "path to the IOS startup configuration file",
"type": "string",
+5 -1
View File
@@ -189,6 +189,10 @@ IOU_OBJECT_SCHEMA = {
"description": "Path of iou binary",
"type": "string"
},
"md5sum": {
"description": "Checksum of iou binary",
"type": "string"
},
"serial_adapters": {
"description": "How many serial adapters are connected to the IOU",
"type": "integer"
@@ -227,7 +231,7 @@ IOU_OBJECT_SCHEMA = {
}
},
"additionalProperties": False,
"required": ["name", "vm_id", "console", "project_id", "path", "serial_adapters", "ethernet_adapters",
"required": ["name", "vm_id", "console", "project_id", "path", "md5sum", "serial_adapters", "ethernet_adapters",
"ram", "nvram", "l1_keepalives", "startup_config", "private_config", "use_default_iou_values"]
}
+28 -3
View File
@@ -282,18 +282,34 @@ QEMU_OBJECT_SCHEMA = {
"description": "QEMU hda disk image path",
"type": "string",
},
"hda_disk_image_md5sum": {
"description": "QEMU hda disk image checksum",
"type": ["string", "null"]
},
"hdb_disk_image": {
"description": "QEMU hdb disk image path",
"type": "string",
},
"hdb_disk_image_md5sum": {
"description": "QEMU hdb disk image checksum",
"type": ["string", "null"],
},
"hdc_disk_image": {
"description": "QEMU hdc disk image path",
"type": "string",
},
"hdc_disk_image_md5sum": {
"description": "QEMU hdc disk image checksum",
"type": ["string", "null"],
},
"hdd_disk_image": {
"description": "QEMU hdd disk image path",
"type": "string",
},
"hdd_disk_image_md5sum": {
"description": "QEMU hdd disk image checksum",
"type": ["string", "null"],
},
"ram": {
"description": "amount of RAM in MB",
"type": "integer"
@@ -325,10 +341,18 @@ QEMU_OBJECT_SCHEMA = {
"description": "QEMU initrd path",
"type": "string",
},
"initrd_md5sum": {
"description": "QEMU initrd path",
"type": ["string", "null"],
},
"kernel_image": {
"description": "QEMU kernel image path",
"type": "string",
},
"kernel_image_md5sum": {
"description": "QEMU kernel image checksum",
"type": ["string", "null"],
},
"kernel_command_line": {
"description": "QEMU kernel command line",
"type": "string",
@@ -367,9 +391,10 @@ QEMU_OBJECT_SCHEMA = {
},
"additionalProperties": False,
"required": ["vm_id", "project_id", "name", "qemu_path", "platform", "hda_disk_image", "hdb_disk_image",
"hdc_disk_image", "hdd_disk_image", "ram", "adapters", "adapter_type", "mac_address", "console",
"initrd", "kernel_image", "kernel_command_line", "legacy_networking", "acpi_shutdown", "kvm",
"cpu_throttling", "process_priority", "options"]
"hdc_disk_image", "hdd_disk_image", "hda_disk_image_md5sum", "hdb_disk_image_md5sum",
"hdc_disk_image_md5sum", "hdd_disk_image_md5sum", "ram", "adapters", "adapter_type", "mac_address",
"console", "initrd", "kernel_image", "initrd_md5sum", "kernel_image_md5sum", "kernel_command_line",
"legacy_networking", "acpi_shutdown", "kvm", "cpu_throttling", "process_priority", "options"]
}
QEMU_BINARY_LIST_SCHEMA = {
+64
View File
@@ -0,0 +1,64 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2014 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import hashlib
def md5sum(path):
"""
Return the md5sum of an image and cache it on disk
:param path: Path to the image
:returns: Digest of the image
"""
if path is None or len(path) == 0:
return None
try:
with open(path + '.md5sum') as f:
return f.read()
except OSError:
pass
m = hashlib.md5()
with open(path, 'rb') as f:
while True:
buf = f.read(128)
if not buf:
break
m.update(buf)
digest = m.hexdigest()
try:
with open('{}.md5sum'.format(path), 'w+') as f:
f.write(digest)
except OSError as e:
log.error("Can't write digest of %s: %s", path, str(e))
return digest
def remove_checksum(path):
"""
Remove the checksum of an image from cache if exists
"""
path = '{}.md5sum'.format(path)
if os.path.exists(path):
os.remove(path)