#!/bin/bash #------------------------------------------------------------------------------ set -u # This script verifies that the Swarm trigger and its configuration file are # properly configured to access the Swarm server using the configured # protocol (http or https), and that the Swarm trigger user is logged in # with a long-term ticket. It tests the existing configuration, but does not # make configuration changes. The tests done are: # 1. Making a 'wget' call to the Swarm server and ensuring it gets a valid # response back. # 2. Doing a 'p4 login -s' test to ensure the configured swarm user is # logged in with a long-term ticket. # # This script implements a check in the Knowledge Base article, # "Troubleshooting Helix Swarm", from # https://community.perforce.com/s/article/3696 # # Preparation: # First, edit the /opt/perforce/etc/swarm-trigger.conf file as per the # Swarm trigger installation documentation. Configure the swarm-trigger.conf # file in this directory. # # Execution: # Run this script with no arguments: # /opt/perforce/etc/trigger_to_swarm_server_access_test.sh declare ThisScript=${0##*/} declare ThisUser= declare Version=1.1.2 declare TmpDir= declare Log= declare Cmd= declare SwarmTriggerConf="/opt/perforce/etc/swarm-trigger.conf" declare TicketExpiration= declare -i ErrorCount=0 declare -i WarningCount=0 function msg () { echo -e "$*"; } function errmsg () { msg "\\nError: ${1:-Unknown Error}\\n"; ErrorCount+=1; } function warnmsg () { msg "\\nWarning: ${1:-Unknown Warning}\\n"; WarningCount+=1; } function bail () { errmsg "Error: ${1:-Unknown Error}"; exit "${2:-1}"; } ThisUser=$(whoami) msg "\\nStarting $ThisScript v$Version as $ThisUser@${HOSTNAME%%.*}." msg "Loading Swarm trigger configuration from: $SwarmTriggerConf" # shellcheck disable=SC1090 if source "$SwarmTriggerConf"; then msg "Settings loaded:" cat "$SwarmTriggerConf" else bail "Failed to load Swarm trigger configuration. Aborting." fi TmpDir=$(mktemp -d) Log="wget.out" Cmd="wget --timeout 10 --post-data shelve,1234 $SWARM_HOST/queue/add/$SWARM_TOKEN" cd "$TmpDir" || bail "Could not cd to tmp dir: $TmpDir\\nAborting." msg "\\nChecking access to Swarm server: ${SWARM_HOST:-UnsetSWARM_HOST}." msg "Running: $Cmd" if eval "$Cmd" > "$Log" 2>&1; then if grep -q "200 OK" "$Log"; then cat "$Log" msg "Verified: Swarm host is accessible and returned expected 200 message." else cat "$Log" errmsg "Swarm host did not reply with expected 200 message. Output:\\n" fi else cat "$Log" errmsg "The wget command failed.\\n" fi # Provide basic environment isolation. For SSL, rely on whatever value for # P4TRUST is defined in the environment. unset P4CONFIG P4TICKETS export P4ENVIRO=/dev/null/.p4enviro # The ADMIN_TICKET_FILE, ADMIN_USER, and P4_PORT settings should be # defined in the Swarm trigger configuration file. export P4TICKETS="${ADMIN_TICKET_FILE:-UnsetADMIN_TICKET_FILE}" export P4USER="${ADMIN_USER:-UnsetADMIN_USER}" export P4PORT="${P4_PORT:-UnsetP4_PORT}" msg "\\nChecking that Swarm trigger user $P4USER has a valid ticket." Cmd="$P4 -ztag -F %TicketExpiration% -p $P4PORT -u $P4USER login -s" msg "Running: $Cmd" TicketExpiration=$($Cmd 2>/dev/null) if [[ "$TicketExpiration" =~ [0-9]+ ]]; then # A 'long-term' ticket is one that expires more than a month # (31 days +1 second) from now. if [[ "$TicketExpiration" -ge 2678401 ]]; then msg "Verified: Swarm user $P4USER is logged into $P4PORT with a long-term ticket, valid for valid for $((TicketExpiration/3600)) hours $((TicketExpiration%3600/60)) minutes $((TicketExpiration%60)) seconds.\\n" else warnmsg "Swarm user $P4USER is logged into $P4PORT, but with only a\\nshort-term ticket, valid for $((TicketExpiration/3600)) hours $((TicketExpiration%3600/60)) minutes $((TicketExpiration%60)) seconds. Consider using a long-term ticket or renewing logins to prevent Swarm service disruption.\\n" fi else errmsg "Swarm user $P4USER is not logged into $P4PORT." fi if [[ "$ErrorCount" -eq 0 && "$WarningCount" -eq 0 ]]; then msg "\\nAll OK! The Swarm triggers can be enabled in the Triggers table." elif [[ "$ErrorCount" -eq 0 ]]; then msg "\\nNo errors were detected, but $WarningCount warnings(s) were reported. The Swarm triggers can be enabled in the Triggers table." else errmsg "Some errors were reported. See the output above." fi # Cleanup Swarm tokens and the temp file. rm -rf "$TmpDir" exit "$ErrorCount"
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#4 | 27909 | C. Thomas Tyler | Fixed bug in output display. | ||
#3 | 27908 | C. Thomas Tyler | Added info re: ticket duration; replaced error with warning if trigger is short-term. | ||
#2 | 27907 | C. Thomas Tyler |
Added internal documentation. Enhanced to safely run from anywhere, and cleand up the cleanup logic. Added test that the configured Swarm user has a long-term ticket. chmod +x |
||
#1 | 27906 | C. Thomas Tyler | Added baseline script. |