Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix environment variables sharing #151

Merged
merged 11 commits into from
Jan 25, 2023
7 changes: 4 additions & 3 deletions bin/compile
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ chmod +x "${BUILD_DIR}/.profile.d/00-test-endpoint.sh"
chmod +x "${BUILD_DIR}/.profile.d/02-redirect-logs.sh"
chmod +x "${BUILD_DIR}/.profile.d/01-run-datadog.sh"

# Export all environment variables to a file that scripts can use later
# We need to handle quotes and comments; sed command found in https://gist.github.com/mihow/9c7f559807069a03e302605691f85572
printenv | sed -e '/^#/d;/^\s*$/d' -e "s/'/'\\\''/g" -e "s/=\(.*\)/='\1'/g" > "${BUILD_DIR}/.datadog/.raw_datadog_env"
# export DD environment variables
. "${DATADOG_DIR}/scripts/utils.sh"
dd_export_env "${DATADOG_DIR}/.raw_datadog_env"

# sanitize env vars and export a new a env file
python "${DATADOG_DIR}/scripts/parse_env_vars.py" "${DATADOG_DIR}/.raw_datadog_env" "${DATADOG_DIR}/.datadog_env"
Expand All @@ -69,5 +69,6 @@ DD_TAGS=$(python "${DATADOG_DIR}"/scripts/get_tags.py)
export DD_TAGS
DD_DOGSTATSD_TAGS=$(python "${DATADOG_DIR}"/scripts/get_tags.py)
export DD_DOGSTATSD_TAGS

# mark the script as finished, useful to sync the update_agent_config script
touch "${DATADOG_DIR}/.setup_completed"
6 changes: 3 additions & 3 deletions bin/supply
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ chmod +x "${BUILD_DIR}/.profile.d/00-test-endpoint.sh"
chmod +x "${BUILD_DIR}/.profile.d/02-redirect-logs.sh"
chmod +x "${BUILD_DIR}/.profile.d/01-run-datadog.sh"

# Export all environment variables to a file that scripts can use later
# We need to handle quotes and comments; sed command found in https://gist.github.com/mihow/9c7f559807069a03e302605691f85572
printenv | sed -e '/^#/d;/^\s*$/d' -e "s/'/'\\\''/g" -e "s/=\(.*\)/='\1'/g" > "${BUILD_DIR}/.datadog/.raw_datadog_env"
# export DD environment variables
. "${DATADOG_DIR}/scripts/utils.sh"
dd_export_env "${DATADOG_DIR}/.raw_datadog_env"

