Logo Search packages:      
Sourcecode: maxdb-7.5.00 version File versions  Download package

instdb-demo.py

#! /usr/bin/python
#
#  basically a trimmed down version of instdb.py from the maxdb-buildtools
#  package
#
#  changes by Martin Kittel.
#
#
#  remarks:      For including a private configuration this script
#                looks for a file named "myparams.dbm" in the current
#                directory. In this file you can provide private
#                definitions of parameters and devspaces.
#
#                Lines starting with "param_put" contains a valid
#                "param_put" DBMServer command.
#                This lines will be used in the "Create param file..."
#                section of this script.
#
#                Lines starting with "param_adddevspace" contains a
#                a valid "param_adddevspace" DBMServer command.                definition. Use format from the DBMServer command
#                This lines will be used in the "Add devspaces..."
#                section of this script.
#
#                All other lines will be ignored.
#
#    ========== licence begin  GPL
#    Copyright (C) 2001 SAP AG
#
#    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 the Free Software Foundation; either version 2
#    of the License, or (at your option) any later version.
#
#    This program 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 this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#    ========== licence end
#

import sys
import string
import re
import os

import sdb.dbm
import getopt

_protFile = 'instdb.prt'

def fatalError (msg):
    sys.stderr.write ('\ninstdb.py: ' + msg + '\n')
    sys.stderr.write ('(See ' + _protFile + ' for more information.)\n')
    sys.exit (3)

def show (comment):
    if comment:
        print '%-30s' % (comment + '...'),

def getLines (fname):
    result = []
    if os.path.exists (fname):
        data = open (fname, 'r').read ()
        result = string.split (data, '\n')
    return result

def dbmcmd (session, cmd, comment = None):
    log = open (_protFile, 'a')
    try:
        log.write ('DBMServer-Command ' + cmd + '\n')
        try:
            show (comment)
            result = session.cmd (cmd)
            log.write ('  => ' + result + '\n')
            if comment:
                print 'OK'
        except sdb.dbm.DBMServError, err:
            log.write (str (err) + '\n')
            fatalError ('ERR at DBMServer command ' + cmd + ': ' + str (err))
    finally:
        log.close ()
    return result

def dbm2cmd (session, comment, *cmds):
    show (comment)
    for cmd in cmds:
        dbmcmd (session, cmd)
    print 'OK'

def splitUserInfo (userInfo):
    pos = string.find (userInfo, ',')
    if pos != -1:
        name = userInfo [:pos]
        pwd = userInfo [pos + 1:]
    else:
        name = pwd = userInfo
    return name, pwd

def checkVersion (session, options):
    show ('Checking version')
    versionString = dbmcmd (session, 'version')
    match = re.search ('"(\d+)[.](\d+)[.](\d+)"', versionString)
    version = tuple (map (int, match.group (1, 2, 3)))
    if version < (7, 5, 0):
        fatalError ('ERR  Sorry, no support for version %d.%d.%d' % version)
    print 'OK'
    return version

def removeDB (options):
    session = sdb.dbm.DBM (options.node, options.dbname, '', options.dbmUser)
    dbmcmd (session, 'db_offline', 'Set serverdb offline')
    dbmcmd (session, 'db_drop', 'Drop serverdb')

def createDB (options):
    session = sdb.dbm.DBM (options.node, '', options.dbroot)
    version = checkVersion (session, options)
    #
    # create database
    #
    dbmcmd (session,
        'db_create %s %s %s' % (options.dbname, options.dbmUser, options.osUser),
        'Create registry entry')

    localParams = getLines (options.paramfile)

    #
    # init kernel parameters
    #
    show ('Create param file')
    dbmcmd (session, 'param_startsession')
    dbmcmd (session, 'param_init ' + options.instanceType)
    dbmcmd (session, 'param_put MAXUSERTASKS  10')
    if options.dbEncoding <> 'ascii':
        dbmcmd (session, 'param_put _UNICODE  YES')
        dbmcmd (session, 'param_put DEFAULT_CODE  UNICODE')
      # using myparams.dbm
    for line in localParams:
        if line [:9] == 'param_put':
            dbmcmd (session, line)
    #
    dbmcmd (session, "param_checkall");
    dbmcmd (session, "param_commitsession");
    print 'OK'

    #
    # set devspaces
    #
    show ('Add devspaces')
    dbmcmd (session, 'param_adddevspace 1 LOG  LOG_001  F  2000 ');
    dbmcmd (session, 'param_adddevspace 1 DATA DAT_001  F 10000 ');
    for line in localParams:
        if line [:len ('param_adddevspace')] == 'param_adddevspace':
            dbmcmd (session, line)
    print 'OK'

    #
    # start database
    #
    dbmcmd (session, 'db_admin', 'Start admin')

    dbm2cmd (session, 'Activate serverdb', 'db_activate ' + options.dbaUser)
    dbm2cmd (session, 'Load system tables', 'load_systab -ud domp')

    #
    # create demo users
    #
    if options.sqluser:
        dbmcmd (session, 'sql_connect ' + options.dbaUser)
        for userInfo in options.sqluser:
            username, pwd = splitUserInfo (userInfo)
            dbm2cmd (session, 'Create ' + username,
                'sql_execute CREATE USER %s PASSWORD %s DBA NOT EXCLUSIVE' % (username, pwd))
        dbmcmd (session, 'sql_release')


def main (options, args):
    open (_protFile, 'w')
    if options.remove:
        removeDB (options)
    else:
        createDB (options)

def _options ():

    return [
        # (optstring, varname, typechar, default, help)
        ('r', 'remove', '', None, 'removes database'),
        ('d', 'dbname', ':', 'testdb', 'specifies serverdb to create'),
        ('u', 'dbmUser', ':', 'dbm,dbm', 'specifies DBMServer user'),
        ('n', 'node', ':', '', 'specifies servernode'),
        ('R', 'dbroot', ':', '', 'specifies INSTROOT path'),
        ('a', 'dbaUser', ':', 'dba,dba', 'specifies SYSDBA user'),
        ('s', 'osUser', ':', '', 'specifies operating system user'),
        ('i', 'paramfile', ':', 'myparams.dbm', 'specifies a private param file'),
        ('t', 'instanceType', ':', '', 'specifies the type of the instance to create'),
            ('e', 'dbEncoding',':','ascii', 'specifies encoding type of database'),
        ('sqluser', None, '::', None, 'create these sql users (name[,pwd])'),
        ]

if __name__ == '__main__':
    import optlib
    optlib.optMain2 (main, _options ())

Generated by  Doxygen 1.6.0   Back to index