diff --git a/Dockerfile b/Dockerfile index 202c5464e..3fe0ceb79 100644 --- a/Dockerfile +++ b/Dockerfile @@ -30,7 +30,7 @@ COPY --from=base /opt/venv /opt/venv # Add default config files COPY octobot/config /octobot/octobot/config -COPY docker/*.sh /octobot/ +COPY docker/* /octobot/ # 1. Install requirements # 2. Add cloudflare gpg key and add cloudflare repo in apt repositories (from https://pkg.cloudflare.com/index.html) @@ -44,7 +44,7 @@ RUN apt-get update \ && curl -fsSL https://pkg.cloudflare.com/cloudflare-main.gpg | tee /usr/share/keyrings/cloudflare-main.gpg >/dev/null \ && echo 'deb [signed-by=/usr/share/keyrings/cloudflare-main.gpg] https://pkg.cloudflare.com/cloudflared buster main' | tee /etc/apt/sources.list.d/cloudflared.list \ && apt-get update \ - && apt-get install -y --no-install-recommends jq curl cloudflared libxslt-dev libxcb-xinput0 libjpeg62-turbo-dev zlib1g-dev libblas-dev liblapack-dev libatlas-base-dev libopenjp2-7 libtiff-dev \ + && apt-get install -y --no-install-recommends curl cloudflared libxslt-dev libxcb-xinput0 libjpeg62-turbo-dev zlib1g-dev libblas-dev liblapack-dev libatlas-base-dev libopenjp2-7 libtiff-dev \ && rm -rf /var/lib/apt/lists/* \ && ln -s /opt/venv/bin/OctoBot OctoBot # Make sure we use the virtualenv \ && chmod +x docker-entrypoint.sh diff --git a/docker/aws.py b/docker/aws.py new file mode 100644 index 000000000..5208e217c --- /dev/null +++ b/docker/aws.py @@ -0,0 +1,24 @@ +import os +import json +import re +from urllib.request import urlopen + + +def remove_special_chars(input_string): + return re.sub(r'[^a-zA-Z0-9_]', '', input_string) + + +if __name__ == '__main__': + # https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-metadata-endpoint-v4.html + container_metadata_url = os.getenv("ECS_CONTAINER_METADATA_URI_V4", None) + if container_metadata_url is not None: + try: + with urlopen(container_metadata_url + "/taskWithTags") as response: + body = response.read() + container_metadata = json.loads(body) + with open('.env', 'w') as env_file: + for key, value in container_metadata['TaskTags'].items(): + env_file.write(remove_special_chars( + key) + "=" + value+"\n") + except Exception as e: + print("Error when requesting or parsing aws metadata") diff --git a/docker/aws.sh b/docker/aws.sh deleted file mode 100644 index fd566059d..000000000 --- a/docker/aws.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/bash - -source util.sh - -# https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-metadata-endpoint-v4.html -if [[ -n "$ECS_CONTAINER_METADATA_URI_V4" ]]; then - AWS_TASK_WITH_TAGS_DETAILS=$(curl --silent "$ECS_CONTAINER_METADATA_URI_V4/taskWithTags") - AWS_TASK_TAGS=$(echo $AWS_TASK_WITH_TAGS_DETAILS | jq -r ".TaskTags") - json_to_env "$AWS_TASK_TAGS" -fi diff --git a/docker/docker-entrypoint.sh b/docker/docker-entrypoint.sh index a6468853a..3db070980 100755 --- a/docker/docker-entrypoint.sh +++ b/docker/docker-entrypoint.sh @@ -5,7 +5,8 @@ if [[ -n "${OCTOBOT_CONFIG}" ]]; then echo "$OCTOBOT_CONFIG" | tee /octobot/user/config.json >/dev/null fi -bash aws.sh +python aws.py + bash tunnel.sh ./OctoBot diff --git a/docker/tunnel.sh b/docker/tunnel.sh index 64a454ac7..34cd5269a 100644 --- a/docker/tunnel.sh +++ b/docker/tunnel.sh @@ -1,5 +1,7 @@ #!/bin/bash +source .env + export TUNNEL_LOGFILE=/root/cloudflared.log export TUNNEL_LOGLEVEL=info diff --git a/docker/util.sh b/docker/util.sh deleted file mode 100644 index fb5261c96..000000000 --- a/docker/util.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash - -json_to_env() { - for s in $(echo $1 | jq -r 'keys[] as $k | "export \($k)=\(.[$k])"'); do export $s; done -}