Create section in file

This commit is contained in:
Julien Duponchelle 2015-05-22 12:52:16 +02:00
parent 38cccb7928
commit 7048d9a9b0
7 changed files with 86 additions and 75 deletions

View File

@ -1,7 +1,6 @@
{
"name": "Arista vEOS",
"category": "router",
"emulator": "qemu",
"vendor_name": "Arista",
"vendor_url": "http://www.arista.com/",
"documentation_url": "http://www.arista.com/docs/Manuals/ConfigGuide.pdf",
@ -12,26 +11,31 @@
"maintainer": "GNS3 Team",
"maintainer_email": "developers@gns3.net",
"adapter_type": "e1000",
"adapters": 8,
"hda_disk_image": [
{
"filename": "Aboot-veos-2.1.0.iso",
"version": "2.1.0",
"sha1sum": "ea9dc1989764fc6db1d388b061340743016214a7",
"download_url": "https://www.arista.com/en/support/software-download"
}
],
"hdb_disk_image": [
{
"filename": "vEOS-lab-4.13.8M.vmdk",
"version": "4.13.8M",
"sha1sum": "5f42d71b30bc682e44ccf7340e20ea7ea8967ef5",
"download_url": "https://www.arista.com/en/support/software-download"
}
],
"options": "-nographics",
"kvm_support": true,
"ram": 2048,
"processor": "x64"
"qemu": {
"adapter_type": "e1000",
"adapters": 8,
"options": "-nographics",
"kvm_support": true,
"ram": 2048,
"processor": "x64"
},
"images": {
"hda_disk_image": [
{
"filename": "Aboot-veos-2.1.0.iso",
"version": "2.1.0",
"sha1sum": "ea9dc1989764fc6db1d388b061340743016214a7",
"download_url": "https://www.arista.com/en/support/software-download"
}
],
"hdb_disk_image": [
{
"filename": "vEOS-lab-4.13.8M.vmdk",
"version": "4.13.8M",
"sha1sum": "ff50656fe817c420e9f7fbb0c0ee41f1ca52fee2",
"download_url": "https://www.arista.com/en/support/software-download"
}
]
}
}

View File

@ -1,7 +1,6 @@
{
"name": "Micro Core Linux",
"category": "guest",
"emulator": "qemu",
"vendor_name": "Team Tiny Core",
"vendor_url": "http://distro.ibiblio.org/tinycorelinux",
"documentation_url": "http://wiki.tinycorelinux.net/",
@ -12,24 +11,29 @@
"maintainer": "GNS3 Team",
"maintainer_email": "developers@gns3.net",
"adapter_type": "e1000",
"adapters": 1,
"hda_disk_image": [
{
"filename": "linux-microcore-3.4.1.img",
"version": "3.4.1",
"sha1sum": "5f42d71b30bc682e44ccf7340e20ea7ea8967ef5",
"download_url": "https://sourceforge.net/projects/gns-3/files/Qemu%20Appliances/",
"direct_download_url": "http://downloads.sourceforge.net/project/gns-3/Qemu%20Appliances/linux-microcore-3.4.1.img"
},
{
"filename": "linux-microcore-4.0.2-clean.img",
"version": "4.0.2",
"sha1sum": "0252f2c913519c993b812325bbd553af2d77218a",
"download_url": "https://sourceforge.net/projects/gns-3/files/Qemu%20Appliances/",
"direct_download_url": "http://downloads.sourceforge.net/project/gns-3/Qemu%20Appliances/linux-microcore-4.0.2-clean.img"
}
],
"ram": 32,
"processor": "i386"
"qemu": {
"adapter_type": "e1000",
"adapters": 1,
"ram": 32,
"processor": "i386"
},
"images": {
"hda_disk_image": [
{
"filename": "linux-microcore-3.4.1.img",
"version": "3.4.1",
"sha1sum": "5f42d71b30bc682e44ccf7340e20ea7ea8967ef5",
"download_url": "https://sourceforge.net/projects/gns-3/files/Qemu%20Appliances/",
"direct_download_url": "http://downloads.sourceforge.net/project/gns-3/Qemu%20Appliances/linux-microcore-3.4.1.img"
},
{
"filename": "linux-microcore-4.0.2-clean.img",
"version": "4.0.2",
"sha1sum": "0252f2c913519c993b812325bbd553af2d77218a",
"download_url": "https://sourceforge.net/projects/gns-3/files/Qemu%20Appliances/",
"direct_download_url": "http://downloads.sourceforge.net/project/gns-3/Qemu%20Appliances/linux-microcore-4.0.2-clean.img"
}
]
}
}

View File

