Skip to content

Commit

Permalink
feat: implement docker-compose (#1252)
Browse files Browse the repository at this point in the history
Implement docker-compose to streamline the steps to launch a server. With just one command, you will have a server ready. Add more options to the existing scripts to adapt them for both Docker and for those who have configured VCPKG differently from the tutorial.

Keep the old Dockerfile, as it serves a different purpose.
  • Loading branch information
Glatharth authored Aug 22, 2023
1 parent 84527dc commit 0b675d8
Show file tree
Hide file tree
Showing 8 changed files with 253 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ tasks.json
config.lua
config_canary.lua
client_assertions.txt
.env
# Extensions
*.ini
*.otb
Expand Down
11 changes: 11 additions & 0 deletions docker/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
vcproj/
tests/
docs/
docker/

sonar-project.properties
*.md
Jenkinsfile
.*
gdb_debug
client_assertions.txt
25 changes: 25 additions & 0 deletions docker/.env.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Mariadb
MYSQL_DATABASE=otservbr-global
MYSQL_USER=canary
MYSQL_PASSWORD=canary
MYSQL_ROOT_PASSWORD=root

# Login
LOGIN_HTTP_PORT=8080
LOGIN_GRPC_PORT=9090
MYSQL_HOST=database
MYSQL_PORT=3306
MYSQL_DBNAME=otservbr-global
MYSQL_USER=canary
MYSQL_PASS=canary
SERVER_NAME=OTServBR-Global
SERVER_IP=127.0.0.1
SERVER_PORT=7172
SERVER_LOCATION=BRA
RATE_LIMITER_RATE=2
RATE_LIMITER_BURST=5

# Server
toggleDownloadMap=false
statusProtocolPort=7171
gameProtocolPort=7172
38 changes: 38 additions & 0 deletions docker/Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Stage 1: Download all dependencies
FROM ubuntu:23.04 AS dependencies

RUN --mount=type=cache,target=/var/cache/apt \
apt-get update && apt-get install -y --no-install-recommends cmake git \
unzip build-essential ca-certificates curl zip unzip tar \
pkg-config ninja-build autoconf automake libtool \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

WORKDIR /opt
COPY vcpkg.json /opt
RUN vcpkgCommitId=$(grep '.builtin-baseline' vcpkg.json | awk -F: '{print $2}' | tr -d '," ') \
&& echo "vcpkg commit ID: $vcpkgCommitId" \
&& git clone https://github.com/Microsoft/vcpkg.git \
&& cd vcpkg \
&& git checkout "$vcpkgCommitId" \
&& ./bootstrap-vcpkg.sh

WORKDIR /opt/vcpkg
COPY vcpkg.json /opt/vcpkg/
RUN --mount=type=cache,target=/var/cache/vcpkg \
/opt/vcpkg/vcpkg --feature-flags=binarycaching,manifests,versions install

# Stage 2: create build
FROM dependencies AS build
WORKDIR /srv/build
COPY src ./src
COPY cmake ./cmake
COPY recompile.sh CMakeLists.txt CMakePresets.json vcpkg.json ./
RUN ./recompile.sh "/opt"

# Stage 3: execute
FROM ubuntu:23.04 AS prod
COPY --from=build /srv/build/build/linux-release/bin/canary /bin/canary
WORKDIR /srv/canary
ENTRYPOINT ["/srv/canary/start.sh", "canary"]

96 changes: 96 additions & 0 deletions docker/config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/bin/bash

# ./config.sh
# ./config.sh maxPlayers 88
# ./config.sh maxPlayers 88 serverName Teste maxItem 5 --file other_name.lua --env .env

# Replace values in .lua file
substitute_lua_variable() {
variable_name="$1"
new_value="$2"
if [[ "$new_value" == "true" || "$new_value" == "false" ]]; then
sed "s|$variable_name =.*|$variable_name = $new_value|" "$lua_file" > "$lua_file.tmp"
elif [[ $( echo "$new_value" | grep -E "^[0-9]+$") ]]; then
sed "s|$variable_name =.*|$variable_name = $new_value|" "$lua_file" > "$lua_file.tmp"
else
sed "s|$variable_name =.*|$variable_name = \"$new_value\"|" "$lua_file" > "$lua_file.tmp"
fi

mv "$lua_file.tmp" "$lua_file" 2>&1
}

# Get a named argument
get_named_arg() {
arg_name="$1"
shift
while [[ $# -gt 0 ]]; do
if [[ $1 == "$arg_name" && $# -gt 1 ]]; then
echo "$2"
return
fi
shift
done
}

lua_file=$( get_named_arg "--file" "$@")
env_file=$( get_named_arg "--env" "$@")

if [ -z "$lua_file" ]; then
lua_file="config.lua"
fi
if [ -z "$env_file" ]; then
env_file=".env"
fi

verify_file() {
if [ ! -f "$1" ]; then
echo "$2 not found"
echo "path: $1"
exit 1
fi
}

verify_file "$env_file" "env"
verify_file "$lua_file" "lua"

# Reads the env file
while IFS='=' read -r key value; do
if [[ "$key" != "#"* && "$key" != "" ]]; then
case $key in
MYSQL_HOST)
substitute_lua_variable "mysqlHost" "$value"
;;
MYSQL_DBNAME)
substitute_lua_variable "mysqlDatabase" "$value"
;;
MYSQL_USER)
substitute_lua_variable "mysqlUser" "$value"
;;
MYSQL_PASS)
substitute_lua_variable "mysqlPass" "$value"
;;
SERVER_NAME)
substitute_lua_variable "serverName" "$value"
;;
SERVER_IP)
substitute_lua_variable "ip" "$value"
;;
SERVER_PORT)
substitute_lua_variable "gameProtocolPort" "$value"
;;
*)
substitute_lua_variable "$key" "$value"
;;
esac
fi
done < "$env_file"

