Skip to content

Commit 7e3dbe0

Browse files
authored
[Nexus] Add docker image and set up CD (#32)
1 parent 7e08cf5 commit 7e3dbe0

File tree

19 files changed

+289
-52
lines changed

19 files changed

+289
-52
lines changed

.github/workflows/__CD__deploy-image.yml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ on:
1717
required: true
1818
ice-port-range:
1919
required: true
20+
admin-username:
21+
required: true
22+
admin-password:
23+
required: true
2024

2125
jobs:
2226
deploy-image:
@@ -34,5 +38,14 @@ jobs:
3438
export TAG=${TAG#*-v}
3539
docker stop ${{ inputs.app-name }}
3640
docker rm ${{ inputs.app-name }}
37-
docker run -d --restart unless-stopped --name ${{ inputs.app-name }} -e SECRET_KEY_BASE=${{ secrets.secret-key-base }} -e PHX_HOST=${{ secrets.phx-host }} -e ICE_PORT_RANGE=${{ secrets.ice-port-range }} --network host ghcr.io/elixir-webrtc/apps/${{ inputs.app-name }}:${TAG}
41+
docker run -d \
42+
--restart unless-stopped \
43+
--name ${{ inputs.app-name }} \
44+
-e SECRET_KEY_BASE=${{ secrets.secret-key-base }} \
45+
-e PHX_HOST=${{ secrets.phx-host }} \
46+
-e ICE_PORT_RANGE=${{ secrets.ice-port-range }} \
47+
-e ADMIN_USERNAME=${{ secrets.admin-username }} \
48+
-e ADMIN_PASSWORD=${{ secrets.admin-password }} \
49+
--network host \
50+
ghcr.io/elixir-webrtc/apps/${{ inputs.app-name }}:${TAG}
3851
docker image prune --all --force

.github/workflows/__CD__nexus.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Nexus CD
2+
3+
on:
4+
push:
5+
tags:
6+
- "nexus-v*.*.*"
7+
8+
permissions:
9+
contents: read
10+
packages: write
11+
12+
jobs:
13+
build-publish-nexus-image:
14+
name: "Build and publish Nexus image"
15+
uses: ./.github/workflows/__CD__build-publish-image.yml
16+
with:
17+
app-name: nexus
18+
deploy-nexus:
19+
name: "Deploy Nexus image"
20+
needs: build-publish-nexus-image
21+
uses: ./.github/workflows/__CD__deploy-image.yml
22+
with:
23+
app-name: nexus
24+
secrets:
25+
ssh-host: ${{ secrets.NEXUS_SSH_HOST }}
26+
ssh-username: ${{ secrets.NEXUS_SSH_USERNAME }}
27+
ssh-priv-key: ${{ secrets.NEXUS_SSH_PRIV_KEY }}
28+
secret-key-base: ${{ secrets.NEXUS_SECRET_KEY_BASE }}
29+
phx-host: ${{ secrets.NEXUS_PHX_HOST }}
30+
ice-port-range: ${{ secrets.NEXUS_ICE_PORT_RANGE }}
31+
admin-username: ${{ secrets.NEXUS_ADMIN_USERNAME }}
32+
admin-password: ${{ secrets.NEXUS_ADMIN_PASSWORD }}

.github/workflows/__CD__recognizer.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,5 @@ jobs:
2828
secret-key-base: ${{ secrets.RECOGNIZER_SECRET_KEY_BASE }}
2929
phx-host: ${{ secrets.RECOGNIZER_PHX_HOST }}
3030
ice-port-range: ${{ secrets.RECOGNIZER_ICE_PORT_RANGE }}
31+
admin-username: ${{ secrets.RECOGNIZER_ADMIN_USERNAME }}
32+
admin-password: ${{ secrets.RECOGNIZER_ADMIN_PASSWORD }}

broadcaster/config/dev.exs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ config :broadcaster, BroadcasterWeb.Endpoint,
5151
]
5252
]
5353

54-
# Enable dev routes for dashboard and mailbox
55-
config :broadcaster, dev_routes: true
56-
5754
# Do not include metadata nor timestamps in development logs
5855
config :logger, :console, format: "[$level] $message\n"
5956

