#!/bin/tcsh -f # Use of tcsh necessary to assure use of shell built-in which below - MB #+ #OSIRIS library of scripts # #NAME # osiris - base start/stop/restart script for servers, guis, etc. # #SYNOPSIS # osiris function process [-C configfile] [-D displayname] [-H runhost] # [-U username] [-L "text"] # #DESCRIPTION # this main purpose of this script is to parse and check the optional # command line arguments - it doesn't actually start or stop anything, # but calls other scripts for that. # # the sequence of scripts called is: # 1. osiris {function} {process} # ->there is one top level script for all processes (this one!) # 2. {process}.csh {function} # ->there is a unique script for each process, defining specific # options for that process # 3. ONE of: osiris_servers.csh, osiris_guis.csh, osiris_idl.csh # ->there are three general scripts, one each for servers, guis, and # idl tools. these are the scripts that actually do the starting # or stopping. # #OPTIONS # function # # process # # -C configfile # # -D displayname # # -H runhost # # -U username # # -L "text for first line of logfile" # # #EXAMPLES # osiris start odec -C special_odec_cfg.xml -D myhost:0.0 -H myhost # -U matthew # This would start the process "odec" on matthew@myhost, diplay it on # myhost:0.0, and use the config file special_odec_cfg.xml. # #ENVIRONMENT VARIABLES # USER, HOST, SSH_TTY # #FILES # none # #SERVERS & KEYWORDS # none # #SCRIPTS CALLED # list of the scripts called by this script, for example: # # help, ping, id, procstatus, which, ssh # NOTE: this script does not use syncheck, so that it can be run from the # OSIRIS DRP computer (olowalu) which is Linux. # #EXIT STATUS # 0 - normal exit, no error # 1 - script aborted by an interrupt # 2 - syncheck error # 3 - error parsing command line input # other errors... # #SEE ALSO # ??? #- # # Modification History: # 20050615 - MB: New version created, adapted from earlier versions # by Weiss and NIRC2. # 20051208 - MB: Uses different ping commands for Solaris vs. Linux # 20200630 - jlyke: computer osirisbuild becomes napili # 20210405 - jlyke: handle traffic/music daemons # 20211201 - jlyke: update default IDL host to osiris-drp # # Boiler plate for "-h" support for command autohelp. if ("$1" == "-h") then help $0 | more exit $status endif # Boilerplate for syncheck. # Note that the boiler plate should be transparent for all usages, # but to make correct use of syncheck you will need to specify the # correct pattern. # NOTE: we do not use syncheck on this script so that it can be called # from the OSIRIS DRP computer (olowalu) which runs Linux. # Check that two required arguments (function & process) are present if ($#argv < 2) then help $0 | more exit 3 endif # More error checking for the arguments is performed below # End of help/syncheck boiler plate. # Set this variable for testing mode... #set test # Set up to trap interrupts (Ctrl-C, etc.) onintr abort # Set default variable values set fcmd = $0 set cmd = ${fcmd:t} set configfile = "" set display = "" set runuser = $USER set runhost = $HOST set logtext = "" # Get the required arguments and shift to the optional arguments set function = $1 shift set process = $1 shift # Check that a valid function was specifed switch ($function) case start: case stop: case status: # Do nothing - these are valid functions. breaksw default: echo "${cmd}: Invalid function <${function}> specified - script"\ "aborted." exit 3 breaksw endsw # Determine whether we are on Solaris (SunOS) or Linux set systype = `uname -s` # Set the default runhost and username for each process # these values will be overridden by any specified on the command line # the pkgtype is needed to put the log files in the right place switch ($process) # Global server case osiris: case ogs: # ogs needs to run on a host that can see the other osiris servers set runhost = napili set runuser = osiris set pkgtype = "servers" breaksw # All the hardware controlled thru the terminal server or ethernet # (no special hardware needs to be on the host where they run) case oprs: case op1s: case op2s: case otcs: case ot1s: case ot2s: case om1s: case om2s: case om3s: case om4s: case om5s: case om6s: set runhost = napili # set runhost = osirisbuild set runuser = osiris set pkgtype = "servers" breaksw # Special cases for starting multiple servers at once case omAs: case omIs: case omSs: set runhost = napili set runuser = osiris set pkgtype = "NA" breaksw # IMAG Detector Server # (must run on kuiaha or wherever the IMAG SDSU interface card is) case oids: set runhost = kuiaha set runuser = osiris set pkgtype = "servers" breaksw # SPEC Detector Server # (must run on puunoa or wherever the SPEC SDSU interface card is) case osds: set runhost = puunoa set runuser = osiris set pkgtype = "servers" breaksw # Music-based daemons # music communicates via traffic, traffic runs on all computers # with a music-based daemon # The music-based commands will start using the LickStartStop # method--it will handle the runuser and logging, etc. # Use an if-block below for execution separate from the csh methods case traffic: set runhost = (napili vm-osiris) set runuser = $USER set pkgtype = "daemons" breaksw case osirissoundboard: case soundboard: set runhost = vm-osiris set runuser = $USER set pkgtype = "daemons" set process = osirissoundboard breaksw case osirisproc_1: set runhost = napili set runuser = $USER set pkgtype = "daemons" breaksw case osirisproc_2: set runhost = vm-osiris set runuser = $USER set pkgtype = "daemons" breaksw case osirisproc: case proc*: set runhost = (napili vm-osiris) set runuser = $USER set pkgtype = "daemons" set process = (osirisproc_1 osirisproc_2) breaksw case osirishistory: case history: set runhost = moolelo set runuser = $USER set pkgtype = "daemons" set process = osirishistory breaksw case telsimpower: case callamp*: case var*: set runhost = napili set runuser = $USER set pkgtype = "daemons" set process = telsimpower breaksw # Control GUIs # these should normally run as current user@vm-osiris, but don't try to # override people if they're trying to run them locally somewhere else. case ohgui: case osgui: case otgui: case odec: case oopgui: case oorp: case oorgui: set runhost = $HOST set runuser = $USER set pkgtype = "guis" breaksw # Online Reduction GUI seems to work only from osirisbuild 2020-05-22 # osirisbuild renamed to napili 2020-06-30 # case oorgui: # set runhost = napili # set runuser = $USER # set pkgtype = "guis" # breaksw # The DRP Backbone # should normally run on the dedicated DRP computer (olowalu) case odrp: # set runhost = olowalu set runhost = osiris-drp #set runhost = napili set runuser = $USER set pkgtype = "" breaksw case offline_drp: # set runhost = olowalu set runhost = osiris-drp #set runhost = napili set runuser = $USER set pkgtype = "" breaksw # IDL apps (QL2, etc.) # these should normally run on the DRP computer (olowalu) case idl: case ql2: case iql2: case fql2: case sql2: case oql2: # set runhost = olowalu set runhost = osiris-drp #set runhost = napili set runuser = $USER set pkgtype = "NA" breaksw # "General" servers # only run for testing, so run as current user@host case ods: case opws: case otms: case oms: set runhost = $HOST set runuser = $USER set pkgtype = "NA" breaksw case obbs: set runhost = napili set runuser = $USER set pkgtype = "servers" breaksw case check_keywords: set runhost = napili set runuser = $USER set pkgtype = "scripts" breaksw # For anything else run as current user@host default: set runhost = $HOST set runuser = $USER set pkgtype = "NA" breaksw endsw # Check for optional flags while ($#argv != 0) ###echo "${cmd}: Checking ${1}..." switch ($1) case -C: set configfile = $2 shift breaksw case -D: set display = $2 shift breaksw case -H: # if a hostname was specified, override the defaults above set runhost = $2 shift # make sure the host exists and is up, too switch ($systype) case SunOS: set junk = `ping ${runhost}` set error = $status breaksw case Linux: set junk = `ping -c 1 ${runhost}` set error = $status breaksw default: set error = 0 breaksw endsw if ($error != 0) then echo "${cmd}: Unable to contact ${runhost} - script aborted." # set the error code for an error with command line input exit 3 endif unset error breaksw case -U: # if a username was specified, override the defaults above set runuser = $2 shift # check if the user account exists (on the current host anyways) set junk = `id ${runuser}` set error = $status if ($error != 0) then echo "${cmd}: Invalid username specified <${runuser}> -"\ "script aborted." # set the error code for an error with command line input exit 3 endif unset error breaksw case -L: set logtext = "$2" shift breaksw default: echo "${cmd}: Unknown flag <${1}> specified - script aborted." echo "${cmd}: Usage: $0 function process [-C file] [-D display]"\ '[-H host] [-U user] [-L "text"]' # set error code for error with command line input exit 3 breaksw endsw shift end ### BODY OF SCRIPT if ($?test) then echo "" echo "Current Script Variables:" echo function = $function echo process = $process echo display = $display echo configfile = $configfile echo runuser = $runuser echo runhost = $runhost echo logtext = $logtext exit endif # 2021-04-05 jlyke add an if-block to invoke init.d for music-based daemons if ( ${pkgtype} == "daemons" ) then # daemons is reserved for init.d functionality # as noted above, could probe system for host of traffic daemons # grep ^$proc $DATDIR/music/services | grep \.keck\.hawaii\.edu | \ # awk '{print $2}' | cut -f 1 -d. # as traffic and osirisproc run on multiple systems, use an index set idx = 1 while ($idx <= $#runhost ) set rhost = $runhost[$idx] # if we have defined multiple process values, needs to match runhost if ( $#process == $#runhost ) then set proc = $process[$idx] else set proc = $process endif # Check if we are on the right host as the right user # if we are, call the next script, otherwise ssh there first if ( ("$rhost" == "$HOST") && ("$runuser" == "$USER") ) then # We are the correct user on the correct host set sshcmd = "" else set sshcmd = "ssh -f -X -l $runuser $rhost " endif # execute the command $sshcmd $RELDIR/etc/init.d/$proc $function @ idx++ end else # Check if we are on the right host as the right user # if we are, call the next script, otherwise ssh there first if ( ("$runhost" == "$HOST") && ("$runuser" == "$USER") ) then # We are the correct user on the correct host if ("${function}" == "status") then # Just return the status and exit procstatus $process goto done endif # Make sure the command to be called exists # If /usr/bin/which is called instead of shell built-in which, the # following line will return with status=0 no matter what... - MB set junk = `which ${process}.csh` set error = $status if ($error != 0) then osirisScriptMsg -T "${cmd}: Cannot find a script for the"\ "${process} process." exit 4 endif unset error # Start a log file if we are starting something and have correct pkgtype if (("${function}" == "start") && ("${pkgtype}" != "NA")) then # Construct log file directory and name set logdir = "${KROOT}/var/log/osiris/${pkgtype}/${process}" set tag = `date '+%y%m%d_%H%M'` set logfile = "${logdir}/${tag}_${process}.log" # Make sure the logfile exists umask 000 mkdir -p $logdir touch $logfile # First write any passed in log file text if ("${logtext}" != "") then echo "${logtext}" >> $logfile echo "" >> $logfile endif # Make an entry in the logfile echo -n "${process} start attempted by ${USER}@${HOST} at " >> $logfile echo `date` >> $logfile # Alert the user that output will be logged echo "STDOUT and STDERR logged to:" echo " ${logfile}" endif # Build up the command to call set newcmd = "${process}.csh ${function}" if ("${configfile}" != "") then set newcmd = "${newcmd} -C ${configfile}" endif if ("${display}" != "") then set newcmd = "${newcmd} -D ${display}" endif # Call the command, but first check if we should use a log file, and, # check this script was called from a command line or another script # If from a command line, we want to start it with an & # (if SSH_TTY is set, then it was called from a command line) # If from another script, we leave it up to it to background the task if (("${function}" == "start") && ("${pkgtype}" != "NA")) then if ($?SSH_TTY) then $newcmd >>& $logfile & else $newcmd >>& $logfile endif else $newcmd # Don't think this matters since things always get backgrounded farther # down the road. In particular, QL2 doesn't run from the IDL Start tool # if an ampersand is used. # if ($?SSH_TTY) then # $newcmd & # else # $newcmd # endif endif else # Either host or user doesn't match, so ssh there # String to pass for log file to be created set logtext = "'SSH from ${USER}@${HOST} to ${runuser}@${runhost}'" # single quotes inside double quotes needed to pass entire string # as one argument when doing the ssh - MB # Build up the command to call after sshing set newcmd = "osiris ${function} ${process} -U ${runuser} -H ${runhost}" if ("${configfile}" != "") then set newcmd = "${newcmd} -C ${configfile}" endif if ("${display}" != "") then set newcmd = "${newcmd} -D ${display}" endif if (("${logtext}" != "") && ("${pkgtype}" != "NA")) then set newcmd = "${newcmd} -L ${logtext}" endif echo $newcmd # Actually do the ssh ssh -f -X -l $runuser $runhost "${newcmd}" endif endif goto done abort: # Block of code to handle interrupts. exit 1 done: # is there anything that needs to go here? exit end