Dcache-pool

From SysadminWiki

In order to be able to start pools in solaris, we've coded the dcache-pool script from 1.7.0-35 using python so it works in solaris and linux.

Although it could be coded in a simpler way, and it's a pre-pre-alpha, its working at PIC. :)

#!/usr/local/bin/python

import re
import os.path
import sys
import socket
dcacheHome="/opt/d-cache"

def getConfigOption( option ):
        return parseForOptionInFiles( option, ( "%s/etc/node_config" %dcacheHome, "%s/etc/pool_config" % dcacheHome ) )

def getSetupOption( option ):
        return parseForOptionInFiles( option, ( "%s/config/dCacheSetup" % dcacheHome, ) ).replace( '"', "" ).strip()

def parseForOptionInFiles( option, lFiles ):
        for filename in lFiles:
                try:
                        oFD = file( filename )
                        for line in oFD.readlines():
                                line = line.strip()
                                line = re.sub( "#.*$", "", line )
                                fields = [ field.strip() for field in line.split( "=" ) ]
                                if fields[0] == option:
                                        return fields[1]
                except IOError:
                        print "[WARN] %s does not exist" % filename
        return False


nodeType = getConfigOption( "NODE_TYPE" )
if not nodeType or nodeType == "dummy":
        print "[ERROR] %s/etc/node_config missing or not useful. Exiting." % dcacheHome
        sys.exit( 1 )


dcacheHome = getConfigOption( "DCACHE_BASE_DIR" )
pnfsRoot = getConfigOption( "PNFS_ROOT" )

if not os.path.isfile( "%s/config/dCacheSetup" % dcacheHome ):
        print "[ERROR] %s/config/dCacheSetup does not exist. Exiting." % dcacheHome
        sys.exit( 1 )

serviceLocatorHost = getSetupOption( "serviceLocatorHost" )
if not serviceLocatorHost or serviceLocatorHost == "SERVER":
        print "[ERROR] The variable 'serviceLocatorHost' in '%s/config/dCacheSetup' has to be set properly. Exiting." %  dcacheHome
        sys.exit(1)

java = getSetupOption( "java" )
if not java or not os.path.isfile( java ) or os.system( '%s -version 2>&1| grep \'version "1\.[45]\' >/dev/null' % java ):
        print "[ERROR] The variable 'java' in '%s/config/dCacheSetup' has to be set to a java VM version 1.4 or 1.5. Exiting.  " % dcacheHome
        sys.exit(1)

logArea = getSetupOption( "logArea" )
logParam = ""
if not logArea:
        logParam = "-logArea=%s/log" % dcacheHome

if len(sys.argv) < 2:
        print "Usage %s {start|stop|restart}" % sys.argv[0]
        sys.exit(1)

hostname = socket.getfqdn().split( "." )[0]
poolsFile = "%s/config/%s.poollist" % ( dcacheHome, hostname )
if not os.path.isfile( poolsFile ):
        print "[ERROR] No pools file (%s)" % poolsFile
        sys.exit(1)

if sys.argv[1] == "start":
        print "Starting dcache pools..."
        if not os.system( "%s/jobs/pool -pool=%s %s start" % ( dcacheHome, hostname, logParam ) ):
                print " started!"
        else:
                print " oops!"
elif sys.argv[1] == "stop":
        print "Stopping dcache pools..."
        if not os.system( "%s/jobs/pool -pool=%s %s stop" % ( dcacheHome, hostname, logParam ) ):
                print " stopped!"
        else:
                print " oops!"
elif sys.argv[1] == "restart":
        os.system( "%s stop" % sys.argv[0] )
        os.system( "%s start" % sys.argv[0] )