cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
node.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.node
22 # @alldecoratedby{src.cm.utils.decorators.admin_cm_log}
23 #
24 # @author Tomek Sośnicki <tom.sosnicki@gmail.com>
25 #
26 from cm.models.node import Node
27 from cm.utils import log
28 from cm.utils.decorators import admin_cm_log
29 from cm.utils.exception import CMException
30 from common.states import node_states # , lease_states
31 from cm.tools import node as node_tools
32 
33 
34 @admin_cm_log(log=True)
35 ##
36 #
37 # Adds new Node to this Cluster. Node must be a machine preconfigured to be
38 # CC1 node.
39 #
40 # @cmview_admin_cm
41 # @param_post{username,string} Node's operating system username for
42 # transport. Should be @val{cc1}.
43 # @param_post{address,string} added Node IP adress or domain name
44 # @param_post{transport,string} @val{unix}, @val{ssh}, @val{tls} or other
45 # available transport name for hypervisor
46 # @param_post{suffix,string} optional suffix for transport (i.e. /system for KVM)
47 # @param_post{driver,string} hypervisior name (XEN, KVM or other, KVM is
48 # recommended)
49 # @param_post{cpu,int}
50 # @param_post{memory,int}
51 # @param_post{disk,int}
52 #
53 # @note Not used (but might be used one day)
54 #
55 def add(caller_id, address, username, transport, driver, suffix, cpu, memory, disk):
56  try:
57  node_tools.add(address, username, transport, driver, suffix, cpu, memory, disk)
58  except Exception, e:
59  log.error(caller_id, 'Cannot add node: %s' % str(e))
60  raise CMException(str(e))
61 
62 
63 @admin_cm_log(log=True)
64 ##
65 #
66 # @cmview_admin_cm
67 # @param_post{node_id,int} id of the Node where cc1-node should be deployed
68 # @param_post{distribution,string} OS distribution name, e.g. Debian
69 #
70 # @note Not used (but might be used one day)
71 #
72 def install(caller_id, node_id, distribution):
73  try:
74  node_tools.install(node_id, distribution)
75  except Exception, e:
76  log.error(caller_id, 'Cannot install node: %s' % str(e))
77  raise CMException(str(e))
78 
79 
80 @admin_cm_log(log=True)
81 ##
82 #
83 # @cmview_admin_cm
84 # @param_post{node_id,int} node id
85 # @param_post{interfaces,string list} list of interfaces, which node should
86 # use to communicate with other nodes and cm.
87 #
88 # @note Not used (but might be used one day)
89 #
90 def configure(caller_id, node_id, interfaces):
91  try:
92  node_tools.configure(node_id, interfaces)
93  except Exception, e:
94  log.error(caller_id, 'Cannot configure node: %s' % str(e))
95  raise CMException(str(e))
96 
97 
98 @admin_cm_log(log=True)
99 ##
100 #
101 # Tries to restart cc1-node service on each specified Node
102 #
103 # @cmview_admin_cm
104 # @param_post{node_id_list,list(int)}
105 #
106 # @note Not used (but might be used one day)
107 #
108 def check(caller_id, node_id_list):
109  try:
110  for node_id in node_id_list:
111  node_tools.check(node_id)
112  except Exception, e:
113  log.error(caller_id, 'Cannot check node: %s' % str(e))
114  raise CMException(str(e))
115 
116 
117 # returns list of added nodes
118 @admin_cm_log(log=False)
119 ##
120 #
121 # @cmview_admin_cm
122 # @response{list(dict)} Node.dict property for each Node added to current CM
123 #
124 def get_list(caller_id):
125  return [node.dict for node in Node.objects.exclude(state__exact=node_states['deleted'])]
126 
127 
128 @admin_cm_log(log=True)
129 ##
130 #
131 # Returns details of the requested Node.
132 #
133 # @cmview_admin_cm
134 # @param_post{node_id,int} id of the requested Node
135 # @response{dict} Node.long_dict property of the requested Node
136 #
137 def get_by_id(caller_id, node_id):
138  node = Node.get(caller_id, node_id) # use static method get by Node
139  return node.long_dict
140 
141 
142 @admin_cm_log(log=True)
143 ##
144 #
145 # Returns more details of the requested Node.
146 #
147 # @cmview_admin_cm
148 # @param_post{node_id,int} id of the requested Node
149 # @response{dict} Node.long_long_dict property of the requested Node
150 #
151 def get_by_id_details(caller_id, node_id):
152  node = Node.get(caller_id, node_id) # use static method get by Node
153  return node.long_long_dict
154 
155 
156 @admin_cm_log(log=True)
157 ##
158 #
159 # Sets specified Node's state as @val{locked}. No VMs can be run on locked Node.
160 #
161 # @cmview_admin_cm
162 # @param_post{node_id_list,int} list of the specified Nodes ids
163 #
164 # @response{None}
165 #
166 # @raises{node_lock,CMException}
167 #
168 def lock(caller_id, node_id_list):
169  for node_id in node_id_list:
170  node = Node.get(caller_id, node_id)
171  node.state = node_states['locked']
172 
173  try:
174  node.save()
175  # TODO:
176  # start_monia()
177  except:
178  raise CMException('node_lock')
179 
180 
181 @admin_cm_log(log=True)
182 ##
183 #
184 # Unlocks specified Node. After unlock Node's state is @val{ok} and Users
185 # are able to run VMs on that Node.
186 #
187 # @cmview_admin_cm
188 # @param_post{node_id_list,int} list of the specified Nodes ids
189 #
190 # @response{None}
191 #
192 # @raises{node_unlock,CMException}
193 #
194 def unlock(caller_id, node_id_list):
195 
196  for node_id in node_id_list:
197  node = Node.get(caller_id, node_id)
198  node.state = node_states['ok']
199 
200  try:
201  node.save()
202  # TODO:
203  # start_monia()
204  except:
205  raise CMException('node_unlock')
206 
207 
208 @admin_cm_log(log=True)
209 ##
210 #
211 # Deletes specified Node from database provided the Node does not host any
212 # VM's. Node's operating system setup isn't affected. To bring deleted Node
213 # back available for CC1 Cluster, one has to add it once again via Web
214 # Interface.
215 #
216 # @cmview_admin_cm
217 # @param_post{node_id,int} id of the Node to delete
218 #
219 # @raises{node_has_vms,CMException}
220 # @raises{node_delete,CMException}
221 #
222 def delete(caller_id, node_id):
223  node = Node.get(caller_id, node_id)
224 
225  # check if the node has vms
226  if node.vm_set.exists():
227  raise CMException('node_has_vms')
228 
229  try:
230  node.delete()
231  # TODO:
232  # start_monia() # odswiezenie listy nodow w monitoringu
233  except:
234  raise CMException('node_delete')
235 
236 
237 @admin_cm_log(log=True)
238 ##
239 #
240 # Updates Node attributes according to data provided in node_info.
241 #
242 # @cmview_admin_cm
243 # @param_post{node_id,int} id of the Node to edit
244 # @param_post{node_info,string} dictionary where cm.models.Node model's
245 # fields are the keys and values are values to set
246 #
247 # @raises{node_edit,CMException}
248 #
249 def edit(caller_id, node_id, **node_info):
250  node = Node.get(caller_id, node_id)
251 
252  # set the values sent in dictionary
253  for k, v in node_info.iteritems():
254  # if hasattr(node, k):
255  setattr(node, k, v)
256 
257  try:
258  node.save()
259  # TODO:
260  # start_monia() # odswiezenie listy nodow w monitoringu
261  except:
262  raise CMException('node_edit')
263