Proyecto

General

Perfil

« Anterior | Siguiente » 

Revisión 213

Thinclients

Ver diferencias:

controlies/trunk/applications/controlies/modules/Thinclients.py
##############################################################################
# -*- coding: utf-8 -*-
# Project: ControlIES
# Module: Thinclients.py
# Purpose: Thinclients class
# Language: Python 2.5
# Date: 4-Oct-2011.
# Ver: 4-Oct-2011.
# Author: Manuel Mora Gordillo
# Copyright: 2011 - Manuel Mora Gordillo <manuito @no-spam@ gmail.com>
#
# ControlIES is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# ControlIES is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with ControlIES. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import ldap
import logging
import time
from math import ceil
from operator import itemgetter
from Utils import ValidationUtils
class Thinclients(object):
def __init__(self):
pass
def __init__(self,ldap,name,mac):
self.ldap = ldap
self.name = name
self.mac = mac
def validation(self,action):
if self.name == "":
return "name"
if action == "add":
if self.existsHostname():
return "hostAlreadyExists"
if self.mac == "":
return "mac"
if not ValidationUtils.validMAC(self.mac):
return "mac"
if action == "add":
if self.existsMAC():
return "macAlreadyExists"
elif action == "modify":
if self.groupOverflow(300):
return "groupOverflow"
if not self.equalMAC():
if self.existsMAC():
return "macAlreadyExists"
return "OK"
def process(self,action):
if action == "add":
val = self.validation(action)
if val != "OK":
return val
else:
response = self.add()
return response
if action == "modify":
val = self.validation(action)
if val != "OK":
return val
else:
response = self.modify()
return response
if action == "delete":
response = self.delete()
return response
if action == "list":
response = self.list();
return response
def list(self,args):
# grid parameters
limit = int(args['rows'])
page = int(args['page'])
start = limit * page - limit
finish = start + limit;
# sort by field
sortBy = args['sidx']
#if sortBy == "uid":
#sortBy = "id"
# reverse Sort
reverseSort = False
if args['sord'] == "asc":
reverseSort = True
search = self.ldap.search("cn=THINCLIENTS,cn=DHCP Config","cn=*",["cn","dhcpHWAddress"])
filter="(|(dhcpOption=*subnet*)(dhcpOption=*log*))"
rows = []
# esto hay que cambiarlo: tenemos 4 groups en thinclientes
for i in search[6:len(search)]:
nodeinfo=i[0][0].replace ("cn=","").split(",")
row = {
"id":i[0][1]["cn"][0],
"cell":[i[0][1]["cn"][0], i[0][1]["dhcpHWAddress"][0].replace("ethernet ",""), nodeinfo[1]],
"cn":i[0][1]["cn"][0],
"dhcpHWAddress":i[0][1]["dhcpHWAddress"][0],
"groupName":i[0][1]["dhcpHWAddress"][0]
}
rows.append(row)
if len(rows) > 0:
totalPages = ceil( len(rows) / int(limit) )
else:
totalPages = 0
if page > totalPages:
page = totalPages
result = sorted(rows, key=itemgetter(sortBy), reverse=reverseSort)
return { "page":page, "total":totalPages, "records":len(rows), "rows":result[start:finish] }
def add(self):
attr = [
('objectclass', ['top','dhcpHost']),
('cn', [self.name] ),
('dhcpStatements', ['filename "/var/lib/tftpboot/ltsp/i386/pxelinux.0"'] ),
('dhcpHWAddress', ['ethernet ' + self.mac] )
]
freeGroup = self.getFreeGroup()
print freeGroup
self.ldap.add("cn="+self.name +",cn="+self.group+",cn=THINCLIENTS,cn=DHCP Config", attr)
return "OK"
def modify(self):
attr = [(ldap.MOD_REPLACE, 'dhcpHWAddress', ['ethernet ' + self.mac])]
self.ldap.modify("cn="+self.name+",cn="+self.group+",cn=THINCLIENTS,cn=DHCP Config", attr)
return "OK"
def delete(self):
self.ldap.delete('cn='+ self.name +',cn=group1,cn=THINCLIENTS,cn=DHCP Config')
return "OK"
def existsHostname(self):
result = self.ldap.search("cn=THINCLIENTS,cn=DHCP Config","cn="+self.name,["cn"])
if len(result) > 0:
return True
return False
def existsMAC(self):
result = self.ldap.search("cn=THINCLIENTS,cn=DHCP Config","dhcpHWAddress=*",["dhcpHWAddress"])
for i in range (0, len(result) - 1):
if result [i][0][1]['dhcpHWAddress'][0].replace ("ethernet ", "") == self.mac:
return True
return False
def equalMAC(self):
result = self.ldap.search("ou=hosts","cn="+self.name,["macAddress"])
if result[0][0][1]['macAddress'][0] == self.mac:
return True
return False
def getName (self):
return self.mac
def getThinclientGroups (self):
groups = []
search = self.ldap.search("cn=THINCLIENTS,cn=DHCP Config","cn=group*",["cn"])
for g in search:
groups.append (g[0][1]["cn"][0])
return { "groups":groups }
def getFreeGroup (self):
freeGroup = []
groups = self.getThinclientGroups()
for g in groups:
if not self.groupOverflow(g,300):
return { "freeGroup":freeGroup }
return { "freeGroup" : freeGroup }
def groupOverflow(self,group,overflow):
if self.type_host == 'thinclient':
search = self.ldap.search("cn="+group+",cn=THINCLIENTS,cn=DHCP Config","cn=*",["cn"])
if len(search)-2 >= overflow:
return True
return False
controlies/trunk/applications/controlies/controllers/thinclients.py
# coding: utf8
from applications.controlies.modules.Thinclients import Thinclients
def index():
#equivalente a list
return dict(message="hello from thinclients.py")
@service.json
def getThinclientGroups():
l=conecta()
h = Hosts (l,"","","","")
response = h.getThinclientGroups()
l.close()
return dict(response=response)
@service.json
def getHostData():
l=conecta()
h = Hosts(l,request.vars['cn'],"","",request.vars['type_host'])
response = h.getHostData()
l.close()
return dict(response=response)
@service.json
@auth.requires_login()
def list_ltspservers():
l=conecta()
h = Hosts (l,"","","","")
response = h.getLTSPServers()
l.close()
return response
@service.json
@auth.requires_login()
def list():
l=conecta()
h = Thinclients (l,"","")
a=request.vars
response = h.list(a)
l.close()
return response
@service.json
@auth.requires_login()
def modify():
l=conecta()
h = Thinclients(l,request.vars['name'],request.vars['mac'])
response=h.process(request.vars['action'])
l.close()
return dict(response=response)
@service.json
@auth.requires_login()
def delete():
l=conecta()
h = Hosts(l,request.vars['cn'],"","",request.vars['type_host'])
response=h.process(request.vars['action'] )
l.close()
return dict(response=response)
#necesaria estas funciones en el controlador para poder cargar las vistas correspondientes:
def form():
return dict()
def ltspservers():
return dict()
def workstations():
return dict()
def thinclients():
return dict()
def form_thinclient():
return dict()
def form_ltspserver():
return dict()
def call():
"""
exposes services. for example:
http://..../[app]/default/call/jsonrpc
decorate with @services.jsonrpc the functions to expose
supports xml, json, xmlrpc, jsonrpc, amfrpc, rss, csv
"""
session.forget()
return service()

Exportar a: Unified diff