p4dtg_instance_init.template #3

  • //
  • guest/
  • russell_jackson/
  • sdp/
  • Server/
  • Unix/
  • p4/
  • common/
  • etc/
  • init.d/
  • p4dtg_instance_init.template
  • View
  • Commits
  • Open Download .zip Download (3 KB)
#!/bin/bash
#------------------------------------------------------------------------------
# P4DTG server init script

# p4dtg Init Script.
# chkconfig: - 90 30
# description: P4DTG Start Up Script

# Load SDP controlled shell environment.
source /p4/common/bin/p4_vars REPL_SDP_INSTANCE ||\
   { echo -e "\nError: Failed to load SDP environment for instance REPL_SDP_INSTANCE.\n"; exit 1; }

export TMP="$P4TMP"

source "$P4CBIN/ps_functions.sh" ||\
   { echo -e "\nError: Failed to load SDP ps_functions.sh lib.\n"; exit 1; }

if [[ $(id -u) -eq 0 ]]; then
   exec su - $OSUSER -c "$0 $*"
elif [[ $(id -u -n) != $OSUSER ]]; then
   echo "$0 can only be run by root or $OSUSER"
   exit 1
fi

cd $P4DTG_ROOT
if [[ $? -ne 0 ]]; then
   echo "The P4DTG_ROOT directory [$P4DTG_ROOT] does not exist."
   exit 1
fi

declare StopFile="$P4DTG_ROOT/repl/stop-$P4DTG_CFG"
declare ErrorFile="$P4DTG_ROOT/repl/err-$P4DTG_CFG"
declare MovedErrorFile=

# See how we were called.
case "${1:-status}" in
   start)
      p4dtg_pids=$(get_pids "$P4DTGBIN")
      if [[ -n "$p4dtg_pids" ]]; then
         echo -e "Error: ${P4DTGBIN} is already running as pids: $p4dtg_pids."
         exit 1
      fi

      [[ -e "$StopFile" ]] && rm -f "$StopFile"

      if [[ -e "$ErrorFile" ]]; then
         if [[ "${2:-Unset}" == "-f" ]]; then
            MovedFile="$P4DTG_ROOT/repl/MOVED_FILE_err-$(date +'%Y%m%d-%H%M')"
            mv -f "$ErrorFile" "$MovedFile"
            echo -e "\nWarning: P4DTG Error File detected and moved aside to: $MovedFile\nStarting anyway due to -f.\n"
         else
            echo -e "\Error: Refusing to start because a P4DTG error file was found:\n\t$ErrorFile\nReview this file.\nTo start P4DTG after reviewing this file, try again with -f which will the error file aside first, i.e.: $0 start -f\n"
            exit 1
         fi
      fi

      echo -e "\nStarting P4DTG with: $P4DTGBIN $P4DTG_CFG $P4DTG_ROOT"
      $P4DTGBIN $P4DTG_CFG $P4DTG_ROOT &
   ;;

   stop)
      p4dtg_pids=$(get_pids "$P4DTGBIN")
      if [[ -z "$p4dtg_pids" ]]; then
         echo -e "Error: ${P4DTGBIN} is NOT running.\n"
         exit 1
      fi

      echo -e -n "\nShutting down P4DTG instance P4DTG_CFG ..."
      cd "$P4DTG_ROOT/repl" || exit 1
      touch "$StopFile" || exit 1
      p4dtg_pids=$(get_pids "$P4DTGBIN")
      while [[ -n "$p4dtg_pids" ]]; do
         sleep 2
         echo -n "."
         p4dtg_pids=$(get_pids "$P4DTGBIN")
      done
      echo -e "\n\nShutdown of P4DTG confirmed.\n"
   ;;

   restart)
      "$0" stop || exit $?
      sleep 1
      "$0" start || exit $?
   ;;

   status)
      p4dtg_pids=$(get_pids "$P4DTGBIN")
      if [[ -n "$p4dtg_pids" ]]; then
         echo "${P4DTGBIN} is running as pids: $p4dtg_pids."
         exit 0
      else
         echo "${P4DTGBIN} is NOT running."
         exit 1
      fi
   ;;

   *)
      echo -e "\nUsage: $0 {start [-f] | stop | status | restart}\n"
      exit 1
   ;;
esac

