This repository has been archived by the owner on May 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
docker-entrypoint.sh
executable file
·171 lines (137 loc) · 7.01 KB
/
docker-entrypoint.sh
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/bin/bash
# Copyright 2024 Thoughtworks, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
yell() { echo "$0: $*" >&2; }
die() { yell "$*"; exit 111; }
try() { echo "$ $*" 1>&2; "$@" || die "cannot $*"; }
declare -a _stringToArgs
function stringToArgsArray() {
_stringToArgs=("$@")
}
setup_autoregister_properties_file_for_elastic_agent() {
{
echo "# Auto-registration properties in key=value format, encoded in UTF-8"
echo "agent.auto.register.key=${GO_EA_AUTO_REGISTER_KEY}"
echo "agent.auto.register.environments=${GO_EA_AUTO_REGISTER_ENVIRONMENT}"
echo "agent.auto.register.elasticAgent.agentId=${GO_EA_AUTO_REGISTER_ELASTIC_AGENT_ID}"
echo "agent.auto.register.elasticAgent.pluginId=${GO_EA_AUTO_REGISTER_ELASTIC_PLUGIN_ID}"
echo "agent.auto.register.hostname=${AGENT_AUTO_REGISTER_HOSTNAME}"
} >> "$1"
export GO_SERVER_URL="${GO_EA_SERVER_URL}"
# unset variables, so we don't pollute and leak sensitive stuff to the agent process...
unset GO_EA_AUTO_REGISTER_KEY GO_EA_AUTO_REGISTER_ENVIRONMENT GO_EA_AUTO_REGISTER_ELASTIC_AGENT_ID GO_EA_AUTO_REGISTER_ELASTIC_PLUGIN_ID GO_EA_SERVER_URL AGENT_AUTO_REGISTER_HOSTNAME
}
setup_autoregister_properties_file_for_normal_agent() {
{
echo "# Auto-registration properties in key=value format, encoded in UTF-8"
echo "agent.auto.register.key=${AGENT_AUTO_REGISTER_KEY}"
echo "agent.auto.register.resources=${AGENT_AUTO_REGISTER_RESOURCES}"
echo "agent.auto.register.environments=${AGENT_AUTO_REGISTER_ENVIRONMENTS}"
echo "agent.auto.register.hostname=${AGENT_AUTO_REGISTER_HOSTNAME}"
} >> "$1"
# unset variables, so we don't pollute and leak sensitive stuff to the agent process...
unset AGENT_AUTO_REGISTER_KEY AGENT_AUTO_REGISTER_RESOURCES AGENT_AUTO_REGISTER_ENVIRONMENTS AGENT_AUTO_REGISTER_HOSTNAME
}
setup_autoregister_properties_file() {
if [ -n "$GO_EA_SERVER_URL" ]; then
setup_autoregister_properties_file_for_elastic_agent "$1"
else
setup_autoregister_properties_file_for_normal_agent "$1"
fi
}
if [ -e /run-docker-daemon.sh ]; then
try sudo /run-docker-daemon.sh
fi
AGENT_WORK_DIR="/go"
# no arguments are passed so assume user wants to run the GoCD agent
# we prepend "/${AGENT_WORK_DIR}/bin/go-agent console" to the argument list
if [[ $# -eq 0 ]] ; then
set -- "${AGENT_WORK_DIR}/bin/go-agent" console "$@"
fi
if [ "$1" = "${AGENT_WORK_DIR}/bin/go-agent" ]; then
[ -z "${VOLUME_DIR}" ] && VOLUME_DIR="/godata"
agent_data_dirs=(config logs pipelines)
yell "Creating directories and symlinks to hold GoCD configuration, data, and logs"
for each_dir in "${agent_data_dirs[@]}"; do
if [ ! -e "${VOLUME_DIR}/${each_dir}" ]; then
try mkdir -v "${VOLUME_DIR}/${each_dir}"
fi
if [ ! -e "${AGENT_WORK_DIR}/${each_dir}" ]; then
try ln -sv "${VOLUME_DIR}/${each_dir}" "${AGENT_WORK_DIR}/${each_dir}"
fi
done
wrapper_dirs=(bin lib run wrapper wrapper-config)
yell "Creating directories and symlinks to hold GoCD wrapper binaries"
for each_dir in "${wrapper_dirs[@]}"; do
if [ ! -e "${AGENT_WORK_DIR}/${each_dir}" ]; then
try ln -sv "/go-agent/${each_dir}" "${AGENT_WORK_DIR}/${each_dir}"
fi
done
if [ ! -e "${AGENT_WORK_DIR}/config/agent-bootstrapper-logback-include.xml" ]; then
try cp -rfv "/go-agent/config/agent-bootstrapper-logback-include.xml" "${AGENT_WORK_DIR}/config/agent-bootstrapper-logback-include.xml"
fi
if [ ! -e "${AGENT_WORK_DIR}/config/agent-launcher-logback-include.xml" ]; then
try cp -rfv "/go-agent/config/agent-launcher-logback-include.xml" "${AGENT_WORK_DIR}/config/agent-launcher-logback-include.xml"
fi
if [ ! -e "${AGENT_WORK_DIR}/config/agent-logback-include.xml" ]; then
try cp -rfv "/go-agent/config/agent-logback-include.xml" "${AGENT_WORK_DIR}/config/agent-logback-include.xml"
fi
setup_autoregister_properties_file "${AGENT_WORK_DIR}/config/autoregister.properties"
yell "Running custom scripts in /docker-entrypoint.d/ ..."
# to prevent expansion to literal string `/docker-entrypoint.d/*` when there is nothing matching the glob
shopt -s nullglob
for file in /docker-entrypoint.d/*; do
if [ -f "$file" ] && [ -x "$file" ]; then
try "$file"
else
yell "Ignoring $file, it is either not a file or is not executable"
fi
done
# setup the java binary and wrapper log
try sed -i \
-e "s@wrapper.logfile=.*@wrapper.logfile=${AGENT_WORK_DIR}/logs/go-agent-bootstrapper-wrapper.log@g" \
-e "s@wrapper.java.command=.*@wrapper.java.command=${GO_JAVA_HOME}/bin/java@g" \
-e "s@wrapper.working.dir=.*@wrapper.working.dir=${AGENT_WORK_DIR}@g" \
/go-agent/wrapper-config/wrapper.conf
# Set the agent-bootstrapper args to point to configured Go Server from the environment
try sed -i \
-e "s@wrapper.app.parameter.100=.*@wrapper.app.parameter.100=-serverUrl@g" \
-e "s@wrapper.app.parameter.101=.*@wrapper.app.parameter.101=${GO_SERVER_URL}@g" \
/go-agent/wrapper-config/wrapper-properties.conf
echo "" >> /go-agent/wrapper-config/wrapper-properties.conf
echo "###### Properties automatically set from environment by default entrypoint" >> /go-agent/wrapper-config/wrapper-properties.conf
# parse/split the AGENT_BOOTSTRAPPER_ARGS for configuring additional SSL settings
eval stringToArgsArray "$AGENT_BOOTSTRAPPER_ARGS"
AGENT_BOOTSTRAPPER_ARGS=("${_stringToArgs[@]}")
for array_index in "${!AGENT_BOOTSTRAPPER_ARGS[@]}"
do
tanuki_index=$((array_index + 200))
echo "wrapper.app.parameter.${tanuki_index}=${AGENT_BOOTSTRAPPER_ARGS[$array_index]}" >> /go-agent/wrapper-config/wrapper-properties.conf
done
# parse/split an environment var to an array like how it should pass to the CLI
# AGENT_BOOTSTRAPPER_JVM_ARGS is mostly for advanced users.
eval stringToArgsArray "$AGENT_BOOTSTRAPPER_JVM_ARGS"
AGENT_BOOTSTRAPPER_JVM_ARGS=("${_stringToArgs[@]}")
AGENT_BOOTSTRAPPER_JVM_ARGS+=("-Dgo.console.stdout=true")
# write out each system property using its own index
for array_index in "${!AGENT_BOOTSTRAPPER_JVM_ARGS[@]}"
do
tanuki_index=$((array_index + 100))
echo "wrapper.java.additional.${tanuki_index}=${AGENT_BOOTSTRAPPER_JVM_ARGS[$array_index]}" >> /go-agent/wrapper-config/wrapper-properties.conf
done
# Allow configuration of the forked agent process when started by the bootstrapper
echo "set.default.GOCD_AGENT_JVM_OPTS=" >> /go-agent/wrapper-config/wrapper-properties.conf
echo "set.AGENT_STARTUP_ARGS=%AGENT_STARTUP_ARGS% -Dgo.console.stdout=true %GOCD_AGENT_JVM_OPTS%" >> /go-agent/wrapper-config/wrapper-properties.conf
fi
try exec /usr/local/sbin/tini -g -- "$@"