cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
system_image.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 # @COPYRIGHT_begin
3 #
4 # Copyright [2010-2014] Institute of Nuclear Physics PAN, Krakow, Poland
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # @COPYRIGHT_end
19 
20 ##
21 # @package src.cm.views.admin_cm.system_image
22 # @alldecoratedby{src.cm.utils.decorators.user_log}
23 #
24 # @author Tomek Sośnicki <tom.sosnicki@gmail.com>
25 # @author Miłosz Zdybał <milosz.zdybal@ifj.edu.pl>
26 # @author Maciej Nabożny <mn@mnabozny.pl>
27 #
28 
29 import urllib
30 
31 from cm.utils.decorators import admin_cm_log
32 from cm.utils.exception import CMException
33 from cm.utils import log
34 from cm.utils.threads.image import DownloadImage
35 from cm.models.user import User
36 from cm.models.system_image import SystemImage
37 from cm.models.storage_image import StorageImage
38 from cm.models.system_image_group import SystemImageGroup
39 from common.states import image_access, image_states
40 from common.hardware import disk_filesystems, disk_controllers, video_devices, network_devices
41 import os
42 from cm.utils.threads.image import CopyImage
43 
44 
45 @admin_cm_log(log=True)
46 ##
47 #
48 # Downloads specified SystemImage.
49 #
50 # @cmview_admin_cm
51 # @param_post{description,string}
52 # @param_post{name,string}
53 # @param_post{path,string} HTTP or FTP path to image to download
54 # @param_post{disk_controller}
55 # @param_post{network_device}
56 # @param_post{platform}
57 # @param_post{video_device}
58 #
59 def download(caller_id, description, name, path, disk_controller, network_device, platform, video_device):
60 
61  # size value is taken
62  try:
63  connection = urllib.urlopen(path)
64  size = int(connection.info()["Content-Length"])
65  except IOError:
66  log.exception(caller_id, 'Cannot find image')
67  raise CMException('image_not_found')
68  except KeyError:
69  log.exception(caller_id, 'Cannot calculate size')
70  raise CMException('image_calculate_size')
71 
72  user = User.get(caller_id)
73 
74  image = SystemImage.create(name=name, description=description, user=user, platform=platform,
75  disk_controller=disk_controller, network_device=network_device,
76  video_device=video_device)
77 
78  try:
79  image.save()
80  except Exception, e:
81  log.error(caller_id, "Unable to save image to DB: %s" % str(e))
82  raise CMException('image_create')
83 
84  DownloadImage(image, path, size).start()
85 
86 
87 @admin_cm_log(log=False)
88 ##
89 #
90 # Returns images.
91 #
92 # @cmview_admin_cm
93 # @param_post{access} ( image_access['group'] | image_access['private'] | image_access['public'] , necessary for system and cd images)
94 # @param_post{group_id,list(int)} list of Group ids necessary when access is group, for system and cd images
95 #
96 # @response{list(dict)} SystemImage.dict property for each SystemImage.
97 #
98 def get_list(caller_id, access, group_id=None):
99  # retrieve list of the type requested
100  images = SystemImage.objects.exclude(state=image_states['locked']).filter(access=access)
101 
102  # if group access, we need 'group_id' parameter in 'data'
103  # list only images with group access that belong to the group of 'gid' given
104  if access == image_access['group']:
105  images = images.filter(systemimagegroup__group_id__in=group_id)
106 
107  return [img.dict for img in images]
108 
109 
110 @admin_cm_log(log=True)
111 ##
112 #
113 # @cmview_admin_cm
114 # @param_post{system_image_id,int} id of the Image to get
115 #
116 # @response{dict} extended information about specified Image
117 #
118 def get_by_id(caller_id, system_image_id):
119  return SystemImage.admin_get(system_image_id).dict
120 
121 
122 @admin_cm_log(log=True)
123 ##
124 #
125 # Sets SystemImage state as 'locked'.
126 #
127 # @cmview_admin_cm
128 # @param_post{system_image_id_list,list(int)} list of the specified Images ids
129 #
130 # @todo Should delete SystemImage and set its state to @val{deleted}.
131 #
132 def delete(caller_id, system_image_id_list):
133  for system_image_id in system_image_id_list:
134  image = SystemImage.admin_get(system_image_id)
135  image.state = image_states['locked']
136  image.save()
137 
138 
139 @admin_cm_log(log=True)
140 ##
141 #
142 # Sets Image's new attributes. Those should be get by src.cm.manager.image.get_by_id().
143 #
144 # @cmview_admin_cm
145 # @param_post{system_image_id,string} new Image name
146 # @param_post{name,string} new Image name
147 # @param_post{description,string} new Image description
148 # @param_post{disk_controller} new Image controller optional
149 # @param_post{video_device} new video device optional
150 # @param_post{network_device} new network device optional
151 # @param_post{platform} optional
152 #
153 def edit(caller_id, system_image_id, name=None, description=None, disk_controller=None, video_device=None, network_device=None, platform=None):
154 
155  image = SystemImage.admin_get(system_image_id)
156 
157  if image.state != image_states['ok']:
158  raise CMException('image_edit')
159 
160  if name:
161  image.name = name
162  if description:
163  image.description = description
164  if disk_controller:
165  image.disk_controller = disk_controller
166  if video_device:
167  image.video_device = video_device
168  if network_device:
169  image.network_device = network_device
170  if platform:
171  image.platform = platform
172  try:
173  image.save()
174  except:
175  raise CMException('image_edit')
176 
177 
178 @admin_cm_log(log=True)
179 ##
180 #
181 # Removes SystemImage from public pool.
182 #
183 # @cmview_admin_cm
184 # @param_post{system_image_id,int}
185 #
186 def set_private(caller_id, system_image_id):
187 
188  image = SystemImage.admin_get(system_image_id)
189  image.access = image_access['private']
190  # delete the existing group association
191  try:
192  image.save()
193  image.systemimagegroup_set.all().delete()
194  except:
195  log.exception(caller_id, 'image_set_private')
196  raise CMException('image_set_private')
197 
198 
199 @admin_cm_log(log=True)
200 ##
201 #
202 # Method sets specified Image access type as group (belonging to specified Group).
203 #
204 # @cmview_admin_cm
205 # @param_post{system_image_id,int}
206 # @param_post{group_id,int} id of the Group Image should belong to
207 #
208 # @response{None}
209 #
210 # @raises{image_set_group,CMException} cannot set group access type
211 #
212 def set_group(caller_id, system_image_id, group_id):
213  image = SystemImage.admin_get(system_image_id)
214  image.access = image_access['group']
215 
216  # create new group-image object
217  ig = SystemImageGroup()
218  ig.image = image
219  ig.group_id = group_id
220 
221  try:
222  ig.save()
223  except:
224  raise CMException('image_set_group')
225 
226 
227 @admin_cm_log(log=True)
228 ##
229 #
230 # Makes SystemImage available in public pool.
231 #
232 # @cmview_admin_cm
233 # @param_post{system_image_id,int}
234 #
235 def set_public(caller_id, system_image_id):
236 
237  image = SystemImage.admin_get(system_image_id)
238  image.access = image_access['public']
239  # delete the existing group association
240  try:
241  image.systemimagegroup_set.all().delete()
242  image.save()
243  except:
244  raise CMException('image_set_public')
245 
246 
247 @admin_cm_log(log=True)
248 ##
249 #
250 # Changes type of the given Image.
251 #
252 # @cmview_admin_cm
253 # @param_post{system_image_id,int} ID of an Image to change type of
254 #
255 # @response{None}
256 #
257 def convert_to_storage_image(caller_id, system_image_id):
258  image = SystemImage.admin_get(system_image_id)
259 
260  storage_image = StorageImage.create(name=image.name, description=image.description, user=image.user,
261  disk_controller=image.disk_controller)
262  storage_image.state = image_states['ok']
263  storage_image.size = image.size
264 
265  try:
266  storage_image.save()
267  os.rename(image.path, storage_image.path)
268  image.delete()
269  except Exception:
270  raise CMException('image_change_type')
271 
272 
273 @admin_cm_log(log=True)
274 ##
275 #
276 # @cmview_admin_cm
277 # @response{list(dict)} supported filesystems
278 #
279 def get_filesystems(caller_id):
280  return disk_filesystems
281 
282 
283 @admin_cm_log(log=True)
284 ##
285 #
286 # @cmview_admin_cm
287 # @response{list(dict)} video devices
288 #
289 def get_video_devices(caller_id):
290  return video_devices
291 
292 
293 @admin_cm_log(log=True)
294 ##
295 #
296 # @cmview_admin_cm
297 # @response{list(dict)} network devices
298 #
299 def get_network_devices(caller_id):
300  return network_devices
301 
302 
303 @admin_cm_log(log=True)
304 ##
305 #
306 # @cmview_admin_cm
307 # @response{list(dict)} disk controllers
308 #
309 def get_disk_controllers(caller_id):
310  return disk_controllers
311 
312 
313 @admin_cm_log(log=True)
314 ##
315 #
316 # Copy selected image to user's images
317 #
318 # @cmview_admin_cm
319 # @param_post{src_image_id,int}
320 # @param_post{dest_user_id,int}
321 #
322 def copy(caller_id, src_image_id, dest_user_id):
323  src_image = SystemImage.admin_get(src_image_id)
324  dest_user = User.get(dest_user_id)
325  dest_image = SystemImage.create(name=src_image.name, description=src_image.description, user=dest_user,
326  platform=src_image.platform, disk_controller=src_image.disk_controller,
327  network_device=src_image.network_device, video_device=src_image.video_device)
328 
329  try:
330  dest_image.save()
331  except Exception, e:
332  log.error(caller_id, "Unable to commit: %s" % str(e))
333  raise CMException('image_create')
334 
335  CopyImage(src_image, dest_image).start()
336