#!/usr/bin/env bash

# {{ ansible_managed }}

source {{ credentials_home }}/credentials_env
COMMAND=$1

case $COMMAND in
    start)
        {% set credentials_venv_bin = credentials_home + "/venvs/" + credentials_service_name + "/bin" %}

        {{ supervisor_venv_bin }}/supervisord --configuration {{ supervisor_cfg }}

        cd /edx/app/edx_ansible/edx_ansible/docker/plays
        /edx/app/edx_ansible/venvs/edx_ansible/bin/ansible-playbook credentials.yml -c local -i '127.0.0.1,' \
            -t 'install:app-requirements,assets:gather,devstack,migrate,manage:start' \
            --extra-vars="migrate_db=yes" \
            --extra-vars="@/ansible_overrides.yml"

        # Docker requires an active foreground task. Tail the logs to appease Docker and
        # provide useful output for development.
        cd {{ supervisor_log_dir }}
        tail -f {{ credentials_service_name }}-stderr.log -f {{ credentials_service_name }}-stdout.log

        ;;
    open)
        cd {{ credentials_code_dir }}/
        . {{ credentials_venv_bin }}/activate
        /bin/bash
        ;;
esac
