Skip to content

Commit

Permalink
Update OTP and elixir and use debian image
Browse files Browse the repository at this point in the history
  • Loading branch information
ringvold committed Nov 23, 2024
1 parent a97b101 commit 720cfcd
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 79 deletions.
100 changes: 51 additions & 49 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,83 +1,85 @@
ARG ELIXIR_VERSION=1.17.2
ARG OTP_VERSION=27.0.1
# ARG DEBIAN_VERSION=bookworm-20240701-slim
ARG ALPINE_VERSION=3.18.7
ARG DEBIAN_VERSION=bookworm-20240701-slim

ARG BUILDER_IMAGE="hexpm/elixir:${ELIXIR_VERSION}-erlang-${OTP_VERSION}-alpine-${ALPINE_VERSION}"
# ARG RUNNER_IMAGE="hexpm/elixir:${ELIXIR_VERSION}-erlang-${OTP_VERSION}-alpine-${ALPINE_VERSION}"
ARG RUNNER_IMAGE="alpine:${ALPINE_VERSION}"
ARG BUILDER_IMAGE="hexpm/elixir:${ELIXIR_VERSION}-erlang-${OTP_VERSION}-debian-${DEBIAN_VERSION}"
ARG RUNNER_IMAGE="debian:${DEBIAN_VERSION}"


###
### Fist Stage - Building the Release
###
FROM ${BUILDER_IMAGE} AS build
FROM ${BUILDER_IMAGE} as builder

