Browse Source

Code style changes

* make code PEP8 clean (addresses #18)
 * add copyright information to all python files


git-svn-id: file:///home/www/usr01/svn/gnuviechadmin/trunk@257 a67ec6bc-e5d5-0310-a910-815c51eb3124
master
Jan Dittberner 14 years ago
parent
commit
09180938f1
  1. 5
      bin/gva
  2. 17
      data/dbrepo/versions/2/2.py
  3. 11
      data/dbrepo/versions/3/3.py
  4. 1
      gnuviechadmin/__init__.py
  5. 13
      gnuviechadmin/backend/BackendEntity.py
  6. 8
      gnuviechadmin/backend/BackendEntityHandler.py
  7. 27
      gnuviechadmin/backend/BackendTo.py
  8. 4
      gnuviechadmin/backend/__init__.py
  9. 26
      gnuviechadmin/backend/client.py
  10. 111
      gnuviechadmin/backend/domain.py
  11. 9
      gnuviechadmin/backend/record.py
  12. 9
      gnuviechadmin/backend/settings.py
  13. 63
      gnuviechadmin/backend/sysuser.py
  14. 9
      gnuviechadmin/backend/tables.py
  15. 31
      gnuviechadmin/cli/CliCommand.py
  16. 2
      gnuviechadmin/cli/__init__.py
  17. 65
      gnuviechadmin/cli/client.py
  18. 31
      gnuviechadmin/cli/domain.py
  19. 42
      gnuviechadmin/cli/record.py
  20. 43
      gnuviechadmin/cli/sysuser.py
  21. 13
      gnuviechadmin/exceptions.py
  22. 1
      gnuviechadmin/util/__init__.py
  23. 30
      gnuviechadmin/util/getenttools.py
  24. 42
      gnuviechadmin/util/gpgmail.py
  25. 14
      gnuviechadmin/util/passwordutils.py
  26. 31
      gnuviechadmin/util/stmtcreator.py
  27. 3
      gnuviechadmin/xmlrpc/XMLRPCFacade.py
  28. 2
      gnuviechadmin/xmlrpc/__init__.py
  29. 58
      gnuviechadmin/xmlrpc/users.py
  30. 5
      setup.py
  31. 3
      test/manage.py
  32. 29
      test/ormaptest.py
  33. 2
      test/ormaptest_repo/versions/1/1.py
  34. 2
      test/ormaptest_repo/versions/2/2.py
  35. 8
      test/ormaptest_repo/versions/3/3.py
  36. 145
      testdb/addclient.py
  37. 118
      testdb/adddomain.py
  38. 89
      testdb/addpopuser.py
  39. 91
      testdb/addsysuser.py
  40. 4
      testdb/dbschema/gnuviechadmin/versions/1/1.py
  41. 2
      testdb/dbschema/gnuviechadmin/versions/2/2.py
  42. 2
      testdb/dbschema/gnuviechadmin/versions/3/3.py
  43. 3
      testdb/dbschema/manage.py
  44. 20
      testdb/gnuviechadmin/dblayer.py
  45. 29
      testdb/gnuviechadmin/entities.py

5
bin/gva

@ -1,7 +1,8 @@
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# -*- python -*-
# -*- coding: utf-8 -*-
#
# Copyright (C) 2007 by Jan Dittberner.
# Copyright (C) 2007, 2008 by Jan Dittberner.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by

17
data/dbrepo/versions/2/2.py

@ -18,8 +18,7 @@ client = Table(
Column('mobile', String(32)),
Column('fax', String(32)),
Column('email', String(64), unique=True, nullable=False),
schema = dbschema
)
schema = dbschema)
sysuser = Table(
'sysuser', meta,
Column('sysuserid', Integer, primary_key=True),
@ -33,8 +32,7 @@ sysuser = Table(
nullable=False),
Column('sysuid', Integer, nullable=False, unique=True),
Column('lastchange', DateTime, default=func.now()),
schema = dbschema
)
schema = dbschema)
domain = Table(
'domain', meta,
Column('domainid', Integer, primary_key=True),
@ -45,8 +43,7 @@ domain = Table(
Column('notified_serial', Integer),
Column('sysuserid', Integer, ForeignKey(sysuser.c.sysuserid),
nullable=False),
schema = dbschema
)
schema = dbschema)
record = Table(
'record', meta,
Column('recordid', Integer, primary_key=True),
@ -58,16 +55,15 @@ record = Table(
Column('ttl', Integer),
Column('prio', Integer),
Column('change_date', Integer),
schema = dbschema
)
schema = dbschema)
supermaster = Table(
'supermaster', meta,
Column('ip', String(25), nullable=False),
Column('nameserver', String(255), nullable=False),
Column('account', Integer, ForeignKey(sysuser.c.sysuserid),
nullable=False),
schema = dbschema
)
schema = dbschema)
def upgrade():
client.create()
@ -76,6 +72,7 @@ def upgrade():
record.create()
supermaster.create()
def downgrade():
supermaster.drop()
record.drop()

11
data/dbrepo/versions/3/3.py

@ -17,8 +17,7 @@ mailaccount = Table(
Column('home', String(128), nullable = False),
Column('spamcheck', Boolean, nullable = False, default = False),
Column('sajunkscore', Integer),
schema = dbschema
)
schema = dbschema)
mailaddress = Table(
'mailaddress', meta,
Column('mailaddressid', Integer, primary_key = True),
@ -26,8 +25,7 @@ mailaddress = Table(
nullable = False),
Column('email', String(255), nullable = False),
UniqueConstraint('email', 'domainid'),
schema = dbschema
)
schema = dbschema)
mailtarget = Table(
'mailtarget', meta,
Column('mailtargetid', Integer, primary_key = True),
@ -35,14 +33,15 @@ mailtarget = Table(
nullable = False),
Column('target', String(128), nullable = False),
UniqueConstraint('target', 'mailaddressid'),
schema = dbschema
)
schema = dbschema)
def upgrade():
mailaccount.create()
mailaddress.create()
mailtarget.create()
def downgrade():
mailtarget.drop()
mailaddress.drop()

