#!/bin/sh set -e set -u set -x usage() { cat < status_data <&2 ./manage.py initial_datas if [ "${OIDC_ORGS:-}" ]; then config_oidc4vp fi fi } _set() { key="${1}" value="${2}" domain="${3}" sqlite3 db.sqlite3 "update oidc4vp_organization set ${key}='${value}' where domain='${domain}';" } _get() { sqlite3 -json db.sqlite3 "select * from oidc4vp_organization;" } _lines () { local myfile="${1}" cat "${myfile}" | wc -l } config_oidc4vp() { # populate your config data="$(_get)" echo "${data}" | jq --arg domain "${DOMAIN}" '{ ($domain): .}' > /sharedsecret/${DOMAIN} while true; do echo wait the other idhubs to write, this is the only oportunity to sync with other idhubs in the docker compose ## break when no empty files left if ! wc -l /sharedsecret/* | awk '{print $1;}' | grep -qE '^0$'; then break fi sleep 1 done # get other configs for host in /sharedsecret/*; do # we are flexible on querying for DOMAIN: the first one based on regex target_domain="$(cat "${host}" | jq -r 'keys[0]')" if [ "${target_domain}" != "${DOMAIN}" ]; then filtered_data="$(cat "${host}" | jq --arg domain "${DOMAIN}" 'first(.[][] | select(.domain | test ($domain)))')" client_id="$(echo "${filtered_data}" | jq -r '.client_id')" client_secret="$(echo "${filtered_data}" | jq -r '.client_secret')" _set my_client_id ${client_id} ${target_domain} _set my_client_secret ${client_secret} ${target_domain} fi done } runserver() { if [ ! "${DEBUG:-}" = "true" ]; then ./manage.py collectstatic gunicorn --access-logfile - --error-logfile - --workers 4 -b :${PORT} trustchain_idhub.wsgi:application else ./manage.py runserver 0.0.0.0:${PORT} fi } check_app_is_there() { if [ ! -f "./manage.py" ]; then usage fi } main() { idhub_dir='/opt/idhub' cd "${idhub_dir}" check_app_is_there deployment_strategy inject_env_vars runserver } main "${@}"