root/ossiedev/branches/jsnyder/trunk/tools/cornetApps/DomainMan/models.py @ 11092

Revision 11092, 3.2 KB (checked in by edent, 14 months ago)

add ports and ip when loading node

Line 
1from django.db import models
2from os import environ
3from paramiko import SSHClient, AutoAddPolicy
4"""Models for the DomainMan application
5-Node makes it easy to extract the ip and port fields
6"""
7
8
9"""Creates a node with ip and port fields
10"""
11class Node(models.Model):
12    ip = models.IPAddressField()
13    port=models.IntegerField()
14    floor=models.IntegerField()
15    num=models.IntegerField()
16
17    """
18    Constructor defines its internal ip address and port number for the node
19    @param floor: The floor for the node
20    @param num:  The number of the node
21    """
22    def __init__(self, thisFloor, thisNum):
23        self.ip = self.getIP(thisFloor, thisNum)
24        self.port = self.getPort(thisFloor,thisNum)
25        self.floor = thisFloor
26        self.num = thisNum
27       
28    def __unicode__(self):
29        return unicode("node_id: " + str(self.floor)+ "-" + str(self.num))
30   
31    def setClient(self, user, password):
32        c = SSHClient()
33        c.set_missing_host_key_policy(AutoAddPolicy())
34        self.client = c.connect(self.ip, self.port, user, password)
35   
36    """Calculates the internal ip address of the node based on the ICTAS floor and number
37   
38    @param floor: The floor of ICTAS where the node is located
39    @param num:  The number of the node on the particular floor
40    @return: The ip address used in the CORNET server to access the node;
41            Through an external network, returns address to cornet server;
42            exception if params are not in the range
43    """
44    def getIP(self, floor, num):
45        if floor > 0 and floor < 5:
46            if num > 0 and num < 13:
47                if environ['DJANGO_SETTINGS_MODULE'] == 'cornetApps.settings':
48                    return "128.173.221.40"
49                ip_num=floor*12-2+num
50                return "192.168.1.".__add__(str(ip_num))
51            else:
52                raise Exception("Num input must be greater than 0 and less than 13")
53        else:
54            raise Exception("Floor input must be greater than 0 and less than 5")
55           
56    """Calculates the port number based on the floor and node number of the floor
57   
58    @param floor: The floor of ICTAS where the node is located
59    @param num:  The number of the node on the particular floor
60    @return: The port number used in the CORNET server to access the node; exception if params are not in the range
61    """
62    def getPort(self, floor, num):
63        if floor > 0 and floor < 5:
64            if num > 0 and num < 13:
65                if environ['DJANGO_SETTINGS_MODULE'] == 'cornetApps.settingsWeb':
66                    return 22
67                port_num = (floor - 1) * 12 + num
68                return 7000+port_num
69            else:
70                raise Exception("Num input must be greater than 0 and less than 13")
71        else:
72            raise Exception("Floor input must be greater than 0 and less than 5")
73       
74"""Connects to a server through a given ip, port and associated username and password
75Returns the SSHClient connected to the server
76"""
77def connect(ip, port, user, password):
78    client = SSHClient()
79    client.set_missing_host_key_policy(AutoAddPolicy())
80    try:
81        client.connect(ip, int(port), user, password)
82    except:
83        raise Http404(u'Incorrect login information')
84    return client
Note: See TracBrowser for help on using the browser.