Skip to content
This repository has been archived by the owner on Jul 18, 2022. It is now read-only.

Commit

Permalink
Add RCON API (#8)
Browse files Browse the repository at this point in the history
* Add rcon.py

* Skip doctest from example in rcon.py

* Fix ImportError raised by Django when generating docs

* Add forgotten __init__.py file in /lib

* Synchronise pyproject.toml and docs/requirements.txt

* Remove __slots__ from rcon.py

* Upgrade RCON system
  • Loading branch information
PerchunPak committed Feb 19, 2022
1 parent 493551e commit 179a6a5
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 15 deletions.
14 changes: 10 additions & 4 deletions autodonate/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
SECRET_KEY: a variable that sets the cookie encryption key.
ALLOWED_HOSTS: a variable in the "list:host1.ru,host2.org" format defines the allowed "Host" headers.
RCON_HOST: Minecraft RCON host (enable-rcon=true in server.properties)
RCON_PASSWORD: Minecraft RCON password
"""
from os import environ
from shutil import copy as copy_file
Expand Down Expand Up @@ -201,10 +205,12 @@ def _load(self) -> None:
self.CONFIG = toml_decode(opened_file)

def _check(self) -> None:
"""Check if in config SECRET_KEY, if not, logging fatal error."""
try: # noqa: WPS229
self["SECRET_KEY"] # noqa: WPS428
self["ALLOWED_HOSTS"] # noqa: WPS428
"""Checking all necessary variables before starting."""
try:
self["SECRET_KEY"]
self["ALLOWED_HOSTS"]
self["RCON_HOST"]
self["RCON_PASSWORD"]
except ConfigVariableNotFoundError:
log.fatal(
"The main variables in the config are not configured. "
Expand Down
44 changes: 44 additions & 0 deletions autodonate/lib/utils/rcon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""Module for manage RCON connection."""

from mcrcon_ipv6 import MCRcon
from django.conf import settings
from autodonate.lib.utils.logger import get_logger

connection: MCRcon = MCRcon(
settings.CONFIG["RCON_HOST"],
settings.CONFIG["RCON_PASSWORD"],
settings.CONFIG.get("RCON_PORT", 25575),
)
log = get_logger(__name__)


class Rcon:
"""Class for manage RCON connection.
Example:
In[1]: Rcon.run("whitelist add Bob")
Out[2]: 'Added Bob to whitelist'
"""

@staticmethod
def run(command: str) -> str:
"""Run command in RCON.
Args:
command: Command which to run.
Returns:
Server answer.
"""
global connection

# noinspection PyBroadException
try:
return str(connection.command(command))
except Exception:
try:
connection.connect()
except Exception as exc:
log.warning("RCON connection unavailable! Is server offline?")
raise exc
return str(connection.command(command))
7 changes: 6 additions & 1 deletion config.toml.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
SECRET_KEY = "supersecretrandomstring"
SECRET_KEY = "Sup3rsecr33tranDomstr1ng"
DEBUG = true
ALLOWED_HOSTS = ["host1.ru", "host2.com", "*.something.info"]

# RCON settings
RCON_HOST = "localhost"
RCON_PASSWORD = "Sup3rsecr33tranDomstr1ng"
RCON_PORT = 25575 # optional

# Change this to your db config or path
DATABASE = {"ENGINE" = "django.db.backends.sqlite3", "NAME" = "db.sqlite3"}
14 changes: 8 additions & 6 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# This file is used to setup env
# to generate documentation.

sphinx==4.4.0
sphinx-autodoc-typehints==1.17.0
sphinx>=4.4.0,<4.5.0
sphinx-autodoc-typehints>=1.17.0,<1.18.0
sphinxcontrib-apidoc>=0.3.0
furo>=2022.1.2
m2r2==0.3.2
tomlkit==0.9.2
toml==0.10.2
m2r2>=0.3.0,<0.4.0
tomlkit>=0.10.0,<0.11.0

# Dependencies of our project:
Django>=3.2.12,<5.0.0
Django>=4.0.2,<4.1.0
requests>=2.27.1
toml>=0.10.2,<0.11.0
mcrcon-ipv6>=0.1.2
88 changes: 84 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ poetry = "^1"
Django = "^4.0.2"
requests = "^2.27.1"
toml = "^0.10.2"
mcrcon-ipv6 = "^0.1.2"

[tool.poetry.dev-dependencies]
black = "^22.1.0"
Expand All @@ -37,6 +38,8 @@ pytest-randomly = "^3.11"

sphinx = "^4.4"
sphinx-autodoc-typehints = "^1.17"
sphinxcontrib-apidoc = "^0.3"
furo = "^2022.1.2"
doc8 = "^0.10"
m2r2 = "^0.3"
tomlkit = "^0.10"

0 comments on commit 179a6a5

Please sign in to comment.