From f643604c78de172493736feb6746785572c464b0 Mon Sep 17 00:00:00 2001 From: No767 <73260931+No767@users.noreply.github.com> Date: Sat, 22 Feb 2025 01:14:54 -0800 Subject: [PATCH] Fix code --- .dockerignore | 6 +- docker/Dockerfile | 7 -- docker/docker-compose.dev.yml | 2 + docker/docker-compose.prod.yml | 16 +-- docker/docker-compose.yml | 13 ++- docker/example.env | 11 +- docker/wait-for | 191 --------------------------------- taskfile.yml | 32 ++++++ 8 files changed, 62 insertions(+), 216 deletions(-) delete mode 100644 docker/wait-for create mode 100644 taskfile.yml diff --git a/.dockerignore b/.dockerignore index 23ec718..e1418a2 100644 --- a/.dockerignore +++ b/.dockerignore @@ -4,7 +4,11 @@ # Logs **/*.log +# YAML configuration +**/*.yaml +**/*.yml + # Cache files **/__pycache__ **/*.py[cod] -**/*$py.class \ No newline at end of file +**/*$py.class diff --git a/docker/Dockerfile b/docker/Dockerfile index 5b90d2e..f24339b 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,6 +1,3 @@ -#################################################################################################### -## Final image -#################################################################################################### FROM python:3.13-slim-bookworm ENV DEBIAN_FRONTEND=noninteractive @@ -9,16 +6,12 @@ RUN apt-get update \ && apt-get install -y --no-install-recommends \ bash \ git \ - netcat-traditional \ tini \ - gcc \ - libc6-dev \ && apt-get clean WORKDIR /rodhaj COPY /bot /rodhaj/bot/ COPY /docker/start.sh /rodhaj/start.sh -COPY /docker/wait-for /rodhaj/wait-for COPY /requirements.txt /rodhaj/requirements.txt RUN adduser --disabled-password --gecos "" rodhaj \ diff --git a/docker/docker-compose.dev.yml b/docker/docker-compose.dev.yml index 3640df0..e29481f 100644 --- a/docker/docker-compose.dev.yml +++ b/docker/docker-compose.dev.yml @@ -13,6 +13,8 @@ services: POSTGRES_USER: ${DB_USERNAME} volumes: - database:/var/lib/postgresql/data + env_file: + - .env ports: - 5432:5432 diff --git a/docker/docker-compose.prod.yml b/docker/docker-compose.prod.yml index b15fd8a..36aea88 100644 --- a/docker/docker-compose.prod.yml +++ b/docker/docker-compose.prod.yml @@ -7,13 +7,14 @@ services: volumes: # Do not edit the next line. If you want to change the path of the configuration file, please edit the CONFIG_LOCATION variable - ${CONFIG_LOCATION}:/rodhaj/bot/config.yml - env_file: - - .env ports: - 8555:8555 depends_on: - - database - command: sh -c '/rodhaj/wait-for database:5432 -- echo "[Wait-for] PostgreSQL is fully up. Starting Rodhaj." && /rodhaj/start.sh' + database: + condition: service_healthy + restart: true + env_file: + - .env restart: always database: @@ -29,7 +30,6 @@ services: ports: - 5432:5432 volumes: - # Do not edit the next line. If you want to change the database storage location on your system, edit the value of DB_DATA_LOCATION in the .env file - database:/var/lib/postgresql/data healthcheck: test: pg_isready --dbname='${DB_DATABASE_NAME}' --username='${DB_USERNAME}' || exit 1; Chksum="$$(psql --dbname='${DB_DATABASE_NAME}' --username='${DB_USERNAME}' --tuples-only --no-align --command='SELECT COALESCE(SUM(checksum_failures), 0) FROM pg_stat_database')"; echo "checksum failure count is $$Chksum"; [ "$$Chksum" = '0' ] || exit 1 @@ -43,8 +43,10 @@ services: ports: - 9090:9090 image: prom/prometheus:latest + env_file: + - .env volumes: - - ./prometheus.yml:/etc/prometheus/prometheus.yml + - ${PROMETHEUS_CONFIG_LOCATION}:/etc/prometheus/prometheus.yml - prometheus-data:/prometheus # first login uses admin/admin @@ -61,4 +63,4 @@ services: volumes: database: prometheus-data: - grafana-data: \ No newline at end of file + grafana-data: diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index fb03f86..c1bd390 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -7,14 +7,14 @@ services: volumes: # Do not edit the next line. If you want to change the path of the configuration file, please edit the CONFIG_LOCATION variable - ${CONFIG_LOCATION}:/rodhaj/bot/config.yml - env_file: - - .env ports: - 8555:8555 depends_on: - - database - # Oftentimes if Rodhaj started too early (aka starting without this script), then it would entirely not run the migrations and error out - command: sh -c '/rodhaj/wait-for database:5432 -- echo "[Wait-for] PostgreSQL is fully up. Starting Rodhaj." && /rodhaj/start.sh' + database: + condition: service_healthy + restart: true + env_file: + - .env restart: always database: @@ -30,7 +30,6 @@ services: ports: - 5432:5432 volumes: - # Do not edit the next line. If you want to change the database storage location on your system, edit the value of DB_DATA_LOCATION in the .env file - database:/var/lib/postgresql/data healthcheck: test: pg_isready --dbname='${DB_DATABASE_NAME}' --username='${DB_USERNAME}' || exit 1; Chksum="$$(psql --dbname='${DB_DATABASE_NAME}' --username='${DB_USERNAME}' --tuples-only --no-align --command='SELECT COALESCE(SUM(checksum_failures), 0) FROM pg_stat_database')"; echo "checksum failure count is $$Chksum"; [ "$$Chksum" = '0' ] || exit 1 @@ -40,4 +39,4 @@ services: restart: always volumes: - database: \ No newline at end of file + database: diff --git a/docker/example.env b/docker/example.env index 178228c..ced1370 100644 --- a/docker/example.env +++ b/docker/example.env @@ -1,11 +1,16 @@ # The location of where Rodhaj's configuration is stored. # The configuration can be found under the config-example.yml +# MUST be absolute path CONFIG_LOCATION=./config.yml +# Location for prometheus configuration +# MUST be absolute path +PROMETHEUS_CONFIG_LOCATION=./prometheus.yml + # Connection secret for the postgres. You should change it to a random password -POSTGRES_PASSWORD=postgres +DB_PASSWORD=postgres # The values below this line do not need to be changed ################################################################################### -POSTGRES_USER=postgres -POSTGRES_DB=rodhaj \ No newline at end of file +DB_USERNAME=postgres +DB_DATABASE_NAME=rodhaj diff --git a/docker/wait-for b/docker/wait-for deleted file mode 100644 index eae37dd..0000000 --- a/docker/wait-for +++ /dev/null @@ -1,191 +0,0 @@ -#!/bin/sh - -# The MIT License (MIT) -# -# Copyright (c) 2017 Eficode Oy -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in all -# copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -# SOFTWARE. - -VERSION="2.2.3" - -set -- "$@" -- "$TIMEOUT" "$QUIET" "$PROTOCOL" "$HOST" "$PORT" "$result" -TIMEOUT=15 -QUIET=0 -# The protocol to make the request with, either "tcp" or "http" -PROTOCOL="tcp" - -echoerr() { - if [ "$QUIET" -ne 1 ]; then printf "%s\n" "$*" 1>&2; fi -} - -usage() { - exitcode="$1" - cat << USAGE >&2 -Usage: - $0 host:port|url [-t timeout] [-- command args] - -q | --quiet Do not output any status messages - -t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout - -v | --version Show the version of this tool - -- COMMAND ARGS Execute command with args after the test finishes -USAGE - exit "$exitcode" -} - -wait_for() { - case "$PROTOCOL" in - tcp) - if ! command -v nc >/dev/null; then - echoerr 'nc command is missing!' - exit 1 - fi - ;; - http) - if ! command -v wget >/dev/null; then - echoerr 'wget command is missing!' - exit 1 - fi - ;; - esac - - TIMEOUT_END=$(($(date +%s) + TIMEOUT)) - - while :; do - case "$PROTOCOL" in - tcp) - nc -w 1 -z "$HOST" "$PORT" > /dev/null 2>&1 - ;; - http) - wget --timeout=1 -q "$HOST" -O /dev/null > /dev/null 2>&1 - ;; - *) - echoerr "Unknown protocol '$PROTOCOL'" - exit 1 - ;; - esac - - result=$? - - if [ $result -eq 0 ] ; then - if [ $# -gt 7 ] ; then - for result in $(seq $(($# - 7))); do - result=$1 - shift - set -- "$@" "$result" - done - - TIMEOUT=$2 QUIET=$3 PROTOCOL=$4 HOST=$5 PORT=$6 result=$7 - shift 7 - exec "$@" - fi - exit 0 - fi - - if [ $TIMEOUT -ne 0 -a $(date +%s) -ge $TIMEOUT_END ]; then - echo "Operation timed out" >&2 - exit 1 - fi - - sleep 1 - done -} - -while :; do - case "$1" in - http://*|https://*) - HOST="$1" - PROTOCOL="http" - shift 1 - ;; - *:* ) - HOST=$(printf "%s\n" "$1"| cut -d : -f 1) - PORT=$(printf "%s\n" "$1"| cut -d : -f 2) - shift 1 - ;; - -v | --version) - echo $VERSION - exit - ;; - -q | --quiet) - QUIET=1 - shift 1 - ;; - -q-*) - QUIET=0 - echoerr "Unknown option: $1" - usage 1 - ;; - -q*) - QUIET=1 - result=$1 - shift 1 - set -- -"${result#-q}" "$@" - ;; - -t | --timeout) - TIMEOUT="$2" - shift 2 - ;; - -t*) - TIMEOUT="${1#-t}" - shift 1 - ;; - --timeout=*) - TIMEOUT="${1#*=}" - shift 1 - ;; - --) - shift - break - ;; - --help) - usage 0 - ;; - -*) - QUIET=0 - echoerr "Unknown option: $1" - usage 1 - ;; - *) - QUIET=0 - echoerr "Unknown argument: $1" - usage 1 - ;; - esac -done - -if ! [ "$TIMEOUT" -ge 0 ] 2>/dev/null; then - echoerr "Error: invalid timeout '$TIMEOUT'" - usage 3 -fi - -case "$PROTOCOL" in - tcp) - if [ "$HOST" = "" ] || [ "$PORT" = "" ]; then - echoerr "Error: you need to provide a host and port to test." - usage 2 - fi - ;; - http) - if [ "$HOST" = "" ]; then - echoerr "Error: you need to provide a host to test." - usage 2 - fi - ;; -esac - -wait_for "$@" diff --git a/taskfile.yml b/taskfile.yml new file mode 100644 index 0000000..3cc1bb5 --- /dev/null +++ b/taskfile.yml @@ -0,0 +1,32 @@ +version: '3' + +tasks: + dev-up: + preconditions: + - test -f docker/docker-compose.dev.yml + cmds: + - docker compose -f docker/docker-compose.dev.yml up -d + + dev-stop: + preconditions: + - test -f docker/docker-compose.dev.yml + cmds: + - docker compose -f docker/docker-compose.dev.yml stop + + start: + preconditions: + - test -f bot/config.yml + cmds: + - python bot/launcher.py + silent: true + + check: + cmds: + - ruff check bot --fix --exit-non-zero-on-fix + - ruff format bot + silent: true + + fmt: + cmds: + - ruff format server + silent: true