#!/bin/bash
source $1

usage() {
    echo "Usage: run-lxc-playbook.sh path/to/env"
}

if [[ -z "$SERVER" ]]; then
  echo "You must defined SERVER"
  usage
  exit 1
fi

if [[ -z "$LXC_HOST" ]]; then
  echo "You must defined LXC_HOST"
  usage
  exit 1
fi

if [[ -z "$PLAYBOOK" ]]; then
  echo "You must defined PLAYBOOK"
  usage
  exit 1
fi

PLAYBOOK_FILEPATH="playbooks/$PLAYBOOK.yml"
if [[ ! -f "$PLAYBOOK_FILEPATH" ]]; then
  echo "Playbook file is not exists: $PLAYBOOK_FILEPATH"
  usage
  exit 1
fi

if [[ -z "$SITE_NAME" ]]; then
  echo "You must defined SITE_NAME"
  usage
  exit 1
fi

if [[ -z "$DOMAIN_NAME" ]]; then
  echo "You must defined DOMAIN_NAME"
  usage
  exit 1
fi

if [[ -z "$DATABASE_NAME" ]]; then
  echo "You must defined DATABASE_NAME"
  usage
  exit 1
fi

ansible-playbook \
  -e "lxc_host=$LXC_HOST" \
  -e "site_name=$SITE_NAME" \
  -e "domain_name=$DOMAIN_NAME" \
  -e "database_name=$DATABASE_NAME" \
  -e "runner=lxc" \
  --ssh-common-args="-o ProxyCommand='ssh -W %h:%p -q root@$SERVER'" \
  $PLAYBOOK_FILEPATH