1
- #! /bin/sh
1
+ #! /bin/bash
2
2
set -eo pipefail
3
3
4
- function log_level () {
5
- if [ " $1 " = debug ]; then
6
- echo 3
7
- elif [ " $1 " = info ]; then
8
- echo 2
9
- elif [ " $1 " = warn ]; then
10
- echo 1
11
- elif [ " $1 " = error ]; then
12
- echo 0
13
- fi
14
- }
15
- LOG_LEVEL=error
4
+ # Do our best to match the logging requested by the user running the container.
5
+ declare -rA LOG_LEVELS=([error]=0 [warn]=1 [info]=2 [debug]=3)
6
+ declare LOG_LEVEL=error
16
7
17
8
# Mimic the structured logging used by InfluxDB.
18
9
# Usage: log <level> <msg> [<key> <val>]...
19
10
function log () {
20
- local level=$1 msg=$2
11
+ local -r level=$1 msg=$2
21
12
shift 2
22
13
23
- if [ " $( log_level ${level} ) " -gt " $( log_level ${LOG_LEVEL} ) " ]; then
14
+ if [ " ${LOG_LEVELS[ ${level}]} " -gt " ${LOG_LEVELS[ ${LOG_LEVEL}]} " ]; then
24
15
return
25
16
fi
26
17
@@ -30,31 +21,31 @@ function log () {
30
21
shift 2
31
22
done
32
23
33
- local logtime=" $( date --utc +' %FT%T.%NZ' ) "
24
+ local -r logtime=" $( date --utc +' %FT%T.%NZ' ) "
34
25
1>&2 echo -e " ${logtime} \t${level} \t${msg} \t{${attrs} }"
35
26
}
36
27
37
28
# Set the global log-level for the entry-point to match the config passed to influxd.
38
29
function set_global_log_level () {
39
30
local level=" $( influxd print-config --key-name log-level " ${@ } " ) "
40
- if [ -z " ${level} " ] || [ -z " $( log_level ${level} ) " ]; then
31
+ if [ -z " ${level} " ] || [ -z " ${LOG_LEVELS[ ${level}]} " ]; then
41
32
return 1
42
33
fi
43
- LOG_LEVEL=" ${level} "
34
+ LOG_LEVEL=${level}
44
35
}
45
36
46
37
# Look for standard config names in the volume configured in our Dockerfile.
47
- readonly CONFIG_VOLUME=/etc/influxdb2
48
- readonly CONFIG_NAMES=" config.json config.toml config.yaml config.yml"
38
+ declare -r CONFIG_VOLUME=/etc/influxdb2
39
+ declare -ra CONFIG_NAMES=( config.json config.toml config.yaml config.yml)
49
40
50
41
# Search for a V2 config file, and export its path into the env for influxd to use.
51
42
function set_config_path () {
52
43
local config_path=/etc/defaults/influxdb2/config.yml
53
44
54
- if [ -n " ${ INFLUXD_CONFIG_PATH} " ]; then
45
+ if [ -n " $INFLUXD_CONFIG_PATH " ]; then
55
46
config_path=" ${INFLUXD_CONFIG_PATH} "
56
47
else
57
- for name in ${CONFIG_NAMES} ; do
48
+ for name in " ${CONFIG_NAMES[@]} " ; do
58
49
if [ -f " ${CONFIG_VOLUME} /${name} " ]; then
59
50
config_path=" ${CONFIG_VOLUME} /${name} "
60
51
break
@@ -72,31 +63,31 @@ function set_data_paths () {
72
63
73
64
# Ensure all the data directories needed by influxd exist with the right permissions.
74
65
function create_directories () {
75
- local bolt_dir=" $( dirname " ${BOLT_PATH} " ) "
76
- local user=" $( id -u) "
66
+ local -r bolt_dir=" $( dirname " ${BOLT_PATH} " ) "
67
+ local user=$( id -u)
77
68
78
69
mkdir -p " ${bolt_dir} " " ${ENGINE_PATH} "
79
70
chmod 700 " ${bolt_dir} " " ${ENGINE_PATH} " || :
80
71
81
72
mkdir -p " ${CONFIG_VOLUME} " || :
82
73
chmod 775 " ${CONFIG_VOLUME} " || :
83
74
84
- if [ " ${user} " = 0 ]; then
75
+ if [ ${user} = 0 ]; then
85
76
find " ${bolt_dir} " \! -user influxdb -exec chown influxdb ' {}' +
86
77
find " ${ENGINE_PATH} " \! -user influxdb -exec chown influxdb ' {}' +
87
78
find " ${CONFIG_VOLUME} " \! -user influxdb -exec chown influxdb ' {}' +
88
79
fi
89
80
}
90
81
91
82
# List of env vars required to auto-run setup or upgrade processes.
92
- readonly REQUIRED_INIT_VARS=" INFLUXDB_INIT_USERNAME INFLUXDB_INIT_PASSWORD INFLUXDB_INIT_ORG INFLUXDB_INIT_BUCKET "
83
+ declare -ra REQUIRED_INIT_VARS=(DOCKER_INFLUXDB_INIT_USERNAME DOCKER_INFLUXDB_INIT_PASSWORD DOCKER_INFLUXDB_INIT_ORG DOCKER_INFLUXDB_INIT_BUCKET)
93
84
94
85
# Ensure all env vars required to run influx setup or influxd upgrade are set in the env.
95
86
function ensure_init_vars_set () {
96
87
local missing_some=0
97
- for var in ${REQUIRED_INIT_VARS} ; do
98
- if [ -z " $( printenv ${ var}) " ]; then
99
- log error " missing parameter, cannot init InfluxDB" parameter " ${var} "
88
+ for var in " ${REQUIRED_INIT_VARS[@]} " ; do
89
+ if [ -z " ${ ! var} " ]; then
90
+ log error " missing parameter, cannot init InfluxDB" parameter ${var}
100
91
missing_some=1
101
92
fi
102
93
done
@@ -116,55 +107,55 @@ function cleanup_influxd () {
116
107
# Upgrade V1 data into the V2 format using influxd upgrade.
117
108
# The process will use either a V1 config file or a V1 data dir to drive
118
109
# the upgrade, with precedence order:
119
- # 1. Config file pointed to by INFLUXDB_INIT_UPGRADE_V1_CONFIG env var
120
- # 2. Data dir pointed to by INFLUXDB_INIT_UPGRADE_V1_DIR env var
110
+ # 1. Config file pointed to by DOCKER_INFLUXDB_INIT_UPGRADE_V1_CONFIG env var
111
+ # 2. Data dir pointed to by DOCKER_INFLUXDB_INIT_UPGRADE_V1_DIR env var
121
112
# 3. Config file at /etc/influxdb/influxdb.conf
122
113
# 4. Data dir at /var/lib/influxdb
123
114
function upgrade_influxd () {
124
- set -- \
125
- --force \
126
- --username " ${INFLUXDB_INIT_USERNAME} " \
127
- --password " ${INFLUXDB_INIT_PASSWORD} " \
128
- --org " ${INFLUXDB_INIT_ORG} " \
129
- --bucket " ${INFLUXDB_INIT_BUCKET} " \
130
- --v2-config-path " ${CONFIG_VOLUME} /config.toml" \
131
- --influx-configs-path " ${INFLUX_CONFIGS_PATH} " \
132
- --continuous-query-export-path " ${CONFIG_VOLUME} /v1-cq-export.txt" \
133
- --log-path " ${CONFIG_VOLUME} /upgrade.log" \
134
- --log-level " ${LOG_LEVEL} " \
135
- --bolt-path " ${BOLT_PATH} " \
136
- --engine-path " ${ENGINE_PATH} " \
115
+ local -a upgrade_args=(
116
+ --force
117
+ --username " ${DOCKER_INFLUXDB_INIT_USERNAME} "
118
+ --password " ${DOCKER_INFLUXDB_INIT_PASSWORD} "
119
+ --org " ${DOCKER_INFLUXDB_INIT_ORG} "
120
+ --bucket " ${DOCKER_INFLUXDB_INIT_BUCKET} "
121
+ --v2-config-path " ${CONFIG_VOLUME} /config.toml"
122
+ --influx-configs-path " ${INFLUX_CONFIGS_PATH} "
123
+ --continuous-query-export-path " ${CONFIG_VOLUME} /v1-cq-export.txt"
124
+ --log-path " ${CONFIG_VOLUME} /upgrade.log"
125
+ --log-level " ${LOG_LEVEL} "
126
+ --bolt-path " ${BOLT_PATH} "
127
+ --engine-path " ${ENGINE_PATH} "
137
128
--overwrite-existing-v2
138
-
139
- if [ -n " ${INFLUXDB_INIT_RETENTION } " ]; then
140
- set -- " ${@ } " --retention " ${INFLUXDB_INIT_RETENTION} "
129
+ )
130
+ if [ -n " ${DOCKER_INFLUXDB_INIT_RETENTION } " ]; then
131
+ upgrade_args=( " ${upgrade_args[@] } " --retention " ${DOCKER_INFLUXDB_INIT_RETENTION} " )
141
132
fi
142
- if [ -n " ${INFLUXDB_INIT_ADMIN_TOKEN } " ]; then
143
- set -- " ${@ } " --token " ${INFLUXDB_INIT_ADMIN_TOKEN} "
133
+ if [ -n " ${DOCKER_INFLUXDB_INIT_ADMIN_TOKEN } " ]; then
134
+ upgrade_args=( " ${upgrade_args[@] } " --token " ${DOCKER_INFLUXDB_INIT_ADMIN_TOKEN} " )
144
135
fi
145
136
146
- if [[ -n " ${INFLUXDB_INIT_UPGRADE_V1_CONFIG } " && -f " ${INFLUXDB_INIT_UPGRADE_V1_CONFIG } " ]]; then
147
- set -- " ${@ } " --config-file " ${INFLUXDB_INIT_UPGRADE_V1_CONFIG} "
148
- elif [[ -n " ${INFLUXDB_INIT_UPGRADE_V1_DIR } " && -d " ${INFLUXDB_INIT_UPGRADE_V1_DIR } " ]]; then
149
- set -- " ${@ } " --v1-dir " ${INFLUXDB_INIT_UPGRADE_V1_DIR} "
137
+ if [[ -n " ${DOCKER_INFLUXDB_INIT_UPGRADE_V1_CONFIG } " && -f " ${DOCKER_INFLUXDB_INIT_UPGRADE_V1_CONFIG } " ]]; then
138
+ upgrade_args=( " ${upgrade_args[@] } " --config-file " ${DOCKER_INFLUXDB_INIT_UPGRADE_V1_CONFIG} " )
139
+ elif [[ -n " ${DOCKER_INFLUXDB_INIT_UPGRADE_V1_DIR } " && -d " ${DOCKER_INFLUXDB_INIT_UPGRADE_V1_DIR } " ]]; then
140
+ upgrade_args=( " ${upgrade_args[@] } " --v1-dir " ${DOCKER_INFLUXDB_INIT_UPGRADE_V1_DIR} " )
150
141
elif [ -f /etc/influxdb/influxdb.conf ]; then
151
- set -- " ${@ } " --config-file /etc/influxdb/influxdb.conf
142
+ upgrade_args=( " ${upgrade_args[@] } " --config-file /etc/influxdb/influxdb.conf)
152
143
elif [ -d /var/lib/influxdb ]; then
153
- set -- " ${@ } " --v1-dir /var/lib/influxdb
144
+ upgrade_args=( " ${upgrade_args[@] } " --v1-dir /var/lib/influxdb)
154
145
else
155
146
log error " failed to autodetect usable V1 config or data dir, aborting upgrade"
156
147
exit 1
157
148
fi
158
149
159
- influxd upgrade " ${@ } "
150
+ influxd upgrade " ${upgrade_args[@] } "
160
151
161
152
# Reset global influxd config to pick up new file written by the upgrade process.
162
153
set_config_path
163
154
}
164
155
165
156
# Wait up to a minute for the DB to boot
166
- readonly STARTUP_PING_WAIT_SECONDS=2
167
- readonly STARTUP_PING_ATTEMPTS=30
157
+ declare -r STARTUP_PING_WAIT_SECONDS=2
158
+ declare -r STARTUP_PING_ATTEMPTS=30
168
159
169
160
# Ping influxd until it responds.
170
161
# Used to block execution until the server is ready to process setup requests.
@@ -184,34 +175,34 @@ function wait_for_influxd () {
184
175
185
176
# Create an initial user/org/bucket in the DB using the influx CLI.
186
177
function setup_influxd () {
187
- set -- \
188
- --force \
189
- --username " ${INFLUXDB_INIT_USERNAME} " \
190
- --password " ${INFLUXDB_INIT_PASSWORD} " \
191
- --org " ${INFLUXDB_INIT_ORG} " \
192
- --bucket " ${INFLUXDB_INIT_BUCKET } "
193
-
194
- if [ -n " ${INFLUXDB_INIT_RETENTION } " ]; then
195
- set -- " ${@ } " --retention " ${INFLUXDB_INIT_RETENTION} "
178
+ local -a setup_args=(
179
+ --force
180
+ --username " ${DOCKER_INFLUXDB_INIT_USERNAME} "
181
+ --password " ${DOCKER_INFLUXDB_INIT_PASSWORD} "
182
+ --org " ${DOCKER_INFLUXDB_INIT_ORG} "
183
+ --bucket " ${DOCKER_INFLUXDB_INIT_BUCKET } "
184
+ )
185
+ if [ -n " ${DOCKER_INFLUXDB_INIT_RETENTION } " ]; then
186
+ setup_args=( " ${setup_args[@] } " --retention " ${DOCKER_INFLUXDB_INIT_RETENTION} " )
196
187
fi
197
- if [ -n " ${INFLUXDB_INIT_ADMIN_TOKEN } " ]; then
198
- set -- " ${@ } " --token " ${INFLUXDB_INIT_ADMIN_TOKEN} "
188
+ if [ -n " ${DOCKER_INFLUXDB_INIT_ADMIN_TOKEN } " ]; then
189
+ setup_args=( " ${setup_args[@] } " --token " ${DOCKER_INFLUXDB_INIT_ADMIN_TOKEN} " )
199
190
fi
200
191
201
- influx setup " ${@ } "
192
+ influx setup " ${setup_args[@] } "
202
193
}
203
194
204
195
# Get the IDs of the initial user/org/bucket created during setup, and export them into the env.
205
196
# We do this to help with arbitrary user scripts, since many influx CLI commands only take IDs.
206
197
function set_init_resource_ids () {
207
- export INFLUXDB_INIT_USER_ID =" $( influx user list -n " ${INFLUXDB_INIT_USER } " --hide-headers | cut -f 1) "
208
- export INFLUXDB_INIT_ORG_ID =" $( influx org list -n " ${INFLUXDB_INIT_ORG } " --hide-headers | cut -f 1) "
209
- export INFLUXDB_INIT_BUCKET_ID =" $( influx bucket list -n " ${INFLUXDB_INIT_BUCKET } " --hide-headers | cut -f 1) "
198
+ export DOCKER_INFLUXDB_INIT_USER_ID =" $( influx user list -n " ${DOCKER_INFLUXDB_INIT_USER } " --hide-headers | cut -f 1) "
199
+ export DOCKER_INFLUXDB_INIT_ORG_ID =" $( influx org list -n " ${DOCKER_INFLUXDB_INIT_ORG } " --hide-headers | cut -f 1) "
200
+ export DOCKER_INFLUXDB_INIT_BUCKET_ID =" $( influx bucket list -n " ${DOCKER_INFLUXDB_INIT_BUCKET } " --hide-headers | cut -f 1) "
210
201
}
211
202
212
203
# Allow users to mount arbitrary startup scripts into the container,
213
204
# for execution after initial setup/upgrade.
214
- readonly USER_SCRIPT_DIR=/docker-entrypoint-initdb.d
205
+ declare -r USER_SCRIPT_DIR=/docker-entrypoint-initdb.d
215
206
216
207
# Execute all shell files mounted into the expected path for user-defined startup scripts.
217
208
function run_user_scripts () {
@@ -231,23 +222,23 @@ function handle_signal () {
231
222
# Perform initial setup on the InfluxDB instance, either by setting up fresh metadata
232
223
# or by upgrading existing V1 data.
233
224
function init_influxd () {
234
- if [[ " ${INFLUXDB_INIT_MODE } " != setup && " ${INFLUXDB_INIT_MODE } " != upgrade ]]; then
235
- log error " found invalid INFLUXDB_INIT_MODE , valid values are 'setup' and 'upgrade'" INFLUXDB_INIT_MODE " ${INFLUXDB_INIT_MODE } "
225
+ if [[ " ${DOCKER_INFLUXDB_INIT_MODE } " != setup && " ${DOCKER_INFLUXDB_INIT_MODE } " != upgrade ]]; then
226
+ log error " found invalid DOCKER_INFLUXDB_INIT_MODE , valid values are 'setup' and 'upgrade'" DOCKER_INFLUXDB_INIT_MODE " ${DOCKER_INFLUXDB_INIT_MODE } "
236
227
exit 1
237
228
fi
238
229
ensure_init_vars_set
239
230
trap " cleanup_influxd" EXIT
240
231
241
232
# The upgrade process needs to run before we boot the server, otherwise the
242
233
# boltdb file will be generated and cause conflicts.
243
- if [ " ${INFLUXDB_INIT_MODE } " = upgrade ]; then
234
+ if [ " ${DOCKER_INFLUXDB_INIT_MODE } " = upgrade ]; then
244
235
upgrade_influxd
245
236
fi
246
237
247
238
# Generate a config file with a known HTTP port
248
- local init_config=/tmp/config.yml
249
- local final_bind_addr=" $( influxd print-config --key-name http-bind-address " ${@ } " ) "
250
- local init_bind_addr=" :${INFLUXD_INIT_PORT} "
239
+ local -r init_config=/tmp/config.yml
240
+ local -r final_bind_addr=" $( influxd print-config --key-name http-bind-address " ${@ } " ) "
241
+ local -r init_bind_addr=" :${INFLUXD_INIT_PORT} "
251
242
if [ " ${init_bind_addr} " = " ${final_bind_addr} " ]; then
252
243
log warn " influxd setup binding to same addr as final config, server will be exposed before ready" addr " ${init_bind_addr} "
253
244
fi
@@ -256,15 +247,15 @@ function init_influxd () {
256
247
# Start influxd in the background.
257
248
log info " booting influxd server in the background"
258
249
INFLUXD_CONFIG_PATH=${init_config} influxd " ${@ } " &
259
- local influxd_init_pid=" $! "
250
+ local -r influxd_init_pid=" $! "
260
251
trap " handle_signal TERM ${influxd_init_pid} " TERM
261
252
trap " handle_signal INT ${influxd_init_pid} " INT
262
253
263
254
export INFLUX_HOST=" http://localhost:${INFLUXD_INIT_PORT} "
264
255
wait_for_influxd
265
256
266
257
# Use the influx CLI to create an initial user/org/bucket.
267
- if [ " ${INFLUXDB_INIT_MODE } " = setup ]; then
258
+ if [ " ${DOCKER_INFLUXDB_INIT_MODE } " = setup ]; then
268
259
setup_influxd
269
260
fi
270
261
@@ -277,16 +268,16 @@ function init_influxd () {
277
268
trap - EXIT INT TERM
278
269
279
270
# Rewrite the ClI configs to point at the server's final HTTP address.
280
- local final_port=" $( echo ${final_bind_addr} | sed -E ' s#[^:]*:(.*)#\1#' ) "
271
+ local -r final_port=" $( echo " ${final_bind_addr} " | sed -E ' s#[^:]*:(.*)#\1#' ) "
281
272
sed -i " s#http://localhost:${INFLUXD_INIT_PORT} #http://localhost:${final_port} #g" " ${INFLUX_CONFIGS_PATH} "
282
273
}
283
274
284
275
# Run influxd, with optional setup logic.
285
276
function influxd_main () {
286
277
if [ -f " ${BOLT_PATH} " ]; then
287
278
log info " found existing boltdb file, skipping setup wrapper" bolt_path " ${BOLT_PATH} "
288
- elif [ -z " ${INFLUXDB_INIT_MODE } " ]; then
289
- log warn " boltdb not found at configured path, but INFLUXDB_INIT_MODE not specified, skipping setup wrapper" bolt_path " ${bolt_path} "
279
+ elif [ -z " ${DOCKER_INFLUXDB_INIT_MODE } " ]; then
280
+ log warn " boltdb not found at configured path, but DOCKER_INFLUXDB_INIT_MODE not specified, skipping setup wrapper" bolt_path " ${bolt_path} "
290
281
else
291
282
init_influxd " ${@ } "
292
283
fi
@@ -301,9 +292,9 @@ function main () {
301
292
set_config_path
302
293
303
294
local run_influxd=false
304
- if [ $# -eq 0 -o " $1 " = run -o " $( echo " $1 " | cut -c1-1 ) " = ' -' ]; then
295
+ if [[ $# = 0 || " $1 " = run || " ${1 : 0 : 1} " = ' -' ] ]; then
305
296
run_influxd=true
306
- elif [ " $1 " = influxd ] && [ $# -eq 1 -o " $2 " = run -o " $( echo " $2 " | cut -c1-1 ) " = ' -' ]; then
297
+ elif [[ " $1 " = influxd && ( $# = 1 || " $2 " = run || " ${2 : 0 : 1} " = ' -' ) ] ]; then
307
298
run_influxd=true
308
299
shift 1
309
300
fi
@@ -324,7 +315,7 @@ function main () {
324
315
create_directories
325
316
326
317
if [ " $( id -u) " = 0 ]; then
327
- exec gosu influxdb " $0 " " ${@ } "
318
+ exec gosu influxdb " $BASH_SOURCE " " ${@ } "
328
319
return
329
320
fi
330
321
0 commit comments