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

Revision 11093, 7.4 KB (checked in by edent, 14 months ago)

move divs around and connect to webServer from local development

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"""
119Connect to a node through a form
120"""
121def connect_to_node(request):
122    if 'floor' in request.POST and 'number' in request.POST and 'user' in request.POST and 'pass' in request.POST:
123        floor = int(request.POST['floor'])
124        num = int(request.POST['number'])
125        user = str(request.POST['user'])
126        password = str(request.POST['pass'])
127       
128        node = Node(floor, num)
129        ip = node.ip
130        port = node.port
131        client = connect(ip, port, user, password)
132        runningNodebooter = nodeBooter_running(client)
133    else:
134        message = 'Missing ssh connection parameters'
135        print message
136    return render_to_response('DomainMan/runningNodebooter.html', locals(), context_instance=RequestContext(request))
137
138"""
139Raise exception if the inputs of the floor and node number are invalid
140"""
141def testValidInputs(floor, num):
142    if floor < 1 or floor > 4:
143        raise Exception("Floor must be between 1 and 4")
144    if num < 1 or num > 12:
145        raise Exception("Node must be between 1 and 12")
146
147#@login_required
148def loadNode(request, floor_num, node_num):
149    floor = int(floor_num)
150    num = int(node_num)
151    testValidInputs(floor, num)
152    node = Node(floor, num)
153    ip = node.ip
154    port = node.port
155    address = str(ip)+":"+str(port)
156    index2(request, address)
157    return render_to_response("DomainMan/nodes.html", locals(), context_instance=RequestContext(request))
158
159"""
160Returns connection form
161"""
162def connect_to_node_view(request, selectFloor, selectNum):
163    number_of_floors = range(1,5)
164    number_of_nodes = range(1,13)
165    selectedFloor=int(selectFloor)
166    selectedNum=int(selectNum)
167    testValidInputs(selectedFloor, selectedNum)
168    return render_to_response('DomainMan/connect.html', locals(), RequestContext(request))
169
170def ajax_example(request):
171    if not request.method == 'GET':
172        name = "did not work"
173        print request.GET
174        return render_to_response('DomainMan/ajax_example.html', locals(), context_instance=RequestContext(request))
175    response_dict = {}
176    name = request.GET.get('name', False)
177    total = request.GET.get('total', False)
178    response_dict.update({'name': name, 'total': total})
179    if total:
180        try:
181            total = int(total)
182        except:
183            total = False
184    if name and total and int(total) == 10:
185        response_dict.update({'success': True })
186    else:
187        response_dict.update({'errors': {}})
188        if not name:
189            response_dict['errors'].update({'name': 'This field is required'})
190        if not total and total is not False:
191            response_dict['errors'].update({'total': 'This field is required'})
192        elif int(total) != 10:
193            response_dict['errors'].update({'total': 'Incorrect total'})
194    return render_to_response('DomainMan/ajax_example.html', response_dict, context_instance=RequestContext(request))
Note: See TracBrowser for help on using the browser.