Skip to content

Commit

Permalink
GCP one click deployment changes - DRAFT
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneLightsOn committed May 23, 2024
1 parent 658332d commit fe1f9f1
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 51 deletions.
76 changes: 37 additions & 39 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,62 +1,48 @@
# This Dockerfile is intended for One-Click deployment to Google Cloud Run
# ------------------------------------------------------------------------
FROM ghcr.io/cohere-ai/terrarium:latest as terrarium

FROM buildpack-deps:buster
FROM python:3.11
LABEL authors="Cohere"

## set ENV for python
ENV PG_APP_HOME=/etc/docker-app
ENV PYTHON_VERSION=3.11.8
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ENV PYTHONIOENCODING=utf-8
ENV LANG C.UTF-8
ENV PYTHONPATH=/workspace/src/
# "Activate" the venv manually for the context of the container
ENV VIRTUAL_ENV=/workspace/.venv
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
# Keep the poetry venv name and location predictable
ENV POETRY_VIRTUALENVS_IN_PROJECT=true
ENV APP_HOME=/workspace
COPY docker_scripts/gcp-entrypoint.sh /sbin/gcp-entrypoint.sh

# Install python
RUN cd /usr/src \
&& wget https://www.python.org/ftp/python/$PYTHON_VERSION/Python-$PYTHON_VERSION.tgz \
&& tar -xzf Python-$PYTHON_VERSION.tgz \
&& cd Python-$PYTHON_VERSION \
&& ./configure --enable-optimizations \
&& make install \
&& ldconfig \
&& rm -rf /usr/src/Python-$PYTHON_VERSION.tgz /usr/src/Python-$PYTHON_VERSION \
&& update-alternatives --install /usr/bin/python python /usr/local/bin/python3 1
RUN chmod 755 /sbin/gcp-entrypoint.sh \
&& curl -sL https://deb.nodesource.com/setup_18.x | bash - \
&& apt-get update \
&& apt-get install --no-install-recommends -y nginx nodejs \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
&& npm install -g pnpm \
&& npm install -g pm2

# Install poetry
RUN pip3 install --no-cache-dir poetry==1.6.1
# Copy nginx config \
COPY docker_scripts/nginx.conf /etc/nginx/nginx.conf

WORKDIR /workspace

# Copy dependency files to avoid cache invalidations
COPY pyproject.toml poetry.lock ./

# Install dependencies
RUN poetry install
RUN pip3 install --no-cache-dir poetry==1.6.1 \
&& poetry config installer.max-workers 10 \
&& poetry install --without setup \
&& (poetry cache clear --all --no-interaction PyPI || true) \
&& (poetry cache clear --all --no-interaction _default_cache || true)

# Copy the rest of the code
COPY src/backend src/backend

COPY docker_scripts/ ${APP_HOME}/
COPY docker_scripts/cloudrun-entrypoint.sh /sbin/entrypoint.sh
RUN chmod 755 /sbin/entrypoint.sh

# Install nodejs
RUN curl -sL https://deb.nodesource.com/setup_18.x | bash -
RUN apt-get install -y nodejs
RUN npm install -g pnpm
# pm2 to start frontend
RUN npm install -g pm2

# ENV for frontend
ENV NEXT_PUBLIC_API_HOSTNAME="http://localhost:8000"
ENV PYTHON_INTERPRETER_URL="http://terrarium:8080"
COPY docker_scripts/ ${PG_APP_HOME}/

# Install frontend dependencies
WORKDIR /workspace/src/interfaces/coral_web
Expand All @@ -70,10 +56,22 @@ COPY src/interfaces/coral_web/package.json src/interfaces/coral_web/yarn.lock* s
COPY src/interfaces/coral_web/.env.development .
COPY src/interfaces/coral_web/.env.production .

RUN pnpm install
ENV NEXT_PUBLIC_API_HOSTNAME='/api'
RUN pnpm install \
&& pnpm next:build

# Terrarium
WORKDIR /usr/src/app
COPY --from=terrarium /usr/src/app/package*.json ./
RUN npm install -g ts-node \
&& npm install \
&& npm prune --production
COPY --from=terrarium /usr/src/app/. .
ENV ENV_RUN_AS "docker"

EXPOSE 9000/tcp
EXPOSE 3000/tcp
WORKDIR ${APP_HOME}
# Ports to expose
EXPOSE 4000/tcp
EXPOSE 8000/tcp
EXPOSE 8090/tcp

CMD ["/sbin/entrypoint.sh"]
CMD ["/sbin/gcp-entrypoint.sh"]
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,10 @@ Contributions are what drive an open source community, any contributions made ar
<img src="https://contrib.rocks/image?repo=cohere-ai/cohere-toolkit" />
</a>

