1#!/usr/bin/env bash 2 3# The MIT License 4# 5# Copyright (c) 2015-2020, CloudBees, Inc. 6# 7# Permission is hereby granted, free of charge, to any person obtaining a copy 8# of this software and associated documentation files (the "Software"), to deal 9# in the Software without restriction, including without limitation the rights 10# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11# copies of the Software, and to permit persons to whom the Software is 12# furnished to do so, subject to the following conditions: 13# 14# The above copyright notice and this permission notice shall be included in 15# all copies or substantial portions of the Software. 16# 17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23# THE SOFTWARE. 24 25# Usage jenkins-agent.sh [options] -url http://jenkins [SECRET] [AGENT_NAME] 26# Optional environment variables : 27# * JENKINS_TUNNEL : HOST:PORT for a tunnel to route TCP traffic to jenkins host, when jenkins can't be directly accessed over network 28# * JENKINS_URL : alternate jenkins URL 29# * JENKINS_SECRET : agent secret, if not set as an argument 30# * JENKINS_AGENT_NAME : agent name, if not set as an argument 31# * JENKINS_AGENT_WORKDIR : agent work directory, if not set by optional parameter -workDir 32# * JENKINS_WEB_SOCKET: true if the connection should be made via WebSocket rather than TCP 33# * JENKINS_DIRECT_CONNECTION: Connect directly to this TCP agent port, skipping the HTTP(S) connection parameter download. 34# Value: "<HOST>:<PORT>" 35# * JENKINS_INSTANCE_IDENTITY: The base64 encoded InstanceIdentity byte array of the Jenkins master. When this is set, 36# the agent skips connecting to an HTTP(S) port for connection info. 37# * JENKINS_PROTOCOLS: Specify the remoting protocols to attempt when instanceIdentity is provided. 38 39if [ $# -eq 1 ]; then 40 41 # if `docker run` only has one arguments, we assume user is running alternate command like `bash` to inspect the image 42 exec "$@" 43 44else 45 46 # if -tunnel is not provided, try env vars 47 case "$@" in 48 *"-tunnel "*) ;; 49 *) 50 if [ ! -z "$JENKINS_TUNNEL" ]; then 51 TUNNEL="-tunnel $JENKINS_TUNNEL" 52 fi ;; 53 esac 54 55 # if -workDir is not provided, try env vars 56 if [ ! -z "$JENKINS_AGENT_WORKDIR" ]; then 57 case "$@" in 58 *"-workDir"*) echo "Warning: Work directory is defined twice in command-line arguments and the environment variable" ;; 59 *) 60 WORKDIR="-workDir $JENKINS_AGENT_WORKDIR" ;; 61 esac 62 fi 63 64 if [ -n "$JENKINS_URL" ]; then 65 URL="-url $JENKINS_URL" 66 fi 67 68 if [ -n "$JENKINS_NAME" ]; then 69 JENKINS_AGENT_NAME="$JENKINS_NAME" 70 fi 71 72 if [ "$JENKINS_WEB_SOCKET" = true ]; then 73 WEB_SOCKET=-webSocket 74 fi 75 76 if [ -n "$JENKINS_PROTOCOLS" ]; then 77 PROTOCOLS="-protocols $JENKINS_PROTOCOLS" 78 fi 79 80 if [ -n "$JENKINS_DIRECT_CONNECTION" ]; then 81 DIRECT="-direct $JENKINS_DIRECT_CONNECTION" 82 fi 83 84 if [ -n "$JENKINS_INSTANCE_IDENTITY" ]; then 85 INSTANCE_IDENTITY="-instanceIdentity $JENKINS_INSTANCE_IDENTITY" 86 fi 87 88 # if java home is defined, use it 89 JAVA_BIN="java" 90 if [ "$JAVA_HOME" ]; then 91 JAVA_BIN="$JAVA_HOME/bin/java" 92 fi 93 94 # if both required options are defined, do not pass the parameters 95 OPT_JENKINS_SECRET="" 96 if [ -n "$JENKINS_SECRET" ]; then 97 case "$@" in 98 *"${JENKINS_SECRET}"*) echo "Warning: SECRET is defined twice in command-line arguments and the environment variable" ;; 99 *) 100 OPT_JENKINS_SECRET="${JENKINS_SECRET}" ;; 101 esac 102 fi 103 104 OPT_JENKINS_AGENT_NAME="" 105 if [ -n "$JENKINS_AGENT_NAME" ]; then 106 case "$@" in 107 *"${JENKINS_AGENT_NAME}"*) echo "Warning: AGENT_NAME is defined twice in command-line arguments and the environment variable" ;; 108 *) 109 OPT_JENKINS_AGENT_NAME="${JENKINS_AGENT_NAME}" ;; 110 esac 111 fi 112 113 #TODO: Handle the case when the command-line and Environment variable contain different values. 114 #It is fine it blows up for now since it should lead to an error anyway. 115 116 exec $JAVA_BIN $JAVA_OPTS -cp /usr/share/jenkins/agent.jar hudson.remoting.jnlp.Main -headless $TUNNEL $URL $WORKDIR $WEB_SOCKET $DIRECT $PROTOCOLS $INSTANCE_IDENTITY $OPT_JENKINS_SECRET $OPT_JENKINS_AGENT_NAME "$@" 117fi 118