Skip to content

Instantly share code, notes, and snippets.

@daneov
Last active March 29, 2017 12:05
Show Gist options
  • Save daneov/692e4b45033d596447a808c21f11f1a3 to your computer and use it in GitHub Desktop.
Save daneov/692e4b45033d596447a808c21f11f1a3 to your computer and use it in GitHub Desktop.
This script uses the Swarm client to auto-connect to Jenkins master who has the Swarm Plugin installed.
#!/bin/bash
# -------------------------------------------------------------
# | Extract password and username from passed parameters |
# -------------------------------------------------------------
while getopts ":u:p:" opt; do
case $opt in
u) username="$OPTARG"
;;
p) password="$OPTARG"
;;
\?) echo "Invalid option -$OPTARG"
echo "Supported options: -u <username> -p <password>">&2
exit 1
;;
esac
done
# -------------------------------------------------------------
# | Safety net in case user doesn't pass any credentials |
# -------------------------------------------------------------
if [ -z $username ] || [ -z $password ] ; then
echo 'Please provide -u <username> _and_ -p <password>'
exit 1
fi
# ---------------------------------------------------------------------
# | Extract which of the listed tools are installed on the machine |
# ---------------------------------------------------------------------
commands=( "python3" "rbenv" "xcodebuild" "android" "pyenv" "ruby" )
declare -a labels
for command in "${commands[@]}"; do
location=$(command -v $command)
if [ $? -eq 0 ]; then
tool="$command=$location"
labels+=($command)
fi
done
echo "Labeling our agent for the following tools: ${labels[*]}"
# -----------------------------------------------------------------------------
# | Amount of workers = 1/4th of the cores. Memory = 512*workers |
# -----------------------------------------------------------------------------
cores=`getconf _NPROCESSORS_ONLN`
workers=$(($cores/4>=1?$cores/4:1))
let memory="$workers * 512"
# -----------------------------------------------------------------------------
# | Download swarm client if not already present in current folder |
# -----------------------------------------------------------------------------
client_file_name="swarm-client.jar"
version_client="3.3"
if [ ! -f $client_file_name ]; then
echo 'Swarm client not found. Downloading it!'
curl -o "$client_file_name" -JL "https://repo.jenkins-ci.org/releases/org/jenkins-ci/plugins/swarm-client/${version_client}/swarm-client-${version_client}.jar"
fi
# -----------------------------------------------------------------------------
# | Use a consistent hostname when connecting to the swarm master |
# -----------------------------------------------------------------------------
if [ ! -f hostname.txt ]; then
host_name="$HOSTNAME"
echo "$host_name" > hostname.txt
else
host_name=$(cat hostname.txt)
fi
# -----------------------------------------------------------------------------
# | Launch the actual client with the parameters we determined |
# -----------------------------------------------------------------------------
export PASSWORD=${password}
java -Xms${memory}m -Xmx${memory}m -jar "$client_file_name" -deleteExistingClients -labels "${labels[*]}" -executors $workers -name "$host_name" -username "$username" -passwordEnvVariable "PASSWORD"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment