Skip to content

Commit

Permalink
Update cookiecutter generated files
Browse files Browse the repository at this point in the history
  • Loading branch information
kumaranvpl committed Nov 20, 2024
1 parent 736ea92 commit 9d327b5
Show file tree
Hide file tree
Showing 66 changed files with 840 additions and 48 deletions.
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
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 @@ -9,13 +9,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
Loading

0 comments on commit 9d327b5

Please sign in to comment.