Skip to content

Instantly share code, notes, and snippets.

@jmoyers
Last active July 15, 2022 12:46
Show Gist options
  • Save jmoyers/c4e8da6fa9119423a08376bda4662fbe to your computer and use it in GitHub Desktop.
Save jmoyers/c4e8da6fa9119423a08376bda4662fbe to your computer and use it in GitHub Desktop.
Automatically parse docker stack yml, hash stack configs with md5, replace environment variables, rotate new config on deploy

This script lets you parse your docker stack yml file for configs, and set some environment variables that will allow you to roll updates to your configs without manually changing names, adding or removing configs manually.

  • Note the yaml parser is not complete (I didn't write it, its from a convenient stackoverflow post, thanks!)
  • Currently dev.yml is hardcoded into the script, fix that!
  • Note the use of source -- we export environment variables here so I believe thats required. Not a super bashlord, so fix it up if you'd like.

Example usage:

jmoyers @ Joshua-PC stack > source hash_configs.sh
hash stack_configs_dashboard_json_file grafana/dashboard.json
stack_configs_dashboard_json_file=d747eadf0a725a14f30c4e2307d44ac8
hash stack_configs_dashboards_yml_file grafana/dashboards.yml
stack_configs_dashboards_yml_file=83c0aa08b5ab6a3ba726a11f667ce55b
hash stack_configs_datasources_yml_file grafana/datasources.yml
stack_configs_datasources_yml_file=61b96f0084f9835e955807627954c39b
hash stack_configs_kibana_yml_file kibana/kibana.yml
stack_configs_kibana_yml_file=df71852a46553e4ee4b49088d7d8225f
hash stack_configs_logstash_conf_file logstash/logstash.conf
stack_configs_logstash_conf_file=ff40d2c146a0f0cdc4db7023e0070b27
hash stack_configs_prometheus_yml_file prometheus/prometheus.yml
stack_configs_prometheus_yml_file=fa39c330491b6354c0ecab35b8581f25
jmoyers @ Joshua-PC stack > docker stack deploy -c dev.yml dev
Creating network dev_overlay
Creating secret dev_dev.opencourse.app-certchain.crt
Creating secret dev_dev.opencourse.app.key
Creating config dashboards83c0aa08b5ab6a3ba726a11f667ce55b
Creating config dashboardd747eadf0a725a14f30c4e2307d44ac8
Creating config kibanadf71852a46553e4ee4b49088d7d8225f
Creating config logstashff40d2c146a0f0cdc4db7023e0070b27
Creating config prometheusfa39c330491b6354c0ecab35b8581f25
Creating config datasources61b96f0084f9835e955807627954c39b
Creating service dev_grafana
Creating service dev_docker-registry
Creating service dev_prometheus
Creating service dev_reverse-proxy
Creating service dev_node-exporter
Creating service dev_docker-registry-mirror
Creating service dev_logstash
Creating service dev_kibana
Creating service dev_npm-registry
Creating service dev_elasticsearch
... snip ...
configs:
prometheus_yml:
file: prometheus/prometheus.yml
name: prometheus${stack_configs_prometheus_yml_file}
... snip ...
prometheus:
image: prom/prometheus
ports:
- 9090:9090
networks:
- overlay
configs:
- source: prometheus_yml
target: /etc/prometheus/prometheus.yml
#!/usr/bin/env bash
# parses dev.yml, finds config files, sets their "name" to a hash
# of their contents so we can rotate configs on stack deploy
# https://stackoverflow.com/questions/5014632/how-can-i-parse-a-yaml-file-from-a-linux-shell-script
# function parses yaml with optional paramter for prefix
function parse_yaml {
local prefix=$2
local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034')
sed -ne "s|^\($s\):|\1|" \
-e "s|^\($s\)\($w\)$s:$s[\"']\(.*\)[\"']$s\$|\1$fs\2$fs\3|p" \
-e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 |
awk -F$fs '{
indent = length($1)/2;
vname[indent] = $2;
for (i in vname) {if (i > indent) {delete vname[i]}}
if (length($3) > 0) {
vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
printf("%s%s%s=\"%s\"\n", "'$prefix'",vn, $2, $3);
}
}'
}
eval $(parse_yaml dev.yml "stack_")
for config in $(set | cut -d= -f1 | grep "stack_configs" | grep "file"); do
echo hash $config ${!config}
temp="$config=$(md5sum ${!config} | awk '{ print $1 }')"
echo $temp
export $temp
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment