summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--nova/api/ec2/cloud.py2
-rw-r--r--nova/image/local.py64
-rw-r--r--nova/objectstore/image.py3
3 files changed, 46 insertions, 23 deletions
diff --git a/nova/api/ec2/cloud.py b/nova/api/ec2/cloud.py
index 8c2e77d86..aa1dcbe33 100644
--- a/nova/api/ec2/cloud.py
+++ b/nova/api/ec2/cloud.py
@@ -853,7 +853,7 @@ class CloudController(object):
i['imageLocation'] = image['properties'].get('image_location')
i['imageState'] = image['properties'].get('image_state')
i['type'] = image.get('type')
- i['isPublic'] = image['properties'].get('is_public') == 'True'
+ i['isPublic'] = str(image['properties'].get('is_public', '')) == 'True'
i['architecture'] = image['properties'].get('architecture')
return i
diff --git a/nova/image/local.py b/nova/image/local.py
index f78b9aa89..b4616729a 100644
--- a/nova/image/local.py
+++ b/nova/image/local.py
@@ -15,57 +15,81 @@
# License for the specific language governing permissions and limitations
# under the License.
-import cPickle as pickle
+import json
import os.path
import random
-import tempfile
+import shutil
+from nova import flags
from nova import exception
from nova.image import service
-class LocalImageService(service.BaseImageService):
+FLAGS = flags.FLAGS
+flags.DEFINE_string('images_path', '$state_path/images',
+ 'path to decrypted images')
+class LocalImageService(service.BaseImageService):
"""Image service storing images to local disk.
+
It assumes that image_ids are integers.
"""
def __init__(self):
- self._path = tempfile.mkdtemp()
+ self._path = FLAGS.images_path
- def _path_to(self, image_id):
+ def _path_to(self, image_id, fname='info.json'):
+ if fname:
+ return os.path.join(self._path, str(image_id), fname)
return os.path.join(self._path, str(image_id))
def _ids(self):
"""The list of all image ids."""
- return [int(i) for i in os.listdir(self._path)]
+ return [int(i) for i in os.listdir(self._path)
+ if unicode(i).isnumeric()]
def index(self, context):
- return [dict(id=i['id'], name=i['name']) for i in self.detail(context)]
+ return [dict(image_id=i['id'], name=i.get('name'))
+ for i in self.detail(context)]
def detail(self, context):
- return [self.show(context, id) for id in self._ids()]
-
- def show(self, context, id):
+ images = []
+ for image_id in self._ids():
+ try:
+ image = self.show(context, image_id)
+ images.append(image)
+ except exception.NotFound:
+ continue
+ return images
+
+ def show(self, context, image_id):
try:
- return pickle.load(open(self._path_to(id)))
+ with open(self._path_to(image_id)) as metadata_file:
+ return json.load(metadata_file)
except IOError:
raise exception.NotFound
- def create(self, context, data):
+ def create(self, context, metadata, data=None):
"""Store the image data and return the new image id."""
- id = random.randint(0, 2 ** 31 - 1)
- data['id'] = id
- self.update(context, id, data)
- return id
+ image_id = random.randint(0, 2 ** 31 - 1)
+ image_path = self._path_to(image_id, None)
+ if not os.path.exists(image_path):
+ os.mkdir(image_path)
+ return self.update(context, image_id, metadata, data)
- def update(self, context, image_id, data):
+ def update(self, context, image_id, metadata, data=None):
"""Replace the contents of the given image with the new data."""
+ metadata['id'] = image_id
try:
- pickle.dump(data, open(self._path_to(image_id), 'w'))
+ with open(self._path_to(image_id), 'w') as metadata_file:
+ json.dump(metadata, metadata_file)
+ if data:
+ with open(self._path_to(image_id, 'image'), 'w') as image_file:
+ shutil.copyfileobj(data, image_file)
except IOError:
raise exception.NotFound
+ return metadata
def delete(self, context, image_id):
"""Delete the given image.
@@ -79,8 +103,8 @@ class LocalImageService(service.BaseImageService):
def delete_all(self):
"""Clears out all images in local directory."""
- for id in self._ids():
- os.unlink(self._path_to(id))
+ for image_id in self._ids():
+ os.unlink(self._path_to(image_id))
def delete_imagedir(self):
"""Deletes the local directory.
diff --git a/nova/objectstore/image.py b/nova/objectstore/image.py
index 27227e2ca..8013cbd9c 100644
--- a/nova/objectstore/image.py
+++ b/nova/objectstore/image.py
@@ -37,8 +37,7 @@ from nova.objectstore import bucket
FLAGS = flags.FLAGS
-flags.DEFINE_string('images_path', '$state_path/images',
- 'path to decrypted images')
+flags.DECLARE('images_path', 'nova.image.local')
class Image(object):