root/ossiedev/branches/jsnyder/trunk/tools/cornetApps/DomainMan/views.py @ 11091

Revision 11091, 6.7 KB (checked in by edent, 14 months ago)

working from webserver

Line 
1
2from django.shortcuts import render_to_response
3from django.http import HttpResponse, Http404
4from django.template import RequestContext
5from paramiko import SSHClient, AutoAddPolicy
6from models import *
7import time
8
9"""Views for DomainMan application
10Starts and stops nodeBooter/naming service through paramiko
11"""
12
13"""
14Testing login
15"""
16def cornet_login(request):
17    user = request.user
18    return render_to_response('DomainMan/cornet_login.html', locals(), context_instance=RequestContext(request))
19
20"""
21Connects to a server through a given ip, port and associated username and password
22Returns the SSHClient connected to the server
23"""
24def 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'''
34Tests if nodebooter is running.
35@param client: The SSHClient connected to a server/node
36@return: A boolean indicating if nodebooter is running
37'''
38def 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'''
54Starts 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'''
60def 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'''
78Kills 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'''
84def 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'''
91Testing view to check if nodebooter is running.
92Checks 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'''
98def 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"""
110def 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"""Returns connection form
133"""
134def connect_to_node_view(request):
135    number_of_floors = range(1,5)
136    number_of_nodes = range(1,13)
137    return render_to_response('DomainMan/connect.html', locals(), RequestContext(request))
138
139def ajax_example(request):
140    if not request.method == 'GET':
141        name = "did not work"
142        print request.GET
143        return render_to_response('DomainMan/ajax_example.html', locals(), context_instance=RequestContext(request))
144    response_dict = {}
145    name = request.GET.get('name', False)
146    total = request.GET.get('total', False)
147    response_dict.update({'name': name, 'total': total})
148    if total:
149        try:
150            total = int(total)
151        except:
152            total = False
153    if name and total and int(total) == 10:
154        response_dict.update({'success': True })
155    else:
156        response_dict.update({'errors': {}})
157        if not name:
158            response_dict['errors'].update({'name': 'This field is required'})
159        if not total and total is not False:
160            response_dict['errors'].update({'total': 'This field is required'})
161        elif int(total) != 10:
162            response_dict['errors'].update({'total': 'Incorrect total'})
163    return render_to_response('DomainMan/ajax_example.html', response_dict, context_instance=RequestContext(request))
Note: See TracBrowser for help on using the browser.