31 from cm.utils.decorators
import user_log
32 from cm.utils.exception
import CMException
33 from cm.utils
import log
34 from cm.utils.threads.image
import DownloadImage, CreateImage
35 from cm.models.user
import User
36 from cm.models.storage_image
import StorageImage
37 from cm.models.vm
import VM
38 from common.states
import image_states
39 from common.hardware
import disk_controllers, disk_filesystems, live_attach_disk_controllers
59 def create(caller_id, name, description, filesystem, size, disk_controller):
61 raise CMException(
'image_invalid_size')
63 user = User.get(caller_id)
64 user.check_storage(size)
65 image = StorageImage.create(user=user, disk_controller=disk_controller, description=description, name=name,
71 log.error(caller_id,
"Unable to save image to DB: %s" % str(e))
72 raise CMException(
'image_create')
74 CreateImage(image, filesystem).
start()
89 def download(caller_id, name, description, path, disk_controller):
90 user = User.get(caller_id)
92 if path.startswith(
'/'):
93 size = os.path.getsize(path.strip())
95 if not any([path.startswith(
'http://'), path.startswith(
'https://'), path.startswith(
'ftp://')]):
96 path =
'http://' + path.strip()
100 connection = urllib.urlopen(path)
101 size = int(connection.info()[
"Content-Length"])
103 log.exception(caller_id,
'Cannot find image')
104 raise CMException(
'image_not_found')
106 log.exception(caller_id,
'Cannot calculate size')
107 raise CMException(
'image_calculate_size')
109 user.check_storage(size / (1024 * 1024))
111 image = StorageImage.create(name=name, description=description, user=user, disk_controller=disk_controller)
116 log.error(caller_id,
"Unable to save image to DB: %s" % str(e))
117 raise CMException(
'image_create')
119 DownloadImage(image, path, size).
start()
133 images = StorageImage.objects.exclude(state=image_states[
'locked']).filter(user__id=caller_id)
134 return [img.dict
for img
in images]
146 return StorageImage.get(caller_id, storage_image_id).dict
158 def delete(caller_id, storage_image_ids):
161 for storage_image_id
in storage_image_ids:
162 image = StorageImage.get(caller_id, storage_image_id)
164 if image.state != image_states[
'ok']:
165 results.append({
'status':
'image_delete',
'data':
''})
168 image.check_attached()
170 subprocess.call([
'rm', image.path])
172 results.append({
'status':
'image_delete',
'data':
''})
174 image.state = image_states[
'locked']
176 results.append({
'status':
'ok',
'data':
''})
192 def edit(caller_id, storage_image_id, name=None, description=None, disk_controller=None):
194 image = StorageImage.get(caller_id, storage_image_id)
196 if not image.state
in [image_states[
'ok'], image_states[
'adding']]:
197 raise CMException(
'image_edit')
199 image.name = name
or image.name
200 image.description = description
or image.description
201 image.disk_controller = disk_controller
or image.disk_controller
204 image.save(update_fields=[
'name',
'description',
'disk_controller'])
206 raise CMException(
'image_edit')
220 def attach(caller_id, storage_image_id, vm_id):
221 vm = VM.get(caller_id, vm_id)
222 disk = StorageImage.get(caller_id, storage_image_id)
226 raise CMException(
'image_attached')
233 raise CMException(
'storage_image_attach')
245 def detach(caller_id, storage_image_id, vm_id):
246 vm = VM.get(caller_id, vm_id)
247 disk = StorageImage.get(caller_id, storage_image_id)
254 raise CMException(
'storage_image_attach')
271 image = StorageImage.get(caller_id, storage_image_id)
272 image.platform = platform
273 image.disk_controller = disk_controller
274 image.network_device = network_device
275 image.video_device = video_device
278 image.recast(
'cm.systemimage')
281 raise CMException(
'image_change_type')
291 return disk_filesystems
301 return [{
'id': ctrl_id,
303 'live_attach': (name
in live_attach_disk_controllers)}
304 for name, ctrl_id
in disk_controllers.iteritems()]