cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
cluster.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.clm.views.admin_clm.cluster
22 #
23 # @alldecoratedby{src.clm.utils.decorators.admin_clm_log}
24 #
25 from clm.models.cluster import Cluster
26 from clm.models.user import User
27 from clm.utils import log
28 from clm.utils.cm import CM
29 from clm.utils.decorators import admin_clm_log
30 from clm.utils.exception import CLMException
31 from common.states import cluster_states, user_active_states
32 import socket
33 import re
34 
35 
36 @admin_clm_log(log=True)
37 ##
38 #
39 # Adds new Cluster and makes the caller its admin. There should dedicated
40 # and configured CM server exist.
41 #
42 # @clmview_admin_clm
43 # @param_post{name} human-friendly name of the new CM (shown in Web Interface)
44 # @param_post{address} address of the new CM
45 # @param_post{port,string} port on which CM is configured to be running
46 # @param_post{new_password} password protecting the new CM
47 #
48 def add(cm_id, caller_id, name, address, port, new_password):
49 
50  if not re.search('^[a-z0-9-]+$', name):
51  raise CLMException('cluster_invalid_name')
52 
53  try:
54  Cluster.objects.get(name=name)
55  raise CLMException('cluster_duplicate_name')
56  except Cluster.DoesNotExist:
57  pass
58 
59  cluster = Cluster()
60  cluster.address = address
61  cluster.port = port
62  cluster.name = name
63  cluster.state = cluster_states['ok']
64 
65  try:
66  cluster.save()
67  except:
68  raise CLMException('cluster_add')
69 
70  status = None
71 
72  # Get my ip for cluster
73  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
74  s.connect((address, port))
75  clm_ip = s.getsockname()[0]
76 
77  try:
78  status = CM(cluster.id).send_request('user/admin/first_admin_add/',
79  caller_id=caller_id, new_password=new_password, clm_address=clm_ip)['status']
80  except:
81  log.exception(caller_id, "Add cluster admin")
82  status = False
83 
84  if status != 'ok':
85  raise CLMException('cluster_add_admin')
86 
87  for user in User.objects.filter(default_cluster__exact=None):
88  user.default_cluster = cluster
89  try:
90  user.save()
91  except:
92  raise CLMException('cluster_add')
93 
94  status = None
95 
96  users = list(User.objects.values_list('id', flat=True))
97  try:
98  status = CM(cluster.id).send_request('user/user/add_missing/', caller_id=caller_id, remote=users)['status']
99  except Exception, e:
100  log.exception(caller_id, e)
101  status = False
102 
103  if status != 'ok':
104  raise CLMException('cluster_add_users')
105 
106 
107 @admin_clm_log(log=True)
108 ##
109 #
110 # Deletes specified Cluster from CLM database. Now no VMs can be
111 # run on that Cluster. It's not available for CLM anymore. To bring it back
112 # to Cloud resources one needs to add this Cluster once again ground up.
113 # Machine hosting CM deleted with this function keeps its configuration.
114 #
115 # @clmview_admin_clm
116 # @param_post{cluster_id,int} id of the CM to delete
117 #
118 def delete(cm_id, caller_id, cluster_id):
119  try:
120  cluster = Cluster.objects.get(pk=cluster_id)
121  cluster.delete()
122  except:
123  raise CLMException('cluster_delete')
124 
125 
126 @admin_clm_log(log=True)
127 ##
128 #
129 # Updates Cluster's attributes.
130 #
131 # @clmview_admin_clm
132 # @param_post{cluster_id,int} id of the CM to edit
133 # @param_post{name,string} new name for edited CM
134 # @param_post{address,string} new adress of the edited CM
135 # @param_post{port,int} new port on which edited CM is to be running
136 #
137 def edit(cm_id, caller_id, cluster_id, name=None, address=None, port=None,):
138  try:
139  cluster = Cluster.objects.get(pk=cluster_id)
140  if name:
141  cluster.name = name
142  if address:
143  cluster.address = address
144  if port:
145  cluster.port = port
146  cluster.save()
147  except:
148  raise CLMException('cluster_edit')
149 
150 
151 @admin_clm_log(log=True)
152 ##
153 #
154 # Locks specified Cluster. Since called, no VMs can be run on that Cluster,
155 # until unlock() is called.
156 #
157 # @clmview_admin_clm
158 # @param_post{cluster_id,int} id of the CM to lock
159 #
160 def lock(cm_id, caller_id, cluster_id):
161  cluster = Cluster.objects.get(pk=cluster_id)
162  cluster.state = cluster_states['locked']
163  try:
164  cluster.save()
165  except:
166  raise CLMException('cluster_lock')
167 
168 
169 @admin_clm_log(log=True)
170 ##
171 #
172 # Unlocks specified Cluster. Now VMs can be run on
173 # that Cluster.
174 #
175 # @clmview_admin_clm
176 # @param_post{cluster_id,int} id of the CM to unlock
177 #
178 def unlock(cm_id, caller_id, cluster_id):
179  try:
180  cluster = Cluster.objects.get(pk=cluster_id)
181  except:
182  raise CLMException('cluster_get')
183 
184  cluster.state = cluster_states['ok']
185  try:
186  cluster.save()
187  except:
188  raise CLMException('cluster_unlock')
189 
190  users = list(User.objects.filter(is_active__exact=user_active_states['ok']).values_list('id', flat=True))
191  status = None
192  try:
193  status = CM(cluster.id).send_request("user/user/add_missing/", caller_id=caller_id, remote=users)['status']
194  except Exception, e:
195  log.exception(caller_id, "Adding users: %s" % str(e))
196  status = False
197 
198  if status != 'ok':
199  cluster.state = cluster_states['locked']
200  try:
201  cluster.save()
202  except Exception:
203  raise CLMException('cluster_unlock')
204 
205  raise CLMException('cluster_unlock')
206 
207 
208 @admin_clm_log(log=False)
209 ##
210 #
211 # Requests specified Cluster's details.
212 # @clmview_admin_clm
213 # @param_post{cluster_id,int} id of the requested Cluster
214 #
215 # @response{dict} Cluster.dict property of requested Cluster
216 #
217 def get_by_id(cm_id, caller_id, cluster_id):
218  cluster = Cluster.objects.get(pk=cluster_id)
219  return cluster.dict
220 
221 
222 @admin_clm_log(log=False)
223 ##
224 #
225 # Requests list of Clusters.
226 # @clmview_admin_clm
227 # @response{list(dict)} Cluster.dict property of each registered Cluster
228 #
229 def get_list(cm_id, caller_id):
230  return [c.dict for c in Cluster.objects.all()]
231