Skip to content

Commit

Permalink
Merge pull request #34 from picture-pro/33-prototype-deployment-process
Browse files Browse the repository at this point in the history
33 prototype deployment process
  • Loading branch information
johnbchron authored Feb 24, 2024
2 parents 98b502b + 889e92b commit 69aa827
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 20 deletions.
26 changes: 23 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
SURREALDB_WS_URL=localhost:8000
SURREALDB_ROOT_USER=
SURREALDB_ROOT_PASS=

# only for app
SURREAL_WS_URL=localhost:8000 # `picturepro-mono.internal:8000` inside fly
# for both surreal and app
SURREAL_USER=
SURREAL_PASS=

# only for app
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=

# only for QR codes
APP_BASE_URL=

# only for deploy
FLY_API_TOKEN=

# only for app, necessary in fly
LEPTOS_OUTPUT_NAME=site
LEPTOS_SITE_ROOT=site
LEPTOS_SITE_PKG_DIR=pkg
LEPTOS_SITE_ADDR=0.0.0.0:3000
LEPTOS_RELOAD_PORT=3001

# only for app, cannot be `/tmp` in fly
TMPDIR=/
14 changes: 11 additions & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

name: CI
name: CI-CD

on:
push:
Expand All @@ -8,12 +8,20 @@ on:
pull_request:

jobs:
check:
build-check-deploy:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
- uses: DeterminateSystems/magic-nix-cache-action@main
- name: Install Nix
uses: DeterminateSystems/nix-installer-action@v3
- name: Check default package
- name: Build server package
run: nix build
- name: Run flake checks
run: nix flake check
- name: Install flyctl
uses: superfly/flyctl-actions/setup-flyctl@master
- name: Containerize & Deploy to Fly
run: nix build ".#container" -L && docker load < result && flyctl deploy --local-only -i site-server
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
9 changes: 8 additions & 1 deletion crates/artifact/src/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ pub fn object_store_from_env(
Ok(Box::new(object_store))
}

fn cache_path(id: &str) -> std::path::PathBuf { std::env::temp_dir().join(id) }
fn cache_dir() -> std::path::PathBuf { std::env::temp_dir() }
fn cache_path(id: &str) -> std::path::PathBuf { cache_dir().join(id) }

#[instrument(skip(object_store))]
pub async fn download_artifact(
Expand All @@ -35,6 +36,12 @@ pub async fn download_artifact(

let object_store = object_store()?;
let contents = inner_download_artifact(&*object_store, id).await?;

if !cache_dir().exists() {
tokio::fs::create_dir_all(cache_dir())
.await
.wrap_err("Failed to create cache directory")?;
}
tokio::fs::write(&cache_path, &contents)
.await
.wrap_err("Failed to write cached artifact")?;
Expand Down
14 changes: 7 additions & 7 deletions crates/clients/src/surreal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ impl SurrealRootClient {
/// Creates a new client.
pub async fn new() -> Result<Self> {
let client = Surreal::new::<Ws>(
std::env::var("SURREALDB_WS_URL")
.wrap_err("Could not find env var \"SURREALDB_WS_URL\"")?,
std::env::var("SURREAL_WS_URL")
.wrap_err("Could not find env var \"SURREAL_WS_URL\"")?,
)
.await
.wrap_err_with(|| {
format!(
"Could not connect to SurrealDB endpoint: `{}`\n\tNB: don't include \
the ws:// or wss:// prefix, e.g. `example.com:8080` instead of \
`wss://example.com:8080`",
std::env::var("SURREALDB_WS_URL").unwrap()
std::env::var("SURREAL_WS_URL").unwrap()
)
})?;

Expand All @@ -43,10 +43,10 @@ impl SurrealRootClient {
self
.client
.signin(Root {
username: &std::env::var("SURREALDB_ROOT_USER")
.wrap_err("Could not find env var \"SURREALDB_ROOT_USER\"")?,
password: &std::env::var("SURREALDB_ROOT_PASS")
.wrap_err("Could not find env var \"SURREALDB_ROOT_PASS\"")?,
username: &std::env::var("SURREAL_USER")
.wrap_err("Could not find env var \"SURREAL_USER\"")?,
password: &std::env::var("SURREAL_PASS")
.wrap_err("Could not find env var \"SURREAL_PASS\"")?,
})
.await
.wrap_err("Could not sign in to SurrealDB as root")
Expand Down
6 changes: 5 additions & 1 deletion crates/site-app/src/components/photo_upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@ pub async fn photo_upload(
},
)
.await
.map_err(|e| ServerFnError::new(format!("Failed to upload photo: {}", e)))?;
.map_err(|e| {
let error = format!("Failed to upload photo: {:?}", e);
tracing::error!("{error}");
ServerFnError::new(error)
})?;

Ok(photo_group.id)
}
11 changes: 7 additions & 4 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,17 @@
doCheck = false;
cargoArtifacts = site-server-deps;

SQLX_OFFLINE = "true";
APP_ENVIRONMENT = "production";
});

site-server-container = pkgs.dockerTools.buildLayeredImage {
name = "site-server";
tag = "latest";
contents = [ site-server pkgs.cacert ];
config.Cmd = [ "${site-server}/bin/site-server" ];
contents = [ site-server pkgs.cacert pkgs.surrealdb ];
config = {
Cmd = [ "site-server" ];
WorkingDir = "${site-server}/bin";
};
};

in {
Expand Down Expand Up @@ -178,8 +180,9 @@
};

packages = {
inherit site-server site-server-container;
default = site-server;
server = site-server;
container = site-server-container;
};

devShells.default = pkgs.mkShell {
Expand Down
51 changes: 51 additions & 0 deletions fly.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# fly.toml app configuration file generated for picturepro-mono on 2024-02-23T15:08:05-06:00
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#

app = "picturepro-mono"
primary_region = "dfw"
kill_signal = "SIGINT"
kill_timeout = "5s"

[processes]
app = "site-server"
surreal = "surreal start --log=info --auth --bind '[::]':8000 file:/data/srdb.db"

[[mounts]]
source = "surreal_data"
destination = "/data"
initial_size = "1gb"
processes = ["surreal"]
auto_extend_size_threshold = 80
auto_extend_size_increment = "1gb"
auto_extend_size_limit = "5gb"

[http_service]
internal_port = 3000
auto_stop_machines = true
auto_start_machines = true
min_machines_running = 0
processes = ["app"]
[http_service.concurrency]
type = "requests"
hard_limit = 250
soft_limit = 200

[[http_service.checks]]
interval = "30s"
timeout = "5s"
grace_period = "10s"
method = "GET"
path = "/"

[checks]
[checks.surreal_check]
processes = ["surreal"]
grace_period = "30s"
interval = "15s"
method = "get"
path = "/status"
port = 8000
timeout = "10s"
type = "http"
4 changes: 3 additions & 1 deletion justfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@

watch:
cargo leptos watch
trace:
cargo leptos serve --bin-features chrome-tracing
surreal:
mkdir /tmp/surreal_data -p && surreal start file:/tmp/surreal_data --log=info --auth --username=root --password=pass
mkdir /tmp/surreal_data -p && surreal start file:/tmp/surreal_data --log=info --auth
wipe-surreal:
rm -rf /tmp/surreal_data
apply-surreal:
Expand Down

0 comments on commit 69aa827

Please sign in to comment.