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.admin_cm.farm
22 # @alldecoratedby{src.cm.utils.decorators.admin_cm_log}
23 # @author Tomek Sośnicki <tom.sosnicki@gmail.com>
24 #
25 
26 from cm.models.farm import Farm
27 from cm.models.vm import VM
28 from cm.utils import log
29 from cm.utils.decorators import admin_cm_log
30 from common.states import farm_states
31 
32 
33 @admin_cm_log(log=True)
34 ##
35 #
36 # @cmview_admin_cm
37 # @param_post{farm_id,int} id of the requested farm
38 # @response{dict} Farm.dict property of the requested Farm
39 #
40 def get_by_id(caller_id, farm_id):
41  return Farm.admin_get(farm_id).dict
42 
43 
44 @admin_cm_log(log=True)
45 ##
46 #
47 # Destroyes specified Farms. Neither Farm's head nor worker nodes are saved.
48 # Destroyed Farm cannot be recovered.
49 #
50 # @cmview_admin_cm
51 # @param_post{farm_ids,list(int)} ids of the Farms to destroy
52 #
53 # @response{list{HTTPResponse}} list of responses for each VM destruction
54 #
55 def destroy(caller_id, farm_ids):
56  farms = []
57  for farm_id in farm_ids:
58  farms.append(Farm.admin_get(farm_id))
59  return Farm.destroy(farms)
60 
61 
62 @admin_cm_log(log=False)
63 ##
64 #
65 # Returns farms belonging:
66 # - either to the User specified by @prm{user_id}
67 # - or to all Users if no @prm{user_id} is provided.
68 #
69 # @cmview_admin_cm
70 # @param_post{user_id,list} id of the requested Farm's owner
71 #
72 # @response{list(dict)} Farm.dict property for each Farm
73 #
74 def get_list(caller_id, user_id=False):
75  farms = Farm.objects.exclude(state=farm_states['closed']).order_by('-id')
76  if user_id:
77  farms = farms.filter(user__id__exact=caller_id)
78  return [farm.dict for farm in farms]
79 
80 
81 @admin_cm_log(log=True)
82 ##
83 #
84 # Saves and shutdowns specified Farm's head. Worker nodes are destroyed.
85 #
86 # @cmview_admin_cm
87 # @param_post{farm_id,int} id of the Farm to save
88 # @param_post{name,string} name which Farm's head should saved to
89 # @param_post{description,string} description for newly saved Farm
90 #
91 def save_and_shutdown(caller_id, farm_id, name, description):
92  farm = Farm.admin_get(farm_id)
93  return Farm.save_and_shutdown(farm, name, description)
94 
95 
96 @admin_cm_log(log=True)
97 ##
98 #
99 # Cleanes up after failed Farm. Only admin may erase Farm so that he
100 # previously may perform some analytics.
101 #
102 # @cmview_admin_cm
103 # @param_post{farm_ids,list(int)} ids of the Farms to erase
104 #
105 def erase(caller_id, farm_ids):
106  for fid in farm_ids:
107  farm = Farm.admin_get(fid)
108  for vm in farm.vms.all():
109  VM.erase(vm)
110 
111  farm.state = farm_states['closed']
112  try:
113  farm.save()
114  except Exception:
115  log.exception('Cannot commit changes.')
116