Revisión 73
Añadido por Francisco Damián Méndez Palma hace alrededor de 14 años
Hosts.py | ||
---|---|---|
import ldap
|
||
import logging
|
||
from math import ceil
|
||
from operator import itemgetter
|
||
|
||
class Hosts(object):
|
||
|
||
... | ... | |
def getName (self):
|
||
return self.mac
|
||
|
||
def validation(self):
|
||
def validation(self,action):
|
||
|
||
if action == "add":
|
||
if self.type == "none":
|
||
return "type"
|
||
|
||
if self.name == "":
|
||
return "name"
|
||
|
||
if self.type <> 'thinclient':
|
||
if self.ip == "":
|
||
return "ip"
|
||
|
||
if self.ip == "":
|
||
return "ip"
|
||
|
||
if self.mac == "":
|
||
return "mac"
|
||
|
||
|
||
if self.type == "":
|
||
return "type"
|
||
|
||
return "OK"
|
||
if action == "add":
|
||
if self.existsHostname():
|
||
return "hostAlreadyExists"
|
||
|
||
# thinclients no requieren ip, la cogen dinamicamente en el aula
|
||
if self.type <> "thinclient":
|
||
if self.existsIP():
|
||
return "ipAlreadyExists"
|
||
|
||
if self.existsMAC():
|
||
return "macAlreadyExists"
|
||
|
||
elif action == "modify":
|
||
return "OK"
|
||
|
||
return "OK"
|
||
|
||
def process(self,action):
|
||
import pdb
|
||
if action == "add":
|
||
val = self.validation()
|
||
val = self.validation(action)
|
||
|
||
if val != "OK":
|
||
return val
|
||
... | ... | |
|
||
def list(self,args):
|
||
|
||
#Distinguimos entre hosts ltsp, workstations y portatiles
|
||
print "Estoy en Hosts.py"
|
||
#from Plugins.LdapConnection import LdapConnection
|
||
|
||
#l = LdapConnection("172.23.36.5",'cn=admin,ou=People,dc=instituto,dc=extremadura,dc=es',"Sta1987teleco")
|
||
#l.connect()
|
||
|
||
# grid parameters
|
||
limit = int(args['rows'][0])
|
||
page = int(args['page'][0])
|
||
start = limit * page - limit
|
||
finish = start + limit;
|
||
|
||
# sort by field
|
||
sortBy = args['sidx'][0]
|
||
#if sortBy == "uid":
|
||
#sortBy = "id"
|
||
|
||
# reverse Sort
|
||
reverseSort = False
|
||
if args['sord'][0] == "asc":
|
||
reverseSort = True
|
||
|
||
#Distinguimos entre hosts ltsp, workstations y portatiles
|
||
type = args['type'][0]
|
||
|
||
if type == "LTSP":
|
||
search = self.ldap.search("ou=hosts","cn=*",["cn","ipHostNumber","macAddress"])
|
||
elif type == "thinClient":
|
||
search = self.ldap.search("ou=hosts","cn=*",["cn","ipHostNumber","macAddress"])
|
||
|
||
if type == "ltsp":
|
||
# Obtengo todos los elementos del nodo hosts
|
||
#search = l.search("ou=hosts","cn=*",["cn","ipHostNumber","macAddress"])
|
||
#filter = self.buildFilter(args)
|
||
search = self.ldap.search("ou=hosts","cn=*",["cn","ipHostNumber","macAddress"])
|
||
|
||
start = limit * page - limit
|
||
finish = start + limit;
|
||
# triplets que contiene los nombres de los ltsp-servers
|
||
#triplets = l.search("ou=Netgroup","cn=ltsp-server-hosts",["nisNetgroupTriple"])
|
||
triplets = self.ldap.search("ou=Netgroup","cn=ltsp-server-hosts",["nisNetgroupTriple"])
|
||
triplets = triplets [0][0][1]["nisNetgroupTriple"]
|
||
hostnames=list()
|
||
|
||
for node in triplets:
|
||
name = node.replace(",-,-)","").replace("(","")
|
||
hostnames.append(name)
|
||
|
||
# Ahora tengo que quedarme con los elementos de search que estan en hostnames: los que son ltsp
|
||
resultado=list()
|
||
for element in search:
|
||
if element[0][1]["cn"][0] in hostnames:
|
||
resultado.append(element)
|
||
|
||
search = resultado
|
||
|
||
if len(search) > 0:
|
||
totalPages = ceil( len(search) / int(limit) )
|
||
else:
|
||
totalPages = 0
|
||
if page > totalPages:
|
||
page = totalPages
|
||
|
||
rows = []
|
||
for i in search[start:finish]:
|
||
row = {
|
||
"id":i[0][1]["cn"][0],
|
||
"cell":[i[0][1]["cn"][0], i[0][1]["ipHostNumber"][0],i[0][1]["macAddress"]],
|
||
"cn":i[0][1]["cn"][0],
|
||
"ipHostNumber":i[0][1]["ipHostNumber"][0],
|
||
"macAddress":i[0][1]["macAddress"][0]
|
||
}
|
||
#row = { "cn":i[0][0], "cell":[i[0][1]["cn"][0], i[0][1]["ipHostNumber"][0],i[0][1]["macAddress"]]}
|
||
rows.append(row)
|
||
|
||
result = sorted(rows, key=itemgetter(sortBy), reverse=reverseSort)
|
||
return { "page":page, "total":totalPages, "records":len(search), "rows":result }
|
||
|
||
elif type == "thinclient":
|
||
#import pdb
|
||
#search = l.search("cn=THINCLIENTS,cn=DHCP Config", "cn=*",["cn","dhcpHWAddress"])
|
||
search = self.ldap.search("cn=THINCLIENTS,cn=DHCP Config","cn=*",["cn","dhcpHWAddress"])
|
||
#search[6][0][1]["cn"][0]
|
||
|
||
if len(search) > 0:
|
||
totalPages = ceil( len(search) / int(limit) )
|
||
else:
|
||
totalPages = 0
|
||
if page > totalPages:
|
||
page = totalPages
|
||
|
||
rows = []
|
||
# esto hay que cambiarlo: tenemos 4 groups en thinclientes
|
||
for i in search[6:finish]:
|
||
row = {
|
||
"id":i[0][1]["cn"][0],
|
||
"cell":[i[0][1]["cn"][0], i[0][1]["dhcpHWAddress"]],
|
||
"cn":i[0][1]["cn"][0],
|
||
"dhcpHWAddress":i[0][1]["dhcpHWAddress"][0]
|
||
}
|
||
#row = { "cn":i[0][0], "cell":[i[0][1]["cn"][0], i[0][1]["dhcpHWAddress"][0]]}
|
||
rows.append(row)
|
||
|
||
result = sorted(rows, key=itemgetter(sortBy), reverse=reverseSort)
|
||
return { "page":page, "total":totalPages, "records":len(search), "rows":result }
|
||
|
||
if len(search) > 0:
|
||
totalPages = ceil( len(search) / int(limit) )
|
||
else:
|
||
totalPages = 0
|
||
elif type == "workstation":
|
||
# Obtengo todos los elementos del nodo hosts
|
||
#search = l.search("ou=hosts","cn=*",["cn","ipHostNumber","macAddress"])
|
||
search = self.ldap.search("ou=hosts","cn=*",["cn","ipHostNumber","macAddress"])
|
||
# triplets que contiene los nombres de las workstations
|
||
#triplets = l.search("ou=Netgroup","cn=workstation-hosts",["nisNetgroupTriple"])
|
||
triplets = self.ldap.search("ou=Netgroup","cn=workstation-hosts",["nisNetgroupTriple"])
|
||
triplets = triplets [0][0][1]["nisNetgroupTriple"]
|
||
hostnames=list()
|
||
|
||
# obtengo lista de nombres de los hosts workstation
|
||
for node in triplets:
|
||
name = node.replace(",-,-)","").replace("(","")
|
||
hostnames.append(name)
|
||
|
||
# Ahora tengo que quedarme con los elementos de search que estan en hostnames
|
||
resultado=list()
|
||
for element in search:
|
||
if element[0][1]["cn"][0] in hostnames:
|
||
resultado.append(element)
|
||
|
||
search = resultado
|
||
|
||
if page > totalPages:
|
||
page = totalPages
|
||
# print search[0][0][1]["uidNumber"][0]
|
||
rows = []
|
||
for i in search[start:finish]:
|
||
row = { "cn":i[0][0], "cell":[i[0][1]["cn"][0], i[0][1]["ipHostNumber"][0],i[0][1]["macAddress"]]}
|
||
rows.append(row)
|
||
if len(search) > 0:
|
||
totalPages = ceil( len(search) / int(limit) )
|
||
else:
|
||
totalPages = 0
|
||
if page > totalPages:
|
||
page = totalPages
|
||
|
||
rows = []
|
||
for i in search[start:finish]:
|
||
row = {
|
||
"id":i[0][1]["cn"][0],
|
||
"cell":[i[0][1]["cn"][0], i[0][1]["ipHostNumber"][0],i[0][1]["macAddress"]],
|
||
"cn":i[0][1]["cn"][0],
|
||
"ipHostNumber":i[0][1]["ipHostNumber"][0],
|
||
"macAddress":i[0][1]["macAddress"][0]
|
||
}
|
||
#row = { "cn":i[0][0], "cell":[i[0][1]["cn"][0], i[0][1]["ipHostNumber"][0],i[0][1]["macAddress"]]}
|
||
rows.append(row)
|
||
|
||
return { "page":page, "total":totalPages, "records":len(search), "rows":rows }
|
||
result = sorted(rows, key=itemgetter(sortBy), reverse=reverseSort)
|
||
return { "page":page, "total":totalPages, "records":len(search), "rows":result }
|
||
|
||
def add(self):
|
||
record = [
|
||
('objectclass', ['person','organizationalperson','inetorgperson']),
|
||
('uid', ['francis']),
|
||
('cn', [self.name] ),
|
||
('sn', ['Bacon'] ),
|
||
('userpassword', [self.password]),
|
||
('ou', ['users'])
|
||
]
|
||
try:
|
||
self.ldap.add("cn=hosts", record)
|
||
except ldap.ALREADY_EXISTS:
|
||
return "fail"
|
||
#record = [
|
||
#('objectclass', ['person','organizationalperson','inetorgperson']),
|
||
#('uid', ['francis']),
|
||
#('cn', [self.name] ),
|
||
#('sn', ['Bacon'] ),
|
||
#('userpassword', [self.password]),
|
||
#('ou', ['users'])
|
||
#]
|
||
#try:
|
||
# self.ldap.add("cn=hosts", record)
|
||
#except ldap.ALREADY_EXISTS:
|
||
# return "fail"
|
||
|
||
return "OK"
|
||
|
||
... | ... | |
from twisted.internet import defer
|
||
from Plugins import NetworkUtils
|
||
NetworkUtils.startup(self.mac)
|
||
|
||
|
||
def existsHostname(self):
|
||
|
||
if self.type == 'thinclient':
|
||
result = self.ldap.search("ou=hosts","cn="+self.name,["cn"])
|
||
else:
|
||
result = self.ldap.search("ou=hosts","cn="+self.name,["cn"])
|
||
|
||
if len(result) > 0:
|
||
return True
|
||
|
||
return False
|
||
|
||
def existsMAC(self):
|
||
import pdb
|
||
# Compruebo con las macs de la rama hosts
|
||
pdb.set_trace()
|
||
if self.type == 'thinclient':
|
||
search = self.ldap.search("cn=THINCLIENTS,cn=DHCP Config","cn=a*",["dhcpHWAddress"])
|
||
|
||
#print search [0][0][1]['dhcpHWAddress'][0]
|
||
|
||
pdb.set_trace()
|
||
result = self.ldap.search("ou=hosts","macAddress="+self.mac,["macAddress"])
|
||
else:
|
||
result = self.ldap.search("ou=hosts","macAddress="+self.mac,["macAddress"])
|
||
|
||
if len(result) > 0:
|
||
return True
|
||
|
||
return False
|
||
|
||
def existsIP (self):
|
||
# Cojo las ips de la rama hosts -> arpa -> in-addr
|
||
result = self.ldap.search ("dc=23,dc=172,dc=in-addr,dc=arpa,ou=hosts", "dc=*",["associatedDomain"])
|
||
|
||
myIP = self.ip.split (".")
|
||
|
||
for i in range (0, len (result) -1):
|
||
reverseIP = result [i][0][1]['associatedDomain'][0].replace (".in-addr.arpa","").split(".")
|
||
reverseIP.reverse()
|
||
if myIP == reverseIP:
|
||
return True
|
||
|
||
return False
|
||
|
||
|
||
# def wakeup(self):
|
||
# from twisted.internet.task import LoopingCall
|
||
# from twisted.internet import defer
|
Exportar a: Unified diff
Validaciones en formularios de maquinas