# sanitize env vars and export a new a env file
python "${DATADOG_DIR}/scripts/parse_env_vars.py" "${DATADOG_DIR}/.raw_datadog_env" "${DATADOG_DIR}/.datadog_env"
Expand Down
25 changes: 10 additions & 15 deletions lib/run-datadog.sh
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,13 @@ start_datadog() {
date +%s > "${DATADOG_DIR}/startup_time"
echo "setting up datadog"
setup_datadog
FIRST_RUN=false
else
if [ -f "${DATADOG_DIR}/.sourced_datadog_env" ]; then
echo "sourcing .sourced_datadog_env file"
source "${DATADOG_DIR}/.sourced_datadog_env"
safe_source "${DATADOG_DIR}/.sourced_datadog_env"
elif [ -f "${DATADOG_DIR}/.datadog_env" ]; then
echo "sourcing .datadog_env file"
source "${DATADOG_DIR}/.datadog_env"
safe_source "${DATADOG_DIR}/.datadog_env"
fi
fi

Expand Down Expand Up @@ -170,11 +169,14 @@ start_datadog() {
fi
echo $! > run/dogstatsd.pid
fi
echo "Starting trace agent"
if [ "${SUPPRESS_DD_AGENT_OUTPUT}" = "true" ]; then
./trace-agent --config dist/datadog.yaml --pid run/trace-agent.pid > /dev/null 2>&1 &
else
./trace-agent --config dist/datadog.yaml --pid run/trace-agent.pid &
if [ "${FIRST_RUN}" = "true" ]; then
echo "Starting trace agent"
if [ "${SUPPRESS_DD_AGENT_OUTPUT}" = "true" ]; then
./trace-agent --config dist/datadog.yaml --pid run/trace-agent.pid > /dev/null 2>&1 &
else
./trace-agent --config dist/datadog.yaml --pid run/trace-agent.pid &
fi
FIRST_RUN=false
fi
popd
}
Expand All @@ -196,13 +198,6 @@ stop_datadog() {
find_pid_kill_and_wait "${DOGSTATSD_CMD}" "${DOGSTATSD_PIDFILE}" 5 1
rm -f "${DOGSTATSD_PIDFILE}"
fi

if check_if_running "${TRACE_AGENT_PIDFILE}" "${TRACE_AGENT_CMD}"; then
echo "Stopping trace agent process, pid: $(cat "${TRACE_AGENT_PIDFILE}")"
kill_and_wait "${TRACE_AGENT_PIDFILE}" 5 1
find_pid_kill_and_wait "${TRACE_AGENT_CMD}" "${TRACE_AGENT_PIDFILE}" 5 1
rm -f "${TRACE_AGENT_PIDFILE}"
fi
popd
}

Expand Down
7 changes: 6 additions & 1 deletion lib/scripts/parse_env_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@
name, value = env_parts
if name not in ["DD_TAGS", "TAGS"]:
value = value.replace(" ", "_")
new_env_file.write("export {}={}\n".format(name, value))

# skip empty env vars
if value == "''":
continue

new_env_file.write("{}={}\n".format(name, value))
else:
print("Destination file not specified")
else:
Expand Down
4 changes: 2 additions & 2 deletions lib/scripts/update_agent_config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ main() {
done

. "${DATADOG_DIR}/scripts/utils.sh"
. "${DATADOG_DIR}/.datadog_env"
safe_source "${DATADOG_DIR}/.datadog_env"

if [ "${DD_ENABLE_CAPI_METADATA_COLLECTION}" != "true" ]; then
log_info "update script aborted. set DD_ENABLE_CAPI_METADATA_COLLECTION to true to enable metadata tags collection"
Expand Down Expand Up @@ -91,7 +91,7 @@ main() {

# finishing up
log_info "exporting .sourced_datadog_env file"
export > "${DATADOG_DIR}/.sourced_datadog_env"
dd_export_env "${DATADOG_DIR}/.sourced_datadog_env"

# mark to the monit_datadog function in run-datadog.sh that the script is finished
log_info "creating tags_updated file"
Expand Down
65 changes: 60 additions & 5 deletions lib/scripts/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,73 @@ export TRACE_AGENT_CMD="./trace-agent --config dist/datadog.yaml --pid run/trace
export DOGSTATSD_PIDFILE="${DATADOG_DIR}/run/dogstatsd.pid"
export DOGSTATSD_CMD="./dogstatsd start --cfgpath dist/"

log_info() {
log_message "$0" "$$" "$@" "INFO"
# the logging level of the update_agent_script.log file
export DD_UPDATE_SCRIPT_LOG_LEVEL="${DD_UPDATE_SCRIPT_LOG_LEVEL:-"INFO"}"

dd_export_env() {
local env_file="$1"

if [ -n "${DD_ENABLE_CAPI_METADATA_COLLECTION}" ]; then
echo "export DD_ENABLE_CAPI_METADATA_COLLECTION='${DD_ENABLE_CAPI_METADATA_COLLECTION}'" > "${env_file}"
fi
if [ -n "${DD_TAGS}" ]; then
echo "export DD_TAGS='${DD_TAGS}'" >> "${env_file}"
fi
if [ -n "${DD_DOGSTATSD_TAGS}" ]; then
echo "export DD_DOGSTATSD_TAGS='${DD_DOGSTATSD_TAGS}'">> "${env_file}"
fi
if [ -n "${LOGS_CONFIG_DIR}" ]; then
echo "export LOGS_CONFIG_DIR='${LOGS_CONFIG_DIR}'" >> "${env_file}"
fi
if [ -n "${LOGS_CONFIG}" ]; then
echo "export LOGS_CONFIG='${LOGS_CONFIG}'" >> "${env_file}"
fi
if [ -n "${VCAP_APPLICATION}" ]; then
echo "export VCAP_APPLICATION='${VCAP_APPLICATION}'" >> "${env_file}"
fi
if [ -n "${CF_INSTANCE_IP}" ]; then
echo "export CF_INSTANCE_IP='${CF_INSTANCE_IP}'" >> "${env_file}"
fi
if [ -n "${CF_INSTANCE_GUID}" ]; then
echo "export CF_INSTANCE_GUID='${CF_INSTANCE_GUID}'" >> "${env_file}"
fi
if [ -n "${TAGS}" ]; then
echo "export TAGS='${TAGS}'" >> "${env_file}"
fi
if [ -n "${DD_UPDATE_SCRIPT_WARMUP}" ]; then
echo "export DD_UPDATE_SCRIPT_WARMUP='${DD_UPDATE_SCRIPT_WARMUP}'" >> "${env_file}"
fi
if [ -n "${DD_UPDATE_SCRIPT_LOG_LEVEL}" ]; then
echo "export DD_UPDATE_SCRIPT_LOG_LEVEL='${DD_UPDATE_SCRIPT_LOG_LEVEL}'" >> "${env_file}"
fi
}

log_debug() {
log_message "$0" "$$" "$@" "DEBUG"
safe_source() {
local source_file="$1"

while IFS= read -r line; do
eval "$line";
done < "${source_file}"
}

log_error() {
log_message "$0" "$$" "$@" "ERROR" 1>&2
}

log_info() {
if [ "${DD_UPDATE_SCRIPT_LOG_LEVEL}" = "DEBUG" ] || [ "${DD_UPDATE_SCRIPT_LOG_LEVEL}" = "INFO" ]; then
log_message "$0" "$$" "$@" "INFO"
fi
}

log_debug() {
if [ "${DD_UPDATE_SCRIPT_LOG_LEVEL}" = "DEBUG" ]; then
log_message "$0" "$$" "$@" "DEBUG"
fi
}



log_message() {
local component="${1#/home/vcap/app/}"
local pid="$2"
Expand All @@ -43,7 +98,7 @@ log_message() {
check_if_running() {
local pidfile="$1"
local command="${2:-none}"

if [ -f "${pidfile}" ]; then
kill -0 "$(cat "${pidfile}")" > /dev/null
else
Expand Down