cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
vm.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.user.vm
22 # @alldecoratedby{src.cm.utils.decorators.user_log}
23 #
24 # @author Tomek Sośnicki <tom.sosnicki@gmail.com>
25 # @author Maciej Nabożny <di.dijo@gmail.com>
26 # @author Miłosz Zdybał <milosz.zdybal@ifj.edu.pl>
27 #
28 
29 from common.states import vm_states
30 from cm.utils.decorators import user_log
31 from cm.utils.exception import CMException
32 from cm.models.user import User
33 from cm.models.vm import VM
34 from cm.utils.threads.vm import VMThread
35 from cm.utils import message
36 
37 
38 @user_log(log=True)
39 ##
40 #
41 # Creates virtual machines.
42 #
43 # @cmview_user
44 # @param_post{name,string}
45 # @param_post{description,string}
46 # @param_post{image_id,int}
47 # @param_post{template_id,int}
48 # @param_post{public_ip_id,int}
49 # @param_post{iso_list,list(int)} ISOs' ids
50 # @param_post{disk_list,list(int)}
51 # @param_post{vnc}
52 # @param_post{count}
53 # @param_post{groups}
54 # @param_post{user_data} data accessible via ec2ctx
55 # @param_post{ssh_key}
56 # @param_post{ssh_username}
57 #
58 # @returns @asreturned{src.cm.views.utils.vm.create()}
59 #
60 def create(caller_id, name, description, image_id, template_id, public_ip_id, iso_list, disk_list, vnc, groups, count=1, user_data=None,
61  ssh_key=None, ssh_username=None):
62  user = User.get(caller_id)
63  try:
64  user.check_points()
65  except:
66  message.warn(caller_id, 'point_limit', {'used_points': user.used_points, 'point_limit': user.points})
67  vms = VM.create(user, name=name, description=description, image_id=image_id,
68  template_id=template_id, public_ip_id=public_ip_id, iso_list=iso_list, disk_list=disk_list,
69  vnc=vnc, groups=groups, count=count, user_data=user_data, ssh_key=ssh_key, ssh_username=ssh_username)
70 
71  for vm in vms:
72  thread = VMThread(vm, 'create')
73  thread.start()
74 
75  return [vm.dict for vm in vms]
76 
77 
78 @user_log(log=True)
79 ##
80 #
81 # This function only destroys VM. All the cleanup (removing disk, saving,
82 # rescuing resources, ...) is done by hook through
83 # \c contextualization.update_vm method (yeah, intuitive).
84 #
85 # Simple sequence diagram:
86 #
87 # @code
88 # CLM CM CTX Node (HOOK)
89 # .
90 # Destroy -->destroy
91 # | | (LV.destroy)
92 # | |------------------------->HookScript
93 # . . |
94 # . . ctx.update_vm<--|
95 # . . | |
96 # . . |------------->cp
97 # . . |------------->rm
98 # . . update_resources
99 # @endcode
100 #
101 # @cmview_user
102 # @param_post{vm_ids,list} list of virtual machines' ids
103 #
104 # @response{list(dict)} VM.destroy() retval
105 #
106 def destroy(caller_id, vm_ids):
107  vms = []
108  for vm_id in vm_ids:
109  vms.append(VM.get(caller_id, vm_id))
110  return VM.destroy(vms)
111 
112 
113 @user_log(log=True)
114 ##
115 #
116 # Calls VM.save_and_shutdown() on specified VM
117 #
118 # @cmview_user
119 # @param_post{vm_id,int} id of the VM to save and shutdown.
120 # @param_post{name,string} name of the new SystemImage VM should be saved to
121 # @param_post{description,string} description of the new SystemImage VM
122 # should be saved to
123 #
124 def save_and_shutdown(caller_id, vm_id, name, description):
125  user = User.get(caller_id)
126  vm = VM.get(caller_id, vm_id)
127 
128  if user.used_storage + vm.system_image.size > user.storage:
129  raise CMException('user_storage_limit')
130 
131  VM.save_and_shutdown(caller_id, vm, name, description)
132 
133 
134 @user_log(log=False, pack=False)
135 ##
136 #
137 # Returns caller's VMs.
138 #
139 # @cmview_user
140 # @response{list(dict)} VM.dict property of all caller's VMs
141 #
142 def get_list(caller_id):
143 
144  vms = VM.objects.exclude(state__in=[vm_states['closed'], vm_states['erased']]).filter(user__id__exact=caller_id)\
145  .filter(farm=None).order_by('-id')
146  vms_mod = [vm.dict for vm in vms]
147  return vms_mod
148 
149 
150 @user_log(log=False)
151 ##
152 #
153 # Returns requested caller's VM.
154 #
155 # @cmview_user
156 # @param_post{vm_id,int} id of the requested VM
157 #
158 # @response{dict} VM.dict property of the requested VM
159 #
160 def get_by_id(caller_id, vm_id):
161  vm = VM.get(caller_id, vm_id)
162  vm_mod = vm.long_dict
163  # TODO: cpuload to be defined
164  # vm_mod['cpu_load'] = vm_utils.cpu_load(vm_mod)['data']
165  return vm_mod
166 
167 
168 @user_log(log=True)
169 ##
170 #
171 # Safely restarts selected callers VMs
172 #
173 # @cmview_user
174 # @param_post{vm_ids,list(int)} ids of the VMs to restart
175 #
176 # @response{src.cm.views.utils.image.restart()}
177 #
178 def reset(caller_id, vm_ids):
179 
180  # get to check permissions on vms
181  vms = []
182  for vm_id in vm_ids:
183  vms.append(VM.get(caller_id, vm_id))
184 
185  return VM.reset(vms)
186 
187 
188 @user_log(log=True)
189 ##
190 #
191 # Updates VM's attributes.
192 #
193 # @cmview_user
194 # @param_post{vm_id,int} id of the VM to edit
195 # @param_post{name,string}
196 # @param_post{description,string}
197 #
198 # @response{src.cm.views.utils.image.edit()}
199 #
200 def edit(caller_id, vm_id, name=None, description=None):
201  vm = VM.get(caller_id, vm_id)
202 
203  vm.name = name or vm.name
204  vm.description = description or vm.description
205 
206  vm.save(update_fields=['name', 'description'])
207 
208 
209 @user_log(log=True)
210 ##
211 #
212 # Attaches VNC redirection to VM.
213 #
214 # @cmview_user
215 # @param_post{vm_id,int} id of the VM to have attached VM redirection
216 #
217 def attach_vnc(caller_id, vm_id):
218  vm = VM.get(caller_id, vm_id)
219  vm.attach_vnc()
220 
221  try:
222  vm.save()
223  except:
224  raise CMException('vnc_attach')
225 
226 
227 @user_log(log=True)
228 ##
229 #
230 # Detaches VNC redirection from VM.
231 #
232 # @cmview_user
233 # @param_post{vm_id,int} id of the VM to have detached VM redirection
234 #
235 def detach_vnc(caller_id, vm_id):
236  vm = VM.get(caller_id, vm_id)
237  vm.detach_vnc()
238 
239  try:
240  vm.save()
241  except:
242  raise CMException('vnc_detach')
243