Skip to content

Instantly share code, notes, and snippets.

@shalomb
Created January 9, 2019 18:31
Show Gist options
  • Save shalomb/8b2451be6b24f8fe6a6b375a3628497b to your computer and use it in GitHub Desktop.
Save shalomb/8b2451be6b24f8fe6a6b375a3628497b to your computer and use it in GitHub Desktop.
Improved artifactory systemd service manager
#!/bin/bash
set -eu -o pipefail
ping_url='http://localhost:8081/artifactory/api/system/ping'
source /var/opt/jfrog/artifactory/etc/default
ARTIFACTORY_PIDFILE="$ARTIFACTORY_PID"
: ${START_TMO:=120}
function af-status {
ARTIFACTORY_PID=$(< $ARTIFACTORY_PIDFILE)
kill -0 "$ARTIFACTORY_PID"
env | grep -i -e java -e artifactory -e tomcat
pstree -als "$ARTIFACTORY_PID"
ready=0
sleep=15
until (( ready == 1 )); do
echo -n "Waiting for Artifactory Tomcat to accept connections .. "
status=$( curl -s -o /dev/null -w "%{http_code}" "$ping_url" )
echo "pid: $ARTIFACTORY_PID, status: $status, sleep: $sleep"
if (( status != 503 )); then
ready=1
else
sleep $(( sleep = sleep/2 + 1 ))
fi
done
echo
curl -fsSLi "$ping_url"
}
case "$1" in
restart|stop)
/opt/jfrog/artifactory/bin/artifactoryManage.sh stop ||
pkill -f 'artifactory/tomcat'
;&
restart|start)
timeout -s15 -k$(( START_TMO * 2 )) "$START_TMO" \
/opt/jfrog/artifactory/bin/artifactoryManage.sh "$@"
af-status || true
;;
status)
af-status
;;
logs)
cd "$ARTIFACTORY_HOME/logs" && tail -f $(find . -type f)
;;
esac
[Unit]
Description=Setup Systemd script for Artifactory in Tomcat Servlet Engine
After=network.target
[Service]
Type=forking
GuessMainPID=yes
Restart=always
RestartSec=5
PIDFile=/var/opt/jfrog/run/artifactory.pid
ExecStart=/usr/local/bin/artifactory-sm start
ExecStop=/usr/local/bin/artifactory-sm stop
[Install]
WantedBy=multi-user.target
Alias=artifactory.service
@shalomb
Copy link
Author

shalomb commented Jan 9, 2019

A number of times now we've come across situations where the artifactory service starts (systemctl start artifactory succeeds) yet artifactory is unable to service requests (i.e. returning HTTP 503 - Service Unavailable or similar) due to

  • configuration issues (misconfigurations in artifactory.config.latest.xml).
  • system issues (database cluster or binstore provider being inaccessible).

It turns out that the /opt/jfrog/artifactory/bin/artifactoryManage.sh script that comes part of the installation (debian package) does not perform enough checks to ensure artifactory is actually initialized.

artifactory-sm is a proof of concept script to wrap the /opt/jfrog/artifactory/bin/artifactoryManage.sh and ensure the artifactory tomcat application is fully bootstrapped, initialized and is able to serve HTTP requests before returning.

This ensures the following

  • System and Configuration file issues that prevent artifactory from starting cause systemctl start artifactory.service to fail fast.
  • systemctl start artifactory.service does not return unless the artifactory tomcat application is able to serve HTTP requests (as opposed to the default behaviour of starting tomcat and returning early - with no guarantee that artifactory will start).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment