31 from cm.models.iso_image
import IsoImage
32 from cm.models.user
import User
33 from cm.models.vm
import VM
34 from cm.utils
import log
35 from cm.utils.decorators
import user_log
36 from cm.utils.exception
import CMException
37 from cm.utils.threads.image
import DownloadImage
38 from common.hardware
import disk_controllers
39 from common.states
import image_states
54 def download(caller_id, name, description, path, disk_controller):
55 user = User.get(caller_id)
57 if not any([path.startswith(
'http://'), path.startswith(
'https://'), path.startswith(
'ftp://')]):
58 path =
'http://' + path.strip()
62 connection = urllib.urlopen(path)
63 size = int(connection.info()[
"Content-Length"])
65 log.exception(
'Cannot find image')
66 raise CMException(
'image_not_found')
68 log.exception(caller_id,
'Cannot calculate size')
69 raise CMException(
'image_calculate_size')
71 user.check_storage(size / (1024 * 1024))
73 image = IsoImage.create(user=user, description=description, name=name, disk_controller=disk_controller, disk_dev=1)
78 log.error(caller_id,
"Unable to save image to DB: %s" % str(e))
79 raise CMException(
'image_create')
81 DownloadImage(image, path, size).
start()
95 images = IsoImage.objects.exclude(state=image_states[
'locked']).filter(user__id=caller_id)
97 return [img.dict
for img
in images]
111 return IsoImage.get(caller_id, iso_image_id).dict
125 for iso_image_id
in iso_image_ids:
126 image = IsoImage.get(caller_id, iso_image_id)
128 if image.state != image_states[
'ok']:
129 results.append({
'status':
'image_delete',
'data':
''})
132 subprocess.call([
'rm', image.path])
134 results.append({
'status':
'image_delete',
'data':
''})
137 image.check_attached()
138 image.state = image_states[
'locked']
140 results.append({
'status':
'ok',
'data':
''})
156 def edit(caller_id, iso_image_id, name=None, description=None, disk_controller=None):
158 image = IsoImage.get(caller_id, iso_image_id)
160 if image.state != image_states[
'ok']:
161 raise CMException(
'image_edit')
166 image.description = description
168 image.disk_controller = disk_controller
172 raise CMException(
'image_edit')
187 def attach(caller_id, iso_image_id, vm_id):
189 vm = VM.get(caller_id, vm_id)
190 disk = IsoImage.get(caller_id, iso_image_id)
194 raise CMException(
'image_attached')
201 raise CMException(
'iso_image_attach')
215 def detach(caller_id, iso_image_id, vm_id):
216 vm = VM.get(caller_id, vm_id)
217 disk = IsoImage.get(caller_id, iso_image_id)
224 raise CMException(
'iso_image_attach')
234 return [{
'id': ctrl_id,
'name': name}
for name, ctrl_id
in disk_controllers.iteritems()]