Skip to content

Instantly share code, notes, and snippets.

@armandomiani
Created October 10, 2019 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save armandomiani/e376cfdb6eb900160c88350ef77db830 to your computer and use it in GitHub Desktop.
Save armandomiani/e376cfdb6eb900160c88350ef77db830 to your computer and use it in GitHub Desktop.
# Responsible for updating a supervisor configuration
#!/bin/bash
# Responsible for updating a supervisor configuration
# Usage: update_supervisor_config [service] [config] [new_value]
function update_supervisor_config() {
local INPUT_FILE="${SUPERVISOR_CONFIG}"
local SERVICE=$1
local CONFIG=$2
local NEW_VALUE=$3
local TMP_FILE="/tmp/supervisor.conf"
local SERVICE_MATCHED=0
local LINE
local CURRENT_VALUE
# Removes temp file, if is exists
if [[ -f "${TMP_FILE}" ]]; then
rm -f "${TMP_FILE}"
fi
# Iterate over the files' lines
while IFS= read -r LINE
do
# Check if its the selected service
if [[ "${LINE}" =~ ^\[program\:$SERVICE\]$ ]]; then
SERVICE_MATCHED=1
elif [[ "${LINE}" =~ ^\[program\:.*\]$ ]]; then
SERVICE_MATCHED=0
fi
# Check if its the selected config
if [[ ${SERVICE_MATCHED} -eq 1 && ${LINE} =~ ^${CONFIG}=(.*)$ ]]; then
# Gets the current value
CURRENT_VALUE="${BASH_REMATCH[1]}"
# Replace by the new one
LINE=$(echo ${LINE} | sed "s/${CONFIG}=${CURRENT_VALUE}/${CONFIG}=${NEW_VALUE}/")
fi
echo ${LINE} >> ${TMP_FILE}
done < "${INPUT_FILE}"
# Overwrite input file and removes the temp one.
cp "${TMP_FILE}" "${INPUT_FILE}"
rm -f "${TMP_FILE}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment