Skip to content

Commit

Permalink
Update deploy docs (#591)
Browse files Browse the repository at this point in the history
* Update deploy to fly.io section in docs

* Update cookiecutter generated files
  • Loading branch information
kumaranvpl authored Nov 20, 2024
1 parent 5643b0c commit 35e708b
Show file tree
Hide file tree
Showing 67 changed files with 868 additions and 69 deletions.
49 changes: 28 additions & 21 deletions docs/docs/en/user-guide/getting-started/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -392,48 +392,55 @@ Running the above command will start the Docker container in the foreground with

### Deploying to Fly.io

If you created the project using Cookiecutter, there is a built-in script to deploy your workflow to [**Fly.io**](https://fly.io/). Run it as shown below:
If you created the project using Cookiecutter, there are built-in scripts to deploy your workflow to [**Fly.io**](https://fly.io/). In Fly.io, the application namespace is global, so the application name you chose might already be taken. To check your application's name availability and to reserve it, you can run the following script:

```console
./scripts/deploy_to_fly_io.sh
./scripts/register_to_fly_io.sh
```

Running the above command will prompt you to log in to your Fly.io account (if not already logged in) by opening a fly.io URL in your browser. The login prompt will look like this:

```console
Checking if already logged into fly.io
Logging into fly.io
failed opening browser. Copy the url (https://fly.io/app/auth/cli/78366a6d347a377a6e346465776167726f6b693537666a333674346978626d37) into a browser and continue
Opening https://fly.io/app/auth/cli/78366a6d347a377a6e346465776167726f6b693537666a333674346978626d37 ...

Waiting for session...
```

After logging in, the script will deploy your application to Fly.io without any further input. The output will look like this:
After logging in, the script will check if the application name is available and reserve it if it is. The reserved app domain for Fly.io is saved in a file called "registered_app_domain.txt". The output of running the script will look like this:

<details>
<summary>Output</summary>
```console
successfully logged in
Registering app name in fly.io
New app created: test-registration
App name registered successfully
Registered app name is:
test-registration.fly.dev
```
</details>

Once you have reserved your application name, you can deploy your application to Fly.io using the following script:

```console
./scripts/deploy_to_fly_io.sh
```

This script will deploy your application to Fly.io without any further input. The output will look like this:

<details>
<summary>Output</summary>
```console
Checking if already logged into fly.io
Already logged into fly.io
Deploying to fly.io
An existing fly.toml file was found for app write-fastagency-docs-cool-dew-7193
Using build strategies '[the "docker/Dockerfile" dockerfile]'. Remove [build] from fly.toml to force a rescan
Creating app in /workspaces/write_fastagency_docs
We're about to launch your app on Fly.io. Here's what you're getting:

Organization: Organization Name (fly launch defaults to the personal org)
Name: write-fastagency-docs-delicate-waterfall-7272 (generated)
Region: Amsterdam, Netherlands (from your fly.toml)
App Machines: shared-cpu-1x, 1GB RAM (from your fly.toml)
Postgres: <none> (not requested)
Redis: <none> (not requested)
Tigris: <none> (not requested)

Created app 'write-fastagency-docs-delicate-waterfall-7272' in organization 'personal'
Admin URL: https://fly.io/apps/write-fastagency-docs-delicate-waterfall-7272
Hostname: write-fastagency-docs-delicate-waterfall-7272.fly.dev
Wrote config file fly.toml
Validating /workspaces/write_fastagency_docs/fly.toml
==> Verifying app config
Validating fly.toml
✓ Configuration is valid
--> Verified app config
==> Building image
==> Building image with Depot
--> build: (​)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ my_fastagency_app
│   └── workflow.py
├── scripts
│   ├── build_docker.sh
│   ├── check-registered-app-pre-commit.sh
│   ├── check-registered-app.sh
│   ├── deploy_to_fly_io.sh
│   ├── lint-pre-commit.sh
│   ├── lint.sh
│   ├── register_to_fly_io.sh
│   ├── run_docker.sh
│   ├── run_mesop_locally.sh
│   ├── static-analysis.sh
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ pip install --upgrade pip
# install dev packages
pip install -e ".[dev]"

# install pre-commit hooks
pre-commit install

# install fly.io CLI and set fly.io CLI PATH in bashrc and zshrc
curl -L https://fly.io/install.sh | sh
echo 'export FLYCTL_INSTALL="/home/vscode/.fly"' | tee -a ~/.bashrc ~/.zshrc
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

name: Fly Deploy

on:
push:
branches:
- main
workflow_dispatch:

env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: superfly/flyctl-actions/setup-flyctl@master

- name: Check if the app name is registered in fly.io and deploy
run: ./scripts/deploy_to_fly_io.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,12 @@ repos:
hooks:
- id: detect-secrets
args: ["--baseline", ".secrets.baseline"]

- repo: local
hooks:
- id: check-registered-app
name: Check if the app name is registered in fly.io
entry: "scripts/check-registered-app-pre-commit.sh"
language: python
require_serial: true
verbose: true
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,19 @@ This `FastAgency` project includes a `fly.toml` file for deployment to [fly.io](
1. Login into fly.io:

```bash
fly auth login
flyctl auth login
```

2. Launch the fly.io app:

```bash
fly launch --config fly.toml --copy-config --yes
flyctl launch --config fly.toml --copy-config --yes
```

3. Set necessary LLM API key(for example, OPENAI_API_KEY) as a secret:

```bash
fly secrets set OPENAI_API_KEY=paste_openai_api_key_here
flyctl secrets set OPENAI_API_KEY=paste_openai_api_key_here
```

## What's Next?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ upstream mesop_backend {

}

# Extract fly-machine-id cookie value
map $http_cookie $fly_machine_id {
"~*fly-machine-id=([^;]+)" $1;
default "";
}

# Determine action based on cookie value
map $fly_machine_id $sticky_action {
"" "set_cookie"; # Empty cookie - need to set it
$FLY_MACHINE_ID "proceed"; # Cookie matches current instance
default "replay"; # Cookie exists but doesn't match - need to replay
}

# Main server block
server {
listen $MESOP_PORT;
server_name localhost;
Expand All @@ -15,6 +29,17 @@ server {
add_header X-XSS-Protection "1; mode=block";

location / {
# Handle cookie setting
if ($sticky_action = "set_cookie") {
add_header Set-Cookie "fly-machine-id=$FLY_MACHINE_ID; Max-Age=518400; Path=/";
}

# Handle replay
if ($sticky_action = "replay") {
add_header Fly-Replay "instance=$fly_machine_id";
return 307;
}

proxy_pass http://mesop_backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ export MESOP_PORT=${MESOP_PORT:-8888}
WORKERS=${WORKERS:-1}
echo "Number of workers: $WORKERS"

# Check FLY_MACHINE_ID is set, if not set, set it to dummy value
export FLY_MACHINE_ID=${FLY_MACHINE_ID:-dummy_fly_machine_id_value}
echo "Fly machine ID: $FLY_MACHINE_ID"

# Generate nginx config
for ((i=1; i<$WORKERS+1; i++))
do
PORT=$((MESOP_PORT + i))
sed -i "5i\ server 127.0.0.1:$PORT;" nginx.conf.template
done
envsubst '${MESOP_PORT}' < nginx.conf.template >/etc/nginx/conf.d/default.conf
envsubst '${MESOP_PORT},${FLY_MACHINE_ID}' < nginx.conf.template >/etc/nginx/conf.d/default.conf
echo "Nginx config:"
cat /etc/nginx/conf.d/default.conf

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

# taken from: https://jaredkhan.com/blog/mypy-pre-commit

# A script for running mypy,
# with all its dependencies installed.

set -o errexit

# Change directory to the project root directory.
cd "$(dirname "$0")"/..

./scripts/check-registered-app.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

# Check file registered_app_domain.txt exists. If it does not exists, echo and exit.
if [ ! -f registered_app_domain.txt ]; then
echo -e "\033[0;33mWarning: App name is not registered.\033[0m"
echo -e "\033[0;33mGithub Actions may fail if you push without registering.\033[0m"
echo -e "\033[0;33mRegister your app name by running the script 'scripts/register_to_fly_io.sh'.\033[0m"
fi
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
#!/bin/bash

echo -e "\033[0;32mLogging into fly.io\033[0m"
fly auth login
# Check file registered_app_domain.txt exists. If it does not exists, echo and exit.
if [ ! -f registered_app_domain.txt ]; then
echo -e "\033[0;31mError: App name is not registered.\033[0m"
echo -e "\033[0;31mRegister your app name by running the script 'scripts/register_to_fly_io.sh'.\033[0m"
echo -e "\033[0;31mExiting.\033[0m"
exit 1
fi

echo -e "\033[0;32mChecking if already logged into fly.io\033[0m"
if ! flyctl auth whoami > /dev/null 2>&1; then
echo -e "\033[0;32mLogging into fly.io\033[0m"
flyctl auth login
else
echo -e "\033[0;32mAlready logged into fly.io\033[0m"
fi

echo -e "\033[0;32mDeploying to fly.io\033[0m"
fly launch --config fly.toml --copy-config --yes
flyctl deploy --config fly.toml --yes

echo -e "\033[0;32mSetting secrets\033[0m"
fly secrets set OPENAI_API_KEY=$OPENAI_API_KEY
flyctl secrets set OPENAI_API_KEY=$OPENAI_API_KEY
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash


# Check file registered_app_domain.txt exists. If it does, echo and exit.
if [ -f registered_app_domain.txt ]; then
echo -e "\033[1;33mWarning: App name is already registered.\033[0m"
echo -e "\033[0;32mRegistered app name is:\033[0m"
cat registered_app_domain.txt
exit 1
fi

echo -e "\033[0;32mChecking if already logged into fly.io\033[0m"
if ! flyctl auth whoami > /dev/null 2>&1; then
echo -e "\033[0;32mLogging into fly.io\033[0m"
flyctl auth login
else
echo -e "\033[0;32mAlready logged into fly.io\033[0m"
fi

export FLY_APP_NAME=my-fastagency-app

echo -e "\033[0;32mRegistering app name in fly.io\033[0m"
if flyctl apps create $FLY_APP_NAME; then
echo "$FLY_APP_NAME.fly.dev" > registered_app_domain.txt
echo -e "\033[0;32mApp name registered successfully\033[0m"
echo -e "\033[0;32mRegistered app name is:\033[0m"
cat registered_app_domain.txt
else
echo -e "\033[1;31mError: App name is not available.\033[0m"
echo -e "\033[1;31mPlease change the app name in fly.toml and scripts/register_to_fly_io.sh and run this script again.\033[0m"
exit 1
fi
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ my_fastagency_app
│   └── workflow.py
├── scripts
│   ├── build_docker.sh
│   ├── check-registered-app-pre-commit.sh
│   ├── check-registered-app.sh
│   ├── deploy_to_fly_io.sh
│   ├── lint-pre-commit.sh
│   ├── lint.sh
│   ├── register_to_fly_io.sh
│   ├── run_docker.sh
│   ├── run_mesop_locally.sh
│   ├── static-analysis.sh
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ pip install --upgrade pip
# install dev packages
pip install -e ".[dev]"

# install pre-commit hooks
pre-commit install

# install fly.io CLI and set fly.io CLI PATH in bashrc and zshrc
curl -L https://fly.io/install.sh | sh
echo 'export FLYCTL_INSTALL="/home/vscode/.fly"' | tee -a ~/.bashrc ~/.zshrc
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

name: Fly Deploy

on:
push:
branches:
- main
workflow_dispatch:

env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: superfly/flyctl-actions/setup-flyctl@master

- name: Check if the app name is registered in fly.io and deploy
run: ./scripts/deploy_to_fly_io.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,12 @@ repos:
hooks:
- id: detect-secrets
args: ["--baseline", ".secrets.baseline"]

- repo: local
hooks:
- id: check-registered-app
name: Check if the app name is registered in fly.io
entry: "scripts/check-registered-app-pre-commit.sh"
language: python
require_serial: true
verbose: true
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,19 @@ This `FastAgency` project includes a `fly.toml` file for deployment to [fly.io](
1. Login into fly.io:

```bash
fly auth login
flyctl auth login
```

2. Launch the fly.io app:

```bash
fly launch --config fly.toml --copy-config --yes
flyctl launch --config fly.toml --copy-config --yes
```

3. Set necessary LLM API key(for example, OPENAI_API_KEY) as a secret:

```bash
fly secrets set OPENAI_API_KEY=paste_openai_api_key_here
flyctl secrets set OPENAI_API_KEY=paste_openai_api_key_here
```

## What's Next?
Expand Down
Loading

0 comments on commit 35e708b

Please sign in to comment.