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

Revision 11070, 4.2 KB (checked in by edent, 2 years ago)

add more documentation

Line 
1
2from django.shortcuts import render_to_response
3from django.http import HttpResponse
4from django.template import RequestContext
5from paramiko import SSHClient, AutoAddPolicy
6
7"""Views for DomainMan application
8Starts and stops nodeBooter/naming service
9"""
10
11"""
12Testing login
13"""
14def cornet_login(request):
15    user = request.user
16    return render_to_response('DomainMan/cornet_login.html', locals(), context_instance=RequestContext(request))
17
18"""
19Connects to a server through a given ip, port and associated username and password
20Returns the SSHClient connected to the server
21"""
22def connect(request, ip, port, user, password):
23    client = SSHClient()
24    client.set_missing_host_key_policy(AutoAddPolicy())
25    client.connect(ip, int(port), user, password)
26    return client
27
28'''
29Tests if nodebooter is running.
30@param client: The SSHClient connected to a server/node
31@return: A boolean indicating if nodebooter is running
32'''
33def nodeBooter_running(client):
34    stdin, stdout, stderr = client.exec_command('ps x | grep nodeBooter')
35    processes = stdout.readlines()
36    for proc in processes:
37        if 'dev/nodes' in proc and 'DeviceManager.dcd' in proc:
38            runningNodebooter = 1
39            break
40        else:
41            runningNodebooter = 0
42#    runningNodebooter = 'dev/nodes/' in processes
43    return runningNodebooter
44
45'''
46Starts nodeBooter at a given ip and port
47@param ip: The ip address of the server for the nodes
48@param port: The port, typically 22
49@param user: The user's username with access to the node
50@param password: The user's password
51'''
52def startNodebooter(request, ip, port, user, password):
53    client = connect(request, ip, port, user, password)
54    stdin, stdout, stderr = client.exec_command('cd /sdr; nodeBooter -D -d dev/nodes/default_GPP_node/DeviceManager.dcd.xml')
55    return render_to_response('DomainMan/console.html', locals(), context_instance=RequestContext(request))
56
57'''
58Kills all nodeBooter processes at a given ip and port
59@param ip: The ip address of the server for the nodes
60@param port: The port, typically 22
61@param user: The user's username with access to the node
62@param password: The user's password
63'''
64def stopNodebooter(request, ip, port, user, password):
65    client = connect(request, ip, port, user, password)
66    stdin, stdout, stderr = client.exec_command('killall nodeBooter')
67    return render_to_response('DomainMan/console.html', locals(), context_instance=RequestContext(request))
68
69'''
70Testing view to check if nodebooter is running.
71Checks if nodeBooter is running, if not, starts it.
72@param ip: The ip address of the server for the nodes
73@param port: The port, typically 22
74@param user: The user's username with access to the node
75@param password: The user's password
76'''
77def nodeBooter_page(request, ip, port, user, password):
78    client = connect(request, ip, port, user, password)
79    if not nodeBooter_running(client):
80        stdin, stdout, stderr = client.exec_command('cd /sdr; nodeBooter -D -d dev/nodes/default_GPP_node/DeviceManager.dcd.xml')
81    runningNodebooter = nodeBooter_running(client)
82    return render_to_response('DomainMan/console.html', locals(), context_instance=RequestContext(request))
83
84def ajax_example(request):
85    if not request.method == 'POST':
86        name = "did not work"
87        print request.POST
88        return render_to_response('DomainMan/ajax_example.html', locals(), context_instance=RequestContext(request))
89    response_dict = {}
90    name = request.POST.get('name', False)
91    total = request.POST.get('total', False)
92    response_dict.update({'name': name, 'total': total})
93    if total:
94        try:
95            total = int(total)
96        except:
97            total = False
98    if name and total and int(total) == 10:
99        response_dict.update({'success': True })
100    else:
101        response_dict.update({'errors': {}})
102        if not name:
103            response_dict['errors'].update({'name': 'This field is required'})
104        if not total and total is not False:
105            response_dict['errors'].update({'total': 'This field is required'})
106        elif int(total) != 10:
107            response_dict['errors'].update({'total': 'Incorrect total'})
108    return render_to_response('DomainMan/ajax_example.html', response_dict, context_instance=RequestContext(request))
Note: See TracBrowser for help on using the browser.