@ -76,15 +76,15 @@ class Config:
}
if device_config["category"] == "guest":
new_config["category"] = 2
if device_config["emulator"] == "qemu":
if "qemu" in device_config:
self._add_qemu_config(new_config, device_config)
def _add_qemu_config(self, new_config, device_config):
new_config["adapter_type"] = device_config["adapter_type"]
new_config["adapters"] = device_config["adapters"]
new_config["adapter_type"] = device_config["qemu"]["adapter_type"]
new_config["adapters"] = device_config["qemu"]["adapters"]
new_config["cpu_throttling"] = 0
new_config["ram"] = device_config["ram"]
new_config["ram"] = device_config["qemu"]["ram"]
new_config["legacy_networking"] = False
new_config["process_priority"] = "normal"
@ -97,16 +97,16 @@ class Config:
new_config["hdd_disk_image"] = ""
#TODO: Manage Windows
if device_config["processor"] == "i386":
if device_config["qemu"]["processor"] == "i386":
new_config["qemu_path"] = "qemu-system-i386"
if device_config["category"] == "guest":
new_config["default_symbol"] = ":/symbols/qemu_guest.normal.svg"
new_config["hover_symbol"] = ":/symbols/qemu_guest.selected.svg"
if isinstance(device_config["hda_disk_image"], Image):
new_config["name"] += " {}".format(device_config["hda_disk_image"].version)
new_config["hda_disk_image"] = device_config["hda_disk_image"].path
if isinstance(device_config["images"]["hda_disk_image"], Image):
new_config["name"] += " {}".format(device_config["images"]["hda_disk_image"].version)
new_config["hda_disk_image"] = device_config["images"]["hda_disk_image"].path
# Remove VM with the same Name
self._config["Qemu"]["vms"] = [item for item in self._config["Qemu"]["vms"] if item["name"] != new_config["name"]]

View File

@ -71,10 +71,13 @@ if __name__ == "__main__":
if args.add_image:
add_image(args.add_image)
elif args.search:
print("Available images\n")
for res in registry.search_device(args.search):
print("{}: ".format(res["name"]))
for file in res["hda_disk_image"]:
print(" * {} {}: {}".format(file["version"], file["filename"], file["sha1sum"]))
for image_type in res["images"]:
print(" * {}:".format(image_type))
for file in res["images"][image_type]:
print(" * {} {}: {}".format(file["version"], file["filename"], file["sha1sum"]))
elif args.install:
image = registry.download_image(args.install, config.images_dir)
if image:

View File

@ -44,21 +44,21 @@ class Registry:
return configurations
def download_image(self, sha1sum, images_dir):
for config in self._all_configs():
for file in config.get("hda_disk_image", []):
if file["sha1sum"] == sha1sum:
path = os.path.join(images_dir, file["filename"])
for image_type in config.get("images", {}):
for file in config["images"].get(image_type, []):
if file["sha1sum"] == sha1sum:
path = os.path.join(images_dir, "QEMU", file["filename"])
if "direct_download_url" in file:
print("Download {} to {}".format(file["direct_download_url"], path))
#TODO: Skip download if file already exist with same sha1
urllib.request.urlretrieve(file["direct_download_url"], path)
return path
else:
print("You need to download by hand the image {filename} from:\n{download_url}\n\nAnd run: ./bin/gns3-get --add {filename}".format(filename=file["filename"], download_url=file["download_url"]))
return None
if "direct_download_url" in file:
print("Download {} to {}".format(file["direct_download_url"], path))
#TODO: Skip download if file already exist with same sha1
urllib.request.urlretrieve(file["direct_download_url"], path)
return path
else:
print("You need to manually download the image {filename} from:\n{download_url}\n\nAnd run: ./bin/gns3-get --add {filename}".format(filename=file["filename"], download_url=file["download_url"]))
return None
def search_device(self, query):
results = []
@ -78,19 +78,19 @@ class Registry:
if file.endswith(".json"):
with open(os.path.join(devices_path, file)) as f:
config = json.load(f)
config["filename"] = file[:-5]
yield config
def _image_match(self, image, config):
"""
:returns: True if image is present in configuration
"""
for file in config.get("hda_disk_image", []):
if file.get("sha1sum", None) == image.sha1sum:
image.version = file["version"]
config["hda_disk_image"] = image
return True
return False
for image_type in config.get("images", {}):
for file in config["images"].get(image_type, []):
if file.get("sha1sum", None) == image.sha1sum:
image.version = file["version"]
config["images"][image_type] = image
return True
return False
def _get_devices_path(self):
"""

View File

@ -80,7 +80,7 @@ def test_add_image(empty_config, linux_microcore_img):
config = json.load(f)
image = Image(linux_microcore_img)
image.version = "3.4.1"
config["hda_disk_image"] = image
config["images"]["hda_disk_image"] = image
empty_config.add_image(config)
assert empty_config._config["Qemu"]["vms"][0] == {
"adapter_type": "e1000",

View File

@ -31,7 +31,7 @@ def test_detect_image(linux_microcore_img):
registry = Registry()
detected = registry.detect_image(linux_microcore_img)
assert detected[0]["name"] == "Micro Core Linux"
assert detected[0]["hda_disk_image"].version == "3.4.1"
assert detected[0]["images"]["hda_disk_image"].version == "3.4.1"
def test_detect_unknow_image(empty_file):