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

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

add wait time to recognize nodeBooter

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    time.sleep(1)
65    runningNodebooter = nodeBooter_running(client)
66    return render_to_response('DomainMan/runningNodebooter.html', locals(), context_instance=RequestContext(request))
67
68"""Start nodebooter with premade client from client list
69"""
70#def startNodebooter(request, floor, number):
71#    clientID = str(postFloor).__add__("-").__add__(str(postNum))
72#    client = SSHClientList.getClient(clientID)
73#    #stdin, stdout, stderr = client.exec_command('cd /sdr; nodeBooter -D -d dev/nodes/default_GPP_node/DeviceManager.dcd.xml')
74#    client.exec_command('cd /sdr; nodeBooter -D -d dev/nodes/default_GPP_node/DeviceManager.dcd.xml')
75#    runningNodebooter = nodeBooter_running(client)
76#    return render_to_response('DomainMan/runningNodebooter.html', locals(), context_instance=RequestContext(request))
77
78'''
79Kills all nodeBooter processes at a given ip and port
80@param ip: The ip address of the server for the nodes
81@param port: The port, typically 22
82@param user: The user's username with access to the node
83@param password: The user's password
84'''
85def stopNodebooter(request, ip, port, user, password):
86    client = connect(ip, port, user, password)
87    stdin, stdout, stderr = client.exec_command('killall nodeBooter && killall GPP')
88    runningNodebooter = nodeBooter_running(client)
89    return render_to_response('DomainMan/runningNodebooter.html', locals(), context_instance=RequestContext(request))
90
91'''
92Testing view to check if nodebooter is running.
93Checks if nodeBooter is running, if not, starts it.
94@param ip: The ip address of the server for the nodes
95@param port: The port, typically 22
96@param user: The user's username with access to the node
97@param password: The user's password
98'''
99def nodeBooter_page(request, ip, port, user, password):
100    if 'ip' in request.POST and 'port' in request.POST and 'user' in request.POST and 'pass' in request.POST:
101        client = connect(ip, port, user, password)
102    else:
103        raise Exception('client could not connect with POST variables')
104    if not nodeBooter_running(client):
105        stdin, stdout, stderr = client.exec_command('cd /sdr; nodeBooter -D -d dev/nodes/default_GPP_node/DeviceManager.dcd.xml')
106    runningNodebooter = nodeBooter_running(client)
107    return render_to_response('DomainMan/console.html', locals(), context_instance=RequestContext(request))
108
109"""Connect to a nodebooter through a form
110"""
111def connect_to_node(request):
112    if 'floor' in request.POST and 'number' in request.POST and 'user' in request.POST and 'pass' in request.POST:
113        postFloor = int(request.POST['floor'])
114        postNum = int(request.POST['number'])
115        user = str(request.POST['user'])
116        password = str(request.POST['pass'])
117       
118        thisNode = Node(postFloor, postNum)
119        ip = thisNode.ip
120        port = thisNode.port
121        #thisNode.save()
122        #thisNode.setClient(user, password)
123        #node = Node.objects.get(floor=4, num=1)
124        client = connect(ip, port, user, password)
125        #stdin, stdout, stderr = client.exec_command('ls')
126        print 'calling runningNodebooter'
127        runningNodebooter = nodeBooter_running(client)
128    else:
129        message = 'Missing ssh connection parameters'
130        print message
131    return render_to_response('DomainMan/runningNodebooter.html', locals(), context_instance=RequestContext(request))
132
133"""Returns connection form
134"""
135def connect_to_node_view(request):
136    number_of_floors = range(1,5)
137    number_of_nodes = range(1,13)
138    return render_to_response('DomainMan/connect.html', locals(), RequestContext(request))
139
140def ajax_example(request):
141    if not request.method == 'GET':
142        name = "did not work"
143        print request.GET
144        return render_to_response('DomainMan/ajax_example.html', locals(), context_instance=RequestContext(request))
145    response_dict = {}
146    name = request.GET.get('name', False)
147    total = request.GET.get('total', False)
148    response_dict.update({'name': name, 'total': total})
149    if total:
150        try:
151            total = int(total)
152        except:
153            total = False
154    if name and total and int(total) == 10:
155        response_dict.update({'success': True })
156    else:
157        response_dict.update({'errors': {}})
158        if not name:
159            response_dict['errors'].update({'name': 'This field is required'})
160        if not total and total is not False:
161            response_dict['errors'].update({'total': 'This field is required'})
162        elif int(total) != 10:
163            response_dict['errors'].update({'total': 'Incorrect total'})
164    return render_to_response('DomainMan/ajax_example.html', response_dict, context_instance=RequestContext(request))
Note: See TracBrowser for help on using the browser.