# install build dependencies
# git needed for getting ueberauth_steam
RUN apk add --no-cache build-base git
RUN apt-get update -y && apt-get install -y build-essential git \
&& apt-get clean && rm -f /var/lib/apt/lists/*_*

# prepare build dir
WORKDIR /app

# extend hex timeout
ENV HEX_HTTP_TIMEOUT=20

# install hex + rebar
RUN mix local.hex --force && \
mix local.rebar --force

# set build ENV as prod
ENV MIX_ENV=prod
ENV SECRET_KEY_BASE=nokey
ENV DATABASE_URL=$DATABASE_URL
# set build ENV
ENV MIX_ENV="prod"

# Copy over the mix.exs and mix.lock files to load the dependencies. If those
# files don't change, then we don't keep re-fetching and rebuilding the deps.
# install mix dependencies
COPY mix.exs mix.lock ./
COPY config config
RUN mix deps.get --only $MIX_ENV
RUN mkdir config

RUN mix deps.get --only prod && \
mix deps.compile
# copy compile-time config files before we compile dependencies
# to ensure any relevant config change will trigger the dependencies
# to be re-compiled.
COPY config/config.exs config/${MIX_ENV}.exs config/
RUN mix deps.compile

COPY priv priv
COPY assets assets

# NOTE: If using TailwindCSS, it uses a special "purge" step and that requires
# the code in `lib` to see what is being used. Uncomment that here before
# running the npm deploy script if that's the case.
COPY lib lib

# build assets
COPY assets assets

# compile assets
RUN mix assets.deploy
RUN mix phx.digest

# copy source here if not using TailwindCSS
COPY lib lib
# Compile the release
RUN mix compile

# Changes to config/runtime.exs don't require recompiling the code
COPY config/runtime.exs config/

# compile and build release
COPY rel rel
RUN mix do compile, release
RUN mix release

###
### Second Stage - Setup the Runtime Environment
###
# start a new build stage so that the final image will only contain
# the compiled release and other runtime necessities
FROM ${RUNNER_IMAGE}

# prepare release docker image
FROM ${RUNNER_IMAGE} AS app
RUN apk add --no-cache libstdc++ openssl ncurses-libs
RUN apt-get update -y && \
apt-get install -y libstdc++6 openssl libncurses5 locales ca-certificates \
&& apt-get clean && rm -f /var/lib/apt/lists/*_*

WORKDIR /app
# Set the locale
RUN sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen && locale-gen

ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8

WORKDIR "/app"
RUN chown nobody /app

# set runner ENV
ENV MIX_ENV="prod"

RUN chown nobody:nobody /app
# Only copy the final release from the build stage
COPY --from=builder --chown=nobody:root /app/_build/${MIX_ENV}/rel/get5_api ./

USER nobody:nobody
USER nobody

COPY --from=build --chown=nobody:nobody /app/_build/prod/rel/get5_api ./
# If using an environment that doesn't automatically reap zombie processes, it is
# advised to add an init process such as tini via `apt-get install`
# above and adding an entrypoint. See https://github.com/krallin/tini for details
# ENTRYPOINT ["/tini", "--"]

ENV HOME=/app
ENV MIX_ENV=prod
ENV SECRET_KEY_BASE=nokey
ENV PORT=4000
CMD ["/app/bin/server"]

CMD ["bin/get5_api", "start"]
2 changes: 1 addition & 1 deletion Dockerfile-dev
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM elixir:1.15.7
FROM elixir:1.17.2

RUN apt-get update && \
apt-get install -y postgresql-client
Expand Down
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ in alignment with the projects plans. 😄

### All in docker

**NB: This section is outdated. Need to find a docker image for CS2. Any tips?**

Run both app and database (and csgo server) in docker.

1. Start the database and server with `docker-compose up`. This might take a while.
Expand All @@ -104,7 +106,7 @@ Enjoy some quiet time while downloading and compiling. 😄

If you also wish to run a CSGO server configured with Get5 you can uncomment
the csgo service in `docker-compose.yml`. ***Note:** this image does not work on
M1/Apple Silicon and the server needs to be serves somewhere else.*
M1/Apple Silicon and the server needs to be served somewhere else.*
Check out the repo for that image on more information about how it works: https://github.com/ringvold/csgo


Expand Down Expand Up @@ -145,12 +147,12 @@ source ~/.zshrc
asdf plugin-add erlang
asdf plugin-add elixir
asdf install elixir 1.14.2-otp-25
asdf install erlang 25.1.2
# install the versions specified in .tool-versions
asdf install
# Set the installed versions av default
asdf global erlang 25.1.2
asdf global elixir 1.14.2-otp-25
# Set the installed versions av default if you wish
asdf global erlang erlang 27.0.1
asdf global elixir elixir 1.17.2-otp-27
mix local.hex
mix local.rebar
Expand Down
57 changes: 35 additions & 22 deletions lib/get5_api_web/live/match_live/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,55 +24,79 @@ defmodule Get5ApiWeb.MatchLive.Show do
match = socket.assigns.entity || Matches.get_match!(id)
map_stats = Stats.get_by_match(id)
player_stats = Stats.player_stats_by_match(id)
game_server = socket.assigns.entity.game_server

{:noreply,
socket
|> assign(:page_title, page_title(socket.assigns.live_action))
|> assign(:match, match)
|> assign(:map_stats, map_stats)
|> assign(:player_stats, player_stats)
|> start_async(:get_status, fn -> Get5Client.status(socket.assigns.entity.game_server) end)}
|> start_async(:get_status, fn -> Get5Client.status(game_server) end)}
end

@impl true
def handle_event("get_status", _params, socket) do
game_server = socket.assigns.match.game_server

{:noreply,
socket
|> assign(status: AsyncResult.loading())
|> start_async(:get_status, fn -> Get5Client.status(socket.assigns.match.game_server) end)}
|> start_async(:get_status, fn -> Get5Client.status(game_server) end)}
end

@impl true
def handle_event("start_match", _params, socket) do
if socket.assigns.match.user_id == socket.assigns.current_user.id do
match = socket.assigns.match

{:noreply,
socket
|> start_async(:start_match, fn -> Get5Client.start_match(socket.assigns.match) end)}
|> start_async(:start_match, fn -> Get5Client.start_match(match) end)}
else
{:noreply,
socket
|> put_flash(:error, gettext("You are not allowed to start this match"))}
end
end

@impl true
def handle_event("end_match", _params, socket) do
if socket.assigns.match.user_id == socket.assigns.current_user.id do
match = socket.assigns.match

{:noreply,
socket
|> start_async(:end_match, fn -> Get5Client.end_match(match) end)}
else
{:noreply,
socket
|> put_flash(:error, gettext("You are not allowed to end this match"))}
end
end

@impl true
def handle_async(:start_match, {:ok, {:ok, _resp}}, socket) do
game_server = socket.assigns.match.game_server

{:noreply,
socket
|> start_async(:get_status, fn ->
Get5Client.status(socket.assigns.match.game_server)
Get5Client.status(game_server)
end)
|> put_flash(:info, gettext("Match sendt to server"))}
end

@impl true
def handle_async(:start_match, {:ok, {:error, error}}, socket) do
game_server = socket.assigns.match.game_server

case error do
:nxdomain ->
{:noreply,
socket
|> start_async(:get_status, fn ->
Get5Client.status(socket.assigns.match.game_server)
Get5Client.status(game_server)
end)
|> put_flash(
:error,
Expand All @@ -85,42 +109,31 @@ defmodule Get5ApiWeb.MatchLive.Show do
{:noreply,
socket
|> start_async(:get_status, fn ->
Get5Client.status(socket.assigns.match.game_server)
Get5Client.status(game_server)
end)
|> put_flash(
:error,
gettext("A match is already loaded on the server")
)}

err ->
_err ->
{:noreply,
socket
|> start_async(:get_status, fn ->
Get5Client.status(socket.assigns.match.game_server)
Get5Client.status(game_server)
end)
|> put_flash(:error, gettext("Failed to start match"))}
end
end

@impl true
def handle_event("end_match", _params, socket) do
if socket.assigns.match.user_id == socket.assigns.current_user.id do
{:noreply,
socket
|> start_async(:end_match, fn -> Get5Client.end_match(socket.assigns.match) end)}
else
{:noreply,
socket
|> put_flash(:error, gettext("You are not allowed to end this match"))}
end
end
def handle_async(:end_match, {:ok, {:ok, _msg}}, socket) do
game_server = socket.assigns.match.game_server

@impl true
def handle_async(:end_match, {:ok, {:ok, msg}}, socket) do
{:noreply,
socket
|> start_async(:get_status, fn ->
Get5Client.status(socket.assigns.match.game_server)
Get5Client.status(game_server)
end)
|> put_flash(:info, gettext("Match ended"))}
end
Expand Down
2 changes: 1 addition & 1 deletion lib/get5_api_web/live/user_login_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ defmodule Get5ApiWeb.UserLoginLive do
end

def mount(_params, _session, socket) do
email = live_flash(socket.assigns.flash, :email)
email = Phoenix.Flash.get(socket.assigns.flash, :email)
form = to_form(%{"email" => email}, as: "user")
{:ok, assign(socket, form: form), temporary_assigns: [form: form]}
end
Expand Down

0 comments on commit 720cfcd

Please sign in to comment.