nexus/.dockerignore

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# This file excludes paths from the Docker build context.
2+
#
3+
# By default, Docker's build context includes all files (and folders) in the
4+
# current directory. Even if a file isn't copied into the container it is still sent to
5+
# the Docker daemon.
6+
#
7+
# There are multiple reasons to exclude files from the build context:
8+
#
9+
# 1. Prevent nested folders from being copied into the container (ex: exclude
10+
# /assets/node_modules when copying /assets)
11+
# 2. Reduce the size of the build context and improve build time (ex. /build, /deps, /doc)
12+
# 3. Avoid sending files containing sensitive information
13+
#
14+
# More information on using .dockerignore is available here:
15+
# https://docs.docker.com/engine/reference/builder/#dockerignore-file
16+
17+
.dockerignore
18+
19+
# Ignore git, but keep git HEAD and refs to access current commit hash if needed:
20+
#
21+
# $ cat .git/HEAD | awk '{print ".git/"$2}' | xargs cat
22+
# d0b8727759e1e0e7aa3d41707d12376e373d5ecc
23+
.git
24+
!.git/HEAD
25+
!.git/refs
26+
27+
# Common development/test artifacts
28+
/cover/
29+
/doc/
30+
/test/
31+
/tmp/
32+
.elixir_ls
33+
34+
# Mix artifacts
35+
/_build/
36+
/deps/
37+
*.ez
38+
39+
# Generated on crash by the VM
40+
erl_crash.dump
41+
42+
# Static artifacts - These should be fetched and built inside the Docker image
43+
/assets/node_modules/
44+
/priv/static/assets/
45+
/priv/static/cache_manifest.json

nexus/Dockerfile

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Find eligible builder and runner images on Docker Hub. We use Ubuntu/Debian
2+
# instead of Alpine to avoid DNS resolution issues in production.
3+
#
4+
# https://hub.docker.com/r/hexpm/elixir/tags?page=1&name=ubuntu
5+
# https://hub.docker.com/_/ubuntu?tab=tags
6+
#
7+
# This file is based on these images:
8+
#
9+
# - https://hub.docker.com/r/hexpm/elixir/tags - for the build image
10+
# - https://hub.docker.com/_/debian?tab=tags&page=1&name=bullseye-20231009-slim - for the release image
11+
# - https://pkgs.org/ - resource for finding needed packages
12+
# - Ex: hexpm/elixir:1.16.0-erlang-26.2.1-debian-bullseye-20231009-slim
13+
#
14+
ARG ELIXIR_VERSION=1.17.2
15+
ARG OTP_VERSION=27.0.1
16+
ARG DEBIAN_VERSION=bookworm-20240701-slim
17+
18+
ARG BUILDER_IMAGE="hexpm/elixir:${ELIXIR_VERSION}-erlang-${OTP_VERSION}-debian-${DEBIAN_VERSION}"
19+
ARG RUNNER_IMAGE="debian:${DEBIAN_VERSION}"
20+
21+
FROM ${BUILDER_IMAGE} as builder
22+
23+
# install build dependencies
24+
RUN apt-get update -y && apt-get install -y build-essential git pkg-config libssl-dev \
25+
&& apt-get clean && rm -f /var/lib/apt/lists/*_*
26+
27+
# prepare build dir
28+
WORKDIR /app
29+
30+
# install hex + rebar
31+
RUN mix local.hex --force && \
32+
mix local.rebar --force
33+
34+
# set build ENV
35+
ENV MIX_ENV="prod"
36+
37+
# install mix dependencies
38+
COPY mix.exs mix.lock ./
39+
RUN mix deps.get --only $MIX_ENV
40+
RUN mkdir config
41+
42+
# copy compile-time config files before we compile dependencies
43+
# to ensure any relevant config change will trigger the dependencies
44+
# to be re-compiled.
45+
COPY config/config.exs config/${MIX_ENV}.exs config/
46+
RUN mix deps.compile
47+
48+
COPY priv priv
49+
50+
COPY lib lib
51+
52+
COPY assets assets
53+
54+
# compile assets
55+
RUN mix assets.deploy
56+
57+
# Compile the release
58+
RUN mix compile
59+
60+
# Changes to config/runtime.exs don't require recompiling the code
61+
COPY config/runtime.exs config/
62+
63+
COPY rel rel
64+
RUN mix release
65+
66+
# start a new build stage so that the final image will only contain
67+
# the compiled release and other runtime necessities
68+
FROM ${RUNNER_IMAGE}
69+
70+
RUN apt-get update -y && \
71+
apt-get install -y libstdc++6 openssl libncurses5 locales ca-certificates \
72+
&& apt-get clean && rm -f /var/lib/apt/lists/*_*
73+
74+
# Set the locale
75+
RUN sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen && locale-gen
76+
77+
ENV LANG en_US.UTF-8
78+
ENV LANGUAGE en_US:en
79+
ENV LC_ALL en_US.UTF-8
80+
81+
WORKDIR "/app"
82+
RUN chown nobody /app
83+
84+
# set runner ENV
85+
ENV MIX_ENV="prod"
86+
87+
# Only copy the final release from the build stage
88+
COPY --from=builder --chown=nobody:root /app/_build/${MIX_ENV}/rel/nexus ./
89+
90+
USER nobody
91+
92+
# If using an environment that doesn't automatically reap zombie processes, it is
93+
# advised to add an init process such as tini via `apt-get install`
94+
# above and adding an entrypoint. See https://github.com/krallin/tini for details
95+
# ENTRYPOINT ["/tini", "--"]
96+
97+
CMD ["/app/bin/server"]

nexus/README.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,45 @@ mix phx.server
1616
Now you can visit [`localhost:4000`](http://localhost:4000) from your browser.
1717
If you join from another tab/browser on the same device, you should see two streams.
1818

19-
### Caveats
19+
## Running with Docker
20+
21+
You can also run Nexus using Docker.
22+
23+
Build an image (or use `ghcr.io/elixir-webrtc/apps/nexus:latest`):
24+
25+
```
26+
docker build -t nexus .
27+
```
28+
29+
and run:
30+
31+
```
32+
docker run \
33+
-e SECRET_KEY_BASE="secert" \
34+
-e PHX_HOST=localhost \
35+
-e ADMIN_USERNAME=admin \
36+
-e ADMIN_PASSWORD=admin \
37+
--network host \
38+
nexus
39+
```
40+
41+
Note that secret has to be at least 64 bytes long.
42+
You can generate one with `mix phx.gen.secret`.
43+
44+
If you are running on MacOS, instead of using `--network host` option, you have to explicitly publish ports:
45+
46+
```
47+
docker run \
48+
-e SECRET_KEY_BASE="secert" \
49+
-e PHX_HOST=localhost \
50+
-e ADMIN_USERNAME=admin \
51+
-e ADMIN_PASSWORD=admin \
52+
-p 4000:4000 \
53+
-p 50000-50010/udp \
54+
nexus
55+
```
56+
57+
## Caveats
2058

2159
Seeing as access to video and audio devices requires the browser to be
2260
in a [secure context](https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts),

nexus/config/dev.exs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ config :nexus, NexusWeb.Endpoint,
5151
]
5252
]
5353

54-
# Enable dev routes for dashboard and mailbox
55-
config :nexus, dev_routes: true
56-
5754
# Do not include metadata nor timestamps in development logs
5855
config :logger, :console, format: "[$level] $message\n"
5956

nexus/config/runtime.exs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,25 @@ import Config
1616
#
1717
# Alternatively, you can use `mix phx.gen.release` to generate a `bin/server`
1818
# script that automatically sets the env var above.
19+
read_ice_port_range! = fn ->
20+
case System.get_env("ICE_PORT_RANGE") do
21+
nil ->
22+
[0]
23+
24+
raw_port_range ->
25+
case String.split(raw_port_range, "-", parts: 2) do
26+
[from, to] -> String.to_integer(from)..String.to_integer(to)
27+
_other -> raise "ICE_PORT_RANGE has to be in form of FROM-TO, passed: #{raw_port_range}"
28+
end
29+
end
30+
end
31+
1932
if System.get_env("PHX_SERVER") do
2033
config :nexus, NexusWeb.Endpoint, server: true
2134
end
2235

36+
config :nexus, ice_port_range: read_ice_port_range!.()
37+
2338
if config_env() == :prod do
2439
# The secret key base is used to sign/encrypt cookies and other secrets.
2540
# A default value is used in config/dev.exs and config/test.exs but you

nexus/lib/nexus/application.ex

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,7 @@ defmodule Nexus.Application do
88
@version Mix.Project.config()[:version]
99

1010
@spec version() :: String.t()
11-
def version() do
12-
"v#{@version} #{commit()}"
13-
end
14-
15-
defp commit() do
16-
case System.cmd("git", ["rev-parse", "--short", "HEAD"]) do
17-
{hash, 0} -> "(#{String.trim(hash)})"
18-
_ -> ""
19-
end
20-
end
11+
def version(), do: @version
2112

2213
@impl true
2314
def start(_type, _args) do

0 commit comments

Comments
 (0)