| 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 | 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 | client.connect(ip, int(port), user, password) |
|---|
| 27 | return client |
|---|
| 28 | |
|---|
| 29 | ''' |
|---|
| 30 | Tests if nodebooter is running. |
|---|
| 31 | @param client: The SSHClient connected to a server/node |
|---|
| 32 | @return: A boolean indicating if nodebooter is running |
|---|
| 33 | ''' |
|---|
| 34 | def nodeBooter_running(client): |
|---|
| 35 | stdin, stdout, stderr = client.exec_command('ps x | grep nodeBooter') |
|---|
| 36 | processes = stdout.readlines() |
|---|
| 37 | for proc in processes: |
|---|
| 38 | if 'dev/nodes' in proc and 'DeviceManager.dcd' in proc: |
|---|
| 39 | runningNodebooter = 1 |
|---|
| 40 | break |
|---|
| 41 | else: |
|---|
| 42 | runningNodebooter = 0 |
|---|
| 43 | # runningNodebooter = 'dev/nodes/' in processes |
|---|
| 44 | return runningNodebooter |
|---|
| 45 | |
|---|
| 46 | ''' |
|---|
| 47 | Starts nodeBooter at a given ip and port |
|---|
| 48 | @param ip: The ip address of the server for the nodes |
|---|
| 49 | @param port: The port, typically 22 |
|---|
| 50 | @param user: The user's username with access to the node |
|---|
| 51 | @param password: The user's password |
|---|
| 52 | ''' |
|---|
| 53 | def startNodebooter(request, ip, port, user, password): |
|---|
| 54 | client = connect(ip, port, user, password) |
|---|
| 55 | stdin, stdout, stderr = client.exec_command('cd /sdr; nodeBooter -D -d dev/nodes/default_GPP_node/DeviceManager.dcd.xml') |
|---|
| 56 | return render_to_response('DomainMan/console.html', locals(), context_instance=RequestContext(request)) |
|---|
| 57 | |
|---|
| 58 | ''' |
|---|
| 59 | Kills all nodeBooter processes at a given ip and port |
|---|
| 60 | @param ip: The ip address of the server for the nodes |
|---|
| 61 | @param port: The port, typically 22 |
|---|
| 62 | @param user: The user's username with access to the node |
|---|
| 63 | @param password: The user's password |
|---|
| 64 | ''' |
|---|
| 65 | def stopNodebooter(request, ip, port, user, password): |
|---|
| 66 | client = connect(ip, port, user, password) |
|---|
| 67 | stdin, stdout, stderr = client.exec_command('killall nodeBooter') |
|---|
| 68 | return render_to_response('DomainMan/console.html', locals(), context_instance=RequestContext(request)) |
|---|
| 69 | |
|---|
| 70 | ''' |
|---|
| 71 | Testing view to check if nodebooter is running. |
|---|
| 72 | Checks if nodeBooter is running, if not, starts it. |
|---|
| 73 | @param ip: The ip address of the server for the nodes |
|---|
| 74 | @param port: The port, typically 22 |
|---|
| 75 | @param user: The user's username with access to the node |
|---|
| 76 | @param password: The user's password |
|---|
| 77 | ''' |
|---|
| 78 | def nodeBooter_page(request, ip, port, user, password): |
|---|
| 79 | if 'ip' in request.POST and 'port' in request.POST and 'user' in request.POST and 'pass' in request.POST: |
|---|
| 80 | client = connect(ip, port, user, password) |
|---|
| 81 | else: |
|---|
| 82 | raise Exception('client could not connect with POST variables') |
|---|
| 83 | if not nodeBooter_running(client): |
|---|
| 84 | stdin, stdout, stderr = client.exec_command('cd /sdr; nodeBooter -D -d dev/nodes/default_GPP_node/DeviceManager.dcd.xml') |
|---|
| 85 | runningNodebooter = nodeBooter_running(client) |
|---|
| 86 | return render_to_response('DomainMan/console.html', locals(), context_instance=RequestContext(request)) |
|---|
| 87 | |
|---|
| 88 | """Connect to a nodebooter through a form |
|---|
| 89 | """ |
|---|
| 90 | def connect_to_node(request): |
|---|
| 91 | if 'floor' in request.POST and 'number' in request.POST and 'user' in request.POST and 'pass' in request.POST: |
|---|
| 92 | postFloor = int(request.POST['floor']) |
|---|
| 93 | postNum = int(request.POST['number']) |
|---|
| 94 | user = str(request.POST['user']) |
|---|
| 95 | password = str(request.POST['pass']) |
|---|
| 96 | |
|---|
| 97 | thisNode = Node(postFloor, postNum) |
|---|
| 98 | #ip = thisNode.ip |
|---|
| 99 | ip = "128.173.221.40" |
|---|
| 100 | port = thisNode.port |
|---|
| 101 | |
|---|
| 102 | client = connect(ip, port, user, password) |
|---|
| 103 | stdin, stdout, stderr = client.exec_command('ls') |
|---|
| 104 | runningNodebooter = nodeBooter_running(client) |
|---|
| 105 | else: |
|---|
| 106 | message = 'Missing ssh connection parameters' |
|---|
| 107 | return render_to_response('DomainMan/runNodeBooter.html', locals(), context_instance=RequestContext(request)) |
|---|
| 108 | |
|---|
| 109 | """Returns connection form |
|---|
| 110 | """ |
|---|
| 111 | def connect_to_node_view(request): |
|---|
| 112 | return render_to_response('DomainMan/connect.html', RequestContext(request)) |
|---|
| 113 | |
|---|
| 114 | def ajax_example(request): |
|---|
| 115 | if not request.method == 'GET': |
|---|
| 116 | name = "did not work" |
|---|
| 117 | print request.GET |
|---|
| 118 | return render_to_response('DomainMan/ajax_example.html', locals(), context_instance=RequestContext(request)) |
|---|
| 119 | response_dict = {} |
|---|
| 120 | name = request.GET.get('name', False) |
|---|
| 121 | total = request.GET.get('total', False) |
|---|
| 122 | response_dict.update({'name': name, 'total': total}) |
|---|
| 123 | if total: |
|---|
| 124 | try: |
|---|
| 125 | total = int(total) |
|---|
| 126 | except: |
|---|
| 127 | total = False |
|---|
| 128 | if name and total and int(total) == 10: |
|---|
| 129 | response_dict.update({'success': True }) |
|---|
| 130 | else: |
|---|
| 131 | response_dict.update({'errors': {}}) |
|---|
| 132 | if not name: |
|---|
| 133 | response_dict['errors'].update({'name': 'This field is required'}) |
|---|
| 134 | if not total and total is not False: |
|---|
| 135 | response_dict['errors'].update({'total': 'This field is required'}) |
|---|
| 136 | elif int(total) != 10: |
|---|
| 137 | response_dict['errors'].update({'total': 'Incorrect total'}) |
|---|
| 138 | return render_to_response('DomainMan/ajax_example.html', response_dict, context_instance=RequestContext(request)) |
|---|