ajout migration depuis camembert

This commit is contained in:
Roven Gabriel 2012-10-12 04:03:12 +02:00
parent 3e95b9a9d6
commit 815bc9ff45
15 changed files with 335 additions and 10 deletions

View File

@ -13,6 +13,7 @@ from brie.model.ldap import *
from brie.controllers import auth
from brie.controllers.auth import AuthenticatedBaseController, AuthenticatedRestController
from brie.controllers.import_actions import *
from operator import itemgetter
@ -43,6 +44,21 @@ class EditController(AuthenticatedBaseController):
def interface(self, interface_id):
return self.show.interface(interface_id)
#end def
@expose("brie.templates.edit.import_from")
def import_from(self, room_number):
success = True
message = ""
# try:
Migration.import_all(self.user.ldap_bind, room_number)
# except Exception as ex:
# success = False
# message = str(ex)
#end try
return {"room_number" : room_number, "success" : success, "message" : message }
#end def
#end class
class WifiRestController(AuthenticatedRestController):

View File

@ -0,0 +1,152 @@
# -*- coding: utf-8 -*-
from brie.config import ldap_config
from brie.lib.ldap_helper import *
from brie.lib.camembert_helpers import *
from brie.model import DBSession
from brie.model.camembert import *
from brie.model.ldap import Machine, Member
class Migration:
@staticmethod
def first(target_list, default = None):
for item in target_list:
return item
#end for
return default
#end def
@staticmethod
def import_all(bind, target_id):
Migration.delete_room_member(bind, target_id)
Migration.import_member(bind, target_id)
Migration.import_machines(bind, target_id)
#end def
@staticmethod
def delete_room_member(bind, target_id):
results = DBSession.query(Room).filter(Room.idroom == target_id)
room = Migration.first(results)
if not room is None:
ldap_room = bind.search_first(ldap_config.room_base_dn, "(cn=" + room.name + ")")
print room.name
member_of = {
"x-memberIn" : None
}
try:
bind.delete_attr(ldap_room.dn, member_of)
except:
pass
#end try
#end if
#end def
@staticmethod
def import_member(bind, target_id):
results = DBSession.query(UserPacaterie, Room).filter(UserPacaterie.idroom == Room.idroom).filter(Room.idroom == target_id)
user, room = Migration.first(results)
if user is not None and room is not None:
uid = Translations.to_uid(user.prenom, user.nom)
member = bind.search_first(ldap_config.username_base_dn, "(uid=" + uid + ")")
member_dn = ""
if member is None:
member_dn = "uid=" + uid + ",ou=2012," + ldap_config.username_base_dn
print member_dn
mail = user.mail
if mail is None:
mail = ""
attributes = Member.entry_attr(uid, user.prenom, user.nom, mail, str(-1))
bind.add_entry(member_dn, attributes)
else:
member_dn = member.dn
#end if
ldap_room = bind.search_first(ldap_config.room_base_dn, "(cn=" + room.name + ")")
new_member_of = {
"x-memberIn" : str(member_dn)
}
bind.replace_attr(ldap_room.dn, new_member_of)
#end if
#end def
@staticmethod
def import_machines(bind, target_id):
print target_id
results = DBSession.query(UserPacaterie, Room).filter(UserPacaterie.idroom == Room.idroom).filter(Room.idroom == target_id)
user, room = Migration.first(results)
print room.idroom
print user.idroom
if user is None: return
uid = Translations.to_uid(user.prenom, user.nom)
member = bind.search_first(ldap_config.username_base_dn, "(uid=" + uid + ")")
print user.prenom
print member.dn
machines = DBSession.query(Computer).filter(Computer.iduser == user.iduser)
machine_id = 1
for machine in machines:
print machine_id
print machine.name
print machine.mac
print machine.ip
machine_dn = "cn=" + str(machine_id) + "," + member.dn
existant = bind.search_first(machine_dn, "(objectClass=*)")
if existant is not None:
bind.delete_entry_subtree(existant.dn)
print "deleted : " + existant.dn
machine_attributes = Machine.entry_attr(machine_id)
bind.add_entry(machine_dn, machine_attributes)
dhcp_dn = "cn=" + str(machine.name) + "," + machine_dn
dhcp_attributes = Machine.dhcp_attr(machine.name, machine.mac)
bind.add_entry(dhcp_dn, dhcp_attributes)
mac_auth_dn = "cn=mac," + machine_dn
flat_mac = str(machine.mac).replace(":", "")
mac_auth_attributes = Machine.auth_attr(flat_mac)
bind.add_entry(mac_auth_dn, mac_auth_attributes)
dns_dn = "dlzHostName=" + machine.name + "," + machine_dn
dns_attributes = Machine.dns_attr(machine.name, machine.ip)
bind.add_entry(dns_dn, dns_attributes)
machine_id = 1 + machine_id
#end for machine
#end def
#end class

