#!/bin/bash

## When writing relation script, remember:
##  - they should be idempotents
##  - they can be launched while the dockers is already up
##  - they are launched from the host
##  - the target of the link is launched first, and get a chance to ``relation-set``
##  - both side of the scripts get to use ``relation-get``.

USER=$(relation-get user)
PASSWORD="$(relation-get password 2>/dev/null)" || PASSWORD="$(gen_password)"

config-add "\
services:
  $TARGET_SERVICE_NAME:
    environment:
      POSTGRES_USER: $USER
      POSTGRES_PASSWORD: $PASSWORD
"

relation-set password "$PASSWORD"