#!/bin/bash
ENVIRONMENT=$1

# AWS command opts
TASK_FAMILY="$ENVIRONMENT-wt-search-api"
SERVICE_NAME="$ENVIRONMENT-wt-search-api"
AWS_REGION="eu-west-1"

# container setup options
LATEST_TAG=`git describe --abbrev=0 --tags`

# container startup options
WT_CONFIG=$ENVIRONMENT

# Change port, environment variables, memoryReservation and cpu to be in line with your task
# definition.
TASK_DEF="[{\"portMappings\": [{\"hostPort\": 0,\"protocol\": \"tcp\",\"containerPort\": 1918}],
   \"logConfiguration\": {
      \"logDriver\": \"awslogs\",
      \"options\": {
        \"awslogs-group\": \"shared-docker-cluster-t3\",
        \"awslogs-region\": \"$AWS_REGION\",
        \"awslogs-stream-prefix\": \"$ENVIRONMENT-wt-search-api\"
      }
    },
    \"environment\": [
      {
        \"name\": \"WT_CONFIG\",
        \"value\": \"$WT_CONFIG\"
      },
      {
        \"name\": \"BASE_URL\",
        \"value\": \"https://$WT_CONFIG-search-api.windingtree.com\"
      },
      {
        \"name\": \"READ_API_URL\",
        \"value\": \"https://$WT_CONFIG-api.windingtree.com\"
      }
    ],
    \"image\": \"docker.io/windingtree/wt-search-api:$LATEST_TAG\",
    \"name\": \"wt-search-api\",
    \"memoryReservation\": 64,
    \"cpu\": 128
  }]"

echo "Updating task definition"
aws ecs register-task-definition --region $AWS_REGION --family $TASK_FAMILY --container-definitions "$TASK_DEF" > /dev/null
echo "Updating service"
aws ecs update-service --region $AWS_REGION --cluster shared-docker-cluster-t3 --service "$SERVICE_NAME" --task-definition "$TASK_FAMILY" > /dev/null
