cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
admin.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.admin
22 #
23 # @alldecoratedby{src.cm.utils.decorators.admin_cm_log}
24 # @author Miłosz Zdybał <milosz.zdybal@ifj.edu.pl>
25 #
26 
27 from cm.utils.exception import CMException
28 from cm.utils.decorators import admin_cm_log, guest_log
29 from cm.models.admin import Admin
30 from cm.models.user import User
31 
32 import subprocess
33 
34 
35 @admin_cm_log(log=True)
36 ##
37 #
38 # Creates new admin of the cluster.
39 #
40 # @cmview_admin_cm
41 # @param_post{user_id,int} id of the User to gain CM Admin privileges
42 # @param_post{new_password,string} CM Admin password for User
43 #
44 def add(caller_id, user_id, new_password):
45  # verify if exists an user with the id given (which will become admin)
46  try:
47  user = User.objects.get(pk=user_id)
48  except:
49  raise CMException('admin_add')
50 
51  admin = Admin()
52  admin.user = user
53  admin.password = new_password
54 
55  try:
56  admin.save()
57  except:
58  raise CMException('admin_add')
59 
60 
61 @admin_cm_log(log=True)
62 ##
63 #
64 # Removes specified User with id @prm{cmadmin_id} from CM Admins.
65 #
66 # @cmview_admin_cm
67 # @param_post{user_id,int} id of the User to revoke CM admin privileges
68 #
69 def delete(caller_id, user_id):
70 
71  try:
72  admin = Admin.objects.get(pk=user_id)
73  admin.delete()
74  except:
75  raise CMException('admin_delete')
76 
77 
78 @admin_cm_log(log=True)
79 ##
80 #
81 # Method to change CM Admin password.
82 #
83 # @cmview_admin_cm
84 # @param_post{new_password,string} new password to set
85 #
86 def change_password(caller_id, new_password):
87 
88  try:
89  admin = Admin.objects.get(pk=caller_id)
90  admin.password = new_password
91  admin.save()
92  except:
93  raise CMException('admin_change_password')
94 
95 
96 @admin_cm_log(log=True)
97 ##
98 #
99 # Method returns list of the CM Admins.
100 #
101 # @cmview_admin_cm
102 # @response{list(int)} ids of the CM Admins
103 #
104 def list_admins(caller_id):
105  admins = []
106  for admin in Admin.objects.all():
107  admins.append(admin.user.id)
108  return admins
109 
110 
111 @admin_cm_log(log=True)
112 ##
113 #
114 # Method returns list of the CM Admins.
115 #
116 # @cmview_admin_cm
117 #
118 def restart(caller_id):
119  if subprocess.call(['/usr/sbin/cc1_cm_storage', 'mount']) != 0:
120  raise CMException('cm_restart')
121 
122  if subprocess.call(['/usr/sbin/cc1_cm_node', 'start']) != 0:
123  raise CMException('cm_restart')
124 
125  if subprocess.call(['/usr/sbin/cc1_cm_monitoring', 'start']) != 0:
126  raise CMException('cm_restart')
127 
128  if subprocess.call(['/usr/sbin/cc1_cm_vnc', 'start']) != 0:
129  raise CMException('cm_restart')
130 
131 
132 @guest_log(log=True)
133 ##
134 #
135 # @response{bool} True if caller is admin and False if not
136 #
137 def am_i_admin(caller_id):
138  return caller_id in [admin.user.id for admin in Admin.objects.all()]
139