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

Revision 11096, 7.5 KB (checked in by edent, 13 months ago)

change nodes.html javascript call

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 *
7from WebDash.views import index2
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, floor, num, user, password):
61    floor = int(floor)
62    num = int(num)
63    node = Node(floor, num)
64    ip = node.ip
65    port = node.port
66    client = connect(ip, port, user, password)
67    #stdin, stdout, stderr = client.exec_command('cd /sdr; nodeBooter -D -d dev/nodes/default_GPP_node/DeviceManager.dcd.xml')
68    client.exec_command('cd /sdr ; nodeBooter -D -d dev/nodes/default_GPP_node/DeviceManager.dcd.xml')
69    runningNodebooter = nodeBooter_running(client)
70    return render_to_response('DomainMan/runningNodebooter.html', locals(), context_instance=RequestContext(request))
71
72"""Start nodebooter with premade client from client list
73"""
74#def startNodebooter(request, floor, number):
75#    clientID = str(postFloor).__add__("-").__add__(str(postNum))
76#    client = SSHClientList.getClient(clientID)
77#    #stdin, stdout, stderr = client.exec_command('cd /sdr; nodeBooter -D -d dev/nodes/default_GPP_node/DeviceManager.dcd.xml')
78#    client.exec_command('cd /sdr; nodeBooter -D -d dev/nodes/default_GPP_node/DeviceManager.dcd.xml')
79#    runningNodebooter = nodeBooter_running(client)
80#    return render_to_response('DomainMan/runningNodebooter.html', locals(), context_instance=RequestContext(request))
81
82'''
83Kills all nodeBooter processes 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'''
89def stopNodebooter(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('killall nodeBooter && killall GPP')
97    runningNodebooter = nodeBooter_running(client)
98    return render_to_response('DomainMan/runningNodebooter.html', locals(), context_instance=RequestContext(request))
99
100'''
101Testing view to check if nodebooter is running.
102Checks if nodeBooter is running, if not, starts it.
103@param ip: The ip address of the server for the nodes
104@param port: The port, typically 22
105@param user: The user's username with access to the node
106@param password: The user's password
107'''
108def nodeBooter_page(request, ip, port, user, password):
109    if 'ip' in request.POST and 'port' in request.POST and 'user' in request.POST and 'pass' in request.POST:
110        client = connect(ip, port, user, password)
111    else:
112        raise Exception('client could not connect with POST variables')
113    if not nodeBooter_running(client):
114        stdin, stdout, stderr = client.exec_command('cd /sdr; nodeBooter -D -d dev/nodes/default_GPP_node/DeviceManager.dcd.xml')
115    runningNodebooter = nodeBooter_running(client)
116    return render_to_response('DomainMan/console.html', locals(), context_instance=RequestContext(request))
117
118"""
119Views the available nodeBooters to start
120"""
121def selectNodeboter():
122    return
123
124"""
125Connect to a node through a form
126"""
127def connect_to_node(request):
128    if 'floor' in request.POST and 'number' in request.POST and 'user' in request.POST and 'pass' in request.POST:
129        floor = int(request.POST['floor'])
130        num = int(request.POST['number'])
131        user = str(request.POST['user'])
132        password = str(request.POST['pass'])
133       
134        node = Node(floor, num)
135        ip = node.ip
136        port = node.port
137        client = connect(ip, port, user, password)
138        runningNodebooter = nodeBooter_running(client)
139    else:
140        message = 'Missing ssh connection parameters'
141        print message
142    return render_to_response('DomainMan/runningNodebooter.html', locals(), context_instance=RequestContext(request))
143
144"""
145Raise exception if the inputs of the floor and node number are invalid
146"""
147def testValidInputs(floor, num):
148    if floor < 1 or floor > 4:
149        raise Exception("Floor must be between 1 and 4")
150    if num < 1 or num > 12:
151        raise Exception("Node must be between 1 and 12")
152
153#@login_required
154def loadNode(request, floor_num, node_num):
155    floor = int(floor_num)
156    num = int(node_num)
157    testValidInputs(floor, num)
158    node = Node(floor, num)
159    ip = node.ip
160    port = node.port
161    #index2(request, ip)
162    return render_to_response("DomainMan/nodes.html", locals(), context_instance=RequestContext(request))
163
164"""
165Returns connection form
166"""
167def connect_to_node_view(request, selectFloor, selectNum):
168    number_of_floors = range(1,5)
169    number_of_nodes = range(1,13)
170    selectedFloor=int(selectFloor)
171    selectedNum=int(selectNum)
172    testValidInputs(selectedFloor, selectedNum)
173    return render_to_response('DomainMan/connect.html', locals(), RequestContext(request))
174
175def ajax_example(request):
176    if not request.method == 'GET':
177        name = "did not work"
178        print request.GET
179        return render_to_response('DomainMan/ajax_example.html', locals(), context_instance=RequestContext(request))
180    response_dict = {}
181    name = request.GET.get('name', False)
182    total = request.GET.get('total', False)
183    response_dict.update({'name': name, 'total': total})
184    if total:
185        try:
186            total = int(total)
187        except:
188            total = False
189    if name and total and int(total) == 10:
190        response_dict.update({'success': True })
191    else:
192        response_dict.update({'errors': {}})
193        if not name:
194            response_dict['errors'].update({'name': 'This field is required'})
195        if not total and total is not False:
196            response_dict['errors'].update({'total': 'This field is required'})
197        elif int(total) != 10:
198            response_dict['errors'].update({'total': 'Incorrect total'})
199    return render_to_response('DomainMan/ajax_example.html', response_dict, context_instance=RequestContext(request))
Note: See TracBrowser for help on using the browser.