View File

@ -39,7 +39,9 @@ class ShowController(AuthenticatedBaseController):
.first()
)
return { "member_ldap" : member, "interface" : interface, "room_ldap" : room }
machines = Machine.get_machines_of_member(self.user, member.dn)
return { "member_ldap" : member, "interface" : interface, "room_ldap" : room, "machines" : machines}
#end def
@expose("brie.templates.show.room")

View File

@ -3,7 +3,24 @@
from brie.config import ldap_config
class Member(object):
@staticmethod
def entry_attr(uid, prenom, nom, mail, uid_number):
return {
"objectClass" : ["top", "person", "organizationalPerson", "inetOrgPerson", "pacatnetMember", "pykotaAccount", "posixAccount"],
"uid" :uid.encode("utf-8"),
"cn" : (prenom + " " + nom.upper()).encode("utf-8"),
"sn" : (nom.upper()).encode("utf-8"),
"givenName" : (prenom).encode("utf-8"),
"uidNumber" : uid_number,
"gidNumber" : "10000",
"homeDirectory" : ("/net/home/" + uid).encode("utf-8"),
"mail" : mail.encode("utf-8"),
"loginShell" : "/usr/bin/zsh".encode("utf-8")
}
#end def
@staticmethod
def get_by_dn(user_session, dn):
return user_session.ldap_bind.search_dn(dn)
@ -57,3 +74,65 @@ class Wifi(object):
#end def
#end class
class Machine(object):
@staticmethod
def entry_attr(machine_id):
return {
"objectClass" : ["top", "organizationalRole"],
"cn" : str(machine_id)
}
#end def
@staticmethod
def dhcp_attr(name, mac):
return {
"objectClass" : ["top", "uidObject", "dhcpHost"],
"cn" : str(name),
"uid" : "machine_membre",
"dhcpHWAddress" : str("ethernet " + mac),
"dhcpStatements" : str("fixed-address " + name)
}
#end def
@staticmethod
def dns_attr(name, ip):
return {
"objectClass" : ["top", "dlzAbstractRecord", "dlzGenericRecord"],
"dlzData" : str(ip),
"dlzHostName" : str(name),
"dlzRecordId" : "1",
"dlzTTL" : "3600",
"dlzType" : "A"
}
#end def
@staticmethod
def auth_attr(flat_mac):
return {
"objectClass" : ["top", "organizationalRole", "simpleSecurityObject", "uidObject"],
"cn" : "mac",
"uid" : flat_mac,
"userPassword" : flat_mac
}
#end def
@staticmethod
def get_machines_of_member(user_session, member_dn):
results = user_session.ldap_bind.search(member_dn, "(objectClass=organizationalRole)")
machines = list()
for result in results:
dhcp = user_session.ldap_bind.search_first(result.dn, "(objectClass=dhcpHost)")
dns = user_session.ldap_bind.search_first(result.dn, "(objectClass=dlzGenericRecord)")
if dhcp is not None and dns is not None:
mac = dhcp.dhcpHWAddress.first().replace("ethernet ", "")
machines.append((dhcp.cn.first(), mac, dns.dlzData.first()))
#end if
#end for
return machines
#end def
#end class

View File

@ -29,3 +29,10 @@ a:hover {
text-transform: capitalize;
}
.button {
margin: 2px;
padding: 3px;
font-weight: bold;
background-color: #e0e0e0;
}

View File

@ -0,0 +1,7 @@
.edit_banner {
background-color: #FFE7CC;
padding: 3px;
margin-bottom: 10px;
width: 100%;
font-weight: bold;
}

View File

@ -0,0 +1,19 @@
<html
xmlns:py="http://genshi.edgewall.org/"
xmlns:xi="http://www.w3.org/2001/XInclude">
<xi:include href="member_room_interface_edit_views.html" />
<head>
<link type="text/css" rel="Stylesheet" href="/css/common.css" />
<link type="text/css" rel="Stylesheet" href="/css/show.css" />
<link type="text/css" rel="Stylesheet" href="/css/edit.css" />
</head>
<body>
${edit_banner()}
${back_show("room", room_number)}
<div py:choose="success">
<span py:when="True">Importation reussi</span>
<span py:otherwise="">Erreur d'importation</span>
</div>
</body>
</html>

View File

