#! /bin/bash
: '
Usage:

    USER=root PASSWORD=root ./deploy.sh 192.168.x.x

'

INSTALL_PATH="/data/{{ project_name }}"
IMAGE="ttwshell/hobbit-app:latest"
HOST=$1

commonExclude="*.pyc* .* /.git/* /.test/* /*/__pycache__/* /.pytest_cache/* /logs/* /tests/* zip.sh"
commonInclude="/app/* /migrations/* requirements.txt"
function mzip() {
    serverInclude="docker-compose.yml Dockerfile /configs/*"
    zip -r server.zip . --exclude ${commonExclude} -i ${commonInclude} -i ${serverInclude}
}

function mssh() {
    sshpass -p ${PASSWORD} ssh -o StrictHostKeyChecking=no ${USER}@${HOST} $1
}

mzip server

mssh "sudo mkdir -p ${INSTALL_PATH} && sudo chown -R ${USER}:${USER} ${INSTALL_PATH}"
sshpass -p ${PASSWORD} scp ./server.zip ${USER}@${HOST}:${INSTALL_PATH}
mssh "docker pull ${IMAGE}"
mssh "cd ${INSTALL_PATH} && unzip -o server.zip  -d . && docker-compose down && docker-compose up -d"