Made with [contrib.rocks](https://contrib.rocks).
Made with [contrib.rocks](https://contrib.rocks).

### Deploying to Google Cloud Run

Before deploying to Google Cloud Run, you'll need a postgres database accessible to your Google Cloud Project, authenticated by a username and password. You'll be prompted for a `DATABASE_URL` before the container builds.

[![Run on Google Cloud](https://deploy.cloud.run/button.svg)](https://deploy.cloud.run)
23 changes: 18 additions & 5 deletions app.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
{
"name": "one-click-demo",
"name": "toolkit-deploy",
"env": {
"DATABASE_URI": {
"description": "URI for your Postgres database",
"default": "postgresql+psycopg2://postgre:postgre@localhost:5432/toolkit"
"DATABASE_URL": {
"description": "URL for your Postgres database"
},
"COHERE_API_KEY": {
"description": "Your Cohere API key"
}
},
"hooks": {
"prebuild": {
"commands": [
"gcloud auth configure-docker"
]
}
},
"options": {
"allow-unauthenticated": true
"allow-unauthenticated": true,
"memory": "12288Mi",
"cpu": "4",
"port": 8090,
"max-instances": 2
}
}
2 changes: 1 addition & 1 deletion docker_scripts/env-defaults
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ DB_EXTENSION=${DB_EXTENSION:-}

# Defaults for the toolkit
export NEXT_PUBLIC_API_HOSTNAME=${NEXT_PUBLIC_API_HOSTNAME:-http://localhost:8000}
export PYTHON_INTERPRETER_URL=${PYTHON_INTERPRETER_URL:-http://terrarium:8080}
export PYTHON_INTERPRETER_URL=${PYTHON_INTERPRETER_URL:-http://localhost:8080}
export DATABASE_URL=${DATABASE_URL:-postgresql+psycopg2://postgre:postgre@localhost:5432/toolkit}
33 changes: 28 additions & 5 deletions docker_scripts/functions
Original file line number Diff line number Diff line change
Expand Up @@ -237,23 +237,46 @@ run_postgre() {
echo "PostgreSQL ${PG_VERSION} started"
}

run_nginx() {
echo "Starting Nginx Proxy..."
nginx
echo "Nginx Proxy started"
}


run_backend() {
cd /workspace
source /workspace/.venv/bin/activate
cd /workspace
echo "Migration started..."
echo "DATABASE_URL: $DATABASE_URL"
alembic -c src/backend/alembic.ini upgrade head
echo "Migration finished..."
echo "Starting FastAPI..."
exec uvicorn backend.main:app --host 0.0.0.0 --port 8000 --loop asyncio
}

run_backend_poetry() {
cd /workspace
poetry install --without setup
source .venv/bin/activate
echo "Migration started..."
alembic -c src/backend/alembic.ini upgrade head
echo "Migration finished..."
echo "Starting FastAPI..."
exec uvicorn backend.main:app --host 0.0.0.0 --port 8000 --loop asyncio
}

run_frontend_proxy() {
cd /workspace/src/interfaces/coral_web
echo "Starting Frontend..."
pm2 start pnpm -- start:single-docker
echo "Frontend started..."
}

run_frontend() {
cd /workspace/src/interfaces/coral_web
if [[ ${NEXT_PUBLIC_API_HOSTNAME} != *"localhost:8000"* ]]; then
echo "Building Frontend when we are not in a localhost environment"
pnpm next:build
fi
echo "Building Frontend when we are not in a localhost environment"
pnpm next:build
echo "Starting Frontend..."
pm2 start pnpm -- start:single-docker
echo "Frontend started..."
Expand Down
17 changes: 17 additions & 0 deletions docker_scripts/gcp-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash
set -e

# shellcheck source=runtime/functions
source "${PG_APP_HOME}/functions"

[[ ${DEBUG} == true ]] && set -x

# default behaviour is to launch apps
if [[ -z ${1} ]]; then
run_nginx
run_frontend_proxy
run_terrarium
run_backend
else
exec "$@"
fi
85 changes: 85 additions & 0 deletions docker_scripts/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
worker_connections 768;
# multi_accept on;
}

http {

##
# Basic Settings
##

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
client_max_body_size 50M;
# server_tokens off;

# server_names_hash_bucket_size 64;
# server_name_in_redirect off;

include /etc/nginx/mime.types;
default_type application/octet-stream;

upstream backend {
server 127.0.0.1:8000;
}

upstream frontend {
server 127.0.0.1:4000;
}

server {
listen 8090 default_server;
listen [::]:8090 default_server;

location /api {
rewrite /api/(.*) /$1 break;
proxy_pass http://backend;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}

location / {
proxy_pass http://frontend;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}

##
# SSL Settings
##

ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;

##
# Logging Settings
##

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

##
# Gzip Settings
##

gzip on;

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}

0 comments on commit fe1f9f1

Please sign in to comment.