Skip to content

Commit

Permalink
nix: fix server setup and improve deploy (#42)
Browse files Browse the repository at this point in the history
* nix: fix server setup and improve deploy + modify db scripts so the justfile works inside the vm as well
  • Loading branch information
schonfinkel authored Nov 2, 2024
1 parent 5ab10aa commit da2daff
Show file tree
Hide file tree
Showing 14 changed files with 78 additions and 38 deletions.
1 change: 1 addition & 0 deletions .github/workflows/deploy_server.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ jobs:
- name: "[Server] Push release to the server"
run: |
echo "${{ secrets.VM_ARGS }}" > $(pwd)/server/config/vm.args
echo "${{ secrets.APP_SETTINGS }}" > $(pwd)/.env
- name: "[Server] Push release to the server"
Expand Down
2 changes: 2 additions & 0 deletions .rsyncignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
.vs
.vscode
.*.~undo-tree~
.env_*
client
_build
result
erl_crash.*
LICENSE
README.org
deploy.sh
3 changes: 2 additions & 1 deletion README.org
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

* About

This is Lyceum --- an MMO game with the server written in Erlang and the client written in Zig superchanged with [[https://github.com/raysan5/raylib][raylib]].
This is Lyceum --- an MMO game with the server written in [[https://www.erlang.org/][Erlang]] and the client
written in [[https://ziglang.org/][Zig]] (superchanged with [[https://github.com/raysan5/raylib][raylib]] and [[https://github.com/dont-rely-on-nulls/zerl][Zerl]]).

* How to run

Expand Down
10 changes: 5 additions & 5 deletions deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ rsync \
. \
$DEPLOY_USER@$DEPLOY_HOST:~/$PROJECT_NAME

# Build a docker image with the release
nix build .#dockerImage
# Build the server locally
nix build .#server

# Also copy the built docker image
# And copy the build to the Host
nix copy \
--no-check-sigs \
--to ssh-ng://$DEPLOY_HOST ".#dockerImage"
--to ssh-ng://$DEPLOY_HOST ".#server"

echo "[DEPLOY] Running entrypoint script..."
PROJECT_NAME=$PROJECT_NAME \
ssh $DEPLOY_USER@$DEPLOY_HOST "cd ~/$PROJECT_NAME && ./deploy_entrypoint.sh"
ssh $DEPLOY_USER@$DEPLOY_HOST "cd ~/$PROJECT_NAME && just start"
6 changes: 0 additions & 6 deletions deploy_entrypoint.sh

This file was deleted.

2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
services:
app:
container_name: lyceum
image: lyceum:latest
image: lyceum_server:latest
network_mode: "host"
restart: always
env_file:
Expand Down
20 changes: 16 additions & 4 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
# Erlang
erlangLatest = pkgs.erlang_27;
erlangLibs = getErlangLibs erlangLatest;
erl_app = "lyceum_server";

# Zig shit (Incomplete)
zigLatest = pkgs.zig;
Expand Down Expand Up @@ -96,7 +97,7 @@
};
in
pkgs.beamPackages.rebar3Relx {
pname = "server_app";
pname = erl_app;
version = "0.1.0";
root = ./server;
src = pkgs.lib.cleanSource ./server;
Expand All @@ -107,7 +108,7 @@

# nix build .#dockerImage
dockerImage = pkgs.dockerTools.buildLayeredImage {
name = "lyceum";
name = erl_app;
tag = "latest";
created = "now";
# This will copy the compiled erlang release to the image
Expand All @@ -119,15 +120,26 @@
pkgs.openssl
];
config = {
Volumes = {
"/opt/${erl_app}/etc" = {};
"/opt/${erl_app}/data" = {};
"/opt/${erl_app}/log" = {};
};
WorkingDir = "/opt/${erl_app}";
Cmd = [
"${server}/bin/server"
"${server}/bin/${erl_app}"
"foreground"
];
Env = [
"ERL_DIST_PORT=8080"
"ERL_AFLAGS=\"-kernel shell_history enabled\""
"NODE_NAME=${erl_app}"
];
ExposedPorts = {
"8080/tcp" = { };
"4369/tcp" = {};
"4369/ucp" = {};
"8080/tcp" = {};
"8080/udp" = {};
};
};
};
Expand Down
31 changes: 15 additions & 16 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,6 @@ replace := if os() == "linux" { "sed -i" } else { "sed -i '' -e" }
default:
just --list

[doc('Formats source code. Options = { "client" [Default], "server", "justfile" }')]
format source='client':
#!/usr/bin/env bash
set -euo pipefail
t=$(echo {{ source }} | cut -f2 -d=)
echo "Selected Target: $t"
if [[ $t == "client" ]]; then
zig fmt .
elif [[ $t == "justfile" ]]; then
just --fmt --unstable
elif [[ $t == "server" ]]; then
erlfmt -w $(find ./server/src/ -type f \( -iname \*.erl -o -iname \*.hrl \))
else
echo "No formating selected, skipping step..."
fi
# ---------
# Database
# ---------
Expand All @@ -55,6 +39,8 @@ postgres:
# -------
# Client
# -------
format:
zig fmt .

client:
cd client && zig build run
Expand Down Expand Up @@ -129,6 +115,19 @@ release-nix:
build-docker:
nix build .#dockerImage

# Builds and deploys a release in the host VM
deploy:
@echo "Attemping to deploy to: {{deploy_host}}"
./deploy.sh --deploy-host {{deploy_host}}

# Starts the deployed code
start:
#!/usr/bin/env bash
export SERVER_APP=lyceum_server
export ERL_DIST_PORT=8080
if [[ $(./result/bin/$SERVER_APP ping) == "pong" ]]; then
./result/bin/$SERVER_APP restart
else
./result/bin/$SERVER_APP foreground
fi
12 changes: 12 additions & 0 deletions server/config/sys.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{kernel, [
{logger, [
{handler, structured, logger_std_h,
#{formatter => {flatlog, #{
map_depth => 3,
term_depth => 50
}}}
}
]}
]}
].
5 changes: 5 additions & 0 deletions server/config/vm.args
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Name of the node
-sname lyceum_server

## Cookie for distributed Erlang
-setcookie lyceum
6 changes: 5 additions & 1 deletion server/database/migrate_down.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ SCRIPTPATH=$(dirname "$SCRIPT")

set -euxo pipefail

PG_URL=${PG_URL:-"postgresql://admin:[email protected]:5432/mmo"}
PG_HOST=${PGHOST:-localhost}
PG_USER=${PGUSER:-admin}
PG_PASSWORD=${PGPASSWORD:-admin}
PG_DATABASE=${PGDATABASE:-mmo}
PG_URL=${PG_URL:-"postgresql://$PG_USER:$PG_PASSWORD@$PG_HOST:5432/$PG_DATABASE"}

echo "Setting PG_URL=${PG_URL}"

Expand Down
6 changes: 5 additions & 1 deletion server/database/migrate_input.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ SCRIPTPATH=$(dirname "$SCRIPT")

set -euxo pipefail

PG_URL=${PG_URL:-"postgresql://admin:[email protected]:5432/mmo"}
PG_HOST=${PGHOST:-localhost}
PG_USER=${PGUSER:-admin}
PG_PASSWORD=${PGPASSWORD:-admin}
PG_DATABASE=${PGDATABASE:-mmo}
PG_URL=${PG_URL:-"postgresql://$PG_USER:$PG_PASSWORD@$PG_HOST:5432/$PG_DATABASE"}

echo "Setting PG_URL=${PG_URL}"

Expand Down
6 changes: 5 additions & 1 deletion server/database/migrate_up.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ SCRIPTPATH=$(dirname "$SCRIPT")

set -euxo pipefail

PG_URL=${PG_URL:-"postgresql://admin:[email protected]:5432/mmo"}
PG_HOST=${PGHOST:-localhost}
PG_USER=${PGUSER:-admin}
PG_PASSWORD=${PGPASSWORD:-admin}
PG_DATABASE=${PGDATABASE:-mmo}
PG_URL=${PG_URL:-"postgresql://$PG_USER:$PG_PASSWORD@$PG_HOST:5432/$PG_DATABASE"}

echo "Setting PG_URL=${PG_URL}"

Expand Down
6 changes: 4 additions & 2 deletions server/rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
]}.

{shell, [
% {config, "config/sys.config"},
{sys_config, "config/sys.config"},
{apps, [server_app]}
]}.

Expand All @@ -14,9 +14,11 @@
{sname, 'lyceum_server'}
]}.

{relx, [{release, {server, "0.0.1"},
{relx, [{release, {lyceum_server, "0.0.1"},
[server_app]},

{sys_config, "config/sys.config"},
{vm_args, "config/vm.args"},
{dev_mode, true},
{include_erts, false},

Expand Down

0 comments on commit da2daff

Please sign in to comment.