1
0
Fork 0

r1083@denkpolster: jan | 2008-04-04 20:36:31 +0200

work on compatibility issues
  * make gnuviechadmin compatible with (fixes #6)
  * setup database at startup if necessary (fixes #8)


git-svn-id: file:///home/www/usr01/svn/gnuviechadmin/trunk@248 a67ec6bc-e5d5-0310-a910-815c51eb3124
This commit is contained in:
Jan Dittberner 2008-04-04 18:36:46 +00:00
parent c049fd9bc9
commit 4ae866c559
7 changed files with 52 additions and 30 deletions

View file

@ -2,7 +2,7 @@ from sqlalchemy import *
from migrate import * from migrate import *
from gnuviechadmin.backend.settings import dbschema from gnuviechadmin.backend.settings import dbschema
meta = BoundMetaData(migrate_engine) meta = MetaData(migrate_engine)
client = Table( client = Table(
'client', meta, 'client', meta,
Column('clientid', Integer, primary_key=True), Column('clientid', Integer, primary_key=True),
@ -29,7 +29,7 @@ sysuser = Table(
Column('shell', Boolean, nullable=False, default=False), Column('shell', Boolean, nullable=False, default=False),
Column('clearpass', String(64)), Column('clearpass', String(64)),
Column('md5pass', String(34)), Column('md5pass', String(34)),
Column('clientid', Integer, ForeignKey("client.clientid"), Column('clientid', Integer, ForeignKey(client.c.clientid),
nullable=False), nullable=False),
Column('sysuid', Integer, nullable=False, unique=True), Column('sysuid', Integer, nullable=False, unique=True),
Column('lastchange', DateTime, default=func.now()), Column('lastchange', DateTime, default=func.now()),
@ -43,14 +43,14 @@ domain = Table(
Column('last_check', Integer), Column('last_check', Integer),
Column('type', String(6), nullable=False), Column('type', String(6), nullable=False),
Column('notified_serial', Integer), Column('notified_serial', Integer),
Column('sysuserid', Integer, ForeignKey("sysuser.sysuserid"), Column('sysuserid', Integer, ForeignKey(sysuser.c.sysuserid),
nullable=False), nullable=False),
schema = dbschema schema = dbschema
) )
record = Table( record = Table(
'record', meta, 'record', meta,
Column('recordid', Integer, primary_key=True), Column('recordid', Integer, primary_key=True),
Column('domainid', Integer, ForeignKey("domain.domainid"), Column('domainid', Integer, ForeignKey(domain.c.domainid),
nullable=False), nullable=False),
Column('name', String(255)), Column('name', String(255)),
Column('type', String(6)), Column('type', String(6)),
@ -64,7 +64,7 @@ supermaster = Table(
'supermaster', meta, 'supermaster', meta,
Column('ip', String(25), nullable=False), Column('ip', String(25), nullable=False),
Column('nameserver', String(255), nullable=False), Column('nameserver', String(255), nullable=False),
Column('account', Integer, ForeignKey("sysuser.sysuserid"), Column('account', Integer, ForeignKey(sysuser.c.sysuserid),
nullable=False), nullable=False),
schema = dbschema schema = dbschema
) )

View file

@ -2,12 +2,12 @@ from sqlalchemy import *
from migrate import * from migrate import *
from gnuviechadmin.backend.settings import dbschema from gnuviechadmin.backend.settings import dbschema
meta = BoundMetaData(migrate_engine) meta = MetaData(migrate_engine)
domain = Table('domain', meta, schema = dbschema, autoload = True) domain = Table('domain', meta, schema = dbschema, autoload = True)
mailaccount = Table( mailaccount = Table(
'mailaccount', meta, 'mailaccount', meta,
Column('mailaccountid', Integer, primary_key = True), Column('mailaccountid', Integer, primary_key = True),
Column('domainid', Integer, ForeignKey('domain.domainid'), Column('domainid', Integer, ForeignKey(domain.c.domainid),
nullable = False), nullable = False),
Column('mailaccount', String(12), nullable = False, unique = True), Column('mailaccount', String(12), nullable = False, unique = True),
Column('clearpass', String(64)), Column('clearpass', String(64)),
@ -22,7 +22,7 @@ mailaccount = Table(
mailaddress = Table( mailaddress = Table(
'mailaddress', meta, 'mailaddress', meta,
Column('mailaddressid', Integer, primary_key = True), Column('mailaddressid', Integer, primary_key = True),
Column('domainid', Integer, ForeignKey('domain.domainid'), Column('domainid', Integer, ForeignKey(domain.c.domainid),
nullable = False), nullable = False),
Column('email', String(255), nullable = False), Column('email', String(255), nullable = False),
UniqueConstraint('email', 'domainid'), UniqueConstraint('email', 'domainid'),
@ -31,7 +31,7 @@ mailaddress = Table(
mailtarget = Table( mailtarget = Table(
'mailtarget', meta, 'mailtarget', meta,
Column('mailtargetid', Integer, primary_key = True), Column('mailtargetid', Integer, primary_key = True),
Column('mailaddressid', Integer, ForeignKey('mailaddress.mailaddressid'), Column('mailaddressid', Integer, ForeignKey(mailaddress.c.mailaddressid),
nullable = False), nullable = False),
Column('target', String(128), nullable = False), Column('target', String(128), nullable = False),
UniqueConstraint('target', 'mailaddressid'), UniqueConstraint('target', 'mailaddressid'),

View file

@ -1,6 +1,7 @@
# -*- python -*-
# -*- 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 # 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 # it under the terms of the GNU General Public License as published by
@ -26,6 +27,7 @@ from gnuviechadmin.exceptions import *
from gnuviechadmin.util import gpgmail from gnuviechadmin.util import gpgmail
from subprocess import * from subprocess import *
import sqlalchemy import sqlalchemy
from sqlalchemy.orm import object_mapper
class BackendEntity(object): class BackendEntity(object):
"""This is the abstract base class for all backend entity classes.""" """This is the abstract base class for all backend entity classes."""
@ -92,7 +94,7 @@ class BackendEntity(object):
values.""" values."""
missingfields = [] missingfields = []
for key in [col.name for col in \ for key in [col.name for col in \
sqlalchemy.object_mapper( object_mapper(
self.delegateto).local_table.columns \ self.delegateto).local_table.columns \
if not col.primary_key and not col.nullable]: if not col.primary_key and not col.nullable]:
if self.delegateto.__getattribute__(key) is None: if self.delegateto.__getattribute__(key) is None:

View file

@ -1,6 +1,7 @@
# -*- 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 # 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 # it under the terms of the GNU General Public License as published by
@ -20,6 +21,7 @@
# Version: $Id$ # Version: $Id$
import sqlalchemy, logging import sqlalchemy, logging
from sqlalchemy.orm import create_session
from gnuviechadmin.exceptions import * from gnuviechadmin.exceptions import *
from BackendEntity import * from BackendEntity import *
@ -35,7 +37,7 @@ class BackendEntityHandler(object):
"""Create a new entity of the managed type with the fields set """Create a new entity of the managed type with the fields set
to the values in kwargs.""" to the values in kwargs."""
self.logger.debug("create with params %s", str(kwargs)) self.logger.debug("create with params %s", str(kwargs))
sess = sqlalchemy.create_session() sess = create_session()
transaction = sess.create_transaction() transaction = sess.create_transaction()
delegate = self.toclass(**kwargs) delegate = self.toclass(**kwargs)
entity = self.entityclass(delegate, self.verbose) entity = self.entityclass(delegate, self.verbose)
@ -54,12 +56,12 @@ class BackendEntityHandler(object):
def fetchall(self, **kwargs): def fetchall(self, **kwargs):
"""Fetches all entities of the managed entity type.""" """Fetches all entities of the managed entity type."""
self.logger.debug("fetchall with params %s", str(kwargs)) self.logger.debug("fetchall with params %s", str(kwargs))
session = sqlalchemy.create_session() session = create_session()
query = session.query(self.toclass) query = session.query(self.toclass)
if kwargs: if kwargs:
allentities = query.select_by(**kwargs) allentities = query.filter_by(**kwargs).all()
else: else:
allentities = query.select() allentities = query.all()
return [self.entityclass(entity, self.verbose) \ return [self.entityclass(entity, self.verbose) \
for entity in allentities] for entity in allentities]
@ -67,7 +69,7 @@ class BackendEntityHandler(object):
"""Deletes the entity of the managed entity type that has the """Deletes the entity of the managed entity type that has the
specified primary key value.""" specified primary key value."""
self.logger.debug("delete with primary key %s", str(pkvalue)) self.logger.debug("delete with primary key %s", str(pkvalue))
sess = sqlalchemy.create_session() sess = create_session()
transaction = sess.create_transaction() transaction = sess.create_transaction()
try: try:
to = sess.query(self.toclass).get(pkvalue) to = sess.query(self.toclass).get(pkvalue)

View file

@ -1,3 +1,4 @@
# -*- python -*-
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# #
# Copyright (C) 2007, 2008 by Jan Dittberner. # Copyright (C) 2007, 2008 by Jan Dittberner.
@ -19,7 +20,7 @@
# #
# Version: $Id$ # Version: $Id$
from sqlalchemy.orm import object_mapper from sqlalchemy.orm import object_mapper, mapper, relation
from tables import * from tables import *

View file

@ -1,3 +1,4 @@
# -*- python -*-
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# #
# Copyright (C) 2007, 2008 by Jan Dittberner. # Copyright (C) 2007, 2008 by Jan Dittberner.
@ -30,19 +31,34 @@ try:
config.get('database', 'uri'), config.get('database', 'uri'),
config.get('database', 'repository')) config.get('database', 'repository'))
if dbversion < required_version: if dbversion < required_version:
print("""Database version is %d but required version is %d, run print("""Database version is %d but required version is %d. Trying automatic upgrade.""" %
(dbversion, required_version))
migrate upgrade %s %s try:
migrate.versioning.api.upgrade(
to fix this.""" % config.get('database', 'uri'),
(dbversion, required_version, config.get('database', 'uri'), config.get('database', 'repository'),
config.get('database', 'repository'))) required_version)
except:
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.""" %
(dbversion, required_version))
sys.exit(1) sys.exit(1)
except NoSuchTableError, nste: except NoSuchTableError, nste:
print nste print """The database is not versioned. Trying automatic versioning."""
sys.exit(1) try:
migrate.versioning.api.version_control(
config.get('database', 'uri'),
config.get('database', 'repository'))
migrate.versioning.api.upgrade(
config.get('database', 'uri'),
config.get('database', 'repository', required_version))
except:
print "Automatic setup failed."
raise
meta = BoundMetaData(config.get('database', 'uri')) meta = MetaData(config.get('database', 'uri'))
#meta.engine.echo = True #meta.engine.echo = True
client_table = Table('client', meta, schema = dbschema, autoload = True) client_table = Table('client', meta, schema = dbschema, autoload = True)
sysuser_table = Table('sysuser', meta, schema = dbschema, autoload = True) sysuser_table = Table('sysuser', meta, schema = dbschema, autoload = True)

View file

@ -1,6 +1,7 @@
# -*- python -*- # -*- 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 # 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 # it under the terms of the GNU General Public License as published by