-
Notifications
You must be signed in to change notification settings - Fork 2
/
start.sh
101 lines (91 loc) · 4.14 KB
/
start.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
#!/usr/bin/env bash
# shellcheck disable=SC2034,SC2086
set -ae
function prepare_source_balena() {
if [ -n "$BALENA_DEVICE_UUID" ]; then
mkdir -p sources transforms
envsubst <templates/sources/journald.yaml.template >sources/journald.yaml
envsubst <templates/transforms/source-balena.yaml.template >transforms/source-balena.yaml
fi
}
function prepare_source_kubernetes() {
if [ -n "$KUBERNETES_SERVICE_HOST" ]; then
VECTOR_BUFFER_TYPE=${VECTOR_BUFFER_TYPE:-disk}
VECTOR_BUFFER_WHEN_FULL=${VECTOR_BUFFER_WHEN_FULL:-block}
mkdir -p sources
envsubst <templates/sources/source-kubernetes.yaml.template >sources/source-kubernetes.yaml
fi
}
function write_to_file() {
VARIABLE=$1
FILE_PATH=$2
SUBSTITUTED_VARIABLE=$(echo -n $VARIABLE | envsubst)
if [[ -f "${SUBSTITUTED_VARIABLE}" ]]; then
# Copy the file if a path is set as value
cp -f "${SUBSTITUTED_VARIABLE}" "${FILE_PATH}"
else
# Write the variable value into file if in base64 or multi-line string
echo "${VARIABLE}" | base64 -d >"${FILE_PATH}" 2>/dev/null \
|| echo "${SUBSTITUTED_VARIABLE}" >"${FILE_PATH}"
fi
}
function prepare_sink_vector() {
mkdir -p sinks
# Set default values here
VECTOR_ACKNOWLEDGEMENTS_ENABLED=${VECTOR_ACKNOWLEDGEMENTS_ENABLED:-true}
VECTOR_BUFFER_TYPE=${VECTOR_BUFFER_TYPE:-memory}
VECTOR_BUFFER_WHEN_FULL=${VECTOR_BUFFER_WHEN_FULL:-drop_newest}
# Temporarily handle old environment variable names
test -n "$VECTOR_BUFFER_MAX_EVENTS" && VECTOR_BUFFER_MEMORY_MAX_EVENTS=${VECTOR_BUFFER_MAX_EVENTS}
test -n "$VECTOR_BUFFER_MAX_SIZE" && VECTOR_BUFFER_DISK_MAX_SIZE=${VECTOR_BUFFER_MAX_SIZE}
VECTOR_BUFFER_MEMORY_MAX_EVENTS=${VECTOR_BUFFER_MEMORY_MAX_EVENTS:-1000}
VECTOR_BUFFER_DISK_MAX_SIZE=${VECTOR_BUFFER_DISK_MAX_SIZE:-268435488}
VECTOR_COMPRESSION_ENABLED=${VECTOR_COMPRESSION_ENABLED:-true}
VECTOR_REQUEST_TIMEOUT_SECS=${VECTOR_REQUEST_TIMEOUT_SECS:-300}
# Prepare the configuration file
if [ -n "${VECTOR_ENDPOINT}" ]; then
export VECTOR_ENDPOINT=$(echo -n "${VECTOR_ENDPOINT}" | envsubst)
if [ -n "${VECTOR_TLS_CA_FILE}" ]; then
VECTOR_TLS_ENABLED=true
VECTOR_TLS_VERIFY_CERTIFICATE=${VECTOR_TLS_VERIFY_CERTIFICATE:-true}
VECTOR_TLS_VERIFY_HOSTNAME=${VECTOR_TLS_VERIFY_HOSTNAME:-true}
envsubst <templates/sinks/sink-vector.yaml.template >sinks/sink-vector.yaml
write_to_file "${VECTOR_TLS_CA_FILE}" "${CERTIFICATES_DIR}/ca.pem"
sed -i 's|#ca_file:|ca_file:|g' sinks/sink-vector.yaml
if [ -n "${VECTOR_TLS_CRT_FILE}" ] && [ -n "${VECTOR_TLS_KEY_FILE}" ]; then
write_to_file "${VECTOR_TLS_CRT_FILE}" "${CERTIFICATES_DIR}/client.pem"
write_to_file "${VECTOR_TLS_KEY_FILE}" "${CERTIFICATES_DIR}/client-key.pem"
sed -i 's|#crt_file:|crt_file:|g' sinks/sink-vector.yaml
sed -i 's|#key_file:|key_file:|g' sinks/sink-vector.yaml
sed -i 's|#verify_certificate:|verify_certificate:|g' sinks/sink-vector.yaml
sed -i 's|#verify_hostname:|verify_hostname:|g' sinks/sink-vector.yaml
fi
else
VECTOR_TLS_ENABLED=false
envsubst <templates/sinks/sink-vector.yaml.template >sinks/sink-vector.yaml
fi
if [[ "${VECTOR_BUFFER_TYPE}" == 'disk' ]]; then
sed -i 's|#max_size:|max_size:|g' sinks/sink-vector.yaml
else
sed -i 's|#max_events:|max_events:|g' sinks/sink-vector.yaml
fi
fi
}
function start_vector() {
# https://vector.dev/docs/administration/validating/
find /etc/vector -name "*.y*ml" -exec cat {} \;
vector validate --config-dir /etc/vector &&
vector --config-dir /etc/vector
}
if [[ "$DISABLED" =~ true|True|TRUE|yes|Yes|YES|on|On|ON|1 ]]; then
echo 'logs-to-vector has been disabled. This service is now idle.'
sleep infinity
else
BALENA_FLEET_NAME=${BALENA_APP_NAME}
CERTIFICATES_DIR=/etc/vector/certificates
cd /etc/vector
prepare_source_balena
prepare_source_kubernetes
prepare_sink_vector
start_vector
fi