Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into jupyter-notebook-post…
Browse files Browse the repository at this point in the history
…gres-sample
  • Loading branch information
raphaeltm committed Dec 7, 2024
2 parents 87407c8 + 4ac57ac commit 95799f2
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 71 deletions.
1 change: 1 addition & 0 deletions .github/workflows/deploy-changed-samples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ jobs:
TEST_OPENAI_KEY: ${{ secrets.TEST_OPENAI_KEY }}
TEST_POSTGRES_PASSWORD: ${{ secrets.TEST_POSTGRES_PASSWORD }}
TEST_QUEUE: ${{ secrets.TEST_QUEUE }}
TEST_SECRET_KEY: ${{ secrets.TEST_SECRET_KEY }}
TEST_SECRET_KEY_BASE: ${{ secrets.TEST_SECRET_KEY_BASE }}
TEST_SESSION_SECRET: ${{ secrets.TEST_SESSION_SECRET }}
TEST_SLACK_CHANNEL_ID: ${{ secrets.TEST_SLACK_CHANNEL_ID }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ jobs:
- name: Deploy
uses: DefangLabs/[email protected]
with:
config-env-vars: SECRET_KEY POSTGRES_PASSWORD POSTGRES_PARAMS
config-env-vars: SECRET_KEY POSTGRES_PASSWORD
env:
SECRET_KEY: ${{ secrets.SECRET_KEY }}
POSTGRES_PASSWORD: ${{ secrets.POSTGRES_PASSWORD }}
POSTGRES_PARAMS: ${{ secrets.POSTGRES_PARAMS }}
POSTGRES_PASSWORD: ${{ secrets.POSTGRES_PASSWORD }}
12 changes: 6 additions & 6 deletions samples/django-channels-redis-postgres/compose.dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ services:
- ./app:/app
healthcheck: {}
environment:
REDIS_URL: redis://redis-service:6379
POSTGRES_URL: postgres://djangouser:djangopassword@postgres-service:5432/djangodatabase
DEBUG: "True"
- REDIS_URL=redis://redis-service:6379
- POSTGRES_URL=postgres://djangouser:djangopassword@postgres-service:5432/djangodatabase
- DEBUG="True"
depends_on:
- redis-service
- postgres-service
Expand All @@ -25,9 +25,9 @@ services:
file: compose.yaml
service: postgres-service
environment:
POSTGRES_DB: djangodatabase
POSTGRES_USER: djangouser
POSTGRES_PASSWORD: djangopassword
- POSTGRES_DB=djangodatabase
- POSTGRES_USER=djangouser
- POSTGRES_PASSWORD=djangopassword
volumes:
- postgres_data:/var/lib/postgresql/data

Expand Down
14 changes: 7 additions & 7 deletions samples/django-channels-redis-postgres/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ services:
published: 8000
mode: ingress
environment:
REDIS_URL: redis://redis-service:6379
# if using BYOC set POSTGRES_PARAMS to ?sslmode=require in playground, set to ?
POSTGRES_URL: "postgres://djangouser:${POSTGRES_PASSWORD}@postgres-service:5432/djangodatabase${POSTGRES_PARAMS}"
SECRET_KEY:
- REDIS_URL=redis://redis-service:6379
# if using BYOC add sslmode=require to the POSTGRES_URL query params
- POSTGRES_URL=postgres://djangouser:${POSTGRES_PASSWORD}@postgres-service:5432/djangodatabase?
- SECRET_KEY
healthcheck:
# wget or curl required for healthchecks on services with a published port
# this gets parsed by Defang and provided to the load balancers as well
Expand All @@ -35,6 +35,6 @@ services:
- mode: host
target: 5432
environment:
POSTGRES_PASSWORD:
POSTGRES_DB: djangodatabase
POSTGRES_USER: djangouser
- POSTGRES_PASSWORD
- POSTGRES_DB=djangodatabase
- POSTGRES_USER=djangouser
56 changes: 42 additions & 14 deletions samples/django-postgres/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,55 @@

This template is a customer relationship management list project developed using Python Django framework, offering a starting point to help you quickly build your customer management system. We use PostgreSQL as the database. We have prepared all the essential files for deployment. By spending less than 10 minutes setting up the environment, as detailed in the prerequisites, and executing the commands in our step-by-step guide, your website will be ready to go live to the world!

## NOTE
> [!NOTE]
This sample showcases how you could deploy a full-stack application with Defang and Django. However, it deploys Postgres as a Defang service. Defang [services](https://12factor.net/processes) are ephemeral and should not be used to run stateful workloads in production as they will be reset on every deployment. For production use cases you should use a managed database like RDS, Aiven, or others. If you stick to Django's default SQLite database, your stored data will be lost on every deployment, and in some other situations. In the future, Defang will help you provision and connect to managed databases.

This sample showcases how you could deploy a full-stack application with Defang and Django. However, it deploys postgres as a defang service. Defang [services](https://12factor.net/processes) are ephemeral and should not be used to run stateful workloads in production as they will be reset on every deployment. For production use cases you should use a managed database like RDS, Aiven, or others. If you stick to Rail's default SQLite database, your stored data will be lost on every deployment, and in some other situations. In the future, Defang will help you provision and connect to managed databases.
## Prerequisites

## Essential Setup Files
1. Download [Defang CLI](https://github.com/DefangLabs/defang)
2. (Optional) If you are using [Defang BYOC](https://docs.defang.io/docs/concepts/defang-byoc) authenticate with your cloud provider account
3. (Optional for local development) [Docker CLI](https://docs.docker.com/engine/install/)

1. A [Dockerfile](https://docs.docker.com/develop/develop-images/dockerfile_best-practices/) to describe the basic image of your applications.
2. A [docker-compose file](https://docs.defang.io/docs/concepts/compose) to define and run multi-container Docker applications.
3. A [.dockerignore](https://docs.docker.com/build/building/context/#dockerignore-files) file to comply with the size limit (10MB).
## Development

## Prerequisite
To run the application locally, you can use the following command:

1. Download [Defang CLI](https://github.com/DefangLabs/defang)
2. If you are using [Defang BYOC](https://docs.defang.io/docs/concepts/defang-byoc), make sure you have properly [authenticated your AWS account](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html)
Plus, make sure that you have properly set your environment variables like `AWS_PROFILE`, `AWS_REGION`, `AWS_ACCESS_KEY_ID`, and `AWS_SECRET_ACCESS_KEY`.
```bash
docker compose up --build
```

## Configuration

For this sample, you will need to provide the following [configuration](https://docs.defang.io/docs/concepts/configuration):

> Note that if you are using the 1-click deploy option, you can set these values as secrets in your GitHub repository and the action will automatically deploy them for you.
### `POSTGRES_PASSWORD`
```bash
defang config set POSTGRES_PASSWORD
```

## Deployment

> [!NOTE]
> Download [Defang CLI](https://github.com/DefangLabs/defang)
### Defang Playground

Deploy your application to the Defang Playground by opening up your terminal and typing:
```bash
defang compose up
```

### BYOC (AWS)

## A Step-by-Step Guide for Deployment
If you want to deploy to your own cloud account, you can use Defang BYOC:

1. Open the terminal and type `defang login`
2. Type `defang compose up` in the CLI
3. Now your application will be launched
1. [Authenticate your AWS account](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html), and check that you have properly set your environment variables like `AWS_PROFILE`, `AWS_REGION`, `AWS_ACCESS_KEY_ID`, and `AWS_SECRET_ACCESS_KEY`.
2. Run in a terminal that has access to your AWS environment variables:
```bash
defang --provider=aws compose up
```

---

Expand Down
12 changes: 6 additions & 6 deletions samples/django-postgres/app/crm_platform/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
]

ROOT_URLCONF = 'crm_platform.urls'
Expand Down Expand Up @@ -83,13 +83,13 @@
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"HOST": os.environ.get('DB_HOST', 'db'),
# We put the host in an environment variable because Defang
"HOST": os.environ.get('DB_HOST', 'db'),
# We put the host in an environment variable because Defang
# detects hostnames that match service names and makes sure they
# are properly configured to communicate when deployed
"NAME": 'mydatabase',
"USER": 'myuser',
"PASSWORD": 'mypassword',
"NAME": os.environ.get("POSTGRES_DB"),
"USER": os.environ.get("POSTGRES_USER"),
"PASSWORD": os.environ.get("POSTGRES_PASSWORD"),
}
}

Expand Down
24 changes: 9 additions & 15 deletions samples/django-postgres/compose.yaml
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
services:
db:
restart: unless-stopped
image: postgres:14
image: postgres:16
volumes: ["./tmp/postgres:/var/lib/postgresql/data/"]
environment:
- POSTGRES_DB=mydatabase
- POSTGRES_USER=myuser
- POSTGRES_PASSWORD=mypassword
- POSTGRES_PASSWORD
ports:
- mode: host
target: 5432
healthcheck:
test:
[
"CMD-SHELL",
"PGUSER=myuser PGPASSWORD=mypassword pg_isready -q -d mydatabase",
]
interval: 1m30s
timeout: 30s
retries: 5
#deploy:
# resources:
# reservations:
Expand All @@ -35,10 +26,13 @@ services:
environment:
- DB_HOST=db
- DEBUG=False
deploy:
resources:
reservations:
memory: 2GB
- POSTGRES_USER=myuser
- POSTGRES_DB=mydatabase
- POSTGRES_PASSWORD
# deploy:
# resources:
# reservations:
# memory: 2GB
# volumes: [".:/code"]
depends_on:
- db
Expand Down
60 changes: 42 additions & 18 deletions samples/django/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,58 @@

[![1-click-deploy](https://defang.io/deploy-with-defang.png)](https://portal.defang.dev/redirect?url=https%3A%2F%2Fgithub.com%2Fnew%3Ftemplate_name%3Dsample-django-template%26template_owner%3DDefangSamples)

This is a simple example of how to run Django on Defang. It is a simple Todo app that uses SQLite as the database.

### NOTE

This sample is a simple Django app that uses SQLite as the database, which will be reset every time you deploy. **It is not production-ready**. For production use cases, you should check out the Django + Postgres sample.
This sample is a simple Django to-do app that uses SQLite as the database, which will be reset every time you deploy. **It is not production-ready**. For production use cases, you should check out the Django + Postgres sample.

The app includes a management command which is run on startup to create a superuser with the username `admin` and password `admin`. This means you can login to the admin interface at `/admin/` and see the Django admin interface without any additional steps. The `example_app` is already registered and the `Todo` model is already set up to be managed in the admin interface.

The Dockerfile and compose files are already set up for you and are ready to be deployed. Serving is done using [Gunicorn](https://gunicorn.org/) and uses [WhiteNoise](https://whitenoise.readthedocs.io/en/latest/) for static files. The `CSRF_TRUSTED_ORIGINS` setting is configured to allow the app to run on a `defang.dev` subdomain.

## Essential Setup Files
## Prerequisites

1. A [Dockerfile](https://docs.docker.com/develop/develop-images/dockerfile_best-practices/) to describe the basic image of your applications.
2. A [docker-compose file](https://docs.defang.io/docs/concepts/compose) to define and run multi-container Docker applications.
3. A [.dockerignore](https://docs.docker.com/build/building/context/#dockerignore-files) file to comply with the size limit (10MB).
1. Download [Defang CLI](https://github.com/DefangLabs/defang)
2. (Optional) If you are using [Defang BYOC](https://docs.defang.io/docs/concepts/defang-byoc) authenticate with your cloud provider account
3. (Optional for local development) [Docker CLI](https://docs.docker.com/engine/install/)

## Prerequisite
## Development

1. Download [Defang CLI](https://github.com/DefangLabs/defang)
2. If you are using [Defang BYOC](https://docs.defang.io/docs/concepts/defang-byoc), make sure you have properly [authenticated your AWS account](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html)
Plus, make sure that you have properly set your environment variables like `AWS_PROFILE`, `AWS_REGION`, `AWS_ACCESS_KEY_ID`, and `AWS_SECRET_ACCESS_KEY`.
To run the application locally, you can use the following command:

```bash
docker compose up --build
```

## Configuration

For this sample, you will not need to provide [configuration](https://docs.defang.io/docs/concepts/configuration).

If you wish to provide configuration, see below for an example of setting a configuration for a value named `API_KEY`.

```bash
defang config set API_KEY
```

## Deployment

> [!NOTE]
> Download [Defang CLI](https://github.com/DefangLabs/defang)
### Defang Playground

Deploy your application to the Defang Playground by opening up your terminal and typing:
```bash
defang compose up
```

### BYOC (AWS)

## A Step-by-Step Guide
If you want to deploy to your own cloud account, you can use Defang BYOC:

1. (optional) If you are using Defang BYOC, make sure to update the `CSRF_TRUSTED_ORIGINS` setting in the `settings.py` file to include an appropriate domain.
2. Open the terminal and type `defang login`
3. Type `defang compose up` in the CLI
4. Now your application will be launched
1. [Authenticate your AWS account](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html), and check that you have properly set your environment variables like `AWS_PROFILE`, `AWS_REGION`, `AWS_ACCESS_KEY_ID`, and `AWS_SECRET_ACCESS_KEY`.
2. Make sure to update the `CSRF_TRUSTED_ORIGINS` setting in the `settings.py` file to include an appropriate domain.
3. Run in a terminal that has access to your AWS environment variables:
```bash
defang --provider=aws compose up
```

---

Expand Down
2 changes: 1 addition & 1 deletion samples/nextjs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ Title: Next.js

Short Description: A basic Next.js app.

Tags: Next.js, React, Docker, AWS, Node.js, TypeScript, JavaScript
Tags: Next.js, React, Docker, Node.js, TypeScript, JavaScript

Languages: nodejs
2 changes: 1 addition & 1 deletion samples/python-s3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ Title: Python & Flask & AWS S3

Short Description: An app that demonstrates how to upload and download files from AWS S3 using Python and Flask.

Tags: Python, Flask, AWS S3
Tags: Python, Flask, AWS, S3

Languages: python

0 comments on commit 95799f2

Please sign in to comment.