| [11069] | 1 | |
|---|
| [11017] | 2 | from django.shortcuts import render_to_response |
|---|
| [11073] | 3 | from django.http import HttpResponse, Http404 |
|---|
| [11017] | 4 | from django.template import RequestContext |
|---|
| 5 | from paramiko import SSHClient, AutoAddPolicy |
|---|
| [11071] | 6 | from models import * |
|---|
| [11092] | 7 | from WebDash.views import index2 |
|---|
| [11017] | 8 | |
|---|
| [11100] | 9 | """ |
|---|
| 10 | Views for DomainMan application |
|---|
| [11086] | 11 | Starts and stops nodeBooter/naming service through paramiko |
|---|
| [11069] | 12 | """ |
|---|
| [11070] | 13 | |
|---|
| 14 | """ |
|---|
| [11103] | 15 | Checks if running locally or on the web by testing which Django settings are used |
|---|
| 16 | """ |
|---|
| [11098] | 17 | def isOnWeb(): |
|---|
| 18 | if environ['DJANGO_SETTINGS_MODULE'] == 'cornetApps.settingsWeb': |
|---|
| 19 | return 1 |
|---|
| 20 | if environ['DJANGO_SETTINGS_MODULE'] == 'cornetApps.settings': |
|---|
| 21 | return 0 |
|---|
| 22 | |
|---|
| [11069] | 23 | """ |
|---|
| 24 | Connects to a server through a given ip, port and associated username and password |
|---|
| 25 | Returns the SSHClient connected to the server |
|---|
| 26 | """ |
|---|
| [11071] | 27 | def connect(ip, port, user, password): |
|---|
| [11017] | 28 | client = SSHClient() |
|---|
| 29 | client.set_missing_host_key_policy(AutoAddPolicy()) |
|---|
| [11073] | 30 | try: |
|---|
| 31 | client.connect(ip, int(port), user, password) |
|---|
| 32 | except: |
|---|
| 33 | raise Http404(u'Incorrect username and password combination') |
|---|
| [11025] | 34 | return client |
|---|
| 35 | |
|---|
| [11098] | 36 | """ |
|---|
| 37 | Connects to a node given its floor and number |
|---|
| 38 | Returns the SSHClient connected to the server |
|---|
| [11103] | 39 | @param floor The floor of the node to connect to |
|---|
| 40 | @param num The number of the node to connect to |
|---|
| 41 | @param user The username to login with |
|---|
| 42 | @param password The password of the user to login with |
|---|
| 43 | @return The SSHClient connected to the server |
|---|
| [11098] | 44 | """ |
|---|
| 45 | def connectViaNode(floor, num, user, password): |
|---|
| 46 | floor = int(floor) |
|---|
| 47 | num = int(num) |
|---|
| 48 | node = Node(floor, num) |
|---|
| 49 | ip = node.ip |
|---|
| 50 | port = node.port |
|---|
| 51 | client = SSHClient() |
|---|
| 52 | client.set_missing_host_key_policy(AutoAddPolicy()) |
|---|
| 53 | try: |
|---|
| 54 | client.connect(ip, int(port), user, password) |
|---|
| 55 | except: |
|---|
| 56 | raise Http404(u'Incorrect username and password combination') |
|---|
| 57 | return client |
|---|
| 58 | |
|---|
| [11069] | 59 | ''' |
|---|
| 60 | Tests if nodebooter is running. |
|---|
| 61 | @param client: The SSHClient connected to a server/node |
|---|
| 62 | @return: A boolean indicating if nodebooter is running |
|---|
| 63 | ''' |
|---|
| [11025] | 64 | def nodeBooter_running(client): |
|---|
| [11086] | 65 | stdin, stdout, stderr = client.exec_command('ps -aux | grep GPP') |
|---|
| [11024] | 66 | processes = stdout.readlines() |
|---|
| [11086] | 67 | |
|---|
| 68 | #processes = processes.split() |
|---|
| 69 | |
|---|
| [11025] | 70 | for proc in processes: |
|---|
| [11089] | 71 | #print 'proc: ' + str(proc) |
|---|
| [11086] | 72 | #if '/usr/local/bin/nodeBooter' in proc or '/usr/bin/nodeBooter' in proc or 'nodeBooter' in proc: |
|---|
| 73 | if 'dev/bin/GPP' in proc: |
|---|
| [11089] | 74 | return 1 |
|---|
| 75 | #print 'runningNodebooter: ' + str(runningNodebooter) |
|---|
| [11025] | 76 | # runningNodebooter = 'dev/nodes/' in processes |
|---|
| [11089] | 77 | return 0 |
|---|
| [11025] | 78 | |
|---|
| [11103] | 79 | """ |
|---|
| [11098] | 80 | Starts default nodeBooter at a given ip and port |
|---|
| [11069] | 81 | @param ip: The ip address of the server for the nodes |
|---|
| 82 | @param port: The port, typically 22 |
|---|
| 83 | @param user: The user's username with access to the node |
|---|
| 84 | @param password: The user's password |
|---|
| [11103] | 85 | """ |
|---|
| [11093] | 86 | def startNodebooter(request, floor, num, user, password): |
|---|
| 87 | floor = int(floor) |
|---|
| 88 | num = int(num) |
|---|
| 89 | node = Node(floor, num) |
|---|
| 90 | ip = node.ip |
|---|
| 91 | port = node.port |
|---|
| [11071] | 92 | client = connect(ip, port, user, password) |
|---|
| [11073] | 93 | #stdin, stdout, stderr = client.exec_command('cd /sdr; nodeBooter -D -d dev/nodes/default_GPP_node/DeviceManager.dcd.xml') |
|---|
| [11089] | 94 | client.exec_command('cd /sdr ; nodeBooter -D -d dev/nodes/default_GPP_node/DeviceManager.dcd.xml') |
|---|
| [11073] | 95 | runningNodebooter = nodeBooter_running(client) |
|---|
| [11098] | 96 | client.close |
|---|
| [11073] | 97 | return render_to_response('DomainMan/runningNodebooter.html', locals(), context_instance=RequestContext(request)) |
|---|
| [11025] | 98 | |
|---|
| [11073] | 99 | """Start nodebooter with premade client from client list |
|---|
| 100 | """ |
|---|
| 101 | #def startNodebooter(request, floor, number): |
|---|
| 102 | # clientID = str(postFloor).__add__("-").__add__(str(postNum)) |
|---|
| 103 | # client = SSHClientList.getClient(clientID) |
|---|
| 104 | # #stdin, stdout, stderr = client.exec_command('cd /sdr; nodeBooter -D -d dev/nodes/default_GPP_node/DeviceManager.dcd.xml') |
|---|
| 105 | # client.exec_command('cd /sdr; nodeBooter -D -d dev/nodes/default_GPP_node/DeviceManager.dcd.xml') |
|---|
| 106 | # runningNodebooter = nodeBooter_running(client) |
|---|
| 107 | # return render_to_response('DomainMan/runningNodebooter.html', locals(), context_instance=RequestContext(request)) |
|---|
| 108 | |
|---|
| [11100] | 109 | """Choose a device manager from a populated list |
|---|
| 110 | """ |
|---|
| 111 | def getDomainManagerCommand(): |
|---|
| 112 | return |
|---|
| 113 | |
|---|
| 114 | """ |
|---|
| [11103] | 115 | Populate the list of device managers from a given node |
|---|
| 116 | @param floor The floor of the node to connect to |
|---|
| 117 | @param num The number of the node to connect to |
|---|
| 118 | @param user The username to login with |
|---|
| 119 | @param password The password of the user to login with |
|---|
| 120 | @return An array of the list of device managers on the node |
|---|
| 121 | """ |
|---|
| [11100] | 122 | def getDeviceManagers(floor, num, user, password): |
|---|
| 123 | client = connectViaNode(floor, num, user, password) |
|---|
| 124 | stdin, stdout, stderr = client.exec_command('cd /sdr; ls --format single-column dev/nodes/') |
|---|
| 125 | #stdin, stdout, stderr = client.exec_command('cd /sdr; ls') |
|---|
| 126 | client.close |
|---|
| 127 | return stdout.readlines() |
|---|
| 128 | |
|---|
| 129 | """ |
|---|
| [11103] | 130 | View for displaying device and domain managers |
|---|
| 131 | @param request The Django request parameter for views |
|---|
| 132 | @param floor The floor of the node to connect to |
|---|
| 133 | @param num The number of the node to connect to |
|---|
| 134 | @param user The username to login with |
|---|
| 135 | @param password The password of the user to login with |
|---|
| 136 | @return HttpResponse object with template and local context dictionary or |
|---|
| 137 | Http404 if could not find user and password session variables |
|---|
| 138 | """ |
|---|
| [11100] | 139 | def manager_view(request, floor, num, user, password): |
|---|
| 140 | deviceManagers = getDeviceManagers(floor, num, user, password) |
|---|
| 141 | try: |
|---|
| 142 | request.session["user"] = user |
|---|
| 143 | request.session["password"] = password |
|---|
| 144 | except: |
|---|
| 145 | raise Http404(u'Could not set user and password in session variables') |
|---|
| 146 | if len(deviceManagers) == 0: |
|---|
| 147 | message = "No device managers on this node" |
|---|
| 148 | floors = range(1,5) |
|---|
| 149 | nodes = range(1,13) |
|---|
| 150 | return render_to_response('DomainMan/managers.html', locals(), context_instance=RequestContext(request)) |
|---|
| 151 | |
|---|
| 152 | """ |
|---|
| 153 | Start a device manager and a domain manager |
|---|
| [11103] | 154 | checks post and session variables. |
|---|
| 155 | execute command based domain and device manager options |
|---|
| 156 | @param request The Django request parameter for views |
|---|
| 157 | @return HttpResponse object with template and local context dictionary or |
|---|
| 158 | Http404 if could not find user and password session variables |
|---|
| [11100] | 159 | """ |
|---|
| 160 | def startManager(request): |
|---|
| 161 | if 'floor' in request.POST and 'node' in request.POST: |
|---|
| 162 | if request.session.get('user') and request.session.get('password'): |
|---|
| 163 | sessionUser = request.session.get('user') |
|---|
| 164 | sessionPass = request.session.get('password') |
|---|
| 165 | else: |
|---|
| 166 | raise Http404(u'Could not get session user and password') |
|---|
| 167 | floor = int(request.POST['floor']) |
|---|
| 168 | node = int(request.POST['node']) |
|---|
| 169 | user = str(sessionUser) |
|---|
| 170 | password = str(sessionPass) |
|---|
| 171 | client = connectViaNode(floor, node, user, password) |
|---|
| 172 | else: |
|---|
| 173 | raise Exception('client could not connect with POST variables') |
|---|
| 174 | |
|---|
| 175 | domain = getDomain(request) |
|---|
| 176 | device = getDeviceXML(request) |
|---|
| 177 | domainIP = getDomainIP(request) |
|---|
| 178 | |
|---|
| 179 | command = "cd /sdr; nodeBooter "+domain+" "+device+" "+domainIP |
|---|
| 180 | print command |
|---|
| 181 | client.exec_command(command) |
|---|
| 182 | client.close |
|---|
| 183 | return render_to_response('DomainMan/runningNodebooter.html', {'command' : command}, context_instance=RequestContext(request)) |
|---|
| 184 | |
|---|
| 185 | """ |
|---|
| 186 | Gets the parameters to run domain manager |
|---|
| [11103] | 187 | @param request The Django request parameter for views |
|---|
| 188 | @return The correct command based on the domainLocation session variable |
|---|
| 189 | -D if the domain manager is located on the same node |
|---|
| 190 | nothing if the domain manager is located on a remote node |
|---|
| [11100] | 191 | """ |
|---|
| 192 | def getDomain(request): |
|---|
| 193 | if 'domainLocation' in request.POST: |
|---|
| 194 | if request.POST['domainLocation'] == 'same': |
|---|
| 195 | domain = "-D" |
|---|
| 196 | elif request.POST['domainLocation'] == 'remote': |
|---|
| 197 | domain = "" |
|---|
| 198 | else: |
|---|
| 199 | raise Http404(u'Could not get domain location from POST Request') |
|---|
| 200 | return domain |
|---|
| 201 | |
|---|
| 202 | """ |
|---|
| 203 | Gets the device manager's xml file to run based on a given POST request |
|---|
| [11103] | 204 | @param request The Django request parameter for views |
|---|
| 205 | @return The string command for the device manager xml file |
|---|
| 206 | returns Http404 if cannot find the device parameter in the post request |
|---|
| [11100] | 207 | """ |
|---|
| 208 | def getDeviceXML(request): |
|---|
| 209 | if 'deviceManager' in request.POST: |
|---|
| 210 | if 'device' in request.POST: |
|---|
| 211 | deviceName = str(request.POST['device']) |
|---|
| 212 | else: |
|---|
| 213 | raise Http404(u'Could not get device manager name from POST request') |
|---|
| 214 | #rstrip removes the trailing \n character |
|---|
| 215 | deviceXML = "-d dev/nodes/"+deviceName.rstrip()+"/DeviceManager.dcd.xml" |
|---|
| 216 | else: |
|---|
| 217 | raise Http404(u'DeviceManager not selected in POST request') |
|---|
| 218 | return deviceXML |
|---|
| 219 | |
|---|
| 220 | """ |
|---|
| 221 | Sets up the domain manager ip address to run based on a given POST request |
|---|
| [11103] | 222 | @param request The Django request parameter for views |
|---|
| 223 | @return The domain string options using the -ORBInitRef and ip address using corba |
|---|
| [11100] | 224 | """ |
|---|
| 225 | def getDomainIP(request): |
|---|
| 226 | if 'domain' in request.POST and 'floor' in request.POST and 'node' in request.POST: |
|---|
| 227 | if request.POST['domainLocation'] == 'remote': |
|---|
| 228 | floor = int(request.POST['floor']) |
|---|
| 229 | num = int(request.POST['node']) |
|---|
| 230 | node = Node(floor, num) |
|---|
| 231 | domainIP = "-ORBInitRef NameService=corbaname::"+node.ip |
|---|
| 232 | elif request.POST['domainLocation'] == 'same': |
|---|
| 233 | domainIP = "" |
|---|
| 234 | else: |
|---|
| 235 | raise Http404(u'POST request does not have a floor and node number') |
|---|
| 236 | return domainIP |
|---|
| 237 | |
|---|
| 238 | """Kills all nodeBooter processes at a given ip and port |
|---|
| [11069] | 239 | @param ip: The ip address of the server for the nodes |
|---|
| 240 | @param port: The port, typically 22 |
|---|
| 241 | @param user: The user's username with access to the node |
|---|
| 242 | @param password: The user's password |
|---|
| [11100] | 243 | """ |
|---|
| [11093] | 244 | def stopNodebooter(request, floor, num, user, password): |
|---|
| 245 | floor = int(floor) |
|---|
| 246 | num = int(num) |
|---|
| 247 | node = Node(floor, num) |
|---|
| 248 | ip = node.ip |
|---|
| 249 | port = node.port |
|---|
| [11098] | 250 | client = connect(ip, port, user, password) |
|---|
| [11086] | 251 | stdin, stdout, stderr = client.exec_command('killall nodeBooter && killall GPP') |
|---|
| [11073] | 252 | runningNodebooter = nodeBooter_running(client) |
|---|
| [11098] | 253 | client.close |
|---|
| [11073] | 254 | return render_to_response('DomainMan/runningNodebooter.html', locals(), context_instance=RequestContext(request)) |
|---|
| [11025] | 255 | |
|---|
| [11069] | 256 | ''' |
|---|
| 257 | Testing view to check if nodebooter is running. |
|---|
| 258 | Checks if nodeBooter is running, if not, starts it. |
|---|
| 259 | @param ip: The ip address of the server for the nodes |
|---|
| 260 | @param port: The port, typically 22 |
|---|
| 261 | @param user: The user's username with access to the node |
|---|
| 262 | @param password: The user's password |
|---|
| [11103] | 263 | @return HttpResponse with local variables as a context dictionary inserted into the console.html template |
|---|
| [11069] | 264 | ''' |
|---|
| [11025] | 265 | def nodeBooter_page(request, ip, port, user, password): |
|---|
| [11071] | 266 | if 'ip' in request.POST and 'port' in request.POST and 'user' in request.POST and 'pass' in request.POST: |
|---|
| 267 | client = connect(ip, port, user, password) |
|---|
| 268 | else: |
|---|
| 269 | raise Exception('client could not connect with POST variables') |
|---|
| [11025] | 270 | if not nodeBooter_running(client): |
|---|
| 271 | stdin, stdout, stderr = client.exec_command('cd /sdr; nodeBooter -D -d dev/nodes/default_GPP_node/DeviceManager.dcd.xml') |
|---|
| 272 | runningNodebooter = nodeBooter_running(client) |
|---|
| [11098] | 273 | client.close |
|---|
| [11025] | 274 | return render_to_response('DomainMan/console.html', locals(), context_instance=RequestContext(request)) |
|---|
| 275 | |
|---|
| [11095] | 276 | |
|---|
| [11098] | 277 | |
|---|
| [11095] | 278 | """ |
|---|
| [11103] | 279 | Connect to a node through post variables |
|---|
| 280 | @param request Contains the post variables floor, number, user, and pass to connect to a node |
|---|
| 281 | @return The local variables as dictionary into the DomainMan/runningNodebooter.html template |
|---|
| 282 | If one of the post variables are not there, it will print a "Missing ssh connection parameters" string |
|---|
| [11093] | 283 | """ |
|---|
| [11071] | 284 | def connect_to_node(request): |
|---|
| 285 | if 'floor' in request.POST and 'number' in request.POST and 'user' in request.POST and 'pass' in request.POST: |
|---|
| [11093] | 286 | floor = int(request.POST['floor']) |
|---|
| 287 | num = int(request.POST['number']) |
|---|
| [11071] | 288 | user = str(request.POST['user']) |
|---|
| 289 | password = str(request.POST['pass']) |
|---|
| 290 | |
|---|
| [11093] | 291 | node = Node(floor, num) |
|---|
| 292 | ip = node.ip |
|---|
| 293 | port = node.port |
|---|
| [11071] | 294 | client = connect(ip, port, user, password) |
|---|
| 295 | runningNodebooter = nodeBooter_running(client) |
|---|
| [11098] | 296 | client.close |
|---|
| [11071] | 297 | else: |
|---|
| 298 | message = 'Missing ssh connection parameters' |
|---|
| [11086] | 299 | print message |
|---|
| [11073] | 300 | return render_to_response('DomainMan/runningNodebooter.html', locals(), context_instance=RequestContext(request)) |
|---|
| [11071] | 301 | |
|---|
| [11093] | 302 | """ |
|---|
| 303 | Raise exception if the inputs of the floor and node number are invalid |
|---|
| [11103] | 304 | @param floor The given floor to check |
|---|
| 305 | @param num The given node number to check |
|---|
| 306 | @return Exception if the floor or node num is less than 1 or greater than 4 or 12, respectively |
|---|
| [11093] | 307 | """ |
|---|
| 308 | def testValidInputs(floor, num): |
|---|
| 309 | if floor < 1 or floor > 4: |
|---|
| 310 | raise Exception("Floor must be between 1 and 4") |
|---|
| 311 | if num < 1 or num > 12: |
|---|
| 312 | raise Exception("Node must be between 1 and 12") |
|---|
| 313 | |
|---|
| [11092] | 314 | #@login_required |
|---|
| [11103] | 315 | """ |
|---|
| 316 | Loads the DomainMan/nodes.html template based on a logging into a specific node |
|---|
| 317 | @param floor_num The floor of the node to login to |
|---|
| 318 | @param node_num The node number of the node to login to |
|---|
| 319 | @return The HttpResponse object with the local variables added to the dictionary |
|---|
| 320 | """ |
|---|
| [11092] | 321 | def loadNode(request, floor_num, node_num): |
|---|
| 322 | floor = int(floor_num) |
|---|
| 323 | num = int(node_num) |
|---|
| [11093] | 324 | testValidInputs(floor, num) |
|---|
| [11092] | 325 | node = Node(floor, num) |
|---|
| 326 | ip = node.ip |
|---|
| 327 | port = node.port |
|---|
| [11098] | 328 | if isOnWeb(): |
|---|
| 329 | address = str(ip) + ":" + str(port) |
|---|
| 330 | else: |
|---|
| 331 | address = str(ip) |
|---|
| [11096] | 332 | #index2(request, ip) |
|---|
| [11092] | 333 | return render_to_response("DomainMan/nodes.html", locals(), context_instance=RequestContext(request)) |
|---|
| 334 | |
|---|
| [11071] | 335 | """ |
|---|
| [11093] | 336 | Returns connection form |
|---|
| [11103] | 337 | @param selectFloor The floor of the node to login to |
|---|
| 338 | @param selectNum The node number of the node to login to |
|---|
| 339 | @return the HttpResponse object with the local variable dictionary in the DomainMan/connect.html template |
|---|
| [11093] | 340 | """ |
|---|
| 341 | def connect_to_node_view(request, selectFloor, selectNum): |
|---|
| [11085] | 342 | number_of_floors = range(1,5) |
|---|
| 343 | number_of_nodes = range(1,13) |
|---|
| [11093] | 344 | selectedFloor=int(selectFloor) |
|---|
| 345 | selectedNum=int(selectNum) |
|---|
| 346 | testValidInputs(selectedFloor, selectedNum) |
|---|
| [11085] | 347 | return render_to_response('DomainMan/connect.html', locals(), RequestContext(request)) |
|---|