diff --git a/frontend/src/pages/train/[train_space_id].tsx b/frontend/src/pages/train/[train_space_id].tsx index 86e4ee45..76f88274 100644 --- a/frontend/src/pages/train/[train_space_id].tsx +++ b/frontend/src/pages/train/[train_space_id].tsx @@ -51,6 +51,7 @@ const TrainSpace = () => { const router = useRouter(); useEffect(() => { if (router.isReady && !user) { + console.log("redirect to login") router.replace({ pathname: "/login" }); } }, [user, router.isReady]); diff --git a/training/Dockerfile b/training/Dockerfile index 57c571f0..e7631e2c 100644 --- a/training/Dockerfile +++ b/training/Dockerfile @@ -1,16 +1,12 @@ -# pull base image -FROM condaforge/miniforge3 +FROM condaforge/miniforge3 AS base -WORKDIR /usr/src/training - -# set environment variables +WORKDIR /usr/src/build # install dependencies RUN mamba create --name dlp -y COPY environment.yml pyproject.toml poetry.lock ./ RUN mamba run --live-stream -n dlp mamba env update --file environment.yml --prune -RUN mamba run --live-stream -n dlp poetry install # create directory for the app user RUN mkdir -p /home/app @@ -19,18 +15,39 @@ RUN mkdir -p /home/app RUN addgroup --system app && adduser --system --group app # create the appropriate directories -ENV HOME=/home/app -ENV APP_HOME=/home/app/web +ARG HOME=/home/app +ARG APP_HOME=/home/app/training RUN mkdir $APP_HOME + + + +######################## development target ######################## +FROM base AS development +ENV ENVIRONMENT development + +RUN mamba run --live-stream -n dlp poetry install + + +# we previously mounted training/ to APP_HOME WORKDIR $APP_HOME +RUN chown -R app:app $APP_HOME +USER app + +CMD mamba run --live-stream -n dlp poetry run python manage.py runserver 0.0.0.0:8000 + + + +######################## production target ######################### +FROM base AS production +ENV ENVIRONMENT production + +RUN mamba run --live-stream -n dlp poetry install --without dev # copy project +WORKDIR $APP_HOME COPY . $APP_HOME -# chown all the files to the app user RUN chown -R app:app $APP_HOME - -# change to the app user USER app -CMD mamba run --live-stream -n dlp poetry run python manage.py runserver 0.0.0.0:8000 \ No newline at end of file +CMD mamba run --live-stream -n dlp gunicorn training.wsgi:application --bind 0.0.0.0:8000 \ No newline at end of file diff --git a/training/Dockerfile.prod b/training/Dockerfile.prod deleted file mode 100644 index ff9dc351..00000000 --- a/training/Dockerfile.prod +++ /dev/null @@ -1,36 +0,0 @@ -# pull base image -FROM condaforge/miniforge3 - -WORKDIR /usr/src/training - -# set environment variables - -# install dependencies -RUN mamba create --name dlp -y - -COPY environment.yml pyproject.toml poetry.lock ./ -RUN mamba run --live-stream -n dlp mamba env update --file environment.yml --prune -RUN mamba run --live-stream -n dlp poetry install --without dev - -# create directory for the app user -RUN mkdir -p /home/app - -# create the app user -RUN addgroup --system app && adduser --system --group app - -# create the appropriate directories -ENV HOME=/home/app -ENV APP_HOME=/home/app/web -RUN mkdir $APP_HOME -WORKDIR $APP_HOME - -# copy project -COPY . $APP_HOME - -# chown all the files to the app user -RUN chown -R app:app $APP_HOME - -# change to the app user -USER app - -CMD mamba run --live-stream -n dlp gunicorn training.wsgi:application --bind 0.0.0.0:8000 \ No newline at end of file diff --git a/training/README.md b/training/README.md index 58f0548c..3f6a99cb 100644 --- a/training/README.md +++ b/training/README.md @@ -1,9 +1,8 @@ ## Run docker -From the training/ directory -Dev: docker-compose up -Production: docker-compose -f docker-compose.prod.yml up +Ensure you have logged in to AWS, using (`aws configure sso`) then (`aws sso login --profile=DLP`) -## Rebuild and run docker From the training/ directory -Dev: docker-compose up --build -Production: docker-compose -f docker-compose.prod.yml up --build \ No newline at end of file +Dev: `AWS_PROFILE=DLP docker-compose up` +Production: `AWS_PROFILE=DLP docker compose -f docker-compose.prod.yml up` + +To rebuild, add a --build flag to the command diff --git a/training/docker-compose.prod.yml b/training/docker-compose.prod.yml index bd3d75e9..115050fd 100644 --- a/training/docker-compose.prod.yml +++ b/training/docker-compose.prod.yml @@ -1,11 +1,13 @@ -version: '3.8' +version: "3.8" services: web: build: - dockerfile: Dockerfile.prod + context: . + target: production volumes: - - ./:/usr/src/training/ - - $HOME/.aws/credentials:/home/app/.aws/credentials:ro + - $HOME/.aws:/home/app/.aws:ro ports: - 8000:8000 + environment: + - AWS_PROFILE=$AWS_PROFILE diff --git a/training/docker-compose.yml b/training/docker-compose.yml index 4ec7840b..15a2a680 100644 --- a/training/docker-compose.yml +++ b/training/docker-compose.yml @@ -2,9 +2,31 @@ version: '3.8' services: web: - build: . + build: + context: . + target: development volumes: - - ./:/usr/src/training/ - - $HOME/.aws/credentials:/home/app/.aws/credentials:ro + - ./:/home/app/training + - $HOME/.aws:/home/app/.aws:ro ports: - 8000:8000 + environment: + - AWS_PROFILE=$AWS_PROFILE + depends_on: + - celery + celery: + build: + context: . + dockerfile: ./training/core/celery/Dockerfile + target: development + volumes: + - ./:/home/app/training + - $HOME/.aws:/home/app/.aws:ro + environment: + - AWS_PROFILE=$AWS_PROFILE + depends_on: + - redis + redis: + image: redis:7.2.4 + expose: + - 6379 diff --git a/training/training/celeryconfig.py b/training/training/celeryconfig.py index 36ea4157..bcb88166 100644 --- a/training/training/celeryconfig.py +++ b/training/training/celeryconfig.py @@ -1,11 +1,23 @@ -broker_url = "sqs://" # "redis://localhost:6379" +import os -broker_transport_options = { - "predefined_queues": { - "training-queue.fifo": { - "url": "https://sqs.us-east-1.amazonaws.com/521654603461/training-queue.fifo", +# note, this file is used both in training/ and in the celery worker +if "ENVIRONMENT" in os.environ and os.environ["ENVIRONMENT"] == "production": + print( + "ENVIRONMENT env var set to production, setting broker_url to sqs training-queue" + ) + broker_url = "sqs://" + + broker_transport_options = { + "predefined_queues": { + "training-queue.fifo": { + "url": "https://sqs.us-east-1.amazonaws.com/521654603461/training-queue.fifo", + } } } -} -task_default_queue = "training-queue.fifo" + task_default_queue = "training-queue.fifo" +else: + print( + "ENVIRONMENT env var either not found or not set to production, setting broker_url to redis://redis:6379" + ) + broker_url = "redis://redis:6379" diff --git a/training/training/core/celery/Dockerfile b/training/training/core/celery/Dockerfile new file mode 100644 index 00000000..84d0238a --- /dev/null +++ b/training/training/core/celery/Dockerfile @@ -0,0 +1,51 @@ +FROM condaforge/miniforge3 AS base + +WORKDIR /usr/src/build + +# install dependencies +RUN mamba create --name dlp -y + +COPY environment.yml pyproject.toml poetry.lock ./ +RUN mamba run --live-stream -n dlp mamba env update --file environment.yml --prune + +# create directory for the app user +RUN mkdir -p /home/app + +# create the app user +RUN addgroup --system app && adduser --system --group app + +# create the appropriate directories +ARG HOME=/home/app +ARG APP_HOME=/home/app/training +RUN mkdir $APP_HOME + + + +######################## development target ######################## +FROM base AS development +ENV ENVIRONMENT development + +RUN mamba run --live-stream -n dlp poetry install + + +# we previously mounted training/ to APP_HOME +WORKDIR $APP_HOME +RUN chown -R app:app $APP_HOME +USER app + +CMD mamba run --live-stream -n dlp celery -A training.core.celery.worker worker --loglevel=INFO + +######################## production target ######################### +FROM base AS production +ENV ENVIRONMENT production + +RUN mamba run --live-stream -n dlp poetry install --without dev + +# copy project +WORKDIR $APP_HOME +COPY . $APP_HOME + +RUN chown -R app:app $APP_HOME +USER app + +CMD mamba run --live-stream -n dlp celery -A training.core.celery.worker worker --loglevel=INFO