| 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 | Checks if running locally or on the web by testing which Django settings are used |
|---|
| 15 | """ |
|---|
| 16 | def isOnWeb(): |
|---|
| 17 | if environ['DJANGO_SETTINGS_MODULE'] == 'cornetApps.settingsWeb': |
|---|
| 18 | return 1 |
|---|
| 19 | if environ['DJANGO_SETTINGS_MODULE'] == 'cornetApps.settings': |
|---|
| 20 | return 0 |
|---|
| 21 | |
|---|
| 22 | """ |
|---|
| 23 | Connects to a server through a given ip, port and associated username and password |
|---|
| 24 | Returns the SSHClient connected to the server |
|---|
| 25 | """ |
|---|
| 26 | def connect(ip, port, user, password): |
|---|
| 27 | client = SSHClient() |
|---|
| 28 | client.set_missing_host_key_policy(AutoAddPolicy()) |
|---|
| 29 | try: |
|---|
| 30 | client.connect(ip, int(port), user, password) |
|---|
| 31 | except: |
|---|
| 32 | raise Http404(u'Incorrect username and password combination') |
|---|
| 33 | return client |
|---|
| 34 | |
|---|
| 35 | """ |
|---|
| 36 | Connects to a node given its floor and number |
|---|
| 37 | Returns the SSHClient connected to the server |
|---|
| 38 | """ |
|---|
| 39 | def connectViaNode(floor, num, user, password): |
|---|
| 40 | floor = int(floor) |
|---|
| 41 | num = int(num) |
|---|
| 42 | node = Node(floor, num) |
|---|
| 43 | ip = node.ip |
|---|
| 44 | port = node.port |
|---|
| 45 | client = SSHClient() |
|---|
| 46 | client.set_missing_host_key_policy(AutoAddPolicy()) |
|---|
| 47 | try: |
|---|
| 48 | client.connect(ip, int(port), user, password) |
|---|
| 49 | except: |
|---|
| 50 | raise Http404(u'Incorrect username and password combination') |
|---|
| 51 | return client |
|---|
| 52 | |
|---|
| 53 | ''' |
|---|
| 54 | Tests if nodebooter is running. |
|---|
| 55 | @param client: The SSHClient connected to a server/node |
|---|
| 56 | @return: A boolean indicating if nodebooter is running |
|---|
| 57 | ''' |
|---|
| 58 | def nodeBooter_running(client): |
|---|
| 59 | stdin, stdout, stderr = client.exec_command('ps -aux | grep GPP') |
|---|
| 60 | processes = stdout.readlines() |
|---|
| 61 | |
|---|
| 62 | #processes = processes.split() |
|---|
| 63 | |
|---|
| 64 | for proc in processes: |
|---|
| 65 | #print 'proc: ' + str(proc) |
|---|
| 66 | #if '/usr/local/bin/nodeBooter' in proc or '/usr/bin/nodeBooter' in proc or 'nodeBooter' in proc: |
|---|
| 67 | if 'dev/bin/GPP' in proc: |
|---|
| 68 | return 1 |
|---|
| 69 | #print 'runningNodebooter: ' + str(runningNodebooter) |
|---|
| 70 | # runningNodebooter = 'dev/nodes/' in processes |
|---|
| 71 | return 0 |
|---|
| 72 | |
|---|
| 73 | """ |
|---|
| 74 | Views the available nodeBooters to start |
|---|
| 75 | """ |
|---|
| 76 | def selectNodeboter(request, floor, num, user, password): |
|---|
| 77 | client = connectViaNode(floor, num, user, password) |
|---|
| 78 | |
|---|
| 79 | client.close |
|---|
| 80 | return |
|---|
| 81 | |
|---|
| 82 | ''' |
|---|
| 83 | Starts default nodeBooter at a given ip and port |
|---|
| 84 | @param ip: The ip address of the server for the nodes |
|---|
| 85 | @param port: The port, typically 22 |
|---|
| 86 | @param user: The user's username with access to the node |
|---|
| 87 | @param password: The user's password |
|---|
| 88 | ''' |
|---|
| 89 | def startNodebooter(request, floor, num, user, password): |
|---|
| 90 | floor = int(floor) |
|---|
| 91 | num = int(num) |
|---|
| 92 | node = Node(floor, num) |
|---|
| 93 | ip = node.ip |
|---|
| 94 | port = node.port |
|---|
| 95 | client = connect(ip, port, user, password) |
|---|
| 96 | #stdin, stdout, stderr = client.exec_command('cd /sdr; nodeBooter -D -d dev/nodes/default_GPP_node/DeviceManager.dcd.xml') |
|---|
| 97 | client.exec_command('cd /sdr ; nodeBooter -D -d dev/nodes/default_GPP_node/DeviceManager.dcd.xml') |
|---|
| 98 | runningNodebooter = nodeBooter_running(client) |
|---|
| 99 | client.close |
|---|
| 100 | return render_to_response('DomainMan/runningNodebooter.html', locals(), context_instance=RequestContext(request)) |
|---|
| 101 | |
|---|
| 102 | """Start nodebooter with premade client from client list |
|---|
| 103 | """ |
|---|
| 104 | #def startNodebooter(request, floor, number): |
|---|
| 105 | # clientID = str(postFloor).__add__("-").__add__(str(postNum)) |
|---|
| 106 | # client = SSHClientList.getClient(clientID) |
|---|
| 107 | # #stdin, stdout, stderr = client.exec_command('cd /sdr; nodeBooter -D -d dev/nodes/default_GPP_node/DeviceManager.dcd.xml') |
|---|
| 108 | # client.exec_command('cd /sdr; nodeBooter -D -d dev/nodes/default_GPP_node/DeviceManager.dcd.xml') |
|---|
| 109 | # runningNodebooter = nodeBooter_running(client) |
|---|
| 110 | # return render_to_response('DomainMan/runningNodebooter.html', locals(), context_instance=RequestContext(request)) |
|---|
| 111 | |
|---|
| 112 | ''' |
|---|
| 113 | Kills all nodeBooter processes at a given ip and port |
|---|
| 114 | @param ip: The ip address of the server for the nodes |
|---|
| 115 | @param port: The port, typically 22 |
|---|
| 116 | @param user: The user's username with access to the node |
|---|
| 117 | @param password: The user's password |
|---|
| 118 | ''' |
|---|
| 119 | def stopNodebooter(request, floor, num, user, password): |
|---|
| 120 | floor = int(floor) |
|---|
| 121 | num = int(num) |
|---|
| 122 | node = Node(floor, num) |
|---|
| 123 | ip = node.ip |
|---|
| 124 | port = node.port |
|---|
| 125 | client = connect(ip, port, user, password) |
|---|
| 126 | stdin, stdout, stderr = client.exec_command('killall nodeBooter && killall GPP') |
|---|
| 127 | runningNodebooter = nodeBooter_running(client) |
|---|
| 128 | client.close |
|---|
| 129 | return render_to_response('DomainMan/runningNodebooter.html', locals(), context_instance=RequestContext(request)) |
|---|
| 130 | |
|---|
| 131 | ''' |
|---|
| 132 | Testing view to check if nodebooter is running. |
|---|
| 133 | Checks if nodeBooter is running, if not, starts it. |
|---|
| 134 | @param ip: The ip address of the server for the nodes |
|---|
| 135 | @param port: The port, typically 22 |
|---|
| 136 | @param user: The user's username with access to the node |
|---|
| 137 | @param password: The user's password |
|---|
| 138 | ''' |
|---|
| 139 | def nodeBooter_page(request, ip, port, user, password): |
|---|
| 140 | if 'ip' in request.POST and 'port' in request.POST and 'user' in request.POST and 'pass' in request.POST: |
|---|
| 141 | client = connect(ip, port, user, password) |
|---|
| 142 | else: |
|---|
| 143 | raise Exception('client could not connect with POST variables') |
|---|
| 144 | if not nodeBooter_running(client): |
|---|
| 145 | stdin, stdout, stderr = client.exec_command('cd /sdr; nodeBooter -D -d dev/nodes/default_GPP_node/DeviceManager.dcd.xml') |
|---|
| 146 | runningNodebooter = nodeBooter_running(client) |
|---|
| 147 | client.close |
|---|
| 148 | return render_to_response('DomainMan/console.html', locals(), context_instance=RequestContext(request)) |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | """ |
|---|
| 153 | Connect to a node through a form |
|---|
| 154 | """ |
|---|
| 155 | def connect_to_node(request): |
|---|
| 156 | if 'floor' in request.POST and 'number' in request.POST and 'user' in request.POST and 'pass' in request.POST: |
|---|
| 157 | floor = int(request.POST['floor']) |
|---|
| 158 | num = int(request.POST['number']) |
|---|
| 159 | user = str(request.POST['user']) |
|---|
| 160 | password = str(request.POST['pass']) |
|---|
| 161 | |
|---|
| 162 | node = Node(floor, num) |
|---|
| 163 | ip = node.ip |
|---|
| 164 | port = node.port |
|---|
| 165 | client = connect(ip, port, user, password) |
|---|
| 166 | runningNodebooter = nodeBooter_running(client) |
|---|
| 167 | client.close |
|---|
| 168 | else: |
|---|
| 169 | message = 'Missing ssh connection parameters' |
|---|
| 170 | print message |
|---|
| 171 | return render_to_response('DomainMan/runningNodebooter.html', locals(), context_instance=RequestContext(request)) |
|---|
| 172 | |
|---|
| 173 | """ |
|---|
| 174 | Raise exception if the inputs of the floor and node number are invalid |
|---|
| 175 | """ |
|---|
| 176 | def testValidInputs(floor, num): |
|---|
| 177 | if floor < 1 or floor > 4: |
|---|
| 178 | raise Exception("Floor must be between 1 and 4") |
|---|
| 179 | if num < 1 or num > 12: |
|---|
| 180 | raise Exception("Node must be between 1 and 12") |
|---|
| 181 | |
|---|
| 182 | #@login_required |
|---|
| 183 | def loadNode(request, floor_num, node_num): |
|---|
| 184 | floor = int(floor_num) |
|---|
| 185 | num = int(node_num) |
|---|
| 186 | testValidInputs(floor, num) |
|---|
| 187 | node = Node(floor, num) |
|---|
| 188 | ip = node.ip |
|---|
| 189 | port = node.port |
|---|
| 190 | if isOnWeb(): |
|---|
| 191 | address = str(ip) + ":" + str(port) |
|---|
| 192 | else: |
|---|
| 193 | address = str(ip) |
|---|
| 194 | #index2(request, ip) |
|---|
| 195 | return render_to_response("DomainMan/nodes.html", locals(), context_instance=RequestContext(request)) |
|---|
| 196 | |
|---|
| 197 | """ |
|---|
| 198 | Returns connection form |
|---|
| 199 | """ |
|---|
| 200 | def connect_to_node_view(request, selectFloor, selectNum): |
|---|
| 201 | number_of_floors = range(1,5) |
|---|
| 202 | number_of_nodes = range(1,13) |
|---|
| 203 | selectedFloor=int(selectFloor) |
|---|
| 204 | selectedNum=int(selectNum) |
|---|
| 205 | testValidInputs(selectedFloor, selectedNum) |
|---|
| 206 | return render_to_response('DomainMan/connect.html', locals(), RequestContext(request)) |
|---|
| 207 | |
|---|
| 208 | def ajax_example(request): |
|---|
| 209 | if not request.method == 'GET': |
|---|
| 210 | name = "did not work" |
|---|
| 211 | print request.GET |
|---|
| 212 | return render_to_response('DomainMan/ajax_example.html', locals(), context_instance=RequestContext(request)) |
|---|
| 213 | response_dict = {} |
|---|
| 214 | name = request.GET.get('name', False) |
|---|
| 215 | total = request.GET.get('total', False) |
|---|
| 216 | response_dict.update({'name': name, 'total': total}) |
|---|
| 217 | if total: |
|---|
| 218 | try: |
|---|
| 219 | total = int(total) |
|---|
| 220 | except: |
|---|
| 221 | total = False |
|---|
| 222 | if name and total and int(total) == 10: |
|---|
| 223 | response_dict.update({'success': True }) |
|---|
| 224 | else: |
|---|
| 225 | response_dict.update({'errors': {}}) |
|---|
| 226 | if not name: |
|---|
| 227 | response_dict['errors'].update({'name': 'This field is required'}) |
|---|
| 228 | if not total and total is not False: |
|---|
| 229 | response_dict['errors'].update({'total': 'This field is required'}) |
|---|
| 230 | elif int(total) != 10: |
|---|
| 231 | response_dict['errors'].update({'total': 'Incorrect total'}) |
|---|
| 232 | return render_to_response('DomainMan/ajax_example.html', response_dict, context_instance=RequestContext(request)) |
|---|