#!/bin/bash
MODE=$1
if [[ -z $2 ]];then HEADTAIL_LIMIT=0; else HEADTAIL_LIMIT=$2;fi
cd "$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )/../.."
source scripts/funcs

echo "## Building, running, and pushing a Docker image

We will continue using 04_install project (exchange rates to CSV).
Instead of running the task on the host machine, let's run on Docker.
"

Prompt

d=`which docker`
if [[ -z "$d" ]]
then
echo "
Looks like you don't have Docker installed.
Please follow this web page to install Docker:

    https://docs.docker.com/get-docker/
"
Prompt
fi

CheckAws

if [[ -z "$AWS_PROFILE" ]]
then
ChooseAwsProfile
fi

echo "
Let's build a Docker image.

Try running the following command. Enter y when prompted at the beginning.
The build may take 5~10 minutes.
"

Command "handoff container build -p 04_install"

echo "
Now let's run the code in the Docker container.
"

Command "handoff container run -p 04_install --envs __VARS='start_date=\$(date -I -d \"-7 day\")'"

BUCKET_URL=https://s3.console.aws.amazon.com/s3/buckets
BUCKET=`handoff -p 04_install envs get -v key=HO_BUCKET -l critical`
TASK=`handoff -p 04_install envs get -v key=HO_TASK -l critical`

echo '
Confirm the run by checking the logs. Also check the artifacts on S3:
```shell
'

echo "
    $BUCKET/$TASK/artifacts/last
"

echo '
```

directory.
'

Prompt

echo "
Now that we know the Docker container runs fine, let's push it to
AWS Elastic Container Registry. This may take a few minutes.
"

Command "handoff container push -p 04_install"

REGION=`handoff -p 04_install envs get -v key=AWS_REGION -l critical`
echo "
Confirm that the Docker image has been uploaded to:

https://console.aws.amazon.com/ecr/repositories?region=$REGION
"

Prompt

Thanks

echo "
Now that the Docker image is prepared, we will finally deploy the task on
AWS Fargate in the next tutorial.
"

Continue scripts/aws_get_started/07_fargate