1
gnuviechadmin/__init__.py

@ -1,4 +1,3 @@
# -*- python -*-
# -*- coding: utf-8 -*-
#
# Copyright (C) 2007, 2008 by Jan Dittberner.

13
gnuviechadmin/backend/BackendEntity.py

@ -1,4 +1,3 @@
# -*- python -*-
# -*- coding: utf-8 -*-
#
# Copyright (C) 2007, 2008 by Jan Dittberner.
@ -20,7 +19,9 @@
#
# Version: $Id$
import os, logging, tempfile
import os
import logging
import tempfile
from settings import config
from gnuviechadmin.exceptions import *
@ -29,6 +30,7 @@ from subprocess import *
import sqlalchemy
from sqlalchemy.orm import object_mapper
class BackendEntity(object):
"""This is the abstract base class for all backend entity classes."""
@ -75,10 +77,10 @@ class BackendEntity(object):
for cmdline in cmdlines:
toexec = "%s %s" % (suwrapper, cmdline)
if predecessor is None:
p = Popen(toexec, shell = True, stdout = PIPE)
p = Popen(toexec, shell = True, stdout = PIPE)
else:
p = Popen(toexec, shell = True, stdin = predecessor.stdout,
stdout = PIPE)
p = Popen(toexec, shell = True, stdin = predecessor.stdout,
stdout = PIPE)
predecessor = p
output = predecessor.communicate()[0]
return predecessor.wait()
@ -110,4 +112,3 @@ class BackendEntity(object):
cmd = 'cp "%s" "%s"' % (tmp.name, filename)
self.sucommand(cmd)
tmp.close()

8
gnuviechadmin/backend/BackendEntityHandler.py

@ -1,4 +1,3 @@
# -*- python -*-
# -*- coding: utf-8 -*-
#
# Copyright (C) 2007, 2008 by Jan Dittberner.
@ -20,12 +19,15 @@
#
# Version: $Id$
import sqlalchemy, logging
import sqlalchemy
import logging
from sqlalchemy.orm import create_session
from gnuviechadmin.exceptions import *
from BackendEntity import *
class BackendEntityHandler(object):
def __init__(self, entityclass, toclass, verbose = False):
self.logger = logging.getLogger("%s.%s" % (
self.__class__.__module__, self.__class__.__name__))
@ -40,7 +42,7 @@ class BackendEntityHandler(object):
sess = create_session()
transaction = sess.create_transaction()
delegate = self.toclass(**kwargs)
entity = self.entityclass(delegate, self.verbose)
entity = self.entityclass(delegate, self.verbose)
try:
sess.save(delegate)
sess.flush()

27
gnuviechadmin/backend/BackendTo.py

@ -1,4 +1,3 @@
# -*- python -*-
# -*- coding: utf-8 -*-
#
# Copyright (C) 2007, 2008 by Jan Dittberner.
@ -21,11 +20,12 @@
# Version: $Id$
from sqlalchemy.orm import object_mapper, mapper, relation
from tables import *
class BackendTo(object):
"""Backend transfer object class."""
def __init__(self, **kwargs):
for (key, value) in kwargs.items():
self.__setattr__(key, value)
@ -37,7 +37,7 @@ class BackendTo(object):
format = "%(class)s:"
format = format + ", ".join([col + "=%(" + col + ")s" for col in \
cols])
data = {'class' : self.__class__.__name__}
data = {'class': self.__class__.__name__}
else:
cols = self._shortkeys
format = ",".join("%(" + col + ")s" for col in cols)
@ -45,30 +45,31 @@ class BackendTo(object):
data.update(dict([(col, self.__getattribute__(col)) for col in cols]))
return format % data
class Client(BackendTo):
"""Transfer object class for clients."""
_shortkeys = ('clientid', 'firstname', 'lastname', 'email')
class Sysuser(BackendTo):
"""Transfer object class for system users."""
_shortkeys = ("sysuserid", "clientid", "username", "home", "shell")
class Domain(BackendTo):
"""Transfer object class for DNS domains."""
_shortkeys = ("domainid", "sysuserid", "name", "type")
class Record(BackendTo):
"""Transfer object class for DNS domain records."""
_shortkeys = ("recordid", "domainid", "name", "type", "content")
client_mapper = mapper(Client, client_table)
sysuser_mapper = mapper(Sysuser, sysuser_table)
domain_mapper = mapper(Domain, domain_table)
record_mapper = mapper(Record, record_table)
client_mapper.add_property("sysusers", relation(Sysuser, backref = 'client'))
sysuser_mapper.add_property("domains", relation(Domain, backref = 'sysuser'))
domain_mapper.add_property("records", relation(Record, cascade = 'all',
backref = 'domain'))
client_mapper = mapper(Client, client_table, {
'sysusers': relation(Sysuser, backref = 'client')})
sysuser_mapper = mapper(Sysuser, sysuser_table, {
'domains': relation(Domain, backref = 'sysuser')})
domain_mapper = mapper(Domain, domain_table, {
'records': relation(Record, cascade = 'all', backref = 'domain')})
record_mapper = mapper(Record, record_table)

4
gnuviechadmin/backend/__init__.py

