List images return images on compute and controller

Fix #506
This commit is contained in:
Julien Duponchelle committed 2016-06-08 14:14:03 +02:00
1 parent 08e482004f
commit add546158f
6 files changed
+105 -6

No files matched your search

+20
View File
@@ -19,9 +19,11 @@ import aiohttp
import asyncio
import json
import uuid
import os
import io
from ..utils import parse_version
from ..utils.images import scan_for_images
from ..controller.controller_error import ControllerError
from ..config import Config
from ..version import __version__
@@ -41,6 +43,7 @@ class ComputeConflict(aiohttp.web.HTTPConflict):
:param response: The response of the compute
"""
def __init__(self, response):
super().__init__(text=response["message"])
self.response = response
@@ -380,3 +383,20 @@ class Compute:
"""
res = yield from self.http_query(method, "/{}/{}".format(type, path), data=data, timeout=None)
return res.json
@asyncio.coroutine
def images(self, type):
"""
Return the list of images available for this type on controller
and on the compute node.
"""
images = []
res = yield from self.http_query("GET", "/{}/images".format(type), timeout=120)
images = res.json
for path in scan_for_images(type):
image = os.path.basename(path)
if image not in [i['filename'] for i in images]:
images.append({"filename": image, "path": image})
return images
@@ -77,6 +77,22 @@ class ComputeHandler:
response.set_status(200)
response.json(compute)
@Route.get(
r"/computes/{compute_id}/{emulator}/images",
parameters={
"compute_id": "Compute UUID"
},
status_codes={
200: "OK",
404: "Instance doesn't exist"
},
description="Return the list of images available on compute and controller for this emulator type")
def images(request, response):
controller = Controller.instance()
compute = controller.get_compute(request.match_info["compute_id"])
res = yield from compute.images(request.match_info["emulator"])
response.json(res)
@Route.get(
r"/computes/{compute_id}/{emulator}/{action:.+}",
parameters={
+21
View File
@@ -27,6 +27,27 @@ import logging
log = logging.getLogger(__name__)
def scan_for_images(type):
"""
Scan directories for available image for a type
:param type: emulator type (dynamips, qemu, iou)
"""
files = set()
paths = []
for directory in images_directories(type):
for root, _, filenames in os.walk(directory):
for file in filenames:
path = os.path.join(root, file)
if file not in files:
if (file.endswith(".image") and type == "dynamips") \
or (file.endswith(".bin") and type == "iou") \
or (not file.endswith(".bin") and not file.endswith(".image") and type == "qemu"):
files.add(file)
paths.append(path)
return paths
def images_directories(type):
"""
Return all directory where we will look for images