p4master_run #2

  • //
  • guest/
  • russell_jackson/
  • sdp/
  • Server/
  • Unix/
  • p4/
  • common/
  • bin/
  • p4master_run
  • View
  • Commits
  • Open Download .zip Download (3 KB)
#!/bin/bash
#==============================================================================
# Copyright and license info is available in the LICENSE file included with
# the Server Deployment Package (SDP), and also available online:
# https://swarm.workshop.perforce.com/projects/perforce-software-sdp/view/main/LICENSE
#------------------------------------------------------------------------------
set -u

# A safe way to run something with a controlled shell environment defined by
# sourcing /p4/common/bin/p4_vars with a given SDP instance.

# Usage:
#   p4master_run INSTANCE [-c] PROGRAM [PROGRAM_ARGS ...]
#
# where INSTANCE is an SDP instance number or name.
function usage {
   echo -e "\nUsage:
\tp4master_run INSTANCE [-c] PROGRAM [ARGS ...]

where INSTANCE is an SDP instance number or name.

The optional '-c' flag is for running programs from cron that
send their own emails.  It suppresses some errors to avoid
duplicate emails from the cron daemon.  Some errors, such as
failures to start the called program, still are reported by
cron email.

PROGRAM can be a relative or absolute path, with optional arguments.\n"
exit 3
}

# Bail with usage message if there are less than 2 arguments.
[[ $# -lt 2 ]] && usage

Instance=${1:-Unset}

# Do nothing unless $Instance is defined.  Make sure user didn't
# try to pass a flag (e.g. '-c') as the first parameter.
if [[ $Instance == Unset || $Instance == -* ]]; then
   echo -e "\nError: The Perforce instance name must be the first paramter. Aborting.\n" >&2
   usage
fi

# Save original full set of arguments.  The shift will lose the instance
# argument, which will cause this to fail if you run it as root, and it
# tries to rerun as $OSUSER.
AllArgs="$@"

shift

# Bail with usage message if nothing follows '-c'.
if [[ $1 == -c ]]; then
   [[ $# -lt 2 ]] && usage
   CRON=yes
   shift
fi

SDP_CONF=/p4/common/bin/p4_vars

if [[ ! -r "$SDP_CONF" ]]; then
 echo -e "\nError: No readable SDP config file [$SDP_CONF]. Aborting.\n" >&2
 exit 2
fi

# Load SDP controlled shell environment.
source "$SDP_CONF" "$Instance" ||\
   { echo -e "\nError: Failed to load SDP environment.\n"; exit 1; }

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

Program=$1
if [[ $Program == /* || $Program == \.* ]]; then
   # Non-path depedent, absolute or relative path specified.
   ProgramPath=$Program
else
   # Path-dependent path specified.
   ProgramPath=$(which $Program)
fi

if [[ -z "$ProgramPath" ]]; then
   echo -e "\nError: The specified program [$Program] cannot be found.  Aborting.\n" >&2
   exit 3
fi

if [[ ! -r "$ProgramPath" ]]; then
   echo -e "\nError: The specified program [$Program] cannot be found.  Aborting.\n" >&2
   exit 3
fi

if [[ ! -x "$ProgramPath" ]]; then
   echo -e "\nError: The specified program [$Program] is not executable.  Aborting.\n" >&2
   exit 3
fi

shift
Args=$@

$Program $Args
# Change User Description Committed
#2 22981 Russell C. Jackson (Rusty) Made files writeable so they are easier to update once on the server.
#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/bin/p4master_run
#7 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.
#6 19768 UnstoppableDrew @tom_tyler @russell_jackson
Bug fix for running p4master_run as root, and some comment header cleanup. Job 000543

p4master_run: Preserve original arguments list and use this when exec'ing as $OSUSER.

backup_functions.sh: Add text about sourcing p4_vars yourself instead of using p4master_run.

update_limites.py: Run p4login directly without p4master_run since p4login calls p4_vars now.

everything else: Remove comment block about needing to run with p4master_run. Reword comment
  about SDP_INSTANCE since it is not always an integer value.
#5 16029 C. Thomas Tyler Routine merge to dev from main using:
p4 merge -b perforce_software-sdp-dev
#4 12169 Russell C. Jackson (Rusty) Updated copyright date to 2015

 Updated shell scripts to require an instance parameter to eliminate the need
 for calling p4master_run.    Python and Perl still need it since you have to set the
environment for them to run in.

 Incorporated comments from reviewers. Left the . instead of source as that seems
more common in the field and has the same functionality.
#3 12028 C. Thomas Tyler Refreshed SDP dev branch, merging down from main.
#2 11466 Russell C. Jackson (Rusty) Initial work to simplify p4_vars and remove cluster stuff.

 Testing of named instances surfaced some bugs that are in prod sdp,
 now fixed in dev.

 Added three triggers from RCJ SDP

 Moved p4review.cfg into the new /p4/common/cfg to go along with the
 instance_vars files. mkdirs.sh now generates an instance_p4review.cfg
 as well.

 Removed incremental p4verify to clean up a bit. It didn't support replicas
 and was really never used.

 All port settings now live in <instance>_vars file. You set what you want
 the ports to be in mkdirs.sh. There is no more fancy logic to try to guess
 what the port should be. You set it, and that is what it is.

 Remaining to do is to updated scripts to not need p4master_run. Saved
 that work for later since this is tested and works.
#1 10638 C. Thomas Tyler Populate perforce_software-sdp-dev.
//guest/perforce_software/sdp/main/Server/Unix/p4/common/bin/p4master_run
#1 10148 C. Thomas Tyler Promoted the Perforce Server Deployment Package to The Workshop.