Proyecto

General

Perfil

« Anterior | Siguiente » 

Revisión 295

Primer commit

Ver diferencias:

autorename/trunk/server.py
#!/usr/bin/env python
##############################################################################
# -*- coding: utf-8 -*-
# Project: Autorename
# Module: server.py
# Purpose: Autorename of laptops
# Language: Python 2.5
# Date: 23-Apr-2012.
# Ver: 23-Apr-2012.
# Author: Manuel Mora Gordillo
# Copyright: 2012 - Manuel Mora Gordillo <manuito @nospam@ gmail.com>
#
# Autorename 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.
# Autorename 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 Autorename. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from SimpleXMLRPCServer import SimpleXMLRPCServer
import socket, os, time
def log(hostname, newHostname):
log="/var/log/autorename-server.log"
f = open(log,"a")
f.write(hostname+"#"+newHostname+"#"+time.strftime("%Y-%m-%d %H:%M:%S")+"\n")
f.close()
def getDomain():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
dom ='sindominio'
try:
s.connect(("servidor", 0))
inet_address= s.getsockname()[0]
nombre=socket.gethostbyname_ex(socket.gethostname())[0].split(".")
if len(nombre)>1:
dom=nombre[1]
except:
pass
return dom
def ping():
return "pong"
def removeCerts (hostname, newHostname):
os.system("puppetca --clean "+hostname)
os.system("puppetca --clean "+hostname+"."+getDomain())
os.system("puppetca --clean "+newHostname)
os.system("puppetca --clean "+newHostname+"."+getDomain())
log(hostname,newHostname)
return True
server = SimpleXMLRPCServer (("servidor", 9997))
server.register_function (removeCerts)
server.register_function (ping)
server.serve_forever ()
autorename/trunk/autorename
#!/bin/sh
### BEGIN INIT INFO
# Provides: autorename
# Required-Start: $remote_fs $network
# Required-Stop: $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Autorename RPC Server
# Description: Debian init script for Autorename RCP Server root monitoring daemon
### END INIT INFO
#
# Author: Manuel Mora Gordillo <manuito at gmail.com>
#
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=autorenameRPCServer
message="Autorename RPC Server daemon"
rundir=/var/run
pidfile=$rundir/autorename/rpcServer.pid
logfile=/var/log/autorename-server.log
daemon="/usr/bin/python"
application=/usr/share/autorename-server/server.py
test -x $DAEMON || exit 0
test -r $application || exit 0
rm -rf /tmp/autorename*
. /lib/lsb/init-functions
if [ ! -d /var/run/autorename ]; then
rm -rf /var/run/autorename
mkdir -p /var/run/autorename
fi
case "$1" in
start)
log_daemon_msg "Starting $message"
[ ! -f $logfile ] && touch $logfile
start-stop-daemon --start --quiet --pidfile=$pidfile --make-pidfile --exec $daemon $application &
log_end_msg 0
;;
stop)
log_daemon_msg "Stopping $message"
start-stop-daemon --stop --quiet --pidfile $pidfile
rm -rf /tmp/autorename*
log_end_msg 0
;;
reload)
log_daemon_msg "Reloading $message"
start-stop-daemon --stop --quiet --signal HUP --pidfile $pidfile &
log_end_msg 0
;;
restart|force-reload)
log_daemon_msg "Restarting $message"
$0 stop
sleep 1
$0 start
log_end_msg 0
;;
*)
log_success_msg "Usage: $0 {start|stop|restart|force-reload|reload}"
exit 1
;;
esac
exit 0
autorename/trunk/debian/autorename-server.postinst
#!/bin/sh
# postinst script for controlies
#
# see: dh_installdeb(1)
set -e
case "$1" in
configure)
update-rc.d autorename defaults
;;
abort-upgrade|abort-remove|abort-deconfigure)
;;
*)
echo "postinst called with unknown argument \`$1'" >&2
exit 1
;;
esac
# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.
#DEBHELPER#
exit 0
autorename/trunk/debian/changelog
autorename (0.1-1) unstable; urgency=low
* First Debian package
-- Manuel Mora Gordillo <manuito@gmail.com> Mon, 23 Apr 2012 19:40:00 +0200
autorename/trunk/debian/copyright
This work was packaged for Debian by:
Manuel Mora Gordillo <manuito@gmail.com> on Mon, 23 Apr 2012 19:40:00 +0200
Upstream Author(s):
Manuel Mora Gordillo <manuito @no-spam@ gmail.com>
Copyright:
Copyright (C) 2012 Manuel Mora Gordillo
License:
Autorename is Licensed under the LGPL license version 3
see "/usr/share/common-licenses/LGPL-3".
The Debian packaging is:
Copyright (C) 2012 Manuel Mora Gordillo <manuito@gmail.com>
and is licensed under the GPL version 3,
see "/usr/share/common-licenses/GPL-3".
autorename/trunk/debian/autorename-server.install
server.py usr/share/autorename-server
autorename /etc/init.d
autorename/trunk/debian/rules
#!/usr/bin/make -f
# -*- makefile -*-
# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1
%:
dh $@
autorename/trunk/debian/autorename-client.dirs
var/lib/autorename-client
usr/share/autorename-client
etc/gdm/PostLogin
autorename/trunk/debian/autorename-client.postinst
#!/bin/sh
# postinst script for autorename-client
#
# see: dh_installdeb(1)
set -e
case "$1" in
configure)
if [ ! -f /etc/gdm/PostLogin/Default ];
then
echo "#!/bin/sh" >> /etc/gdm/PostLogin/Default
fi
echo "/usr/share/autorename-client/client.py $LOGNAME" >> /etc/gdm/PostLogin/Default
;;
abort-upgrade|abort-remove|abort-deconfigure)
;;
*)
echo "postinst called with unknown argument \`$1'" >&2
exit 1
;;
esac
# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.
#DEBHELPER#
exit 0
autorename/trunk/debian/control
Source: autorename
Section: python
Priority: extra
Maintainer: Manuel Mora Gordillo <manuito@gmail.com>
Build-Depends: debhelper (>= 7.0.50~)
Standards-Version: 3.8.4
Package: autorename
Architecture: all
Depends: python
Description: Autorenombrado de portatiles
Autorename gestiona el autorenombrado de portatiles asignando
al portatil el nombre del usuario al que esta asignado
autorename/trunk/debian/autorename-client.install
client.py usr/share/autorename-client
autorename/trunk/debian/compat
7
autorename/trunk/debian/autorename-server.dirs
usr/share/autorename-server
/etc/init.d
autorename/trunk/client.py
#!/usr/bin/env python
##############################################################################
# -*- coding: utf-8 -*-
# Project: Autorename
# Module: client.py
# Purpose: Autorename of laptops
# Language: Python 2.5
# Date: 23-Apr-2012.
# Ver: 23-Apr-2012.
# Author: Manuel Mora Gordillo
# Copyright: 2012 - Manuel Mora Gordillo <manuito @nospam@ gmail.com>
#
# Autorename 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.
# Autorename 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 Autorename. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import sys, os, socket, time, shutil, xmlrpclib
loginsToRename=10
lastLogin=sys.argv[1]
loginsDir="/var/lib/autorename-client"
loginsFile=loginsDir+"/userlogins"
log="/var/log/autorename-client.log"
if not os.path.isdir(loginsDir):
os.mkdir(loginsDir)
# Escribimos el ultimo logueo
f = open(loginsFile,"a")
f.write(lastLogin+"\n")
f.close()
# Comprobamos que haya comunicacion con el Servidor
try:
server = xmlrpclib.ServerProxy("http://servidor:9997")
server.ping()
except:
print "No hay comunicacion"
exit()
# Obtenemos los ultimos x logueos
f = open(loginsFile,"r")
f.seek(0,2)
fsize = f.tell()
f.seek (max (fsize-1024, 0), 0)
listLastLogins = f.readlines()[-loginsToRename:]
f.close()
# Le quitamos los saltos de linea
for x, i in enumerate(listLastLogins):
listLastLogins[x]=i.replace("\n","")
hostname = socket.gethostname()
# Si hay al menos x logins, todos son del ultimo usuario logueado
# y el nombre del equipo se llama de una forma diferente, renombramos
if len(listLastLogins)>=loginsToRename and listLastLogins.count(lastLogin)==loginsToRename and lastLogin!=hostname:
newHostname = lastLogin
f = open("/etc/hostname","w")
f.write(newHostname)
f.close()
f = open("/etc/hosts","w")
f.write("127.0.0.1 localhost.localdomain localhost\n")
f.write("127.0.0.1 "+newHostname+"\n\n")
f.write("# The following lines are desirable for IPv6 capable hosts\n")
f.write("::1 localhost ip6-localhost ip6-loopback\n")
f.write("fe00::0 ip6-localnet\n")
f.write("ff00::0 ip6-mcastprefix\n")
f.write("ff02::1 ip6-allnodes\n")
f.write("ff02::2 ip6-allrouters")
f.close()
os.system("hostname -F /etc/hostname")
# Se lo comunicamos al servidor para que borre los certificados
try:
server.removeCerts(hostname, newHostname)
except:
pass
# Borramos los certificados locales y reiniciamos puppet
if os.path.isdir("/var/lib/puppet/ssl"):
shutil.rmtree("/var/lib/puppet/ssl")
os.system("/etc/init.d/puppet restart")
# Guardamos en el log
f = open(log,"a")
f.write(hostname+"#"+newHostname+"#"+time.strftime("%Y-%m-%d %H:%M:%S")+"\n")
f.close()

Exportar a: Unified diff