| 1 | |
|---|
| 2 | from django.shortcuts import render_to_response |
|---|
| 3 | from django.http import HttpResponse, Http404 |
|---|
| 4 | from django.template import RequestContext |
|---|
| 5 | from paramiko import SSHClient, AutoAddPolicy |
|---|
| 6 | from models import * |
|---|
| 7 | |
|---|
| 8 | """Views for DomainMan application |
|---|
| 9 | Starts and stops nodeBooter/naming service |
|---|
| 10 | """ |
|---|
| 11 | |
|---|
| 12 | """ |
|---|
| 13 | Testing login |
|---|
| 14 | """ |
|---|
| 15 | def cornet_login(request): |
|---|
| 16 | user = request.user |
|---|
| 17 | return render_to_response('DomainMan/cornet_login.html', locals(), context_instance=RequestContext(request)) |
|---|
| 18 | |
|---|
| 19 | """ |
|---|
| 20 | Connects to a server through a given ip, port and associated username and password |
|---|
| 21 | Returns the SSHClient connected to the server |
|---|
| 22 | """ |
|---|
| 23 | def connect(ip, port, user, password): |
|---|
| 24 | client = SSHClient() |
|---|
| 25 | client.set_missing_host_key_policy(AutoAddPolicy()) |
|---|
| 26 | try: |
|---|
| 27 | client.connect(ip, int(port), user, password) |
|---|
| 28 | except: |
|---|
| 29 | raise Http404(u'Incorrect username and password combination') |
|---|
| 30 | return client |
|---|
| 31 | |
|---|
| 32 | ''' |
|---|
| 33 | Tests if nodebooter is running. |
|---|
| 34 | @param client: The SSHClient connected to a server/node |
|---|
| 35 | @return: A boolean indicating if nodebooter is running |
|---|
| 36 | ''' |
|---|
| 37 | def nodeBooter_running(client): |
|---|
| 38 | stdin, stdout, stderr = client.exec_command('ps x | grep nodeBooter') |
|---|
| 39 | processes = stdout.readlines() |
|---|
| 40 | for proc in processes: |
|---|
| 41 | if 'dev/nodes' in proc and 'DeviceManager.dcd' in proc: |
|---|
| 42 | runningNodebooter = 1 |
|---|
| 43 | break |
|---|
| 44 | else: |
|---|
| 45 | runningNodebooter = 0 |
|---|
| 46 | # runningNodebooter = 'dev/nodes/' in processes |
|---|
| 47 | return runningNodebooter |
|---|
| 48 | |
|---|
| 49 | ''' |
|---|
| 50 | Starts nodeBooter at a given ip and port |
|---|
| 51 | @param ip: The ip address of the server for the nodes |
|---|
| 52 | @param port: The port, typically 22 |
|---|
| 53 | @param user: The user's username with access to the node |
|---|
| 54 | @param password: The user's password |
|---|
| 55 | ''' |
|---|
| 56 | def startNodebooter(request, ip, port, user, password): |
|---|
| 57 | client = connect(ip, port, user, password) |
|---|
| 58 | #stdin, stdout, stderr = client.exec_command('cd /sdr; nodeBooter -D -d dev/nodes/default_GPP_node/DeviceManager.dcd.xml') |
|---|
| 59 | client.exec_command('cd /sdr; nodeBooter -D -d dev/nodes/default_GPP_node/DeviceManager.dcd.xml') |
|---|
| 60 | runningNodebooter = nodeBooter_running(client) |
|---|
| 61 | return render_to_response('DomainMan/runningNodebooter.html', locals(), context_instance=RequestContext(request)) |
|---|
| 62 | |
|---|
| 63 | """Start nodebooter with premade client from client list |
|---|
| 64 | """ |
|---|
| 65 | #def startNodebooter(request, floor, number): |
|---|
| 66 | # clientID = str(postFloor).__add__("-").__add__(str(postNum)) |
|---|
| 67 | # client = SSHClientList.getClient(clientID) |
|---|
| 68 | # #stdin, stdout, stderr = client.exec_command('cd /sdr; nodeBooter -D -d dev/nodes/default_GPP_node/DeviceManager.dcd.xml') |
|---|
| 69 | # client.exec_command('cd /sdr; nodeBooter -D -d dev/nodes/default_GPP_node/DeviceManager.dcd.xml') |
|---|
| 70 | # runningNodebooter = nodeBooter_running(client) |
|---|
| 71 | # return render_to_response('DomainMan/runningNodebooter.html', locals(), context_instance=RequestContext(request)) |
|---|
| 72 | |
|---|
| 73 | ''' |
|---|
| 74 | Kills all nodeBooter processes at a given ip and port |
|---|
| 75 | @param ip: The ip address of the server for the nodes |
|---|
| 76 | @param port: The port, typically 22 |
|---|
| 77 | @param user: The user's username with access to the node |
|---|
| 78 | @param password: The user's password |
|---|
| 79 | ''' |
|---|
| 80 | def stopNodebooter(request, ip, port, user, password): |
|---|
| 81 | client = connect(ip, port, user, password) |
|---|
| 82 | stdin, stdout, stderr = client.exec_command('killall nodeBooter') |
|---|
| 83 | runningNodebooter = nodeBooter_running(client) |
|---|
| 84 | return render_to_response('DomainMan/runningNodebooter.html', locals(), context_instance=RequestContext(request)) |
|---|
| 85 | |
|---|
| 86 | ''' |
|---|
| 87 | Testing view to check if nodebooter is running. |
|---|
| 88 | Checks if nodeBooter is running, if not, starts it. |
|---|
| 89 | @param ip: The ip address of the server for the nodes |
|---|
| 90 | @param port: The port, typically 22 |
|---|
| 91 | @param user: The user's username with access to the node |
|---|
| 92 | @param password: The user's password |
|---|
| 93 | ''' |
|---|
| 94 | def nodeBooter_page(request, ip, port, user, password): |
|---|
| 95 | if 'ip' in request.POST and 'port' in request.POST and 'user' in request.POST and 'pass' in request.POST: |
|---|
| 96 | client = connect(ip, port, user, password) |
|---|
| 97 | else: |
|---|
| 98 | raise Exception('client could not connect with POST variables') |
|---|
| 99 | if not nodeBooter_running(client): |
|---|
| 100 | stdin, stdout, stderr = client.exec_command('cd /sdr; nodeBooter -D -d dev/nodes/default_GPP_node/DeviceManager.dcd.xml') |
|---|
| 101 | runningNodebooter = nodeBooter_running(client) |
|---|
| 102 | return render_to_response('DomainMan/console.html', locals(), context_instance=RequestContext(request)) |
|---|
| 103 | |
|---|
| 104 | """Connect to a nodebooter through a form |
|---|
| 105 | """ |
|---|
| 106 | def connect_to_node(request): |
|---|
| 107 | if 'floor' in request.POST and 'number' in request.POST and 'user' in request.POST and 'pass' in request.POST: |
|---|
| 108 | postFloor = int(request.POST['floor']) |
|---|
| 109 | postNum = int(request.POST['number']) |
|---|
| 110 | user = str(request.POST['user']) |
|---|
| 111 | password = str(request.POST['pass']) |
|---|
| 112 | |
|---|
| 113 | thisNode = Node(postFloor, postNum) |
|---|
| 114 | ip = thisNode.ip |
|---|
| 115 | port = thisNode.port |
|---|
| 116 | #thisNode.save() |
|---|
| 117 | #thisNode.setClient(user, password) |
|---|
| 118 | #node = Node.objects.get(floor=4, num=1) |
|---|
| 119 | client = connect(ip, port, user, password) |
|---|
| 120 | #stdin, stdout, stderr = client.exec_command('ls') |
|---|
| 121 | runningNodebooter = nodeBooter_running(client) |
|---|
| 122 | else: |
|---|
| 123 | message = 'Missing ssh connection parameters' |
|---|
| 124 | return render_to_response('DomainMan/runningNodebooter.html', locals(), context_instance=RequestContext(request)) |
|---|
| 125 | |
|---|
| 126 | """Returns connection form |
|---|
| 127 | """ |
|---|
| 128 | def connect_to_node_view(request): |
|---|
| 129 | return render_to_response('DomainMan/connect.html', RequestContext(request)) |
|---|
| 130 | |
|---|
| 131 | def ajax_example(request): |
|---|
| 132 | if not request.method == 'GET': |
|---|
| 133 | name = "did not work" |
|---|
| 134 | print request.GET |
|---|
| 135 | return render_to_response('DomainMan/ajax_example.html', locals(), context_instance=RequestContext(request)) |
|---|
| 136 | response_dict = {} |
|---|
| 137 | name = request.GET.get('name', False) |
|---|
| 138 | total = request.GET.get('total', False) |
|---|
| 139 | response_dict.update({'name': name, 'total': total}) |
|---|
| 140 | if total: |
|---|
| 141 | try: |
|---|
| 142 | total = int(total) |
|---|
| 143 | except: |
|---|
| 144 | total = False |
|---|
| 145 | if name and total and int(total) == 10: |
|---|
| 146 | response_dict.update({'success': True }) |
|---|
| 147 | else: |
|---|
| 148 | response_dict.update({'errors': {}}) |
|---|
| 149 | if not name: |
|---|
| 150 | response_dict['errors'].update({'name': 'This field is required'}) |
|---|
| 151 | if not total and total is not False: |
|---|
| 152 | response_dict['errors'].update({'total': 'This field is required'}) |
|---|
| 153 | elif int(total) != 10: |
|---|
| 154 | response_dict['errors'].update({'total': 'Incorrect total'}) |
|---|
| 155 | return render_to_response('DomainMan/ajax_example.html', response_dict, context_instance=RequestContext(request)) |
|---|