Skip to content

Commit

Permalink
[RTC-345] Prefix env vars with JF (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
mickel8 committed Sep 22, 2023
1 parent b016886 commit 5888e33
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 52 deletions.
52 changes: 40 additions & 12 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -1,19 +1,47 @@
# used by the server e.g. to create tokens
SECRET_KEY_BASE=super-secret-key
# IP and PORT an HTTP endpoint will listen to
# In Docker, JF_PORT defaults to 8080
# JF_IP=0.0.0.0
# JF_PORT=8080

# true, if WebRTC peers are used
WEBRTC_USED=true

# hostname used to generate URLs throught the server
VIRTUAL_HOST=localhost
PORT=5002
SERVER_API_TOKEN=development
# Defines how Jellyfish is seen from the outside.
# It can be in one of the following forms:
# * ip:port
# * fqdn:port
# * fqdn
# By default, it is equal to "JF_IP:JF_PORT".
JF_HOST=localhost:8080

# JF_METRICS_IP=0.0.0.0
# JF_METRICS_PORT=9568

# Token used for authorizing HTTP requests
JF_SERVER_API_TOKEN=jellyfish_docker_token

# Used by the server e.g. to create client tokens.
# If not set, it will be generated.
# JF_SECRET_KEY_BASE=super-secret-key

# Decide if jellyfish will check origin of requests
# CHECK_ORIGIN=false
# JF_CHECK_ORIGIN=true

# Where Jellyfish should save its artifacts
# You can get access to this directory e.g. by mounting
# a volume with:
#
# -v $(pwd)/jellyfish_output:/app/jellyfish_output
#
# JF_OUTPUT_BASE_PATH=/app/jellyfish_output


# WEBRTC ENVS

# true, if WebRTC peers are used
JF_WEBRTC_USED=true

# TURN default configuration
# note: loopback address as INTEGRATED_TURN_IP cannot be used inside a Docker container
INTEGRATED_TURN_IP=<your_public_ip_address>
INTEGRATED_TURN_LISTEN_IP=0.0.0.0
INTEGRATED_TURN_PORT_RANGE=50000-65355
# note: when running locally, JF_INTEGRATED_TURN_IP can be your private ip address
JF_INTEGRATED_TURN_IP=<your_public_ip_address>
JF_INTEGRATED_TURN_LISTEN_IP=0.0.0.0
JF_INTEGRATED_TURN_PORT_RANGE=50000-50050
12 changes: 9 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,15 @@ RUN \
WORKDIR /app

# base path where jellyfish saves its artefacts
ENV OUTPUT_BASE_PATH=./jellyfish_output
ENV JF_OUTPUT_BASE_PATH=./jellyfish_output

RUN mkdir ${OUTPUT_BASE_PATH} && chown jellyfish:jellyfish ${OUTPUT_BASE_PATH}
# override default (127, 0, 0, 1) IP by 0.0.0.0
# as docker doesn't allow for connections outside the
# container when we listen to 127.0.0.1
ENV JF_IP=0.0.0.0
ENV JF_METRICS_IP=0.0.0.0

RUN mkdir ${JF_OUTPUT_BASE_PATH} && chown jellyfish:jellyfish ${JF_OUTPUT_BASE_PATH}

COPY --from=build /app/_build/prod/rel/jellyfish ./

Expand All @@ -109,7 +115,7 @@ RUN chmod +x docker-entrypoint.sh

ENV HOME=/app

HEALTHCHECK CMD curl --fail -H "authorization: Bearer ${SERVER_API_TOKEN}" http://localhost:${PORT}/room || exit 1
HEALTHCHECK CMD curl --fail -H "authorization: Bearer ${JF_SERVER_API_TOKEN}" http://localhost:${JF_PORT:-8080}/room || exit 1

ENTRYPOINT ["./docker-entrypoint.sh"]

Expand Down
2 changes: 1 addition & 1 deletion config/prod.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ config :logger, level: :info

# run the server automatically when using prod release
config :jellyfish, JellyfishWeb.Endpoint,
http: [ip: {0, 0, 0, 0, 0, 0, 0, 0}, port: 8080],
http: [ip: {127, 0, 0, 1}, port: 8080],
server: true

# Runtime production configuration, including reading
Expand Down
73 changes: 42 additions & 31 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,58 +11,59 @@ alias Jellyfish.ConfigReader
config :ex_dtls, impl: :nif
config :opentelemetry, traces_exporter: :none

hosts = ConfigReader.read_nodes("NODES")
nodes = ConfigReader.read_nodes("JF_NODES")

if hosts do
if nodes do
config :libcluster,
topologies: [
epmd_cluster: [
strategy: Cluster.Strategy.Epmd,
config: [hosts: hosts]
config: [hosts: nodes]
]
]
end

prod? = config_env() == :prod

ip =
ConfigReader.read_ip("JF_IP") ||
Application.get_env(:jellyfish, JellyfishWeb.Endpoint)[:http][:ip]

port =
ConfigReader.read_port("JF_PORT") ||
Application.get_env(:jellyfish, JellyfishWeb.Endpoint)[:http][:port]

host =
case System.get_env("HOST") do
nil when prod? -> raise "Unset HOST environment variable"
nil -> "localhost"
case System.get_env("JF_HOST") do
nil -> "#{:inet.ntoa(ip)}:#{port}"
other -> other
end

port =
ConfigReader.read_port("PORT") ||
Application.get_env(:jellyfish, JellyfishWeb.Endpoint)[:http][:port]
{host_name, host_port} =
case String.split(host, ":") do
[host_name, host_port] -> {host_name, String.to_integer(host_port)}
_ -> {host, 443}
end

config :jellyfish,
webrtc_used: ConfigReader.read_boolean("WEBRTC_USED") || true,
integrated_turn_ip: ConfigReader.read_ip("INTEGRATED_TURN_IP") || {127, 0, 0, 1},
integrated_turn_listen_ip: ConfigReader.read_ip("INTEGRATED_TURN_LISTEN_IP") || {127, 0, 0, 1},
webrtc_used: ConfigReader.read_boolean("JF_WEBRTC_USED") || true,
integrated_turn_ip: ConfigReader.read_ip("JF_INTEGRATED_TURN_IP") || {127, 0, 0, 1},
integrated_turn_listen_ip:
ConfigReader.read_ip("JF_INTEGRATED_TURN_LISTEN_IP") || {127, 0, 0, 1},
integrated_turn_port_range:
ConfigReader.read_port_range("INTEGRATED_TURN_PORT_RANGE") || {50_000, 59_999},
integrated_turn_tcp_port: ConfigReader.read_port("INTEGRATED_TURN_TCP_PORT"),
ConfigReader.read_port_range("JF_INTEGRATED_TURN_PORT_RANGE") || {50_000, 59_999},
integrated_turn_tcp_port: ConfigReader.read_port("JF_INTEGRATED_TURN_TCP_PORT"),
jwt_max_age: 24 * 3600,
output_base_path: System.get_env("OUTPUT_BASE_PATH", "jellyfish_output") |> Path.expand(),
address: System.get_env("JELLYFISH_ADDRESS") || "#{host}:#{port}",
metrics_ip: ConfigReader.read_ip("METRICS_IP") || {127, 0, 0, 1},
metrics_port: ConfigReader.read_port("METRICS_PORT") || 9568

config :jellyfish, JellyfishWeb.Endpoint,
secret_key_base:
System.get_env("SECRET_KEY_BASE") || Base.encode64(:crypto.strong_rand_bytes(48)),
http: [port: port]
output_base_path: System.get_env("JF_OUTPUT_BASE_PATH", "jellyfish_output") |> Path.expand(),
address: "#{host}",
metrics_ip: ConfigReader.read_ip("JF_METRICS_IP") || {127, 0, 0, 1},
metrics_port: ConfigReader.read_port("JF_METRICS_PORT") || 9568

if check_origin = ConfigReader.read_boolean("CHECK_ORIGIN") do
config :jellyfish, JellyfishWeb.Endpoint, check_origin: check_origin
end

case System.get_env("SERVER_API_TOKEN") do
case System.get_env("JF_SERVER_API_TOKEN") do
nil when prod? == true ->
raise """
environment variable SERVER_API_TOKEN is missing.
SERVER_API_TOKEN is used for HTTP requests and
environment variable JF_SERVER_API_TOKEN is missing.
JF_SERVER_API_TOKEN is used for HTTP requests and
server WebSocket authorization.
"""

Expand All @@ -73,6 +74,16 @@ case System.get_env("SERVER_API_TOKEN") do
config :jellyfish, server_api_token: token
end

config :jellyfish, JellyfishWeb.Endpoint,
secret_key_base:
System.get_env("JF_SECRET_KEY_BASE") || Base.encode64(:crypto.strong_rand_bytes(48)),
http: [ip: ip, port: port],
url: [host: host_name, port: host_port]

if check_origin = ConfigReader.read_boolean("JF_CHECK_ORIGIN") do
config :jellyfish, JellyfishWeb.Endpoint, check_origin: check_origin
end

if prod? do
config :jellyfish, JellyfishWeb.Endpoint, url: [host: host, port: 443, scheme: "https"]
config :jellyfish, JellyfishWeb.Endpoint, url: [scheme: "https"]
end
11 changes: 6 additions & 5 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ x-jellyfish-template: &jellyfish-template
build: .
environment: &jellyfish-environment
ERLANG_COOKIE: "panuozzo-pollo-e-pancetta"
SERVER_API_TOKEN: "development"
HOST: "localhost"
NODES: "app@app1 app@app2"
JF_SERVER_API_TOKEN: "development"
JF_NODES: "app@app1 app@app2"
networks:
- net1
restart: on-failure
Expand Down Expand Up @@ -37,7 +36,8 @@ services:
<<: *jellyfish-environment
RELEASE_NODE: app@app1
NODE_NAME: app@app1
PORT: 4001
JF_HOST: "localhost:4001"
JF_PORT: 4001
ports:
- 4001:4001

Expand All @@ -47,7 +47,8 @@ services:
<<: *jellyfish-environment
RELEASE_NODE: app@app2
NODE_NAME: app@app2
PORT: 4002
JF_HOST: "localhost:4002"
JF_PORT: 4002
ports:
- 4002:4002

Expand Down

0 comments on commit 5888e33

Please sign in to comment.