| 1 | |
|---|
| 2 | from django.shortcuts import render_to_response |
|---|
| 3 | from django.http import HttpResponse |
|---|
| 4 | from django.template import RequestContext |
|---|
| 5 | from paramiko import SSHClient, AutoAddPolicy |
|---|
| 6 | |
|---|
| 7 | """Views for DomainMan application |
|---|
| 8 | Starts and stops nodeBooter/naming service |
|---|
| 9 | """ |
|---|
| 10 | |
|---|
| 11 | """ |
|---|
| 12 | Testing login |
|---|
| 13 | """ |
|---|
| 14 | def cornet_login(request): |
|---|
| 15 | user = request.user |
|---|
| 16 | return render_to_response('DomainMan/cornet_login.html', locals(), context_instance=RequestContext(request)) |
|---|
| 17 | |
|---|
| 18 | """ |
|---|
| 19 | Connects to a server through a given ip, port and associated username and password |
|---|
| 20 | Returns the SSHClient connected to the server |
|---|
| 21 | """ |
|---|
| 22 | def connect(request, ip, port, user, password): |
|---|
| 23 | client = SSHClient() |
|---|
| 24 | client.set_missing_host_key_policy(AutoAddPolicy()) |
|---|
| 25 | client.connect(ip, int(port), user, password) |
|---|
| 26 | return client |
|---|
| 27 | |
|---|
| 28 | ''' |
|---|
| 29 | Tests if nodebooter is running. |
|---|
| 30 | @param client: The SSHClient connected to a server/node |
|---|
| 31 | @return: A boolean indicating if nodebooter is running |
|---|
| 32 | ''' |
|---|
| 33 | def nodeBooter_running(client): |
|---|
| 34 | stdin, stdout, stderr = client.exec_command('ps x | grep nodeBooter') |
|---|
| 35 | processes = stdout.readlines() |
|---|
| 36 | for proc in processes: |
|---|
| 37 | if 'dev/nodes' in proc and 'DeviceManager.dcd' in proc: |
|---|
| 38 | runningNodebooter = 1 |
|---|
| 39 | break |
|---|
| 40 | else: |
|---|
| 41 | runningNodebooter = 0 |
|---|
| 42 | # runningNodebooter = 'dev/nodes/' in processes |
|---|
| 43 | return runningNodebooter |
|---|
| 44 | |
|---|
| 45 | ''' |
|---|
| 46 | Starts nodeBooter at a given ip and port |
|---|
| 47 | @param ip: The ip address of the server for the nodes |
|---|
| 48 | @param port: The port, typically 22 |
|---|
| 49 | @param user: The user's username with access to the node |
|---|
| 50 | @param password: The user's password |
|---|
| 51 | ''' |
|---|
| 52 | def startNodebooter(request, ip, port, user, password): |
|---|
| 53 | client = connect(request, ip, port, user, password) |
|---|
| 54 | stdin, stdout, stderr = client.exec_command('cd /sdr; nodeBooter -D -d dev/nodes/default_GPP_node/DeviceManager.dcd.xml') |
|---|
| 55 | return render_to_response('DomainMan/console.html', locals(), context_instance=RequestContext(request)) |
|---|
| 56 | |
|---|
| 57 | ''' |
|---|
| 58 | Kills all nodeBooter processes at a given ip and port |
|---|
| 59 | @param ip: The ip address of the server for the nodes |
|---|
| 60 | @param port: The port, typically 22 |
|---|
| 61 | @param user: The user's username with access to the node |
|---|
| 62 | @param password: The user's password |
|---|
| 63 | ''' |
|---|
| 64 | def stopNodebooter(request, ip, port, user, password): |
|---|
| 65 | client = connect(request, ip, port, user, password) |
|---|
| 66 | stdin, stdout, stderr = client.exec_command('killall nodeBooter') |
|---|
| 67 | return render_to_response('DomainMan/console.html', locals(), context_instance=RequestContext(request)) |
|---|
| 68 | |
|---|
| 69 | ''' |
|---|
| 70 | Testing view to check if nodebooter is running. |
|---|
| 71 | Checks if nodeBooter is running, if not, starts it. |
|---|
| 72 | @param ip: The ip address of the server for the nodes |
|---|
| 73 | @param port: The port, typically 22 |
|---|
| 74 | @param user: The user's username with access to the node |
|---|
| 75 | @param password: The user's password |
|---|
| 76 | ''' |
|---|
| 77 | def nodeBooter_page(request, ip, port, user, password): |
|---|
| 78 | client = connect(request, ip, port, user, password) |
|---|
| 79 | if not nodeBooter_running(client): |
|---|
| 80 | stdin, stdout, stderr = client.exec_command('cd /sdr; nodeBooter -D -d dev/nodes/default_GPP_node/DeviceManager.dcd.xml') |
|---|
| 81 | runningNodebooter = nodeBooter_running(client) |
|---|
| 82 | return render_to_response('DomainMan/console.html', locals(), context_instance=RequestContext(request)) |
|---|
| 83 | |
|---|
| 84 | def ajax_example(request): |
|---|
| 85 | if not request.method == 'POST': |
|---|
| 86 | name = "did not work" |
|---|
| 87 | print request.POST |
|---|
| 88 | return render_to_response('DomainMan/ajax_example.html', locals(), context_instance=RequestContext(request)) |
|---|
| 89 | response_dict = {} |
|---|
| 90 | name = request.POST.get('name', False) |
|---|
| 91 | total = request.POST.get('total', False) |
|---|
| 92 | response_dict.update({'name': name, 'total': total}) |
|---|
| 93 | if total: |
|---|
| 94 | try: |
|---|
| 95 | total = int(total) |
|---|
| 96 | except: |
|---|
| 97 | total = False |
|---|
| 98 | if name and total and int(total) == 10: |
|---|
| 99 | response_dict.update({'success': True }) |
|---|
| 100 | else: |
|---|
| 101 | response_dict.update({'errors': {}}) |
|---|
| 102 | if not name: |
|---|
| 103 | response_dict['errors'].update({'name': 'This field is required'}) |
|---|
| 104 | if not total and total is not False: |
|---|
| 105 | response_dict['errors'].update({'total': 'This field is required'}) |
|---|
| 106 | elif int(total) != 10: |
|---|
| 107 | response_dict['errors'].update({'total': 'Incorrect total'}) |
|---|
| 108 | return render_to_response('DomainMan/ajax_example.html', response_dict, context_instance=RequestContext(request)) |
|---|