# # Substitutes other variables provided as command line arguments
args=("$@")
for ((i=0; i<${#args[@]}; i+=2)); do
if [[ "${args[i]}" != "--file" ]]; then
variable_name="${args[i]}"
new_value="${args[i+1]}"
substitute_lua_variable "$variable_name" "$new_value"
fi
done
63 changes: 63 additions & 0 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
---
version: '3.8'
name: otbr
services:
database:
image: mariadb:latest
restart: unless-stopped
env_file:
- '.env'
networks:
- canary-net
ports:
- '$MYSQL_PORT:3306'
volumes:
- 'db-volume:/var/lib/mysql'
- ../schema.sql:/docker-entrypoint-initdb.d/init.sql
healthcheck:
test: ["CMD", "healthcheck.sh", "--connect"]
interval: 10s
timeout: 5s
retries: 10


server:
# cpuset: "0-4"
build:
context: ..
dockerfile: docker/Dockerfile.dev
target: prod
restart: unless-stopped
env_file:
- '.env'
networks:
- canary-net
ports:
- '$gameProtocolPort:$gameProtocolPort'
- '$statusProtocolPort:$statusProtocolPort'
volumes:
- '../:/srv/canary'
depends_on:
database:
condition: service_healthy

login:
image: opentibiabr/login-server:latest
restart: unless-stopped
ports:
- '$LOGIN_HTTP_PORT:$LOGIN_HTTP_PORT'
- '$LOGIN_GRPC_PORT:$LOGIN_GRPC_PORT'
env_file:
- '.env'
networks:
- canary-net
depends_on:
database:
condition: service_healthy

volumes:
db-volume:

networks:
canary-net:
driver: bridge
5 changes: 3 additions & 2 deletions recompile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
set -euo pipefail

# Variáveis
VCPKG_PATH="$HOME/vcpkg/scripts/buildsystems/vcpkg.cmake"
BUILD_TYPE="linux-release"
VCPKG_PATH=${1:-"$HOME"}
VCPKG_PATH=$VCPKG_PATH/vcpkg/scripts/buildsystems/vcpkg.cmake
BUILD_TYPE=${2:-"linux-release"}
ARCHITECTURE=$(uname -m)
ARCHITECTUREVALUE=0

Expand Down
19 changes: 16 additions & 3 deletions start.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
#!/bin/bash

BIN_PATH=${1:-"./canary"}
if [ -d "logs" ]
then
echo -e "\e[01;32m Starting server \e[0m"
else
mkdir -p logs
sudo apt install -y gdb
fi

if [ ! -f "config.lua" ]; then
echo -e "\e[01;33m config.lua file not found, new file will be created \e[0m"
cp config.lua.dist config.lua && ./docker/config.sh --env docker/.env
fi

ulimit -c unlimited
set -o pipefail

while true; do ./canary 2>&1 | awk '{ print strftime("%F %T - "),
$0; fflush(); }' | tee "logs/$(date +"%F %H-%M-%S.log")" done;
while true; do
sleep 2
"$BIN_PATH" 2>&1 | awk '{ print strftime("%F %T - "),
$0; fflush(); }' | tee "logs/$(date +"%F %H-%M-%S.log")"
# Verificar se a tecla 'q' foi pressionada
read -t 1 -N 1 -r input
if [[ "$input" == "q" ]]; then
break
fi
done

0 comments on commit 0b675d8

Please sign in to comment.