@ -1,6 +1,6 @@
# -*- coding: UTF-8 -*-
# -*- coding: utf-8 -*-
#
# Copyright (C) 2007 by Jan Dittberner.
# Copyright (C) 2007, 2008 by Jan Dittberner.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by

26
gnuviechadmin/backend/client.py

@ -1,6 +1,6 @@
# -*- coding: UTF-8 -*-
# -*- coding: utf-8 -*-
#
# Copyright (C) 2007 by Jan Dittberner.
# Copyright (C) 2007, 2008 by Jan Dittberner.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@ -25,6 +25,7 @@ from BackendTo import *
from BackendEntity import *
from BackendEntityHandler import *
class ClientEntity(BackendEntity):
"""Entity class for clients."""
@ -39,18 +40,18 @@ class ClientEntity(BackendEntity):
def _client_mail(self):
text = get_template(config.get('common', 'mailtemplates'),
config.get('client', 'create.mail')).substitute({
'firstname' : self.delegateto.firstname,
'lastname' : self.delegateto.lastname,
'email' : self.delegateto.email,
'address1' : self.delegateto.address1,
'zipcode' : self.delegateto.zip,
'city' : self.delegateto.city,
'phone' : self.delegateto.phone})
'firstname': self.delegateto.firstname,
'lastname': self.delegateto.lastname,
'email': self.delegateto.email,
'address1': self.delegateto.address1,
'zipcode': self.delegateto.zip,
'city': self.delegateto.city,
'phone': self.delegateto.phone})
subject = get_template_string(
config.get('client', 'create_subject')).substitute({
'firstname' : self.delegateto.firstname,
'lastname' : self.delegateto.lastname})
self.send_mail(subject, text)
'firstname': self.delegateto.firstname,
'lastname': self.delegateto.lastname})
self.send_mail(subject, text)
def create_hook(self, session):
"""Actions to perform when a client is created."""
@ -69,6 +70,7 @@ class ClientEntity(BackendEntity):
"""Gets the default country."""
return config.get('common', 'defaultcountry')
class ClientHandler(BackendEntityHandler):
"""BackendEntityHandler for Client entities."""

111
gnuviechadmin/backend/domain.py

