#!/usr/bin/env bash

set -eou pipefail

# SET ALL THESE VALUES MANUALLY IN DOCKER ENV
# export IMAGE_HASH=c111dcb497e8a66f6cd6652f93999a137c8f6e4e
# export REPO_HASH=2b3671d6ada190c6a7fa41f628d38dcbbb0fc727
# export VERSION_NUM=39  # TODO get these from the pipedream/framework/bin/version_num.py file
# export DB_VERSION_NUM=111
# export SCHEDULING_VERSION_NUM=6
# export AWS_ACCESS_KEY_ID= # INPUT
# export AWS_SECRET_ACCESS_KEY= # INPUT
# export SKIP_TRIAGE= # set to 1 to skip triage
# export USERNAME= # INPUT
# export LDAP_PASSWORD= # INPUT


sudo su - -c "chmod 777 /var/run/docker.sock"

git config --global user.email "kbambridge@zoox.com"
git config --global user.name "Kenneth Bambridge"

git pull origin master
git checkout 2b0479efc8383da5a8b0e25135b6c164bd242d73

source scripts/shell/zooxrc.sh

docker pull docker-registry.zooxlabs.com/driving:$IMAGE_HASH
sudo docker tag docker-registry.zooxlabs.com/driving:$IMAGE_HASH docker-registry.zooxlabs.com/driving:$VERSION_NUM
docker push docker-registry.zooxlabs.com/driving:$VERSION_NUM


vault login -method ldap username=$USERNAME password=$LDAP_PASSWORD

if [ -z "$SKIP_TRIAGE" ];
then
  cat pipedream/pipelines/triage.json | jq  ".job[0].image = \"docker-registry.zooxlabs.com/driving:$VERSION_NUM\"" | cat  > testjob.json

  mv testjob.json pipedream/pipelines/triage.json
  bazel_run //pipedream/api:schedule -- --pipeline_file=pipedream/pipelines/triage.json 2>&1 > out.txt
  export PIPE_ID=`cat out.txt | sed  's?Track the status of this pipeline at https://piper.zooxlabs.com/pipeline/\(.*\)$?\1?' | head -1`
  # TODO: make this a --wait flag that exits on completed (0) or error (1)
  until brun //pipedream/tools:pipeline_status -- --pipe_id=$PIPE_ID | grep -m 1 "completed"; do sleep 1 ; done
fi;

sed -i "s/\(DOCKER_DRIVING_TAG\)=[0-9]*/\1=$VERSION_NUM/" pipedream/framework/bin/version_num.py

if ! [ -d "/mnt/flashblade/pipedream/bin/v$DB_VERSION_NUM" ];
then
  python pipedream/framework/scripts/deploy_pipedream.py
else
  sed -i "s/\(PIPEDREAM_VERSION\)=[0-9]*/\1=$DB_VERSION_NUM/" pipedream/framework/bin/version_num.py
fi

git add pipedream/framework/bin/version_num.py


# get version number from previous command $DB_VERSION_NUM
cd infra/ansible
ansible-galaxy install --roles-path plugins/lookup --force --role-file /home/zoox/driving/scripts/ansible/requirements-plugins.yaml
echo "prestaging driving image"
if [ "`ANSIBLE_HOST_KEY_CHECKING=false ansible-playbook -i inventory/slurm/production --extra-vars image=docker-registry.zooxlabs.com/driving:$VERSION_NUM slurm_pull_image.yml | grep 'failed=' | sed 's/.*failed=\([0-9]*\)/\1/g' | awk '{s+=$1} END {print s}'` " -gt "10" ]; 
then
  echo "Too many prestage docker pulls failed"
  exit 1
fi

cd -

# Separate job

web_apps/pipedream_dashboard/dockerize.sh
docker pull docker-registry.zooxlabs.com/pipedream_dashboard:$DB_VERSION_NUM && echo "Redeploying old dashboard version" || echo "DB version doesn't exist" # verify the image doesn't exist
docker tag docker-registry.zooxlabs.com/pipedream_dashboard docker-registry.zooxlabs.com/pipedream_dashboard:$DB_VERSION_NUM
docker push docker-registry.zooxlabs.com/pipedream_dashboard:$DB_VERSION_NUM

cat infra/terraform/pipedream_dashboard/main.tf | sed "s/\(\s*docker_tag\s*\=\s*\"\)[0-9]*\"/\1$DB_VERSION_NUM\"/" > tmp.tf
mv tmp.tf infra/terraform/pipedream_dashboard/main.tf # find replace script to update version
git add infra/terraform/pipedream_dashboard/main.tf

terraform workspace new production

cd infra/terraform/pipedream_dashboard &&  \
terraform init && \
terraform workspace select production && \
terraform plan
terraform apply

cd -

# Separate job
pipedream/scheduling/dockerize.sh
cat infra/terraform/pipedream_scheduling/main.tf | sed "s/\(\s*docker_tag\s*\=\s*\"\)[0-9]*\"/\1$SCHEDULING_VERSION_NUM\"/" > tmp.tf
mv tmp.tf infra/terraform/pipedream_scheduling/main.tf # find replace script to update version
git add infra/terraform/pipedream_scheduling/main.tf

docker pull docker-registry.zooxlabs.com/pipedream_scheduling:$SCHEDULING_VERSION_NUM && echo "Redeploying same scheduling version" || echo "DB version doesn't exist" # verify the image doesn't exist
docker tag docker-registry.zooxlabs.com/pipedream_scheduling docker-registry.zooxlabs.com/pipedream_scheduling:$SCHEDULING_VERSION_NUM
docker push docker-registry.zooxlabs.com/pipedream_scheduling:$SCHEDULING_VERSION_NUM

cd infra/terraform/pipedream_scheduling && \
terraform init && \
terraform workspace select production && \
terraform apply


git checkout -B pipedream_deploy && git add . && git commit -m "pipedream deploy" && git push origin pipedream_deploy

git tag driving-image-$VERSION_NUM $IMAGE_HASH
git tag pipedream-dasbhoard-$DB_VERSION_NUM $REPO_HASH
git push origin --tags