exit 0
# Change User Description Committed
#3 34003 Russell C. Jackson (Rusty) Improve SDP diagnostics and correct maintenance/recovery failures.

Add durable logs and explicit failure handling across operational, Maintenance,
setup, trigger, snapshot and download scripts. Correct retention selection,
restore preflights, environment setup and command/API handling. Include offline
regression coverage and the review report with deferred improvement proposals.

Include requested journal archiving and automatic acceptance of new SSH host
keys for remote rsync. Preserve the NumJnls > 2 threshold and existing shell
options. Remove Windows scripts as requested.

Validation: full review suite 138 passed, 8 skipped; subsequent rsync/logging
suite 29 passed. Shell/Python/JSON static checks passed; no live operations run.
#2 32388 Russell C. Jackson (Rusty) Updates using Claude.ai to clean up the code, reduce duplication, enhanace security, and use current standards.
#1 22693 Russell C. Jackson (Rusty) Branched a Unix only version of the SDP.
Removed extra items to create a cleaner tree.
Moved a few items around to make more sense without Windows in the mix.
//guest/perforce_software/sdp/dev/Server/Unix/p4/common/etc/init.d/p4dtg_instance_init.template
#7 21986 C. Thomas Tyler Enhanced P4DTG init script:
* Now removes 'stop' file on startup, eliminating a manual step.
* Provides useful error message if a P4DTG error file is found.
* Provides a '-f' flag to move P4DTG error file aside and start anyway,
  but with a good error message telling the user to review the error
  file first.  (Typically errors are just related to p4d being down.)
* Made 'stop' wait for and confirm shutdown.
* Made 'stop' error out if already stopped.
* Made 'start' error out if already started.
* Made 'restart' robust (by making stop wait for confirmed shutdown).
* Cosmetic and output tweaks.
#6 20491 C. Thomas Tyler Fixed an environment insulation bug in init scripts.

More testing (and thinking) revealed that the only way to truly
provide a guarantee of insulation from user-set defaults in
~perforce/.bashrc (etc.) is to pass SDP_INSTANCE as a parameter to
the *_base scripts, so that the instance name is explicitly passed
into the su/exec call (when run as root).

This change also includes minor cleanup in init scripts and *_base
scripts.
#5 20448 C. Thomas Tyler Fixed env bug seen when running init scripts as root.

Updated *_base scripts and *_init script templates to a new
standard.  Goals:

1. Init scripts that use corresponding *_base scripts are
minimized so that do nothing more than set SDP_INSTANCE and
then call the corresponding *_base script.

2. The 'su' commands always pass $* instead of $1, deferring
all processing to the *_base script.

3. The shell environment is now guaranteeds to have the same
results regardless of whether it is called as 'root' or as
the defined OSUSER.

4. The p4_vars file is always sourced exactly once.  Two
calls to 'source p4_vars' appear in some cases, one
immediatley before the su/exec call, and another after the
after the 'su/exec'.  Only one or the other is sourced.

5. All init scripts have a reasonably consistent usage message.

6. All init scripts accept a 'status' argument.

This change fixes a bug where 'p4broker_N_init status' run as the
'perforce' user would report many pids unrelated to Perforce
if run as root, e.g. doing 'service p4broker_N_init status'.

This also eliminates a potential issue where the 'perforce'
user might source a p4_vars with a default instance in ~/.profile
or ~/.bashrc, thus invalidating the instance specified when the
user ran the init script as root.
#4 20341 C. Thomas Tyler Tweaked all init scripts to default to 'status' (rather than an ugly
syntax error) of the user forgets the start/stop/status parameter,
as was done previosly for the broker init script.

Removed unnecessary 'source' call when calling p4<whateve>_base scripts.

Cleaned up comments.
#3 18852 C. Thomas Tyler Enhanced P4DTG config to be more SDP-ified.
#2 16029 C. Thomas Tyler Routine merge to dev from main using:
p4 merge -b perforce_software-sdp-dev
#1 10638 C. Thomas Tyler Populate perforce_software-sdp-dev.
//guest/perforce_software/sdp/main/Server/Unix/p4/common/etc/init.d/p4dtg_instance_init.template
#1 10148 C. Thomas Tyler Promoted the Perforce Server Deployment Package to The Workshop.