@ -1,4 +1,3 @@
# -*- python -*-
# -*- coding: utf-8 -*-
#
# Copyright (C) 2007, 2008 by Jan Dittberner.
@ -18,9 +17,10 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.
#
# Version: $Id: client.py 1101 2007-02-28 21:15:20Z jan $
# Version: $Id$
import datetime, os
import datetime
import os
from gnuviechadmin.exceptions import *
from settings import *
@ -28,6 +28,7 @@ from BackendTo import Record, Domain
from BackendEntity import BackendEntity
from BackendEntityHandler import BackendEntityHandler
class DomainEntity(BackendEntity):
"""Entity class for DNS domains."""
@ -130,12 +131,12 @@ class DomainEntity(BackendEntity):
template = get_template(config.get('domain', 'htdocstemplate'),
tpl)
template = template.substitute({
'domain' : self.delegateto.name})
'domain': self.delegateto.name})
self.write_to_file(os.path.join(vhostdir, tpl), template)
cmd = 'chown -R %(username)s:%(group)s "%(dir)s"' % {
'username' : self.delegateto.sysuser.username,
'group' : config.get('sysuser', 'defaultgroup'),
'dir' : vhostdir}
'username': self.delegateto.sysuser.username,
'group': config.get('sysuser', 'defaultgroup'),
'dir': vhostdir}
self.sucommand(cmd)
def _create_log_dir(self):
@ -164,8 +165,8 @@ class DomainEntity(BackendEntity):
template = get_template(config.get('domain', 'conftemplates'),
config.get('domain', 'statshtaccesstemplate'))
template = template.substitute({
'domain' : self.delegateto.name,
'userfile' : authfile})
'domain': self.delegateto.name,
'userfile': authfile})
self.write_to_file(os.path.join(self._get_stats_dir(),
'.htaccess'), template)
@ -178,10 +179,10 @@ class DomainEntity(BackendEntity):
template = get_template(config.get('domain', 'conftemplates'),
config.get('domain', 'modlogantemplate'))
template = template.substitute({
'statsdir' : self._get_stats_dir(),
'logdir' : self._get_log_dir(),
'domain' : self.delegateto.name,
'domainesc' : self.delegateto.name.replace('.', '\.')})
'statsdir': self._get_stats_dir(),
'logdir': self._get_log_dir(),
'domain': self.delegateto.name,
'domainesc': self.delegateto.name.replace('.', '\.')})
self.write_to_file(os.path.join(modlogandir,
self.delegateto.name + '.conf'),
template)
@ -190,11 +191,11 @@ class DomainEntity(BackendEntity):
template = get_template(config.get('domain', 'conftemplates'),
config.get('domain', 'apachetemplate'))
template = template.substitute({
'ipaddr' : self.ipaddr,
'statsdir' : self._get_stats_dir(),
'logdir' : self._get_log_dir(),
'domain' : self.delegateto.name,
'docroot' : self._get_vhost_dir()})
'ipaddr': self.ipaddr,
'statsdir': self._get_stats_dir(),
'logdir': self._get_log_dir(),
'domain': self.delegateto.name,
'docroot': self._get_vhost_dir()})
self.write_to_file(os.path.join(config.get('domain', 'sitesdir'),
self.delegateto.name), template)
@ -202,14 +203,14 @@ class DomainEntity(BackendEntity):
template = get_template(config.get('common', 'mailtemplates'),
config.get('domain', 'create.mail'))
text = template.substitute({
'sysuser' : self.delegateto.sysuser.username,
'domain' : self.delegateto.name,
'docroot' : self._get_vhost_dir(),
'statspass' : self.delegateto.sysuser.clearpass})
'sysuser': self.delegateto.sysuser.username,
'domain': self.delegateto.name,
'docroot': self._get_vhost_dir(),
'statspass': self.delegateto.sysuser.clearpass})
template = get_template_string(config.get('domain', 'create_subject'))
subject = template.substitute({
'domain' : self.delegateto.name})
self.send_mail(subject, text)
'domain': self.delegateto.name})
self.send_mail(subject, text)
def create_hook(self, session):
self.delegateto.records.append(Record(
@ -223,7 +224,7 @@ class DomainEntity(BackendEntity):
name = self.delegateto.name, type = 'NS', content = self.ns2,
ttl = config.getint('domain', 'defaultttl')))
self.delegateto.records.append(Record(
name = self.delegateto.name, type = 'MX', content = self.mx,
name = self.delegateto.name, type = 'MX', content = self.mx,
ttl = config.getint('domain', 'defaultttl'),
prio = config.getint('domain', 'defaultmxprio')))
self.delegateto.records.append(Record(
@ -258,65 +259,69 @@ class DomainEntity(BackendEntity):
def _archive_stats_dir(self):
archive = os.path.join(self.delegateto.sysuser.home,
'%(domain)s-stats.tar.gz' % {
'domain' : self.delegateto.name})
cmd = 'tar czf "%(archive)s" --directory="%(statsbase)s" "%(statsdir)s"' % {
'archive' : archive,
'statsbase' : config.get('domain', 'statspath'),
'statsdir' : self.delegateto.name}
'domain': self.delegateto.name})
cmd = 'tar czf "%(archive)s" --directory="%(statsbase)s" ' + \
'"%(statsdir)s"' % {
'archive': archive,
'statsbase': config.get('domain', 'statspath'),
'statsdir': self.delegateto.name}
self.sucommand(cmd)
cmd = 'rm -r "%(statsdir)s"' % {
'statsdir' : self._get_stats_dir()}
'statsdir': self._get_stats_dir()}
self.sucommand(cmd)
cmd = 'chown "%(username)s:%(group)s" "%(archive)s"' % {
'username' : self.delegateto.sysuser.username,
'group' : config.get('sysuser', 'defaultgroup'),
'archive' : archive}
'username': self.delegateto.sysuser.username,
'group': config.get('sysuser', 'defaultgroup'),
'archive': archive}
self.sucommand(cmd)
def _archive_log_dir(self):
archive = os.path.join(self.delegateto.sysuser.home,
'%(domain)s-logs.tar.gz' % {
'domain' : self.delegateto.name})
cmd = 'tar czf "%(archive)s" --directory="%(logbase)s" "%(logdir)s"' % {
'archive' : archive,
'logbase' : config.get('domain', 'logpath'),
'logdir' : self.delegateto.name}
'domain': self.delegateto.name})
cmd = 'tar czf "%(archive)s" --directory="%(logbase)s" ' + \
'"%(logdir)s"' % {
'archive': archive,
'logbase': config.get('domain', 'logpath'),
'logdir': self.delegateto.name}
self.sucommand(cmd)
cmd = 'rm -r "%(logdir)s"' % {
'logdir' : self._get_log_dir()}
'logdir': self._get_log_dir()}
self.sucommand(cmd)
cmd = 'chown "%(username)s:%(group)s" "%(archive)s"' % {
'username' : self.delegateto.sysuser.username,
'group' : config.get('sysuser', 'defaultgroup'),
'archive' : archive}
'username': self.delegateto.sysuser.username,
'group': config.get('sysuser', 'defaultgroup'),
'archive': archive}
self.sucommand(cmd)
def _archive_vhost_dir(self):
archive = os.path.join(self.delegateto.sysuser.home,
'%(domain)s-vhost.tar.gz' % {
'domain' : self.delegateto.name})
cmd = 'tar czf "%(archive)s" --directory="%(vhostbase)s" "%(vhostdir)s"' % {
'archive' : archive,
'vhostbase' : self.delegateto.sysuser.home,
'vhostdir' : self.delegateto.name}
'domain': self.delegateto.name})
cmd = 'tar czf "%(archive)s" --directory="%(vhostbase)s" ' + \
'"%(vhostdir)s"' % {
'archive': archive,
'vhostbase': self.delegateto.sysuser.home,
'vhostdir': self.delegateto.name}
self.sucommand(cmd)
cmd = 'rm -r "%(vhostdir)s"' % {
'vhostdir' : os.path.join(self.delegateto.sysuser.home,
'vhostdir': os.path.join(self.delegateto.sysuser.home,
self.delegateto.name)}
self.sucommand(cmd)
cmd = 'chown "%(username)s:%(group)s" "%(archive)s"' % {
'username' : self.delegateto.sysuser.username,
'group' : config.get('sysuser', 'defaultgroup'),
'archive' : archive}
'username': self.delegateto.sysuser.username,
'group': config.get('sysuser', 'defaultgroup'),
'archive': archive}
self.sucommand(cmd)
def delete_hook(self, session):
def delete_hook(self, session):
self._delete_apache_conf()
self._delete_stats_conf()
self._archive_stats_dir()
self._archive_log_dir()
self._archive_vhost_dir()
class DomainHandler(BackendEntityHandler):
"""BackendEntityHandler for Domain entities."""

9
gnuviechadmin/backend/record.py

