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.admin_cm.vm
22 # @alldecoratedby{src.cm.utils.decorators.admin_cm_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 admin_cm_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 
36 
37 @admin_cm_log(log=True)
38 ##
39 #
40 # Creates new VM with specified attributes.
41 #
42 # @cmview_admin_cm
43 # @param_post{name,string}
44 # @param_post{description,string}
45 # @param_post{image_id,int}
46 # @param_post{template_id,int}
47 # @param_post{public_ip_id,int}
48 # @param_post{iso_list,list(int)}
49 # @param_post{disk_list,list(int)}
50 # @param_post{vnc}
51 # @param_post{node_id}
52 #
53 def create(caller_id, name, description, image_id, template_id, public_ip_id, iso_list, disk_list, vnc, node_id):
54  user = User.get(caller_id)
55  vms = VM.create(user, name=name, description=description, image_id=image_id,
56  template_id=template_id, public_ip_id=public_ip_id, iso_list=iso_list, disk_list=disk_list,
57  vnc=vnc, groups=[], node_id=node_id)
58  for vm in vms:
59  thread = VMThread(vm, 'create')
60  thread.start()
61 
62  return [vm.dict for vm in vms]
63 
64 
65 @admin_cm_log(log=True)
66 ##
67 #
68 # Destroyes specified VMs. Destroyed VM can in no way be recovered.
69 #
70 # @cmview_admin_cm
71 # @param_post{vm_id_list,list} ids to destroy
72 #
73 # @response{src.cm.views.utils.image.destroy()}
74 #
75 def destroy(caller_id, vm_id_list):
76 
77  vms = []
78  for vm_id in vm_id_list:
79  vms.append(VM.admin_get(vm_id))
80  return VM.destroy(vms)
81 
82 
83 @admin_cm_log(log=True)
84 ##
85 #
86 # Cleans up after each of the specified VMs. Erase should be called for
87 # failed machines after the inspection of the failure.
88 #
89 # @cmview_admin_cm
90 # @param_post{vm_id_list,list} list of VM id's
91 #
92 def erase(caller_id, vm_id_list):
93 
94  for vm_id in vm_id_list:
95  vm = VM.admin_get(vm_id)
96  VM.erase(vm)
97 
98 
99 @admin_cm_log(log=True)
100 ##
101 #
102 # Saves and shutdowns specified VM, without checking User quota.
103 #
104 # @cmview_admin_cm
105 # @param_post{vm_id,string} id of the VM to save
106 # @param_post{name,string}
107 # @param_post{description,string}
108 #
109 def save_and_shutdown(caller_id, vm_id, name, description):
110 
111  vm = VM.admin_get(vm_id)
112  user = User.get(vm.user.id)
113 
114 # if user.used_storage + vm.system_image.size > user.storage:
115 # raise CMException('user_storage_limit')
116 
117  VM.save_and_shutdown(user.id, vm, name, description)
118 
119 
120 @admin_cm_log(log=False)
121 ##
122 #
123 # Returns list of VMs that are neither closed nor erased. If user_id is
124 # provided, only VMs belonging to that user are returned.
125 #
126 # @cmview_admin_cm
127 # @param_post{user_id,int}
128 #
129 # @response{dict} VM.long_dict property of each VM
130 #
131 def get_list(caller_id, user_id):
132 
133  vms = VM.objects.exclude(state__in=[vm_states['closed'], vm_states['erased']]).order_by('-id')
134 
135  # if parameter all is false, 'user_id' parameter have to be sent
136  if user_id:
137  vms = vms.filter(user__id__exact=user_id)
138 
139  return [vm.long_dict for vm in vms]
140 
141 
142 @admin_cm_log(log=False)
143 ##
144 #
145 # @cmview_admin_cm
146 # @param_post{vm_id} id of the requested VM
147 #
148 # @response{dict} VM with id @prm{id}
149 #
150 def get_by_id(caller_id, vm_id):
151  vm = VM.admin_get(vm_id)
152  vm_mod = vm.long_dict
153  # TODO: cpuload to be defined
154  # vm_mod['cpu_load'] = vm_utils.cpu_load(vm_mod)['data']
155  return vm_mod
156 
157 
158 @admin_cm_log(log=True)
159 ##
160 #
161 # Sends signal to reboot specified VMs. VM is not saved to SystemImage
162 # during reboot.
163 #
164 # @cmview_admin_cm
165 # @param_post{vm_id_list,(list(int))} ids of the VMs to restart
166 #
167 def restart(caller_id, vm_id_list):
168 
169  return VM.restart(vm_id_list)
170 
171 
172 @admin_cm_log(log=True)
173 ##
174 #
175 # Updates VM attributes.
176 #
177 # @cmview_admin_cm
178 # @param_post{vm_id}
179 # @param_post{name} (optional) new VM name
180 # @param_post{description} (optional) new VM description
181 #
182 # @response{src.cm.views.utils.vm.edit()}
183 #
184 def edit(caller_id, vm_id, name=None, description=None):
185 
186  vm = VM.admin_get(vm_id)
187 
188  if name:
189  vm.name = name
190  if description:
191  vm.description = description
192  vm.save()
193 
194 
195 @admin_cm_log(log=True)
196 ##
197 #
198 # Attaches VNC redirection to VM.
199 #
200 # @cmview_admin_cm
201 # @param_post{vm_id,int} id of the VM to have attached VM redirection
202 #
203 def attach_vnc(caller_id, vm_id):
204  vm = VM.admin_get(vm_id)
205  vm.attach_vnc()
206 
207  try:
208  vm.save()
209  except:
210  raise CMException('vnc_attach')
211 
212 
213 @admin_cm_log(log=True)
214 ##
215 #
216 # Detaches VNC redirection from VM.
217 #
218 # @cmview_admin_cm
219 # @param_post{vm_id,int} id of the VM to have detached VM redirection
220 #
221 def detach_vnc(caller_id, vm_id):
222  vm = VM.admin_get(vm_id)
223  vm.detach_vnc()
224 
225  try:
226  vm.save()
227  except:
228  raise CMException('vnc_detach')
229