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