@ -1,6 +1,6 @@
# -*- coding: UTF-8 -*-
# -*- coding: utf-8 -*-
#
# Copyright (C) 2007 by Jan Dittberner.
# Copyright (C) 2007, 2008 by Jan Dittberner.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@ -25,16 +25,19 @@ from domain import DomainEntity
from BackendEntity import *
from BackendEntityHandler import *
class RecordEntity(BackendEntity):
"""Entity class for DNS domain records."""
def create_hook(self, session):
domain = session.load(Domain, self.delegateto.domainid)
DomainEntity(domain).update_serial(session)
def delete_hook(self, session):
domain = session.load(Domain, self.delegateto.domainid)
DomainEntity(domain).update_serial(session)
class RecordHandler(BackendEntityHandler):
"""BackendEntityHandler for Record entities."""

9
gnuviechadmin/backend/settings.py

@ -1,4 +1,3 @@
# -*- python -*-
# -*- coding: utf-8 -*-
#
# Copyright (C) 2007, 2008 by Jan Dittberner.
@ -26,7 +25,10 @@ This module handles all central configuration of Gnuviech Admin. It
parses configuration files and provides functions for reading
templates."""
import ConfigParser, os, string, logging.config
import ConfigParser
import os
import string
import logging.config
# global settings which must not be user configurable
required_version = 3
@ -40,11 +42,13 @@ dbschema = None
if config.get('database', 'uri').startswith('postgres://'):
dbschema = 'gva'
def get_template_dir(dirname):
"""Returns the template directory for the given directory."""
templatepath = config.get('common', 'templatedir')
return os.path.join(templatepath, dirname)
def get_template(dirname, filename):
"""Returns the template data from the given template file."""
templatefile = file(os.path.join(get_template_dir(dirname),
@ -52,6 +56,7 @@ def get_template(dirname, filename):
templatedata = templatefile.read()
return string.Template(templatedata.decode('utf_8'))
def get_template_string(templatestring):
"""Returns a template object for the given template string."""
return string.Template(templatestring)

63
gnuviechadmin/backend/sysuser.py

@ -27,6 +27,7 @@ from BackendEntity import *
from BackendEntityHandler import *
import os
class SysuserEntity(BackendEntity):
"""Entity class for system users."""
@ -54,7 +55,8 @@ class SysuserEntity(BackendEntity):
usernames = [user.username for user in \
getenttools.find_user_by_prefix(prefix)]
if usernames:
maxid = max([int(username[len(prefix):]) for username in usernames])
maxid = max([int(username[len(prefix):]) \
for username in usernames])
maxid += 2
for number in range(1, maxid):
username = "%s%02d" % (prefix, number)
@ -81,17 +83,18 @@ class SysuserEntity(BackendEntity):
def _get_next_sysuid(self):
return getenttools.get_next_uid(int(config.get('sysuser', 'minuid')),
int(config.get('sysuser', 'maxuid')))
int(config.get('sysuser', 'maxuid')))
def _populate_home(self):
templatedir = get_template_dir(config.get('sysuser', 'hometemplate'))
olddir = os.getcwd()
os.chdir(templatedir)
cmd1 = 'find . -depth \! -regex ".*\.svn.*" \! -name "*~" -print0'
cmd2 = 'cpio --pass-through --owner=%(username)s.%(group)s --null --make-directories %(home)s' % {
'username' : self.delegateto.username,
'group' : config.get('sysuser', 'defaultgroup'),
'home' : self.delegateto.home}
cmd2 = 'cpio --pass-through --owner=%(username)s.%(group)s --null ' + \
'--make-directories %(home)s' % {
'username': self.delegateto.username,
'group': config.get('sysuser', 'defaultgroup'),
'home': self.delegateto.home}
self.supipe((cmd1, cmd2))
os.chdir(olddir)
@ -99,33 +102,35 @@ class SysuserEntity(BackendEntity):
template = get_template(config.get('common', 'mailtemplates'),
config.get('sysuser', 'create.mail'))
text = template.substitute({
'uid' : self.delegateto.sysuid,
'firstname' : self.delegateto.client.firstname,
'lastname' : self.delegateto.client.lastname,
'email' : self.delegateto.client.email,
'username' : self.delegateto.username,
'password' : self.delegateto.clearpass,
'home' : self.delegateto.home,
'shell' : self._get_shell_binary()})
'uid': self.delegateto.sysuid,
'firstname': self.delegateto.client.firstname,
'lastname': self.delegateto.client.lastname,
'email': self.delegateto.client.email,
'username': self.delegateto.username,
'password': self.delegateto.clearpass,
'home': self.delegateto.home,
'shell': self._get_shell_binary()})
template = get_template_string(config.get('sysuser', 'create_subject'))
subject = template.substitute({
'username' : self.delegateto.username})
'username': self.delegateto.username})
self.send_mail(subject, text)
def create_hook(self, session):
gecos = config.get('sysuser', 'gecos') % (self.delegateto.username)
cmdline = 'adduser --home "%(home)s" --shell "%(shell)s" --no-create-home --uid %(sysuid)d --ingroup "%(group)s" --disabled-password --gecos "%(gecos)s" %(username)s' % {
'home' : self.delegateto.home,
'shell' : self._get_shell_binary(),
'sysuid' : self.delegateto.sysuid,
'group' : config.get('sysuser', 'defaultgroup'),
'gecos' : gecos,
'username' : self.delegateto.username}
cmdline = 'adduser --home "%(home)s" --shell "%(shell)s" ' + \
'--no-create-home --uid %(sysuid)d --ingroup "%(group)s" ' + \
'--disabled-password --gecos "%(gecos)s" %(username)s' % {
'home': self.delegateto.home,
'shell': self._get_shell_binary(),
'sysuid': self.delegateto.sysuid,
'group': config.get('sysuser', 'defaultgroup'),
'gecos': gecos,
'username': self.delegateto.username}
self.sucommand(cmdline)
cmdline = 'chpasswd --encrypted'
inline = '%(username)s:%(md5pass)s' % {
'username' : self.delegateto.username,
'md5pass' : self.delegateto.md5pass}
'username': self.delegateto.username,
'md5pass': self.delegateto.md5pass}
self.sucommand(cmdline, inline)
self._populate_home()
self._mail_sysuser()
@ -141,15 +146,17 @@ class SysuserEntity(BackendEntity):
config.get('sysuser', 'homebackupdir'))
if not os.path.isdir(backupdir):
cmdline = 'mkdir -p "%(backupdir)s"' % {
'backupdir' : backupdir}
'backupdir': backupdir}
status = self.sucommand(cmdline)
if status != 0:
raise Exception("could not create backup directory")
cmdline = 'deluser --remove-home --backup --backup-to "%(backupdir)s" %(username)s' % {
'backupdir' : backupdir,
'username' : self.delegateto.username}
cmdline = 'deluser --remove-home --backup --backup-to ' + \
' "%(backupdir)s" %(username)s' % {
'backupdir': backupdir,
'username': self.delegateto.username}
self.sucommand(cmdline)
class SysuserHandler(BackendEntityHandler):
"""BackendEntityHandler for Sysuser entities."""

9
gnuviechadmin/backend/tables.py

@ -1,4 +1,3 @@
# -*- python -*-
# -*- coding: utf-8 -*-
#
# Copyright (C) 2007, 2008 by Jan Dittberner.
@ -31,7 +30,8 @@ try:
config.get('database', 'uri'),
config.get('database', 'repository'))
if dbversion < required_version:
print("""Database version is %d but required version is %d. Trying automatic upgrade.""" %
print("""Database version is %d but required version is %d. Trying
automatic upgrade.""" %
(dbversion, required_version))
try:
migrate.versioning.api.upgrade(
@ -42,10 +42,11 @@ try:
print "Automatic upgrade failed."
raise
elif dbversion > required_version:
print("""Database version is %d which is higher than the required version %d. I cannot handle this situation without possible data loss.""" %
print("""Database version is %d which is higher than the required
version %d. I cannot handle this situation without possible data loss.""" %
(dbversion, required_version))
sys.exit(1)
except NoSuchTableError, nste:
except NoSuchTableError, nste:
print """The database is not versioned. Trying automatic versioning."""
try:
migrate.versioning.api.version_control(

31
gnuviechadmin/cli/CliCommand.py

@ -1,4 +1,3 @@
# -*- python -*-
# -*- coding: utf-8 -*-
#
# Copyright (C) 2007, 2008 by Jan Dittberner.
@ -19,10 +18,12 @@
# USA.
#
# Version: $Id$
import getopt, sys, logging
import getopt
import sys
import logging
from gnuviechadmin.exceptions import GnuviechadminError
class CliCommand:
"""Base class for command line interface.
@ -65,17 +66,17 @@ Common options:
%(option)s
%(mandatory)s %(optiondesc)s
""" % { 'called' : sys.argv[0],
'command' : self.name,
'description' : self.description,
'option' : '-v, --verbose',
'optiondesc' : 'verbose operation',
'mandatory' : " "}
""" % {'called': sys.argv[0],
'command': self.name,
'description': self.description,
'option': '-v, --verbose',
'optiondesc': 'verbose operation',
'mandatory': " "}
for commandname in self._optionmap.keys():
cmdl = "%(called)s %(command)s %(subcommand)s [-v|--verbose]" % {
'called' : sys.argv[0],
'command' : self.name,
'subcommand' : commandname}
'called': sys.argv[0],
'command': self.name,
'subcommand': commandname}
desc = """
%s
""" % (self._optionmap[commandname][0])
@ -97,9 +98,9 @@ Common options:
if not mandatory:
cmd = cmd + "]"
descmap = {
'option' : ", ".join(pairs),
'optiondesc' : optiondesc,
'mandatory' : ' '}
'option': ", ".join(pairs),
'optiondesc': optiondesc,
'mandatory': ' '}
if mandatory:
descmap['mandatory'] = '*'
desc = desc + """ %(option)s

2
gnuviechadmin/cli/__init__.py

@ -1,4 +1,3 @@
# -*- python -*-
# -*- coding: utf-8 -*-
#
# Copyright (C) 2007, 2008 by Jan Dittberner.
@ -19,7 +18,6 @@
# USA.
#
# Version: $Id$
"""This is the gnuviechadmin.cli package.
This package provides modules for the command line interface of the

