cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
farm.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.farm
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 cm.models.farm import Farm
30 from cm.models.node import Node
31 from cm.models.template import Template
32 from cm.models.user import User
33 from cm.models.vm import VM
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.vm import VMThread
38 from common.states import farm_states, node_states
39 from cm.utils import message
40 
41 
42 @user_log(log=True)
43 ##
44 #
45 # Returns requested Farm.
46 #
47 # @cmview_user
48 # @param_post{farm_id,int} id of the requested Farm
49 #
50 # @response{dict} Farm.dict property of the requested Farm
51 #
52 def get_by_id(caller_id, farm_id):
53  return Farm.get(caller_id, farm_id).dict
54 
55 
56 @user_log(log=True)
57 ##
58 #
59 # Method creates new caller's Farm.
60 #
61 # @cmview_user
62 # @param_post{name,string} Farm's name
63 # @param_post{description,string}
64 # @param_post{image_id,int} image for WNs and Head
65 # @param_post{head_template_id,int} Head's template
66 # @param_post{worker_template_id,int} Worker Node's template
67 # @param_post{public_ip_id,int} Worker Node's template
68 # @param_post{iso_list,list}
69 # @param_post{disk_list,list}
70 # @param_post{vnc,list}
71 # @param_post{groups,list}
72 # @param_post{count,int} number of Worker Nodes
73 #
74 # @raises{farm_create,CMException}
75 #
76 def create(caller_id, name, description, image_id, head_template_id, worker_template_id, public_ip_id, iso_list, disk_list, vnc, groups, count):
77  user = User.get(caller_id)
78  try:
79  user.check_points()
80  except:
81  message.warn(caller_id, 'point_limit', {'used_points': user.used_points, 'point_limit': user.points})
82 
83  farm = Farm.create(user=user, name=name, description=description)
84 
85  vms = VM.create(user, name=name, description=description, image_id=image_id, template_id=worker_template_id,
86  head_template_id=head_template_id, public_ip_id=public_ip_id, iso_list=iso_list, disk_list=disk_list,
87  vnc=vnc, groups=groups, farm=farm, count=count)
88 
89  farm.save()
90  for vm in vms:
91  vm.farm = farm
92  if not vm.is_head():
93  # disable autosave
94  vm.save_vm = 0
95  vm.save()
96 
97  try:
98  farm.save()
99  except Exception:
100  log.exception(caller_id, 'farm_create')
101  raise CMException('farm_create')
102 
103  VMThread(vms[0], 'create').start()
104  return [vm.dict for vm in vms]
105 
106 
107 @user_log(log=True)
108 ##
109 #
110 # Destroys specified caller's farm.
111 #
112 # @cmview_user
113 # @param_post{farm_id,int} destroyed farm's id
114 #
115 # @response @asreturned{src.cm.manager.farm.utils.destroy()}
116 #
117 def destroy(caller_id, farm_id):
118 
119  return Farm.destroy([Farm.get(caller_id, farm_id)])
120 
121 
122 @user_log(log=False, pack=False)
123 ##
124 #
125 # Returns list of the caller's farms.
126 #
127 # @cmview_user
128 # @response{list(dict)} data of the requested Farms
129 #
130 def get_list(caller_id):
131  farms = [farm.dict for farm in Farm.objects.exclude(state=farm_states['closed']).filter(user__id__exact=caller_id).order_by('-id')]
132  # for farm in farms:
133  # for vm in farm['vms']:
134  # vm['cpu_load'] = VM.cpu_load(vm)['data']
135 
136  return farms
137 
138 
139 @user_log(log=True)
140 ##
141 #
142 # Safely saves and shutdowns Farm's Head.
143 #
144 # @cmview_user
145 # @param_post{farm_id,int}
146 # @param_post{name,string}
147 # @param_post{description,string}
148 #
149 # @response{src.cm.manager.farm.utils.save_and_shutdown()}
150 #
151 def save_and_shutdown(caller_id, farm_id, name, description):
152  farm = Farm.get(caller_id, farm_id)
153  if farm.user.id == caller_id:
154  return Farm.save_and_shutdown(farm, name, description)
155  else:
156  raise CMException("farm_save")
157 
158 
159 @user_log(log=False)
160 ##
161 #
162 # Checks if there is enough resources to start a farm
163 #
164 # @cmview_user
165 # @param_post{count,int}
166 # @param_post{head_template_id,int}
167 # @param_post{template_id,int}
168 #
169 # @response{Boolean}
170 #
171 def check_resources(caller_id, count, head_template_id, template_id):
172 
173  wn_template = Template.get(template_id)
174  available = 0
175  for node in list(Node.objects.filter(state=node_states['ok'])):
176  available += node.cpu_free / wn_template.cpu
177 
178  return available >= count + 1
179