aws_s3_sync.sh #1

  • //
  • p4-sdp/
  • r26.1.0/
  • Unsupported/
  • Samples/
  • cloud/
  • aws/
  • aws_s3_sync.sh
  • View
  • Commits
  • Open Download .zip Download (8 KB)
#!/bin/bash
set -u

#------------------------------------------------------------------------------
# Usage: To get usage info, run: ./aws_sync.sh - man

#==============================================================================
# Declarations and Environment

# Version ID Block. Relies on +k filetype modifier.
#------------------------------------------------------------------------------
# shellcheck disable=SC2016
declare VersionID='$Id: //p4-sdp/r26.1.0/Unsupported/Samples/cloud/aws/aws_s3_sync.sh#1 $ $Change: 33565 $'
declare VersionStream=${VersionID#*//}; VersionStream=${VersionStream#*/}; VersionStream=${VersionStream%%/*};
declare VersionCL=${VersionID##*: }; VersionCL=${VersionCL%% *}
declare Version=${VersionStream}.${VersionCL}
[[ "$VersionStream" == r* ]] || Version="${Version^^}"

declare ThisScript=${0##*/}
declare ThisUser=
declare DepotsDir=
declare DefaultCfgFile="/p4/common/config/aws_s3_sync.cfg"
declare SDPInstance="${SDP_INSTANCE:-}"
declare SDPEnvFile="/p4/common/bin/p4_vars"
declare SDPInstanceVarsFile=
declare P4Depots=
declare CfgFile=
declare -i NoOp=0
declare -i ErrorCount=0
declare -i SilentMode=0
declare -i CmdExitCode=0
declare Cmd=
declare Log=
declare H="=============================================================================="

# Values for these variables are defined in the confgiration values.  See the
# CONFIGURATION FILES section of the manual pages.
declare S3Bucket=
declare StorageClass=

#==============================================================================
# Local Functions
function msg () { echo -e "$*"; }
function errmsg () { msg "\\nError: ${1:-Unknown Error}\\n"; ErrorCount+=1; }
function bail () { errmsg "${1:-Unknown Error}"; exit "${2:-1}"; }

#------------------------------------------------------------------------------
# Function: terminate
function terminate
{
   # Disable signal trapping.
   trap - EXIT SIGINT SIGTERM

   [[ "$Log" != off ]] && msg "Log is: $Log\\n${H}"

   # With the trap removed, exit.
   exit "$ErrorCount"
}

#------------------------------------------------------------------------------
# Function: usage (required function)
#
# Input:
# $1 - style, either -h (for short form) or -man (for man-page like format).
# The default is -h.
#
# $2 - error message (optional).  Specify this if usage() is called due to
# user error, in which case the given message displayed first, followed by the
# standard usage message (short or long depending on $1).  If displaying an
# error, usually $1 should be -h so that the longer usage message doesn't
# obscure the error message.
#
# Sample Usage:
# usage
# usage -h
# usage -man
# usage -h "Incorrect command line usage."
#------------------------------------------------------------------------------
function usage
{
   declare style=${1:--h}
   declare errorMessage=${2:-Unset}

   if [[ "$errorMessage" != Unset ]]; then
      msg "\\n\\nUsage Error:\\n\\n$errorMessage\\n\\n"
   fi

   msg "USAGE for $ThisScript version $Version:

$ThisScript [-c <cfg_file>] [-L <log>] [-si] [-n] [-D]

or

$ThisScript [-h|-man|-V]
"
   if [[ $style == -man ]]; then
      msg "
DESCRIPTION:
	This script wraps the AWS command line, specifically the 'aws s3 sync'
       	command, to sync the depots directory to AWS.

CONFIGURATION FILE:
	This script requires a configuration file to define the following
	settings:

	* S3Bucket - Define the AWS S3 Bucket to copy to.

	* StorageClass Define the storage class to use. This impacts storage
	costs, accessibility, etc. For more information, see:

	https://aws.amazon.com/s3/storage-classes

	The default configuration file location is: $DefaultCfgFile"

   if [[ -r "$DefaultCfgFile" ]]; then
      msg "\\n${H}\\nBEGIN CONTENTS $DefaultCfgFile\\n$(cat "$DefaultCfgFile")\\n${H}\\nEND FILE CONTENTS"
   fi
msg "
EXAMPLES:
	This script can be called directly, though it is intended to be called
	via a crontab entry like this sample:

	INSTANCE=1
	0 1 * * * [[ -e /mnt/p4depots ]] && /p4/common/bin/run_if_master.sh \${INSTANCE} -si

OPTIONS:
 -c <cfg_file>
 	Specify the path to the configuration file. The default is:
	$DefaultCfgFile

 -L <log>
	Specify the path to a log file, or the special value 'off' to disable
	logging.  By default, all output (stdout and stderr) goes to
	
        ${LOGS:-\$LOGS}/aws_s3_sync.<datestamp>.log

	NOTE: This script is self-logging.  That is, output displayed on the screen
	is simultaneously captured in the log file.  Do not run this script with
	redirection operators like '> log' or '2>&1', and do not use 'tee.'

	If an existing log file is specified, this script will append the the
	specified log file.

 -si	Operate silently.  All output (stdout and stderr) is redirected to the log
	only; no output appears on the terminal.  This cannot be used with
	'-L off'.
      
	This is useful when running from cron, as it prevents automatic
	email from being sent by cron directly, as cron does when a script called
	from cron generates any output.  This script is then responsible for
	email handling, if any is to be done.

 -n	No-Op.  Prints commands instead of running them.

 -D     Set extreme debugging verbosity.

HELP OPTIONS:
 -h	Display short help message
 -man	Display man-style help message
"
   fi

   exit 1
}

#==============================================================================
# Command Line Processing

declare -i shiftArgs=0

set +u
while [[ $# -gt 0 ]]; do
   case $1 in
      (-i) SDPInstance="$2"; shiftArgs=1;;
      (-h) usage -h;;
      (-man) usage -man;;
      (-c) CfgFile="$2"; shiftArgs=1;;
      (-L) Log="$2"; shiftArgs=1;;
      (-si) SilentMode=1;;
      (-n) NoOp=1;;
      (-D) set -x;; # Debug; use 'set -x' mode.
      (*) usage -h "Unknown arg ($1).";;
   esac

   # Shift (modify $#) the appropriate number of times.
   shift; while [[ $shiftArgs -gt 0 ]]; do
      [[ $# -eq 0 ]] && usage -h "Incorrect number of arguments."
      shiftArgs=$shiftArgs-1
      shift
   done
done
set -u

#==============================================================================
# Command Line Verification

[[ "$SilentMode" -eq 1 && "$Log" == off ]] && \
   usage -h "Cannot use '-si' with '-L off'."

[[ -n "$SDPInstance" ]] ||\
   bail "The SDP '-i <SDP_Instance>' parameter is required unless \$SDP_INSTANCE is defined."

[[ -z "$CfgFile" ]] && CfgFile="$DefaultCfgFile"

[[ -r "$SDPEnvFile" ]] ||\
   bail "The SDP Environment file is missing: $SDPEnvFile"

SDPInstanceVarsFile="/p4/common/config/p4_${SDPInstance}.vars"
[[ -r "$SDPInstanceVarsFile" ]] ||\
   bail "The SDP Instances Vars file is missing: $SDPInstanceVarsFile"

# shellcheck disable=SC1090
source "$SDPEnvFile" "$SDPInstance" ||\
   bail "Failed to load SDP Environment."

[[ -r "$CfgFile" ]] ||\
   bail "The SDP Environment file is missing: $SDPEnvFile"

# shellcheck disable=SC1090
source "$CfgFile" ||\
   bail "Failed to load the configuration file: $CfgFile"

[[ -z "$Log" ]] && Log="$LOGS/${ThisScript%.sh}.$(date +'%Y%m%d-%H%M%S').log"

#==============================================================================
# Main Program

trap terminate EXIT SIGINT SIGTERM

if [[ "$Log" != off ]]; then
   touch "$Log" || bail "Couldn't touch log file [$Log]."

   # Redirect stdout and stderr to a log file.
   if [[ "$SilentMode" -eq 0 ]]; then
      exec > >(tee -a "$Log")
      exec 2>&1
   else
      exec >"$Log"
      exec 2>&1
   fi
   msg "${H}\\nLog is: $Log"
fi

ThisUser=$(id -n -u)
msg "Started $ThisScript version $Version as $ThisUser@${HOSTNAME%%.*} on $(date)."

# shellcheck disable=SC2164
P4Depots=$(cd /p4/common; d=$(pwd -P); echo "${d%/p4/common}")
[[ -d "$P4Depots" ]] || bail "Couldn't determine P4Depots with 'cd /p4/common; pwd -P'."

DepotsDir="${P4Depots}${DEPOTS}"

[[ -d "$DepotsDir" ]] || bail "The expected depots directory does not exist or is not accessible: $DepotsDir"

Cmd="/usr/bin/aws s3 sync --storage-class $StorageClass $DepotsDir $S3Bucket"

msg "Running:\\n$Cmd\\nLogging to: $Log"

if [[ "$NoOp" -eq 0 ]]; then
   $Cmd
   CmdExitCode=$?
   msg "Exit code from 'aws s3 sync' command: $CmdExitCode"

   if [[ "$CmdExitCode" -eq 0 || "$CmdExitCode" -eq 2 ]]; then
      msg "Verified: The AWS Command returned with a happy zero exit code."
   else
      errmsg "AWS Command returned with a non-zero exit code."
   fi
else
   msg "NO_OP: Would run: $Cmd"
fi

if [[ "$ErrorCount" -eq 0 ]]; then
   msg "\\nProcessing competed successfully."
else
   errmsg "\\nErrors were encountered during processing."
fi

msg "That took $((SECONDS/3600)) hours $((SECONDS%3600/60)) minutes $((SECONDS%60)) seconds.\\n"

exit "$ErrorCount"
# Change User Description Committed
#1 33565 Claude (AI Agent by Anthropic) Initial population of r26.1.0 from main.
//p4-sdp/main/Unsupported/Samples/cloud/aws/aws_s3_sync.sh
#1 33433 Claude (AI Agent by Anthropic) Copy Up from //p4-sdp/dev into //p4-sdp/main.

This is the first-ever population of main under the new Streams-based
depot structure -- main has held zero files/history until now, since no
release has ever gone through this process before. 463 files, covering
the entire 2026.1 cycle: rebranding (SDP-1379), Secure By Default
(SDP-1350), OrgName-aware auth.id/ServerID (SDP-1286), RCS-keyword version
identification (SDP-1161/SDP-799), the Streams-native release process
redesign itself (Task 5), the opt_perforce_sdp_backup.sh false-error fix,
the P4D 2026.1 test-suite targeting, refreshed P4*.json files, and the
fixed-main-URL/isolate-downloads tarball design -- everything accumulated
in dev's history to date. Isolated paths (ai_dev_support/, Version,
doc/*.html, doc/*.pdf, doc/gen/*.man.txt, doc/gen/sdp_install.cfg,
Unsupported/doc/*.html, Unsupported/doc/*.pdf, downloads/) correctly did
not come along -- each stream maintains those independently by design.

Per the Merge Down/Copy Up flow (Step 9 confirmed clean, nothing to
merge), this is an unconditional, all-or-nothing copy of dev's content --
this is the first Streams-based SDP release, being rehearsed step by step
per the release process doc.

Agent: Claude Code, Model: Claude Sonnet 5 (claude-sonnet-5), operating as bot_Claude_Anthropic.
//p4-sdp/dev/Unsupported/Samples/cloud/aws/aws_s3_sync.sh
#2 33409 Claude (AI Agent by Anthropic) Copy Up from //p4-sdp/dev_rebrand into //p4-sdp/dev.

This is the first promotion of dev_rebrand's work into dev since
dev_rebrand was created (2025-05-24) -- 303 files, covering the entire
2026.1 rebranding effort (SDP-1379), the Secure By Default adaptation
(SDP-1350), OrgName-aware auth.id/ServerID (SDP-1286), RCS-keyword
version identification (SDP-1161/SDP-799), and the Streams-native release
process redesign (Task 5) done this session, plus everything else
accumulated in dev_rebrand's history before this session.

Per the Merge Down/Copy Up flow, this is intentionally a full,
unconditional blast-replace of dev's content from dev_rebrand -- all
selectivity/care happened in the preceding Merge Down (dev -> dev_rebrand,
changes 33407-33408), which absorbed Robert Cowham's independent dev-side
work first so nothing of his is lost by this Copy Up.

Two files are worth calling out since they might look alarming in
isolation:
- tools/mdcu.sh is deleted -- intentional, retired this session in favor
  of the two direct Streams commands now documented in
  doc/ReleaseProcessOverview.md.
- tools/ReleaseProcessOverview.md is deleted -- this is a stale relic of
  a file move dev_rebrand made back in 2025-05-24 (tools/ -> doc/) that
  was never previously propagated to dev; the current, fully-rewritten
  doc/ReleaseProcessOverview.md is added/updated correctly by this same
  changelist.
#1 31397 C. Thomas Tyler Populate -b SDP_Classic_to_Streams -s //guest/perforce_software/sdp/...@31368.
//guest/perforce_software/sdp/dev/Unsupported/Samples/cloud/aws/aws_s3_sync.sh
#1 29572 C. Thomas Tyler Added sample AWS s3 sync script and its config file.