65
gnuviechadmin/cli/client.py

@ -1,4 +1,3 @@
# -*- python -*-
# -*- coding: utf-8 -*-
#
# Copyright (C) 2007, 2008 by Jan Dittberner.
@ -19,8 +18,9 @@
# USA.
#
# Version: $Id$
import CliCommand
import sys
import CliCommand, sys
class ClientCli(CliCommand.CliCommand):
"""Command line interface command for client management."""
@ -28,37 +28,36 @@ class ClientCli(CliCommand.CliCommand):
name = "client"
description = "manage clients"
_optionmap = {
'create' : ("creates a new client",
[(["-f", "--firstname"], "firstname",
"the client's first name", True),
(["-l", "--lastname"], "lastname",
"the client's last name", True),
(["-t", "--title"], "title",
"the client's title", False),
(["-a", "--address"], "address1",
"the address of the client", True),
(["--address2"], "address2",
"second line of the client's address", False),
(["-z", "--zip"], "zip",
"the zipcode of the client's address", True),
(["-c", "--city"], "city",
"the city of the client's address", True),
(["--country"], "country",
"the client's country", False),
(["-e", "--email"], "email",
"the client's email address", True),
(["-p", "--phone"], "phone",
"the client's phone number", True),
(["-m", "--mobile"], "mobile",
"the client's mobile phone number", False),
(["-x", "--fax"], "fax",
"the client's fax number", False)]),
'list' : ("lists existing clients",
[]),
'delete' : ("deletes the specified client if it has no dependent data",
[(["-c", "--clientid"], "clientid",
"the client id", True)])}
'create': ("creates a new client",
[(["-f", "--firstname"], "firstname",
"the client's first name", True),
(["-l", "--lastname"], "lastname",
"the client's last name", True),
(["-t", "--title"], "title",
"the client's title", False),
(["-a", "--address"], "address1",
"the address of the client", True),
(["--address2"], "address2",
"second line of the client's address", False),
(["-z", "--zip"], "zip",
"the zipcode of the client's address", True),
(["-c", "--city"], "city",
"the city of the client's address", True),
(["--country"], "country",
"the client's country", False),
(["-e", "--email"], "email",
"the client's email address", True),
(["-p", "--phone"], "phone",
"the client's phone number", True),
(["-m", "--mobile"], "mobile",
"the client's mobile phone number", False),
(["-x", "--fax"], "fax",
"the client's fax number", False)]),
'list': ("lists existing clients", []),
'delete': ("deletes the specified client if it has no dependent data",
[(["-c", "--clientid"], "clientid",
"the client id", True)])}
def _execute(self, subcommand):
self.logger.debug("execute %s with data %s", subcommand,
str(self._data))

