Skip to content

Commit

Permalink
feat: docker images and stack
Browse files Browse the repository at this point in the history
Added docker build images and docker compose stack to allow for local
stack running.

closes ScuffleCloud#25
  • Loading branch information
TroyKomodo committed Feb 19, 2023
1 parent 85a545b commit 657ebc7
Show file tree
Hide file tree
Showing 30 changed files with 386 additions and 30 deletions.
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build]
target = "x86_64-unknown-linux-musl"
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.vscode/
node_modules/
.env*
Dockerfile
dev-stack/
.dockerignore
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ target/
.vscode/
!.vscode/extensions.json
node_modules/
.env
.env*
dev-stack/stack.docker-compose.yaml
31 changes: 30 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ You can find instructions on how to do that [here](https://devblogs.microsoft.co
- [Docker Compose V2](https://docs.docker.com/compose/install)
- [Rust](https://www.rust-lang.org/tools/install)
- [Just](https://just.systems/)
- [Musl](https://musl.libc.org/)

### For Ubuntu

Expand All @@ -49,7 +50,7 @@ echo "deb [signed-by=/usr/share/keyrings/yarnkey.gpg] https://dl.yarnpkg.com/deb

# Running the install for nodejs, yarn, make, docker and git
sudo apt-get update
sudo apt-get install nodejs yarn docker.io git
sudo apt-get install nodejs yarn docker.io git musl-tools

# Installing docker compose v2
DOCKER_CONFIG=${DOCKER_CONFIG:-$HOME/.docker}
Expand Down Expand Up @@ -144,6 +145,34 @@ just db-prepare

This will run the migrations and generate the SQLx code for the database. So that compile time querying can be used.

### Local Stack

You can setup a local stack with the following command:

```bash
just stack-init
```

You need to have fully built local environment before running this command. You can do that with the following command:

```bash
just build
```

Or if you want to build it inside a container you can run:

```bash
just build-container
```

Then to start it run

```
just stack-up
```

You can modify the stack by editing `./dev-stack/docker-compose.yaml` file generated by `just stack-init`.

## Monorepo

For starters, you will notice that this project is a [monorepo](https://semaphoreci.com/blog/what-is-monorepo).
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@ members = [
"frontend/player",
"video/edge",
"video/ingest",
"video/transcode",
"video/transcoder",
"common",
]

# We don't want to build the wasm by default, this is because its built by a yarn script
default-members = [
"backend/api",
"video/edge",
"video/ingest",
"video/transcoder",
"common",
]

Expand Down
48 changes: 42 additions & 6 deletions Justfile
Original file line number Diff line number Diff line change
@@ -1,29 +1,44 @@
set dotenv-load

arch := `uname -m | sed 's/amd64/x86_64/' | sed 's/arm64/aarch64/'`

build:
yarn --cwd ./frontend/website build
cargo build --release

build-container: env-backup
docker run --rm -v $(pwd):/pwd -w /pwd ghcr.io/scuffletv/base-build:1.66.1 just build

env-backup:
test -f .env && (\
mv .env .env.bak \
) || true

format:
yarn --cwd ./frontend/website format
yarn format
cargo fmt
cargo fmt --all
cargo clippy --fix --allow-dirty --allow-staged
cargo clippy --fix --allow-dirty --allow-staged --package player --target wasm32-unknown-unknown

lint:
yarn lint
yarn --cwd ./frontend/website lint
cargo clippy
cargo fmt -- --check
cargo clippy --package player --target wasm32-unknown-unknown
cargo fmt --all --check
cargo sqlx prepare --check --merged -- --all-targets --all-features

test:
cargo test
yarn --cwd ./frontend/website test

setup: setup-deps
setup: setup-deps env
yarn global add wasm-pack
cargo install cargo-watch
cargo install sqlx-cli
rustup target add wasm32-unknown-unknown
rustup target add {{arch}}-unknown-linux-musl

setup-deps:
yarn
Expand Down Expand Up @@ -53,9 +68,30 @@ db-reset:

db-up:
docker network create --driver bridge scuffle-dev || true
docker compose --file ./development-docker/db.docker-compose.yaml up -d
echo "DATABASE_URL=postgres://postgres:postgres@localhost:5432/scuffle-dev" > .env
docker compose --file ./dev-stack/db.docker-compose.yaml up -d
just db-migrate

env:
test -f .env || (\
test -f .env.bak && (\
mv .env.bak .env \
) || (\
echo "DATABASE_URL=postgres://postgres:postgres@localhost:5432/scuffle-dev" > .env \
) \
)

db-down:
docker compose --file ./development-docker/db.docker-compose.yaml down
docker compose --file ./dev-stack/db.docker-compose.yaml down

stack-init:
cp ./dev-stack/stack-example.docker-compose.yaml ./dev-stack/stack.docker-compose.yaml

stack-up:
docker network create --driver bridge scuffle-dev || true
docker compose --file ./dev-stack/stack.docker-compose.yaml up -d --build

stack-down:
docker compose --file ./dev-stack/stack.docker-compose.yaml down

stack-logs *ARGS:
docker compose --file ./dev-stack/stack.docker-compose.yaml logs {{ ARGS }}
6 changes: 5 additions & 1 deletion backend/api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::sync::Arc;

use anyhow::Result;
use common::logging;
use tokio::select;

mod api;
mod config;
Expand All @@ -18,7 +19,10 @@ async fn main() -> Result<()> {

tracing::info!("starting");

api::run(global).await?;
select! {
_ = api::run(global.clone()) => tracing::info!("api stopped"),
_ = tokio::signal::ctrl_c() => tracing::info!("ctrl-c received"),
}

Ok(())
}
File renamed without changes.
File renamed without changes.
34 changes: 34 additions & 0 deletions dev-stack/stack-example.docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
version: "3.1"

services:
api:
build:
context: ..
dockerfile: docker/api.Dockerfile
ports:
- "8080:8080"
environment:
- SCUF_DATABASE_URL=postgres://postgres:postgres@postgres:5432/scuffle-dev
frontend:
build:
context: ..
dockerfile: docker/frontend.Dockerfile
ports:
- "4000:4000"
edge:
build:
context: ..
dockerfile: docker/edge.Dockerfile
ingest:
build:
context: ..
dockerfile: docker/ingest.Dockerfile
transcoder:
build:
context: ..
dockerfile: docker/transcoder.Dockerfile

networks:
default:
name: scuffle-dev
external: true
7 changes: 7 additions & 0 deletions docker/api.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM scratch

COPY target/x86_64-unknown-linux-musl/release/api /app/

STOPSIGNAL SIGINT

ENTRYPOINT ["/app/api"]
25 changes: 25 additions & 0 deletions docker/base-build.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# syntax = docker/dockerfile:1.4
FROM rust:1.66.1-alpine3.17

RUN <<eot
# Install all dependencies for building the backend and frontend

# CVEs fixed in 3.0.8-r0
apk add --no-cache libssl3=3.0.8-r0 libcrypto3=3.0.8-r0 openssl-dev=3.0.8-r0

# We need to install nodejs to build the frontend
apk add --no-cache nodejs=18.14.1-r0 yarn=1.22.19-r0

# We need to install just to use our build script
apk add --no-cache just=1.8.0-r0 musl-dev=1.2.3-r4 curl=7.87.0-r2

# Install wasm-pack
yarn global add wasm-pack

# Add wasm build target
rustup target add wasm32-unknown-unknown

# Clean up cache files
rm -r /usr/local/cargo/registry
yarn cache clean
eot
7 changes: 7 additions & 0 deletions docker/edge.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM scratch

COPY target/x86_64-unknown-linux-musl/release/edge /app/

STOPSIGNAL SIGINT

ENTRYPOINT ["/app/edge"]
15 changes: 15 additions & 0 deletions docker/frontend.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM denoland/deno:alpine-1.30.3

# CVEs fixed in 3.0.8-r0
RUN apk add --no-cache libssl3=3.0.8-r0 libcrypto3=3.0.8-r0

COPY frontend/website/server.ts /app/
COPY frontend/website/build /app/build

WORKDIR /app

RUN deno cache --unstable server.ts

STOPSIGNAL SIGINT

ENTRYPOINT ["deno", "run", "--allow-env", "--allow-read", "--allow-net", "server.ts"]
7 changes: 7 additions & 0 deletions docker/ingest.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM scratch

COPY target/x86_64-unknown-linux-musl/release/ingest /app/

STOPSIGNAL SIGINT

ENTRYPOINT ["/app/ingest"]
7 changes: 7 additions & 0 deletions docker/transcoder.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM scratch

COPY target/x86_64-unknown-linux-musl/release/transcoder /app/

STOPSIGNAL SIGINT

ENTRYPOINT ["/app/transcoder"]
10 changes: 10 additions & 0 deletions frontend/website/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.DS_Store
node_modules
/build
/.svelte-kit
/package
.env
.env.*
vite.config.js.timestamp-*
vite.config.ts.timestamp-*
wasm.d.ts
4 changes: 2 additions & 2 deletions frontend/website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
"wasm": "just --justfile $(realpath ../player/Justfile) build",
"wasm:dev": "just --justfile $(realpath ../player/Justfile) dev",
"wasm:watch": "cargo-watch -w $(realpath ../player) -C $(pwd) -s \"yarn wasm:dev\"",
"clean": "rm -rf node_modules .svelte-kit wasm.d.ts ../player/pkg"
"clean": "rm -rf build .svelte-kit wasm.d.ts ../player/pkg"
},
"devDependencies": {
"@playwright/test": "^1.28.1",
"@sveltejs/adapter-auto": "^2.0.0",
"@sveltejs/kit": "^1.5.0",
"@types/fs-extra": "^11.0.1",
"@typescript-eslint/eslint-plugin": "^5.45.0",
Expand All @@ -33,6 +32,7 @@
"prettier": "^2.8.0",
"prettier-plugin-svelte": "^2.8.1",
"svelte": "^3.54.0",
"svelte-adapter-deno": "^0.9.0",
"svelte-check": "^3.0.1",
"tslib": "^2.4.1",
"typescript": "^4.9.3",
Expand Down
31 changes: 31 additions & 0 deletions frontend/website/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Application, Router } from "https://deno.land/x/[email protected]/mod.ts";
import { handler } from "./build/handler.js";

const app = new Application();

// add a route that lives separately from the SvelteKit app
const router = new Router();

router.get("/healthcheck", (ctx) => {
ctx.response.body = "ok";
});

app.use(router.routes());
app.use(router.allowedMethods());

// let SvelteKit handle everything else, including serving prerendered pages and static assets
app.use(handler);

app.addEventListener("listen", () => {
console.log("listening on port 3000");
});

const shutdownHandler = () => {
console.log("interrupted!");
Deno.exit();
};

Deno.addSignalListener("SIGINT", shutdownHandler);
Deno.addSignalListener("SIGTERM", shutdownHandler);

await app.listen({ port: 3000 });
2 changes: 1 addition & 1 deletion frontend/website/svelte.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import adapter from "@sveltejs/adapter-auto";
import adapter from "svelte-adapter-deno";
import { vitePreprocess } from "@sveltejs/kit/vite";

/** @type {import('@sveltejs/kit').Config} */
Expand Down
Loading

0 comments on commit 657ebc7

Please sign in to comment.