diff --git a/wmagent/manage_reorg b/wmagent/manage_reorg new file mode 100755 index 00000000000..b23ec79b702 --- /dev/null +++ b/wmagent/manage_reorg @@ -0,0 +1,200 @@ +#!/bin/sh + +##H Usage: manage_reorg ACTION [SECURITY-STRING] +##H +##H Available actions: +##H help show this help +##H version get current version of the service +##H status show current service's status +##H sysboot start server from crond if not running +##H restart (re)start the service +##H start (re)start the service +##H stop stop the service +##H deployschema deploy initial database schema +##H destroyschema destroy everything in the database +##H pushcouchapp push wmagent couchapps to couchdb +##H reindex reindex the jobdump views +##H +##H For more details please refer to operations page: +##H https://twiki.cern.ch/twiki/bin/view/CMS/WMAgentDeployment + +if [ $(id -un) = cmsweb ]; then + echo "ERROR: please use another account" 1>&2 + exit 1 +fi + +ME=$(basename $(dirname $0)) +TOP=$(cd $(dirname $0)/../../.. && pwd) +ROOT=$(cd $(dirname $0)/../.. && pwd) +CFGDIR=$(dirname $0) +AUTHDIR=$TOP/current/auth/$ME +LOGDIR=$TOP/logs/$ME +STATEDIR=$TOP/state/$ME +COLOR_OK="\\033[0;32m" +COLOR_WARN="\\033[0;31m" +COLOR_NORMAL="\\033[0;39m" + +. $ROOT/apps/$ME/etc/profile.d/init.sh +export PYTHONPATH=$ROOT/auth/$ME:$PYTHONPATH +export WMAGENT_ROOT + +# Start service conditionally on crond restart. +sysboot() +{ + if [ $(pgrep -u $(id -u) -f "config[=]$CFGDIR[/]WMAgentConfig.py" | wc -l) = 0 ]; then + stop # need to call stop on sysboot because wmagent may have left files + # laying around that will prevent it from starting correctly + start + fi +} + +# Start the service. +start() +{ + cd $STATEDIR + echo "starting $ME" + + dburl=$(egrep -o "^databaseUrl.*=.*\".*\"" $AUTHDIR/WMAgentSecrets.py | cut -d\" -f2) + if [ ${dburl%%://*} = "mysql" ]; then + # Wait until the sock is created + n=0; while [ $n -le 40 -a ! -e $TOP/state/mysql/mysql.sock ]; do + echo "waiting for mysql..."; sleep 1; n=$(expr $n + 1) + done + [ -e $TOP/state/mysql/mysql.sock ] || \ + { echo "Mysql not running. Not starting $ME."; exit 1; } + fi + + wmcoreD --start --config=$CFGDIR/WMAgentConfig.py +} + +# Stop the service. +stop() +{ + echo "stopping $ME" + wmcoreD --shutdown --config=$CFGDIR/WMAgentConfig.py +} + +# Check if the server is running. +status() +{ + pid=$(pgrep -u $(id -u) -f "config[=]$CFGDIR[/]WMAgentConfig.py" | sort -n) + if [ X"$pid" = X ]; then + echo -e "$ME is ${COLOR_WARN}NOT RUNNING${COLOR_NORMAL}." + else + echo -e "$ME is ${COLOR_OK}RUNNING${COLOR_NORMAL}, PID" $pid + fi + wmcoreD --status --config=$CFGDIR/WMAgentConfig.py +} + +# Verify the security string. +check() +{ + CHECK=$(echo "$1" | md5sum | awk '{print $1}') + if [ $CHECK != 94e261a5a70785552d34a65068819993 ]; then + echo "$0: cannot complete operation, please check documentation." 1>&2 + exit 2; + fi +} + +# Main routine, perform action requested on command line. +case ${1:-status} in + sysboot ) + if ps -oargs= $PPID | grep -q crond; then + sysboot + else + echo "$0: sysboot is for cron only" 1>&2 + exit 1 + fi + ;; + + start | restart ) + check "$2" + stop + start + status + ;; + + status ) + status + ;; + + stop ) + check "$2" + stop + status + ;; + + deployschema ) + set -e + dburl=$(egrep -o "^databaseUrl.*=.*\".*\"" $AUTHDIR/WMAgentSecrets.py | cut -d\" -f2) + case ${dburl%%://*} in + mysql ) + [ -n "$(mysql -u root --socket=$TOP/state/mysql/mysql.sock --exec \ + "show databases like '${dburl##*/}'")" ] || { + mysql -u root --socket=$TOP/state/mysql/mysql.sock --exec \ + "create database ${dburl##*/}" + # deploy the schema + wmcore-db-init --config $CFGDIR/WMAgentConfig.py --create \ + --modules=WMCore.WMBS,WMCore.Agent.Database,WMComponent.DBS3Buffer,WMCore.BossAir,WMCore.ResourceControl + } ;; + oracle ) + echo "Assuming the oracle database was already created by CERN/IT." ;; + * ) + echo "Unrecognized DB dialect: \"${dburl%%://*}\". Make sure you" \ + "specified a supported databaseUrl in $AUTHDIR/WMAgentSecrets.py." + exit 1 ;; + esac + ;; + + destroyschema ) + check "$2" + set -e + dburl=$(egrep -o "^databaseUrl.*=.*\".*\"" $AUTHDIR/WMAgentSecrets.py | cut -d\" -f2) + case ${dburl%%://*} in + mysql ) + mysql -u root --socket=$TOP/state/mysql/mysql.sock --exec \ + "drop database ${dburl##*/}" ;; + oracle ) + echo "Don't have a procedure to destroy tables from oracle yet." ;; + * ) + echo "Unrecognized DB dialect: \"${dburl%%://*}\". Make sure you" \ + "specified a supported databaseUrl in $AUTHDIR/WMAgentSecrets.py." + exit 1 ;; + esac + ;; + + pushcouchapp ) + # Ugly hack because can only get admin role through localhost requests. + # FIXME: should instead change wmagent to use localhost for everything. + file=$(python -c 'import tempfile as t; print t.mkstemp(suffix = ".py")[1]') + cat $CFGDIR/WMAgentConfig.py > ${file} + perl -p -i -e 's{^localCouchURL = .*}{localCouchURL = "http://localhost:5984"}g' ${file} + export WMAGENT_CONFIG=${file} + export YUI_ROOT + wmagent-couchapp-init --skip-cron + rm -f ${file} + ;; + + reindex ) + # this came from wmagent-couchapp-init + # FIXME: to check if it is still needed + baseurl=$(python -c "from WMCore.Configuration import loadConfigurationFile;" \ + "cfg = loadConfigurationFile('$CFGDIR/WMAgentConfig.py');" \ + "print cfg.JobStateMachine.couchurl+'/'+cfg.JobStateMachine.couchDBName") + curl -ks -m 5 "$baseurl/fwjr/_design/JobDump/_view/statusByWorkflowName?limit=1" > /dev/null + curl -ks -m 5 "$baseurl/jobs/_design/FWJRDump/_view/outputByJobID?limit=1" > /dev/null + ;; + + help ) + perl -ne '/^##H/ && do { s/^##H ?//; print }' < $0 + ;; + + version ) + echo "$WMAGENT_VERSION" + ;; + + * ) + echo "$0: unknown action '$1', please try '$0 help' or documentation." 1>&2 + exit 1 + ;; +esac