diff --git a/.gitignore b/.gitignore index 83cfd1550f4..9a9fc68fab6 100644 --- a/.gitignore +++ b/.gitignore @@ -371,6 +371,7 @@ tasks.json config.lua config_canary.lua client_assertions.txt +.env # Extensions *.ini *.otb diff --git a/docker/.dockerignore b/docker/.dockerignore new file mode 100644 index 00000000000..bfa88a7fafd --- /dev/null +++ b/docker/.dockerignore @@ -0,0 +1,11 @@ +vcproj/ +tests/ +docs/ +docker/ + +sonar-project.properties +*.md +Jenkinsfile +.* +gdb_debug +client_assertions.txt diff --git a/docker/.env.dist b/docker/.env.dist new file mode 100644 index 00000000000..80724ceec8d --- /dev/null +++ b/docker/.env.dist @@ -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 diff --git a/docker/Dockerfile.dev b/docker/Dockerfile.dev new file mode 100644 index 00000000000..1329df0462a --- /dev/null +++ b/docker/Dockerfile.dev @@ -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"] + diff --git a/docker/config.sh b/docker/config.sh new file mode 100644 index 00000000000..18061f1b017 --- /dev/null +++ b/docker/config.sh @@ -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 diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml new file mode 100644 index 00000000000..46a2fb619b2 --- /dev/null +++ b/docker/docker-compose.yml @@ -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 diff --git a/recompile.sh b/recompile.sh index 01a830641f2..46fe60ff8e3 100755 --- a/recompile.sh +++ b/recompile.sh @@ -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 diff --git a/start.sh b/start.sh index 1296ee8fa82..ef44d24a316 100644 --- a/start.sh +++ b/start.sh @@ -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