cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
iso_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.iso_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.iso_image import IsoImage
37 from common.states import image_states
38 from cm.utils.threads.image import CopyImage
39 
40 
41 @admin_cm_log(log=True)
42 ##
43 #
44 # Downloads Image with given path and saves it with specified name and
45 # description.
46 #
47 # @cmview_admin_cm
48 # @param_post{description,string}
49 # @param_post{name,string}
50 # @param_post{path,string} HTTP or FTP path to IsoImage
51 # @param_post{disk_dev}
52 # @param_post{disk_controller}
53 #
54 def download(caller_id, description, name, path, disk_dev, disk_controller):
55 
56  # size value is taken
57  try:
58  connection = urllib.urlopen(path)
59  size = int(connection.info()["Content-Length"])
60  except IOError:
61  log.exception('Cannot find image')
62  raise CMException('image_not_found')
63  except KeyError:
64  log.exception(caller_id, 'Cannot calculate size')
65  raise CMException('image_calculate_size')
66 
67  user = User.get(caller_id)
68 
69  image = IsoImage.create(name=name, description=description, user=user, disk_dev=disk_dev, disk_controller=disk_controller)
70 
71  try:
72  image.save()
73  except Exception, e:
74  log.error(caller_id, "Unable to save image to DB: %s" % str(e))
75  raise CMException('image_create')
76 
77  DownloadImage(image, path, size).start()
78 
79 
80 @admin_cm_log(log=False)
81 ##
82 #
83 # @cmview_admin_cm
84 # @response{list(dict)} IsoImage.dict property for each IsoImage
85 #
86 def get_list(caller_id):
87  # retrieve list of the type requested
88  images = IsoImage.objects.exclude(state=image_states['locked'])
89 
90  return [img.dict for img in images]
91 
92 
93 @admin_cm_log(log=True)
94 ##
95 #
96 # @cmview_admin_cm
97 # @param_post{iso_image_id,int} id of the IsoImage to get
98 #
99 # @response{dict} IsoImage.dict property for requested IsoImage
100 #
101 def get_by_id(caller_id, iso_image_id):
102  return IsoImage.admin_get(iso_image_id).dict
103 
104 
105 @admin_cm_log(log=True)
106 ##
107 #
108 # Sets IsoImage state to 'locked'.
109 #
110 # @cmview_admin_cm
111 # @param_post{iso_image_id} id of the IsoImage to delete
112 #
113 # @todo Should delete IsoImage and set its state to 'deleted'.
114 #
115 def delete(caller_id, iso_image_id):
116  image = IsoImage.admin_get(iso_image_id)
117 
118  image.check_attached()
119  image.state = image_states['locked']
120  image.save()
121 
122 
123 @admin_cm_log(log=True)
124 ##
125 #
126 # Updates specified IsoImage's attributes.
127 #
128 # @cmview_admin_cm
129 # @param_post{iso_image_id,string} new IsoImage name
130 # @param_post{name,string} new IsoImage name
131 # @param_post{description,string} new IsoImage description
132 # @param_post{disk_controller} new IsoImage controller optional
133 #
134 def edit(caller_id, iso_image_id, name=None, description=None, disk_controller=None):
135 
136  image = IsoImage.admin_get(iso_image_id)
137 
138  if image.state != image_states['ok']:
139  raise CMException('image_edit')
140 
141  if name:
142  image.name = name
143  if description:
144  image.description = description
145  if disk_controller:
146  image.disk_controller = disk_controller
147 
148  try:
149  image.save()
150  except:
151  raise CMException('image_edit')
152 
153 
154 @admin_cm_log(log=True)
155 ##
156 #
157 # Copies selected IsoImage to User's IsoImages pool.
158 #
159 # @cmview_admin_cm
160 # @param_post{src_image_id,int}
161 # @param_post{dest_user_id,int}
162 #
163 def copy(caller_id, src_image_id, dest_user_id):
164  src_image = IsoImage.admin_get(src_image_id)
165  dest_user = User.get(dest_user_id)
166  dest_image = IsoImage.create(name=src_image.name, description=src_image.description, user=dest_user,
167  disk_controller=src_image.disk_controller, disk_dev=src_image.disk_dev)
168 
169  try:
170  dest_image.save()
171  except Exception, e:
172  log.error(caller_id, "Unable to commit: %s" % str(e))
173  raise CMException('image_create')
174 
175  CopyImage(src_image, dest_image).start()
176