* some pylint fixes (addresses #24)
* add pydoc in client and domain backend classes * add support for buildutils in setup.py git-svn-id: file:///home/www/usr01/svn/gnuviechadmin/trunk@262 a67ec6bc-e5d5-0310-a910-815c51eb3124
This commit is contained in:
parent
4ee62d5b2e
commit
daf9517a83
13 changed files with 147 additions and 69 deletions
|
@ -18,16 +18,16 @@
|
|||
# USA.
|
||||
#
|
||||
# Version: $Id$
|
||||
"""This module defines the BackendEntity base class."""
|
||||
|
||||
import os
|
||||
import logging
|
||||
import tempfile
|
||||
|
||||
from settings import config
|
||||
from gnuviechadmin.exceptions import *
|
||||
from gnuviechadmin.exceptions import MissingFieldsError
|
||||
from gnuviechadmin.backend.settings import config
|
||||
from gnuviechadmin.util import gpgmail
|
||||
from subprocess import *
|
||||
import sqlalchemy
|
||||
from subprocess import Popen, PIPE
|
||||
from sqlalchemy.orm import object_mapper
|
||||
|
||||
|
||||
|
@ -52,17 +52,17 @@ class BackendEntity(object):
|
|||
suwrapper = config.get('common', 'suwrapper')
|
||||
toexec = "%s %s" % (suwrapper, cmdline)
|
||||
if pipedata:
|
||||
p = Popen(toexec, shell = True, stdin=PIPE)
|
||||
pipe = p.stdin
|
||||
print >>pipe, pipedata
|
||||
pipeproc = Popen(toexec, shell = True, stdin=PIPE)
|
||||
pipe = pipeproc.stdin
|
||||
print >> pipe, pipedata
|
||||
pipe.close()
|
||||
sts = os.waitpid(p.pid, 0)
|
||||
sts = os.waitpid(pipeproc.pid, 0)
|
||||
if self.verbose:
|
||||
print "%s|%s: %d" % (pipedata, toexec, sts[1])
|
||||
self.logger.info("%s|%s: %d", pipedata, toexec, sts[1])
|
||||
else:
|
||||
p = Popen(toexec, shell = True)
|
||||
sts = os.waitpid(p.pid, 0)
|
||||
pipeproc = Popen(toexec, shell = True)
|
||||
sts = os.waitpid(pipeproc.pid, 0)
|
||||
if self.verbose:
|
||||
print "%s: %s" % (toexec, sts[1])
|
||||
self.logger.info("%s: %s", toexec, sts[1])
|
||||
|
@ -77,11 +77,11 @@ class BackendEntity(object):
|
|||
for cmdline in cmdlines:
|
||||
toexec = "%s %s" % (suwrapper, cmdline)
|
||||
if predecessor is None:
|
||||
p = Popen(toexec, shell = True, stdout = PIPE)
|
||||
pipeproc = Popen(toexec, shell = True, stdout = PIPE)
|
||||
else:
|
||||
p = Popen(toexec, shell = True, stdin = predecessor.stdout,
|
||||
stdout = PIPE)
|
||||
predecessor = p
|
||||
pipeproc = Popen(toexec, shell = True,
|
||||
stdin = predecessor.stdout, stdout = PIPE)
|
||||
predecessor = pipeproc
|
||||
output = predecessor.communicate()[0]
|
||||
return predecessor.wait()
|
||||
|
||||
|
|
|
@ -18,17 +18,18 @@
|
|||
# USA.
|
||||
#
|
||||
# Version: $Id$
|
||||
"""This module defines the BackendEntityHandler class."""
|
||||
|
||||
import sqlalchemy
|
||||
import logging
|
||||
from sqlalchemy.orm import create_session
|
||||
from gnuviechadmin.exceptions import *
|
||||
from BackendEntity import *
|
||||
|
||||
|
||||
class BackendEntityHandler(object):
|
||||
"""This class is a handler for BackendEntity instances."""
|
||||
|
||||
def __init__(self, entityclass, toclass, verbose = False):
|
||||
"""Initialize the handler with a specific entity class,
|
||||
transfer object class and verbosity flag."""
|
||||
self.logger = logging.getLogger("%s.%s" % (
|
||||
self.__class__.__module__, self.__class__.__name__))
|
||||
self.entityclass = entityclass
|
||||
|
@ -74,17 +75,17 @@ class BackendEntityHandler(object):
|
|||
sess = create_session()
|
||||
transaction = sess.create_transaction()
|
||||
try:
|
||||
to = sess.query(self.toclass).get(pkvalue)
|
||||
if to:
|
||||
entity = self.entityclass(to, self.verbose)
|
||||
tobj = sess.query(self.toclass).get(pkvalue)
|
||||
if tobj:
|
||||
entity = self.entityclass(tobj, self.verbose)
|
||||
self.logger.info("delete %s", str(entity))
|
||||
if self.verbose:
|
||||
print "delete %s" % (str(entity))
|
||||
entity.delete_hook(sess)
|
||||
sess.delete(to)
|
||||
sess.delete(tobj)
|
||||
sess.flush()
|
||||
transaction.commit()
|
||||
except Exception, e:
|
||||
except Exception:
|
||||
transaction.rollback()
|
||||
self.logger.exception("Exception in delete.")
|
||||
raise
|
||||
|
|
|
@ -18,18 +18,21 @@
|
|||
# USA.
|
||||
#
|
||||
# Version: $Id$
|
||||
"""This module defines the ClientEntity class."""
|
||||
|
||||
from gnuviechadmin.exceptions import *
|
||||
from settings import config
|
||||
from BackendTo import *
|
||||
from BackendEntity import *
|
||||
from BackendEntityHandler import *
|
||||
from gnuviechadmin.backend.settings import config, get_template, \
|
||||
get_template_string
|
||||
from gnuviechadmin.exceptions import CannotDeleteError
|
||||
from gnuviechadmin.backend.BackendTo import Client
|
||||
from gnuviechadmin.backend.BackendEntity import BackendEntity
|
||||
from gnuviechadmin.backend.BackendEntityHandler import BackendEntityHandler
|
||||
|
||||
|
||||
class ClientEntity(BackendEntity):
|
||||
"""Entity class for clients."""
|
||||
|
||||
def __init__(self, delegate, verbose = False, **kwargs):
|
||||
"""Initializes the client entity instance."""
|
||||
BackendEntity.__init__(self, delegate, verbose)
|
||||
for (key, value) in kwargs.items():
|
||||
self.__setattr__(key, value)
|
||||
|
@ -38,6 +41,7 @@ class ClientEntity(BackendEntity):
|
|||
self.validate()
|
||||
|
||||
def _client_mail(self):
|
||||
"""Mails a summary about the creation of the client."""
|
||||
text = get_template(config.get('common', 'mailtemplates'),
|
||||
config.get('client', 'create.mail')).substitute({
|
||||
'firstname': self.delegateto.firstname,
|
||||
|
|
|
@ -18,27 +18,35 @@
|
|||
# USA.
|
||||
#
|
||||
# Version: $Id$
|
||||
"""This module defines the code for handling domains."""
|
||||
|
||||
import datetime
|
||||
import os
|
||||
|
||||
from gnuviechadmin.exceptions import *
|
||||
from settings import *
|
||||
from BackendTo import Record, Domain
|
||||
from BackendEntity import BackendEntity
|
||||
from BackendEntityHandler import BackendEntityHandler
|
||||
from gnuviechadmin.exceptions import ValidationFailedError
|
||||
from gnuviechadmin.backend.settings import config, get_template, \
|
||||
get_template_dir, get_template_string
|
||||
from gnuviechadmin.backend.BackendTo import Record, Domain
|
||||
from gnuviechadmin.backend.BackendEntity import BackendEntity
|
||||
from gnuviechadmin.backend.BackendEntityHandler import BackendEntityHandler
|
||||
|
||||
|
||||
class DomainEntity(BackendEntity):
|
||||
"""Entity class for DNS domains."""
|
||||
|
||||
# the valid domain types
|
||||
_valid_domain_types = ("MASTER", "SLAVE")
|
||||
|
||||
def __init__(self, delegate, verbose = False, **kwargs):
|
||||
"""Initializes the DomainEntity instance.
|
||||
|
||||
`delegate` is the corresponding database object.
|
||||
If `verbose` is `True` verbose logging is turned on.
|
||||
"""
|
||||
BackendEntity.__init__(self, delegate, verbose)
|
||||
self.ns1 = None
|
||||
self.ns2 = None
|
||||
self.mx = None
|
||||
self.mxrr = None
|
||||
self.ipaddr = None
|
||||
for (key, value) in kwargs.items():
|
||||
self.__setattr__(key, value)
|
||||
|
@ -48,17 +56,20 @@ class DomainEntity(BackendEntity):
|
|||
self.ns1 = config.get('domain', 'defaultns1')
|
||||
if not self.ns2:
|
||||
self.ns2 = config.get('domain', 'defaultns2')
|
||||
if not self.mx:
|
||||
self.mx = config.get('domain', 'defaultmx')
|
||||
if not self.mxrr:
|
||||
self.mxrr = config.get('domain', 'defaultmx')
|
||||
if not self.ipaddr:
|
||||
self.ipaddr = config.get('domain', 'defaultip')
|
||||
self.delegateto.type = self.delegateto.type.upper()
|
||||
self.validate()
|
||||
|
||||
def getdefaultdomaintype(self):
|
||||
"""Returns the default domain type."""
|
||||
return self._valid_domain_types[0]
|
||||
|
||||
def validate(self):
|
||||
"""Validates the consistency if the entity instance and
|
||||
dependent entities."""
|
||||
BackendEntity.validate(self)
|
||||
if not self.delegateto.type in self._valid_domain_types:
|
||||
raise ValidationFailedError(
|
||||
|
@ -70,11 +81,12 @@ class DomainEntity(BackendEntity):
|
|||
if not self.ns1 or not self.ns2:
|
||||
raise ValidationFailedError(
|
||||
self, "two nameservers must be specified.")
|
||||
if not self.mx:
|
||||
if not self.mxrr:
|
||||
raise ValidationFailedError(
|
||||
self, "a primary mx host must be specified.")
|
||||
|
||||
def _getnewserial(self, oldserial = None):
|
||||
"""Gets a new zone serial number for the DNS domain entity."""
|
||||
current = datetime.datetime.now()
|
||||
datepart = "%04d%02d%02d" % \
|
||||
(current.year, current.month, current.day)
|
||||
|
@ -87,6 +99,7 @@ class DomainEntity(BackendEntity):
|
|||
return retval
|
||||
|
||||
def _getnewsoa(self):
|
||||
"""Gets a new SOA record for the DNS domain entity."""
|
||||
return '%s %s %d %d %d %d %d' % \
|
||||
(self.ns1,
|
||||
config.get('domain', 'defaulthostmaster'),
|
||||
|
@ -97,6 +110,7 @@ class DomainEntity(BackendEntity):
|
|||
config.getint('domain', 'defaultminimumttl'))
|
||||
|
||||
def update_serial(self, session):
|
||||
"""Updates the serial of the domain."""
|
||||
query = session.query(Record)
|
||||
soarecord = query.get_by(Record.c.type == 'SOA',
|
||||
Record.c.domainid == self.delegateto.domainid)
|
||||
|
@ -107,19 +121,24 @@ class DomainEntity(BackendEntity):
|
|||
session.flush()
|
||||
|
||||
def _get_vhost_dir(self):
|
||||
"""Gets the directory name for the Apache VirtualHost of the
|
||||
domain."""
|
||||
return os.path.join(self.delegateto.sysuser.home,
|
||||
self.delegateto.name,
|
||||
config.get('domain', 'htdir'))
|
||||
|
||||
def _get_log_dir(self):
|
||||
"""Gets the Apache log file directory for the domain."""
|
||||
return os.path.join(config.get('domain', 'logpath'),
|
||||
self.delegateto.name)
|
||||
|
||||
def _get_stats_dir(self):
|
||||
"""Gets the statistics dir for the domain."""
|
||||
return os.path.join(config.get('domain', 'statspath'),
|
||||
self.delegateto.name)
|
||||
|
||||
def _create_vhost_dir(self):
|
||||
"""Creates the Apache VirtualHost directory for the domain."""
|
||||
vhostdir = self._get_vhost_dir()
|
||||
self.logger.debug("creating virtual host dir %s" % (vhostdir))
|
||||
cmd = 'mkdir -p "%s"' % (vhostdir)
|
||||
|
@ -140,10 +159,13 @@ class DomainEntity(BackendEntity):
|
|||
self.sucommand(cmd)
|
||||
|
||||
def _create_log_dir(self):
|
||||
"""Creates the Apache log file directory for the domain."""
|
||||
cmd = 'mkdir -p "%s"' % (self._get_log_dir())
|
||||
self.sucommand(cmd)
|
||||
|
||||
def _get_auth_userfile(self):
|
||||
"""Gets the file name of the password file for statistic
|
||||
logins for the domain."""
|
||||
authdir = config.get('domain', 'authdir')
|
||||
if not os.path.isdir(authdir):
|
||||
cmd = 'mkdir -p "%s"' % (authdir)
|
||||
|
@ -171,6 +193,8 @@ class DomainEntity(BackendEntity):
|
|||
'.htaccess'), template)
|
||||
|
||||
def _create_stats_conf(self):
|
||||
"""Creates the modlogan statistics configuration for the
|
||||
domain."""
|
||||
modlogandir = os.path.join(config.get('domain',
|
||||
'modlogandir'),
|
||||
self.delegateto.sysuser.username)
|
||||
|
@ -188,6 +212,7 @@ class DomainEntity(BackendEntity):
|
|||
template)
|
||||
|
||||
def _create_apache_conf(self):
|
||||
"""Creates the Apache configuration file for the domain."""
|
||||
template = get_template(config.get('domain', 'conftemplates'),
|
||||
config.get('domain', 'apachetemplate'))
|
||||
template = template.substitute({
|
||||
|
@ -200,6 +225,7 @@ class DomainEntity(BackendEntity):
|
|||
self.delegateto.name), template)
|
||||
|
||||
def _mail_domain(self):
|
||||
"""Mail a summary of the domain data."""
|
||||
template = get_template(config.get('common', 'mailtemplates'),
|
||||
config.get('domain', 'create.mail'))
|
||||
text = template.substitute({
|
||||
|
@ -213,6 +239,11 @@ class DomainEntity(BackendEntity):
|
|||
self.send_mail(subject, text)
|
||||
|
||||
def create_hook(self, session):
|
||||
"""Hook for the creation of the domain.
|
||||
|
||||
This method is called by
|
||||
`gnuviechadmin.backend.BackendEntityHandler.create()`.
|
||||
"""
|
||||
self.delegateto.records.append(Record(
|
||||
name = self.delegateto.name, type = 'SOA',
|
||||
content = self._getnewsoa(),
|
||||
|
@ -224,7 +255,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.mxrr,
|
||||
ttl = config.getint('domain', 'defaultttl'),
|
||||
prio = config.getint('domain', 'defaultmxprio')))
|
||||
self.delegateto.records.append(Record(
|
||||
|
@ -244,6 +275,7 @@ class DomainEntity(BackendEntity):
|
|||
self._mail_domain()
|
||||
|
||||
def _delete_apache_conf(self):
|
||||
"""Deletes the Apache configuration file for the domain."""
|
||||
cmd = 'a2dissite %s' % (self.delegateto.name)
|
||||
self.sucommand(cmd)
|
||||
cmd = 'rm "%s"' % (os.path.join(config.get('domain', 'sitesdir'),
|
||||
|
@ -251,12 +283,15 @@ class DomainEntity(BackendEntity):
|
|||
self.sucommand(cmd)
|
||||
|
||||
def _delete_stats_conf(self):
|
||||
"""Deletes the modlogan stastics configuration for the
|
||||
domain."""
|
||||
cmd = 'rm "%s"' % (os.path.join(config.get('domain', 'modlogandir'),
|
||||
self.delegateto.sysuser.username,
|
||||
self.delegateto.name + '.conf'))
|
||||
self.sucommand(cmd)
|
||||
|
||||
def _archive_stats_dir(self):
|
||||
"""Archives the statistics directory for the domain."""
|
||||
archive = os.path.join(self.delegateto.sysuser.home,
|
||||
'%(domain)s-stats.tar.gz' % {
|
||||
'domain': self.delegateto.name})
|
||||
|
@ -276,6 +311,7 @@ class DomainEntity(BackendEntity):
|
|||
self.sucommand(cmd)
|
||||
|
||||
def _archive_log_dir(self):
|
||||
"""Archives the Apache log file directory for the domain."""
|
||||
archive = os.path.join(self.delegateto.sysuser.home,
|
||||
'%(domain)s-logs.tar.gz' % {
|
||||
'domain': self.delegateto.name})
|
||||
|
@ -295,6 +331,7 @@ class DomainEntity(BackendEntity):
|
|||
self.sucommand(cmd)
|
||||
|
||||
def _archive_vhost_dir(self):
|
||||
"""Archives the Apache VirtualHost directory for the domain."""
|
||||
archive = os.path.join(self.delegateto.sysuser.home,
|
||||
'%(domain)s-vhost.tar.gz' % {
|
||||
'domain': self.delegateto.name})
|
||||
|
@ -315,6 +352,10 @@ class DomainEntity(BackendEntity):
|
|||
self.sucommand(cmd)
|
||||
|
||||
def delete_hook(self, session):
|
||||
"""Deletes domain related files and directories.
|
||||
|
||||
This method is called by `BackendEntityHandler.delete()`.
|
||||
"""
|
||||
self._delete_apache_conf()
|
||||
self._delete_stats_conf()
|
||||
self._archive_stats_dir()
|
||||
|
@ -326,4 +367,5 @@ class DomainHandler(BackendEntityHandler):
|
|||
"""BackendEntityHandler for Domain entities."""
|
||||
|
||||
def __init__(self, verbose = False):
|
||||
"""Initialize the DomainHandler."""
|
||||
BackendEntityHandler.__init__(self, DomainEntity, Domain, verbose)
|
||||
|
|
|
@ -27,8 +27,7 @@ templates."""
|
|||
|
||||
import ConfigParser
|
||||
import os
|
||||
import string
|
||||
import logging.config
|
||||
from string import Template
|
||||
|
||||
# global settings which must not be user configurable
|
||||
required_version = 3
|
||||
|
@ -54,9 +53,9 @@ def get_template(dirname, filename):
|
|||
templatefile = file(os.path.join(get_template_dir(dirname),
|
||||
filename))
|
||||
templatedata = templatefile.read()
|
||||
return string.Template(templatedata.decode('utf_8'))
|
||||
return Template(templatedata.decode('utf_8'))
|
||||
|
||||
|
||||
def get_template_string(templatestring):
|
||||
"""Returns a template object for the given template string."""
|
||||
return string.Template(templatestring)
|
||||
return Template(templatestring)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue