-
Notifications
You must be signed in to change notification settings - Fork 22
/
jenkins-agent
executable file
·91 lines (72 loc) · 2.67 KB
/
jenkins-agent
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/bin/bash -e
#
DOWNLOAD_DIR=/usr/share/jenkins
: ${DOCKER_GROUP:=docker}
: ${DOCKER_SOCKET:=/var/run/docker.sock}
: ${JENKINS_HOME:=/var/jenkins_home}
: ${JENKINS_USER:=jenkins}
: ${JENKINS_AGENT_CONNECTION_MODE:=-http}
: ${JENKINS_AGENT_LABEL:=''}
: ${JENKINS_AGENT_MODE:=NORMAL}
: ${JENKINS_AGENT_NAME:=$HOSTNAME}
: ${JENKINS_AGENT_NUM_EXECUTORS:=1}
JENKINS_AGENT_REMOTE_FS="$JENKINS_HOME"
download_agent_jar() {
curl -Ssfo "$DOWNLOAD_DIR/agent.jar" $JENKINS_URL/jnlpJars/agent.jar
}
download_cli_jar() {
curl -Ssfo "$DOWNLOAD_DIR/cli.jar" $JENKINS_URL/jnlpJars/jenkins-cli.jar
}
# a wrapper for jenkins_cli utility
jenkins_cli() {
java -jar "$DOWNLOAD_DIR/cli.jar" $JENKINS_AGENT_CONNECTION_MODE -s $JENKINS_URL -auth "$JENKINS_AUTH" "$@"
}
# not really used, we use 'jnlpCredentials' instead
# kept for reference
get_node_secret() {
jenkins_cli groovy = \
<<< "println jenkins.model.Jenkins.instance.nodesObject.getNode('$JENKINS_AGENT_NAME')?.computer?.jnlpMac"
}
which java &>/dev/null || {
echo "java is not in the PATH, make sure it's installed"
exit 1
}
which curl &>/dev/null || {
echo "curl is not in the PATH, make sure it's installed"
exit 1
}
[ -z "$JENKINS_URL" -o -z "$JENKINS_AUTH" ] && {
echo "please set both JENKINS_URL and JENKINS_AUTH env. variables"
echo "example:"
echo " JENKINS_AUTH=user:token"
echo " JENKINS_URL=http://localhost:8080"
exit 1
}
# download required jars
mkdir -p "$DOWNLOAD_DIR"
download_agent_jar
download_cli_jar
# if docker socket provided, then
# allow docker cli to run as jenkins user
[ -S "$DOCKER_SOCKET" ] && {
DOCKER_GID=$(stat -c '%g' "$DOCKER_SOCKET")
groupadd -for -g $DOCKER_GID $DOCKER_GROUP
usermod -aG $DOCKER_GROUP $JENKINS_USER
}
# make sure to delete node on exit
trap 'exit' SIGINT SIGTERM
trap 'jenkins_cli delete-node "$JENKINS_AGENT_NAME" || true' EXIT
# create node, unless it's already exists
jenkins_cli get-node "$JENKINS_AGENT_NAME" &>/dev/null || {
CONFIG="<slave><label>$JENKINS_AGENT_LABEL</label><launcher class='hudson.slaves.JNLPLauncher' /><mode>$JENKINS_AGENT_MODE</mode><numExecutors>$JENKINS_AGENT_NUM_EXECUTORS</numExecutors><remoteFS>$JENKINS_AGENT_REMOTE_FS</remoteFS></slave>"
jenkins_cli create-node "$JENKINS_AGENT_NAME" <<< "$CONFIG"
}
# unexport potentially conflicting and sensitive variables
export -n JENKINS_AUTH JENKINS_USER JENKINS_URL
# make sure we don't override variables when running su
truncate -s0 /etc/environment
# run agent
su $JENKINS_USER -c \
"java $JAVA_OPTS -jar '$DOWNLOAD_DIR/agent.jar' -jnlpCredentials '$JENKINS_AUTH' -jnlpUrl '$JENKINS_URL/computer/$JENKINS_AGENT_NAME/jenkins-agent.jnlp'"
# exit gracefully
exit 0