31
gnuviechadmin/cli/domain.py

@ -1,4 +1,3 @@
# -*- python -*-
# -*- coding: utf-8 -*-
#
# Copyright (C) 2007, 2008 by Jan Dittberner.
@ -19,8 +18,9 @@
# USA.
#
# Version: $Id$
import CliCommand
import sys
import CliCommand, sys
class DomainCli(CliCommand.CliCommand):
"""Command line interface command for domain management."""
@ -28,20 +28,19 @@ class DomainCli(CliCommand.CliCommand):
name = "domain"
description = "manage domains"
_optionmap = {
'create' : ("creates a new domain",
[(["-n", "--name"], "name",
"the domain name", True),
(["-t", "--type"], "type",
"domain type m for master or s for slave", False),
(["-m", "--master"], "master",
"master server for slave domains", False),
(["-s", "--sysuserid"], "sysuserid",
"system user id", True)]),
'list' : ("lists existing domains",
[]),
'delete' : ("delete a domain",
[(["-d", "--domainid"], "domainid",
"the domain id", True)])}
'create': ("creates a new domain",
[(["-n", "--name"], "name",
"the domain name", True),
(["-t", "--type"], "type",
"domain type m for master or s for slave", False),
(["-m", "--master"], "master",
"master server for slave domains", False),
(["-s", "--sysuserid"], "sysuserid",
"system user id", True)]),
'list': ("lists existing domains", []),
'delete': ("delete a domain",
[(["-d", "--domainid"], "domainid",
"the domain id", True)])}
def _execute(self, subcommand):
self.logger.debug("execute %s with data %s", subcommand,

42
gnuviechadmin/cli/record.py

@ -1,4 +1,3 @@
# -*- python -*-
# -*- coding: utf-8 -*-
#
# Copyright (C) 2007, 2008 by Jan Dittberner.
@ -19,8 +18,9 @@
# USA.
#
# Version: $Id$
import CliCommand
import sys
import CliCommand, sys
class RecordCli(CliCommand.CliCommand):
"""Command line interface command for DNS record management."""
@ -28,25 +28,25 @@ class RecordCli(CliCommand.CliCommand):
name = "record"
description = "manage DNS records"
_optionmap = {
'create' : ("creates a new record",
[(["-n", "--name"], "name",
"the record name", True),
(["-t", "--type"], "type",
"record type", True),
(["-c", "--content"], "content",
"record content", True),
(["-p", "--prio"], "prio",
"MX record priority", False),
(["--ttl"], "ttl",
"time to live", False),
(["-d", "--domainid"], "domainid",
"domain id", True)]),
'list' : ("lists existing records",
[(["-d", "--domainid"], "domainid",
"domain id", False)]),
'delete' : ("delete a record",
[(["-r", "--recordid"], "recordid",
"the record id", True)])}
'create': ("creates a new record",
[(["-n", "--name"], "name",
"the record name", True),
(["-t", "--type"], "type",
"record type", True),
(["-c", "--content"], "content",
"record content", True),
(["-p", "--prio"], "prio",
"MX record priority", False),
(["--ttl"], "ttl",
"time to live", False),
(["-d", "--domainid"], "domainid",
"domain id", True)]),
'list': ("lists existing records",
[(["-d", "--domainid"], "domainid",
"domain id", False)]),
'delete': ("delete a record",
[(["-r", "--recordid"], "recordid",
"the record id", True)])}
def _execute(self, subcommand):
self.logger.debug("execute %s with data %s", subcommand,

43
gnuviechadmin/cli/sysuser.py

@ -1,4 +1,3 @@
# -*- python -*-
# -*- coding: utf-8 -*-
#
# Copyright (C) 2007, 2008 by Jan Dittberner.
@ -19,33 +18,33 @@
# USA.
#
# Version: $Id$
import CliCommand
import sys
import CliCommand, sys
class SysuserCli(CliCommand.CliCommand):
"""Command line interface command for system user managament."""
name = "sysuser"
name = "sysuser"
description = "manage system users"
_optionmap = {
"create" : ("create a new system user with the given options.",
[(["-n", "--username"], "username",
"the system user name", False),
(["-t", "--usertype"], "usertype",
"the numeric user type", False),
(["-h", "--home"], "home",
"the home directory", False),
(["-s", "--shell"], "shell",
"true if the user should get shell access", False),
(["-p", "--password"], "clearpass",
"the password for the user", False),
(["-c", "--clientid"], "clientid",
"the client id", True)]),
"list" : ("list existing system users.",
[]),
"delete" : ("delete a system user.",
[(["-s", "--sysuserid"], "sysuserid",
"the system user id", True)])}
_optionmap = {
"create": ("create a new system user with the given options.",
[(["-n", "--username"], "username",
"the system user name", False),
(["-t", "--usertype"], "usertype",
"the numeric user type", False),
(["-h", "--home"], "home",
"the home directory", False),
(["-s", "--shell"], "shell",
"true if the user should get shell access", False),
(["-p", "--password"], "clearpass",
"the password for the user", False),
(["-c", "--clientid"], "clientid",
"the client id", True)]),
"list": ("list existing system users.", []),
"delete": ("delete a system user.",
[(["-s", "--sysuserid"], "sysuserid",
"the system user id", True)])}
def _execute(self, subcommand):
self.logger.debug("execute %s with data %s", subcommand,

13
gnuviechadmin/exceptions.py

@ -1,4 +1,3 @@
# -*- python -*-
# -*- coding: utf-8 -*-
#
# Copyright (C) 2007, 2008 by Jan Dittberner.
@ -19,26 +18,30 @@
# USA.
#
# Version: $Id$
"""This file defines the gnuviechadmin specific exception types."""
class GnuviechadminError(Exception):
"""This is the base class for domain specific exceptions of
Gnuviechadmin."""
pass
class MissingFieldsError(GnuviechadminError):
"""This exception should be raised when a required field of a data
class is missing."""
def __init__(self, missingfields):
self.missing = missingfields
def __str__(self):
return "the fields %s are missing." % (repr(self.missing))
class CreationFailedError(GnuviechadminError):
"""This exception should be raised if a business object could not
be created."""
def __init__(self, classname, cause = None):
self.classname = classname
self.cause = cause
@ -49,9 +52,11 @@ class CreationFailedError(GnuviechadminError):
msg += " The reason is %s." % (str(self.cause))
return msg
class DeleteFailedError(GnuviechadminError):
"""This exception should be raise if a business object coild not
be deleted."""
def __init__(self, classname, cause = None):
self.classname = classname
self.cause = cause
@ -62,9 +67,11 @@ class DeleteFailedError(GnuviechadminError):
msg += " The reason is %s." % (str(self.cause))
return msg
class ValidationFailedError(GnuviechadminError):
"""This exception should be raised if the validation of a business
object failed."""
def __init__(self, instance, cause = None):
self.instance = instance
self.cause = cause
@ -75,9 +82,11 @@ class ValidationFailedError(GnuviechadminError):
msg += " The reason is %s." % (str(self.cause))
return msg
class CannotDeleteError(GnuviechadminError):
"""This exception should be raised if an entity cannot be deleted
because of some unmatched precondition."""
def __init__(self, instance, cause = None):
self.instance = instance
self.cause = cause

1
gnuviechadmin/util/__init__.py

@ -1,4 +1,3 @@
# -*- python -*-
# -*- coding: utf-8 -*-
#
# Copyright (C) 2007, 2008 by Jan Dittberner.

30
gnuviechadmin/util/getenttools.py

@ -1,4 +1,3 @@
# -*- python -*-
# -*- coding: utf-8 -*-
#
# Copyright (C) 2007, 2008 by Jan Dittberner.
@ -22,10 +21,13 @@
"""Tools for handling user and group information."""
import pwd, grp
import pwd
import grp
class PasswdUser(object):
"""This class represents users in the user database."""
def __init__(self, username, pw, uid, gid, gecos, home, shell):
"""Create a new PasswdUser."""
self.username = username
@ -45,8 +47,10 @@ class PasswdUser(object):
self.home,
self.shell)
class PasswdGroup(object):
"""This class represents lines in the groups database."""
def __init__(self, groupname, pw, gid, members):
"""Create a new PasswdGroup."""
self.groupname = groupname
@ -60,18 +64,22 @@ class PasswdGroup(object):
self.gid,
",".join(self.members))
def parse_groups():
"""Parses all available groups to PasswdGroup instances."""
return [PasswdGroup(*arr) for arr in grp.getgrall()]
def parse_users():
"""Parses all available users to PasswdUser instances."""
return [PasswdUser(*arr) for arr in pwd.getpwall()]
def find_user_by_prefix(prefix):
"""Finds all user entries with the given prefix."""
"""Finds all user entries with the given prefix."""
return [user for user in parse_users() if user.username.startswith(prefix)]
def get_user_by_id(uid):
"""Gets the user with the given user id."""
users = [user for user in parse_users() if user.uid == uid]
@ -79,6 +87,7 @@ def get_user_by_id(uid):
return users[0]
return None
def get_group_by_id(gid):
"""Gets the group with the given group id."""
groups = [group for group in parse_groups() if group.gid == gid]
@ -86,6 +95,7 @@ def get_group_by_id(gid):
return groups[0]
return None
def get_next_uid(lowerboundary = 10000, upperboundary = 65536):
"""Gets the first available user id in the given range.
@ -98,16 +108,19 @@ def get_next_uid(lowerboundary = 10000, upperboundary = 65536):