Page MenuHomeDevCentral

check-paper-version.py
No OneTemporary

check-paper-version.py

#!/usr/bin/env python3
# -------------------------------------------------------------
# Checks :: Minecraft :: Paper version
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Project: Quilvaryn
# Description: Compare installed Paper version with last stable
# License: BSD-2-Clause
# Usage: check-paper-version.py [minecraft-dir]
# Source file: operations/salt/minecraft/files/checks/check-paper-version.py
# -------------------------------------------------------------
#
# <auto-generated>
# This file is managed by the rQVR Quilvaryn repository.
#
# Changes to this file may cause incorrect behavior
# and will be lost if the state is redeployed.
# </auto-generated>
import os
import re
import subprocess
import sys
from packaging.version import InvalidVersion, Version
EXIT_OK = 0
EXIT_WARNING = 1
EXIT_CRITICAL = 2
EXIT_UNKNOWN = 3
MINECRAFT_DIR = "/usr/local/minecraft"
SERVER_JAR = "server.jar"
LAST_VERSION_HELPER = "/usr/local/bin/query-paper-last-version"
JAVA_VERSION_TIMEOUT = 60
PAPER_LINE_RE = re.compile(r"^(\d+(?:\.\d+)+)-(\d+)(?:-\S+)?$")
# -------------------------------------------------------------
# Command execution
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def run_command(
args: list[str],
cwd: str | None = None,
timeout: int | None = None,
) -> subprocess.CompletedProcess[str]:
try:
return subprocess.run(
args,
cwd=cwd,
capture_output=True,
text=True,
timeout=timeout,
)
except FileNotFoundError:
raise RuntimeError(f"{args[0]} is not installed") from None
except subprocess.TimeoutExpired as e:
raise RuntimeError(f"timed out running {args[0]}") from e
# -------------------------------------------------------------
# Version parsing
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def parse_paper(value: str) -> tuple[Version, int]:
match = PAPER_LINE_RE.match(value.strip())
if not match:
raise RuntimeError(f"invalid Paper version: {value}")
try:
return Version(match.group(1)), int(match.group(2))
except InvalidVersion as e:
raise RuntimeError(f"invalid version: {value}") from e
def find_version_line(output: str) -> str:
for line in reversed(output.splitlines()):
line = line.strip()
if PAPER_LINE_RE.match(line):
return line
raise RuntimeError("cannot parse Paper version from server.jar --version")
# -------------------------------------------------------------
# Installed version
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def is_server_existing(minecraft_dir: str) -> bool:
server_jar = os.path.join(minecraft_dir, SERVER_JAR)
return os.path.isfile(server_jar)
def get_installed_paper(minecraft_dir: str) -> tuple[Version, int]:
if not is_server_existing(minecraft_dir):
raise RuntimeError(f"Can't find {SERVER_JAR}")
result = run_command(
["java", "-jar", SERVER_JAR, "--version"],
cwd=minecraft_dir,
timeout=JAVA_VERSION_TIMEOUT,
)
output = (result.stdout or "") + "\n" + (result.stderr or "")
return parse_paper(find_version_line(output))
# -------------------------------------------------------------
# Last stable version
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def get_last_stable_version() -> tuple[Version, int]:
result = run_command([LAST_VERSION_HELPER, "--full"])
last = result.stdout.strip()
if result.returncode != 0 or not last:
detail = (result.stderr or result.stdout or "query failed").strip()
raise RuntimeError(f"cannot query last Paper version: {detail}")
return parse_paper(last)
# -------------------------------------------------------------
# Check result
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def compare_paper(
installed: Version,
installed_build: int,
last: Version,
last_build: int,
) -> tuple[int, str]:
if installed < last:
return EXIT_CRITICAL, (f"CRITICAL: Paper {installed} installed, last is {last}")
if installed == last and installed_build < last_build:
return EXIT_WARNING, (
f"WARNING: Paper {installed}-{installed_build} installed, "
f"last is {last}-{last_build}"
)
return EXIT_OK, f"OK: Paper {installed}-{installed_build}"
# -------------------------------------------------------------
# Application entry point
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def check(minecraft_dir: str) -> tuple[int, str]:
try:
installed, installed_build = get_installed_paper(minecraft_dir)
last, last_build = get_last_stable_version()
except RuntimeError as e:
return EXIT_UNKNOWN, f"UNKNOWN: {e}"
return compare_paper(installed, installed_build, last, last_build)
def run(minecraft_dir: str) -> int:
exit_code, message = check(minecraft_dir)
print(message)
sys.exit(exit_code)
if __name__ == "__main__":
argc = len(sys.argv)
if argc > 2:
print("Too many arguments", file=sys.stderr)
print(f"Usage: {sys.argv[0]} [minecraft-dir]", file=sys.stderr)
sys.exit(EXIT_UNKNOWN)
parsed_minecraft_dir = sys.argv[1] if argc == 2 else MINECRAFT_DIR
run(parsed_minecraft_dir)

File Metadata

Mime Type
text/x-script.python
Expires
Sat, Nov 14, 19:40 (3 d, 14 h ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4254888
Default Alt Text
check-paper-version.py (5 KB)

Event Timeline