@ -7,6 +7,8 @@
<link type="text/css" rel="Stylesheet" href="/css/show.css" />
</head>
<body>
${edit_banner()}
${back_show("member", member_ldap.uid.first()}
<div py:choose="member_ldap">
<span class="section_name" py:when="None">Entrée inexistante</span>
<div py:otherwise="">${member_view(member_ldap)}</div>

View File

@ -2,6 +2,14 @@
xmlns:py="http://genshi.edgewall.org/"
xmlns:xi="http://www.w3.org/2001/XInclude"
py:strip="">
<py:def function="edit_banner()">
<div class="edit_banner">
EDITION
</div>
</py:def>
<py:def function="back_show(page, id)">
<a href="/show/${page}/${id}" class="button">RETOUR</a>
</py:def>
<py:def function="member_view(member_ldap)">
<div class="section">
<span class="section_name show_section_name">${member_ldap.cn.first()}</span>
@ -47,6 +55,13 @@
</div>
</div>
</div>
<div class="section">
<span class="section_name show_section_name">MACHINES</span>
<div py:for="name, mac, ip in machines">
<span class="item_name">${name}</span>
<span>${mac} : ${ip}</span>
</div>
</div>
</py:def>
<py:def function="room_view(room, member_in = None)">
@ -59,6 +74,12 @@
<a href="/show/member/${member_in.uid.first()}">${member_in.cn.first()}</a>
</span>
</div>
<div>
<span class="item_name"></span>
<span>
<a href="/edit/import_from/${room.uid.first()}" class="button">Importer depuis Camembert</a>
</span>
</div>
</div>
</div>
</py:def>

View File

@ -1,15 +1,18 @@
<html
xmlns:py="http://genshi.edgewall.org/"
xmlns:xi="http://www.w3.org/2001/XInclude">
<xi:include href="member_room_interface_views.html" />
<xi:include href="member_room_interface_edit_views.html" />
<head>
<link type="text/css" rel="Stylesheet" href="/css/common.css" />
<link type="text/css" rel="Stylesheet" href="/css/show.css" />
<link type="text/css" rel="Stylesheet" href="/css/edit.css" />
</head>
<body>
<div py:choose="room">
${edit_banner()}
${back_show("room", room_ldap.uid.first())}
<div py:choose="room_ldap">
<span class="section_name" py:when="None">Pas de chambre associée</span>
<div py:otherwise="">${room_view(room, member_ldap)}</div>
<div py:otherwise="">${room_view(room_ldap, member_ldap)}</div>
</div>
<div py:choose="interface">
<span class="section_name" py:when="None">Pas d'interface associée</span>

View File

@ -1,10 +1,13 @@
<html xmlns:py="http://genshi.edgewall.org/" xmlns:xi="http://www.w3.org/2001/XInclude">
<xi:include href="login_widget.html" />
<head>
<link rel="stylesheet" type="text/css" href="/css/common.css" />
</head>
<body>
<xi:include href="login_widget.html" />
<div>${login_widget(user)}</div>
<h1>Test</h1>
<div py:for="item in materiel">
<span>${item.hostname}</span>
<div>
<a href="/rooms/" class="button">CHAMBRES</a>
</div>
</body>
</html>

View File

@ -7,6 +7,7 @@
<link type="text/css" rel="Stylesheet" href="/css/show.css" />
</head>
<body>
${back_rooms()}
<div py:choose="member_ldap">
<span class="section_name" py:when="None">Entrée inexistante</span>
<div py:otherwise="">${member_view(member_ldap)}</div>

View File

@ -2,6 +2,9 @@
xmlns:py="http://genshi.edgewall.org/"
xmlns:xi="http://www.w3.org/2001/XInclude"
py:strip="">
<py:def function="back_rooms()">
<a href="/rooms/" class="button">RETOUR</a>
</py:def>
<py:def function="member_view(member_ldap)">
<div class="section">
<span class="section_name show_section_name">${member_ldap.cn.first()}</span>
@ -47,6 +50,13 @@
</div>
</div>
</div>
<div class="section">
<span class="section_name show_section_name">MACHINES</span>
<div py:for="name, mac, ip in machines">
<span class="item_name">${name}</span>
<span>${mac} : ${ip}</span>
</div>
</div>
</py:def>
<py:def function="room_view(room, member_in = None)">

View File

@ -7,6 +7,9 @@
<link type="text/css" rel="Stylesheet" href="/css/show.css" />
</head>
<body>
${back_rooms()}
<a href="/edit/room/${room_ldap.uid.first()}" class="button">EDITER</a>
<div py:choose="room_ldap">
<span class="section_name" py:when="None">Pas de chambre associée</span>
<div py:otherwise="">${room_view(room_ldap, member_ldap)}</div>

View File

@ -36,7 +36,7 @@ beaker.session.secret = somesecret
# pick the form for your database
# %(here) may include a ':' character on Windows environments; this can
# invalidate the URI when specifying a SQLite db via path name
sqlalchemy.url=postgresql://camembert:CamembertDB%40Pacat@archange.pacaterie.u-psud.fr:5432/camembert
sqlalchemy.url=postgresql://camembert:CamembertDB%40Pacat@atlas.pacaterie.u-psud.fr:5432/camembert
# sqlalchemy.url=mysql://username:password@hostname:port/databasename