From 7905e9d71ef2b822a472029fa223d442392769c4 Mon Sep 17 00:00:00 2001 From: Jay Huynh Date: Thu, 11 May 2023 02:31:14 +1000 Subject: [PATCH] Added docker log --- scripts/build-container.sh | 10 ++++++++++ scripts/run-container.sh | 21 ++++++++++++++++++--- scripts/sync-volumes.sh | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 3 deletions(-) create mode 100755 scripts/sync-volumes.sh diff --git a/scripts/build-container.sh b/scripts/build-container.sh index dc516abc7..b0e88e714 100755 --- a/scripts/build-container.sh +++ b/scripts/build-container.sh @@ -19,7 +19,17 @@ cd $REPO_DIR/app docker exec -t $(docker ps -a -q --filter ancestor="$APP_NAME" --format="{{.ID}}") bash -c 'pkill -SIGINT -f python3 && sleep 5' || true +echo "Wait for 10 seconds for the container to stop" +# wait for 10 seconds for the container to stop +sleep 10 + +docker exec -t $(docker ps -a -q --filter ancestor="$APP_NAME" --format="{{.ID}}") bash -c 'pkill -SIGINT -f python3 && sleep 5' || true echo "Send SIGINT to all containers with image tag: $APP_NAME" + +echo "Wait for 10 seconds for the container to stop" +# wait for 10 seconds for the container to stop +sleep 10 + docker kill -s SIGINT $(docker ps -a -q --filter ancestor="$APP_NAME" --format="{{.ID}}") || true diff --git a/scripts/run-container.sh b/scripts/run-container.sh index 311baad19..190d6c2ba 100644 --- a/scripts/run-container.sh +++ b/scripts/run-container.sh @@ -11,9 +11,24 @@ docker run -d --rm --init -v $APP_NAME-volume:/mephisto/data \ -e APP_ENV=$APP_ENV \ $APP_NAME; -timeout 30m \ - sh -c "while ! docker logs -f $(docker ps -q --filter ancestor=$APP_NAME --format="{{.ID}}")| grep -q '$M_TURK_PREVIEW_URL_PREFIX'; \ +container_id=$(docker ps -q --filter ancestor=$APP_NAME --format="{{.ID}}"); + +if [ -z "$container_id" ] +then + echo "Container $APP_NAME failed to start"; + exit 1; +fi + +mkdir -p ~/logs/$APP_NAME/; + +echo "Streaming logs from container $container_id to file ~/logs/$APP_NAME/$container_id-$(date +%s).log"; +nohup docker logs -f $container_id > ~/logs/$APP_NAME/$container_id-$(date +%s).log & + +echo "Waiting for MTurk preview URL: "; +timeout 1800 \ + sh -c "while ! docker logs -f $container_id| grep -q '$M_TURK_PREVIEW_URL_PREFIX'; \ do sleep 1; done"; -docker logs $(docker ps -q --filter ancestor=$APP_NAME --format="{{.ID}}"); +echo "MTurk preview URL: "; +docker logs $container_id; \ No newline at end of file diff --git a/scripts/sync-volumes.sh b/scripts/sync-volumes.sh new file mode 100755 index 000000000..6001806a6 --- /dev/null +++ b/scripts/sync-volumes.sh @@ -0,0 +1,37 @@ +#!/bin/bash +# Arg 1: $SYNC_INTERVAL, default 60 seconds +# Arg 2: $S3_BUCKET_NAME, default s3://mephisto-data +# Arg 3: $LOCAL_DIR, default current directory + +SYNC_INTERVAL=${1:-60} +LOCAL_DIR=${2:-.} +S3_BUCKET_NAME=${3:-s3://mephisto-data} +AWS_ACCESS_KEY_ID=${4:-""} +AWS_SECRET_ACCESS_KEY=${5:-""} + +trap "echo '[$(date)] Exit syncing script!'; exit;" SIGINT SIGTERM + +if [ -z "$AWS_ACCESS_KEY_ID" ] +then + echo "AWS_ACCESS_KEY_ID not set"; + exit 1; +fi + +if [ -z "$AWS_SECRET_ACCESS_KEY" ] +then + echo "AWS_SECRET_ACCESS_KEY not set"; + exit 1; +fi + +export AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID +export AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY + +while true; do + echo "[$(date)] Syncing $LOCAL_DIR to $S3_BUCKET_NAME!" + aws s3 sync $LOCAL_DIR $S3_BUCKET_NAME + + echo "[$(date)] Sync complete!" + sleep $SYNC_INTERVAL +done + +# echo when interrupted \ No newline at end of file