Skip to content

Commit

Permalink
Merge pull request #53 from mdegat01/variable-refactor
Browse files Browse the repository at this point in the history
Move constants up top in scripts
  • Loading branch information
mdegat01 authored Apr 11, 2021
2 parents 4940cd0 + 1c85192 commit 87d1dca
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
34 changes: 20 additions & 14 deletions promtail/rootfs/etc/cont-init.d/promtail_setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@
# Home Assistant Add-on: Promtail
# This file makes the config file from inputs
# ==============================================================================
config_file=/etc/promtail/config.yaml
def_scrape_configs=/etc/promtail/default-scrape-config.yaml
scrape_configs="${def_scrape_configs}"
readonly CONFIG_DIR=/etc/promtail
readonly CONFIG_FILE="${CONFIG_DIR}/config.yaml"
readonly BASE_CONFIG="${CONFIG_DIR}/base_config.yaml"
readonly DEF_SCRAPE_CONFIGS="${CONFIG_DIR}/default-scrape-config.yaml"
readonly CUSTOM_SCRAPE_CONFIGS="${CONFIG_DIR}/custom-scrape-config.yaml"
declare cafile
declare add_stages
declare add_scrape_configs
scrape_configs="${DEF_SCRAPE_CONFIGS}"

bashio::log.info 'Setting base config for promtail...'
cp /etc/promtail/base_config.yaml $config_file
cp "${BASE_CONFIG}" "${CONFIG_FILE}"

# Set up client section
if ! bashio::config.is_empty 'client.username'; then
Expand All @@ -19,7 +25,7 @@ if ! bashio::config.is_empty 'client.username'; then
echo " basic_auth:"
echo " username: $(bashio::config 'client.username')"
echo " password: $(bashio::config 'client.password')"
} >> "${config_file}"
} >> "${CONFIG_FILE}"
fi

if ! bashio::config.is_empty 'client.cafile'; then
Expand All @@ -46,10 +52,10 @@ if ! bashio::config.is_empty 'client.cafile'; then
{
echo " tls_config:"
echo " ca_file: ${cafile}"
} >> "${config_file}"
} >> "${CONFIG_FILE}"

if ! bashio::config.is_empty 'client.servername'; then
echo " server_name: $(bashio::config 'client.servername')" >> "${config_file}"
echo " server_name: $(bashio::config 'client.servername')" >> "${CONFIG_FILE}"
fi

if ! bashio::config.is_empty 'client.certfile'; then
Expand All @@ -72,15 +78,15 @@ if ! bashio::config.is_empty 'client.cafile'; then
{
echo " cert_file: $(bashio::config 'client.certfile')"
echo " key_file: $(bashio::config 'client.keyfile')"
} >> "${config_file}"
} >> "${CONFIG_FILE}"
fi
fi

# Add in scrape configs
{
echo
echo "scrape_configs:"
} >> "${config_file}"
} >> "${CONFIG_FILE}"
if bashio::config.true 'skip_default_scrape_config'; then
bashio::log.info 'Skipping default journald scrape config...'
if ! bashio::config.is_empty 'additional_pipeline_stages'; then
Expand All @@ -94,7 +100,7 @@ if bashio::config.true 'skip_default_scrape_config'; then
elif ! bashio::config.is_empty 'additional_pipeline_stages'; then
bashio::log.info "Adding additional pipeline stages to default journal scrape config..."
add_stages="$(bashio::config 'additional_pipeline_stages')"
scrape_configs=/etc/promtail/journal-scrape-configs.yaml
scrape_configs="${CUSTOM_SCRAPE_CONFIGS}"
if ! bashio::fs.file_exists "${add_stages}"; then
bashio::log.fatal
bashio::log.fatal "The file specified for 'additional_pipeline_stages' does not exist!"
Expand All @@ -105,7 +111,7 @@ elif ! bashio::config.is_empty 'additional_pipeline_stages'; then

yq -NP eval-all \
'select(fi == 0) + [{"add_pipeline_stages": select(fi == 1)}]' \
$def_scrape_configs "${add_stages}" \
"${DEF_SCRAPE_CONFIGS}" "${add_stages}" \
| yq -NP e \
'[(.[0] * .[1]) | {"job_name": .job_name, "journal": .journal, "relabel_configs": .relabel_configs, "pipeline_stages": .pipeline_stages + .add_pipeline_stages}]' \
- > "${scrape_configs}"
Expand All @@ -123,11 +129,11 @@ if ! bashio::config.is_empty 'additional_scrape_configs'; then
fi

if bashio::config.true 'skip_default_scrape_config'; then
yq -NP e '[] + .' "${add_scrape_configs}" >> "${config_file}"
yq -NP e '[] + .' "${add_scrape_configs}" >> "${CONFIG_FILE}"
else
yq -NP eval-all 'select(fi == 0) + select(fi == 1)' \
"${scrape_configs}" "${add_scrape_configs}" >> "${config_file}"
"${scrape_configs}" "${add_scrape_configs}" >> "${CONFIG_FILE}"
fi
else
yq -NP e '[] + .' "${scrape_configs}" >> "${config_file}"
yq -NP e '[] + .' "${scrape_configs}" >> "${CONFIG_FILE}"
fi
5 changes: 3 additions & 2 deletions promtail/rootfs/etc/services.d/promtail/run
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
# Home Assistant Add-on: Promtail
# Runs Promtail
# ==============================================================================
readonly PROMTAIL_CONFIG='/etc/promtail/config.yaml'
declare log_level

bashio::log.info 'Starting Promtail...'
promtail_config='/etc/promtail/config.yaml'

journal_path='/var/log/journal'
if ! bashio::fs.directory_exists "${journal_path}" || [ -z "$(ls -A ${journal_path})" ]; then
Expand All @@ -29,7 +30,7 @@ export "URL=$(bashio::config 'client.url')"
export "JOURNAL_PATH=${journal_path}"
export "LOG_LEVEL=${log_level}"

promtail_args=("-config.expand-env=true" "-config.file=${promtail_config}")
promtail_args=("-config.expand-env=true" "-config.file=${PROMTAIL_CONFIG}")
if [ "${log_level}" == "debug" ]; then
bashio::log.debug "Logging full config on startup for debugging..."
promtail_args+=("-print-config-stderr=true")
Expand Down

0 comments on commit 87d1dca

Please sign in to comment.