31 from cm.models.system_image
import SystemImage
32 from cm.models.system_image_group
import SystemImageGroup
33 from cm.models.user
import User
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_filesystems, disk_controllers, video_devices, \
40 from common.states
import image_access, image_states
66 def create(caller_id, name, description, size, disk_controller, network_device, platform, video_device):
70 user = User.get(caller_id)
71 user.check_storage(size / (1024 * 1024))
73 image = SystemImage.create(name=name, description=description, user=user, platform=platform,
74 disk_controller=disk_controller, network_device=network_device, video_device=video_device)
79 log.error(caller_id,
"Unable to save image to DB: %s" % str(e))
80 raise CMException(
'image_create')
82 return {
'system_image_id': image.id}
100 if not any([path.startswith(
'http://'), path.startswith(
'https://'), path.startswith(
'ftp://')]):
101 path =
'http://' + path.strip()
105 connection = urllib.urlopen(path)
106 size = int(connection.info()[
"Content-Length"])
108 log.exception(caller_id,
'Cannot find image')
109 raise CMException(
'image_not_found')
111 log.exception(caller_id,
'Cannot calculate size')
112 raise CMException(
'image_calculate_size')
114 image = SystemImage.get(caller_id, system_image_id)
116 DownloadImage(image, path, size).
start()
139 def download(caller_id, name, description, path, disk_controller, network_device, platform, video_device):
140 if not any([path.startswith(
'http://'), path.startswith(
'https://'), path.startswith(
'ftp://')]):
141 path =
'http://' + path.strip()
145 connection = urllib.urlopen(path)
146 size = int(connection.info()[
"Content-Length"])
148 log.exception(caller_id,
'Cannot find image')
149 raise CMException(
'image_not_found')
151 log.exception(caller_id,
'Cannot calculate size')
152 raise CMException(
'image_calculate_size')
154 user = User.get(caller_id)
155 user.check_storage(size / (1024 * 1024))
157 image = SystemImage.create(name=name, description=description, user=user, platform=platform,
158 disk_controller=disk_controller, network_device=network_device, video_device=video_device)
163 log.error(caller_id,
"Unable to save image to DB: %s" % str(e))
164 raise CMException(
'image_create')
166 DownloadImage(image, path, size).
start()
183 images = SystemImage.objects.exclude(state=image_states[
'locked']).filter(access=access)
186 if access == image_access[
'private']:
187 images = images.filter(user__id__exact=caller_id)
191 if access == image_access[
'group']:
192 images = images.filter(systemimagegroup__group_id__in=group_id)
194 return [img.dict
for img
in images]
207 return SystemImage.get(caller_id, system_image_id, groups).dict
221 for system_image_id
in system_image_ids:
222 image = SystemImage.get(caller_id, system_image_id)
224 if image.state != image_states[
'ok']:
225 results.append({
'status':
'image_delete',
'data':
''})
229 subprocess.call([
'rm', image.path])
231 results.append({
'status':
'image_delete',
'data':
''})
234 image.state = image_states[
'locked']
236 results.append({
'status':
'ok',
'data':
''})
255 def edit(caller_id, system_image_id, name=None, description=None, disk_controller=None, video_device=None, network_device=None, platform=None):
257 image = SystemImage.get(caller_id, system_image_id)
259 if image.state != image_states[
'ok']:
260 raise CMException(
'image_edit')
262 image.name = name
or image.name
263 image.description = description
or image.description
264 image.disk_controller = disk_controller
or image.disk_controller
265 image.video_device = video_device
or image.video_device
266 image.network_device = network_device
or image.network_device
267 image.platform = platform
or image.platform
272 raise CMException(
'image_edit')
289 image = SystemImage.get(caller_id, system_image_id, leader_groups)
290 image.access = image_access[
'private']
294 image.systemimagegroup_set.all().
delete()
296 log.exception(caller_id,
'image_set_private')
297 raise CMException(
'image_set_private')
313 image = SystemImage.get(caller_id, system_image_id)
314 image.access = image_access[
'group']
317 ig = SystemImageGroup()
319 ig.group_id = group_id
325 raise CMException(
'image_set_group')
337 image = SystemImage.get(caller_id, system_image_id)
339 image.recast(
'cm.storageimage')
342 log.exception(caller_id,
"convert_to_storage_image")
343 raise CMException(
'image_change_type')
353 return disk_filesystems
373 return network_devices
383 return [{
'id': ctrl_id,
'name': name}
for name, ctrl_id
in disk_controllers.iteritems()]