mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-12-07 09:52:30 -05:00
Generate default LL server config and attach it to GH release (#6430)
This commit is contained in:
@@ -22,6 +22,7 @@ from red_commons.logging import getLogger
|
||||
from redbot.core import data_manager, Config
|
||||
from redbot.core.i18n import Translator
|
||||
|
||||
from . import managed_node
|
||||
from .errors import (
|
||||
LavalinkDownloadFailed,
|
||||
InvalidArchitectureException,
|
||||
@@ -35,8 +36,8 @@ from .errors import (
|
||||
NoProcessFound,
|
||||
NodeUnhealthy,
|
||||
)
|
||||
from .managed_node.ll_version import LAVALINK_BUILD_LINE, LavalinkVersion, LavalinkOldVersion
|
||||
from .utils import (
|
||||
change_dict_naming_convention,
|
||||
get_max_allocation_size,
|
||||
replace_p_with_prefix,
|
||||
)
|
||||
@@ -55,7 +56,7 @@ _LL_PLUGIN_LOG: Final[Pattern[bytes]] = re.compile(
|
||||
)
|
||||
_FAILED_TO_START: Final[Pattern[bytes]] = re.compile(rb"Web server failed to start\. (.*)")
|
||||
|
||||
# Version regexes
|
||||
# Java version regexes
|
||||
#
|
||||
# We expect the output to look something like:
|
||||
# $ java -version
|
||||
@@ -103,210 +104,14 @@ LAVALINK_LAVAPLAYER_LINE: Final[Pattern] = re.compile(
|
||||
LAVALINK_BUILD_TIME_LINE: Final[Pattern] = re.compile(
|
||||
rb"^Build time:\s+(?P<build_time>\d+[.\d+]*).*$", re.MULTILINE
|
||||
)
|
||||
# present until Lavalink 3.5-rc4
|
||||
LAVALINK_BUILD_LINE: Final[Pattern] = re.compile(rb"^Build:\s+(?P<build>\d+)$", re.MULTILINE)
|
||||
# we don't actually care about what the version format before 3.5-rc4 is exactly
|
||||
# as the comparison is based entirely on the build number
|
||||
LAVALINK_VERSION_LINE_PRE35: Final[Pattern] = re.compile(
|
||||
rb"^Version:\s+(?P<version>\S+)$", re.MULTILINE | re.VERBOSE
|
||||
)
|
||||
# used for LL versions >=3.5-rc4 but below 3.6.
|
||||
# Since this only applies to historical version, this regex is based only on
|
||||
# version numbers that actually existed, not ones that technically could.
|
||||
LAVALINK_VERSION_LINE_PRE36: Final[Pattern] = re.compile(
|
||||
rb"""
|
||||
^
|
||||
Version:\s+
|
||||
(?P<version>
|
||||
(?P<major>3)\.(?P<minor>[0-5])
|
||||
# Before LL 3.6, when patch version == 0, it was stripped from the version string
|
||||
(?:\.(?P<patch>[1-9]\d*))?
|
||||
# Before LL 3.6, the dot in rc.N was optional
|
||||
(?:-rc\.?(?P<rc>0|[1-9]\d*))?
|
||||
# additional build metadata, can be used by our downstream Lavalink
|
||||
# if we need to alter an upstream release
|
||||
(?:\+red\.(?P<red>[1-9]\d*))?
|
||||
)
|
||||
$
|
||||
""",
|
||||
re.MULTILINE | re.VERBOSE,
|
||||
)
|
||||
# used for LL 3.6 and newer
|
||||
# This regex is limited to the realistic usage in the LL version number,
|
||||
# not everything that could be a part of it according to the spec.
|
||||
# We can easily release an update to this regex in the future if it ever becomes necessary.
|
||||
LAVALINK_VERSION_LINE: Final[Pattern] = re.compile(
|
||||
rb"""
|
||||
^
|
||||
Version:\s+
|
||||
(?P<version>
|
||||
(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)
|
||||
(?:-rc\.(?P<rc>0|[1-9]\d*))?
|
||||
# additional build metadata, can be used by our downstream Lavalink
|
||||
# if we need to alter an upstream release
|
||||
(?:\+red\.(?P<red>[1-9]\d*))?
|
||||
)
|
||||
$
|
||||
""",
|
||||
re.MULTILINE | re.VERBOSE,
|
||||
)
|
||||
|
||||
|
||||
class LavalinkOldVersion:
|
||||
def __init__(self, raw_version: str, *, build_number: int) -> None:
|
||||
self.raw_version = raw_version
|
||||
self.build_number = build_number
|
||||
|
||||
def __str__(self) -> None:
|
||||
return f"{self.raw_version}_{self.build_number}"
|
||||
|
||||
@classmethod
|
||||
def from_version_output(cls, output: bytes) -> Self:
|
||||
build_match = LAVALINK_BUILD_LINE.search(output)
|
||||
if build_match is None:
|
||||
raise ValueError(
|
||||
"Could not find 'Build' line in the given `--version` output,"
|
||||
" or invalid build number given."
|
||||
)
|
||||
version_match = LAVALINK_VERSION_LINE_PRE35.search(output)
|
||||
if version_match is None:
|
||||
raise ValueError(
|
||||
"Could not find 'Version' line in the given `--version` output,"
|
||||
" or invalid version number given."
|
||||
)
|
||||
return cls(
|
||||
raw_version=version_match["version"].decode(),
|
||||
build_number=int(build_match["build"]),
|
||||
)
|
||||
|
||||
def __eq__(self, other: object) -> bool:
|
||||
if isinstance(other, LavalinkOldVersion):
|
||||
return self.build_number == other.build_number
|
||||
if isinstance(other, LavalinkVersion):
|
||||
return False
|
||||
return NotImplemented
|
||||
|
||||
def __lt__(self, other: object) -> bool:
|
||||
if isinstance(other, LavalinkOldVersion):
|
||||
return self.build_number < other.build_number
|
||||
if isinstance(other, LavalinkVersion):
|
||||
return True
|
||||
return NotImplemented
|
||||
|
||||
def __le__(self, other: object) -> bool:
|
||||
if isinstance(other, LavalinkOldVersion):
|
||||
return self.build_number <= other.build_number
|
||||
if isinstance(other, LavalinkVersion):
|
||||
return True
|
||||
return NotImplemented
|
||||
|
||||
def __gt__(self, other: object) -> bool:
|
||||
if isinstance(other, LavalinkOldVersion):
|
||||
return self.build_number > other.build_number
|
||||
if isinstance(other, LavalinkVersion):
|
||||
return False
|
||||
return NotImplemented
|
||||
|
||||
def __ge__(self, other: object) -> bool:
|
||||
if isinstance(other, LavalinkOldVersion):
|
||||
return self.build_number >= other.build_number
|
||||
if isinstance(other, LavalinkVersion):
|
||||
return False
|
||||
return NotImplemented
|
||||
|
||||
|
||||
class LavalinkVersion:
|
||||
def __init__(
|
||||
self,
|
||||
major: int,
|
||||
minor: int,
|
||||
patch: int = 0,
|
||||
*,
|
||||
rc: Optional[int] = None,
|
||||
red: int = 0,
|
||||
) -> None:
|
||||
self.major = major
|
||||
self.minor = minor
|
||||
self.patch = patch
|
||||
self.rc = rc
|
||||
self.red = red
|
||||
|
||||
def __str__(self) -> None:
|
||||
version = f"{self.major}.{self.minor}.{self.patch}"
|
||||
if self.rc is not None:
|
||||
version += f"-rc.{self.rc}"
|
||||
if self.red:
|
||||
version += f"+red.{self.red}"
|
||||
return version
|
||||
|
||||
@classmethod
|
||||
def from_version_output(cls, output: bytes) -> Self:
|
||||
match = LAVALINK_VERSION_LINE.search(output)
|
||||
if match is None:
|
||||
# >=3.5-rc4, <3.6
|
||||
match = LAVALINK_VERSION_LINE_PRE36.search(output)
|
||||
if match is None:
|
||||
raise ValueError(
|
||||
"Could not find 'Version' line in the given `--version` output,"
|
||||
" or invalid version number given."
|
||||
)
|
||||
return LavalinkVersion(
|
||||
major=int(match["major"]),
|
||||
minor=int(match["minor"]),
|
||||
patch=int(match["patch"] or 0),
|
||||
rc=int(match["rc"]) if match["rc"] is not None else None,
|
||||
red=int(match["red"] or 0),
|
||||
)
|
||||
|
||||
def _get_comparison_tuple(self) -> Tuple[int, int, int, bool, int, int]:
|
||||
return self.major, self.minor, self.patch, self.rc is None, self.rc or 0, self.red
|
||||
|
||||
def __eq__(self, other: object) -> bool:
|
||||
if isinstance(other, LavalinkVersion):
|
||||
return self._get_comparison_tuple() == other._get_comparison_tuple()
|
||||
if isinstance(other, LavalinkOldVersion):
|
||||
return False
|
||||
return NotImplemented
|
||||
|
||||
def __lt__(self, other: object) -> bool:
|
||||
if isinstance(other, LavalinkVersion):
|
||||
return self._get_comparison_tuple() < other._get_comparison_tuple()
|
||||
if isinstance(other, LavalinkOldVersion):
|
||||
return False
|
||||
return NotImplemented
|
||||
|
||||
def __le__(self, other: object) -> bool:
|
||||
if isinstance(other, LavalinkVersion):
|
||||
return self._get_comparison_tuple() <= other._get_comparison_tuple()
|
||||
if isinstance(other, LavalinkOldVersion):
|
||||
return False
|
||||
return NotImplemented
|
||||
|
||||
def __gt__(self, other: object) -> bool:
|
||||
if isinstance(other, LavalinkVersion):
|
||||
return self._get_comparison_tuple() > other._get_comparison_tuple()
|
||||
if isinstance(other, LavalinkOldVersion):
|
||||
return True
|
||||
return NotImplemented
|
||||
|
||||
def __ge__(self, other: object) -> bool:
|
||||
if isinstance(other, LavalinkVersion):
|
||||
return self._get_comparison_tuple() >= other._get_comparison_tuple()
|
||||
if isinstance(other, LavalinkOldVersion):
|
||||
return True
|
||||
return NotImplemented
|
||||
|
||||
|
||||
class ServerManager:
|
||||
JAR_VERSION: Final[str] = LavalinkVersion(3, 7, 11, red=3)
|
||||
YT_PLUGIN_VERSION: Final[str] = "1.5.2"
|
||||
|
||||
LAVALINK_DOWNLOAD_URL: Final[str] = (
|
||||
"https://github.com/Cog-Creators/Lavalink-Jars/releases/download/"
|
||||
f"{JAR_VERSION}/"
|
||||
f"{managed_node.JAR_VERSION}/"
|
||||
"Lavalink.jar"
|
||||
)
|
||||
YT_PLUGIN_REPOSITORY: Final[str] = "https://maven.lavalink.dev/releases"
|
||||
|
||||
_java_available: ClassVar[Optional[bool]] = None
|
||||
_java_version: ClassVar[Optional[Tuple[int, int]]] = None
|
||||
@@ -429,19 +234,7 @@ class ServerManager:
|
||||
raise
|
||||
|
||||
async def process_settings(self):
|
||||
data = change_dict_naming_convention(await self._config.yaml.all())
|
||||
ll_config = data["lavalink"]
|
||||
sources = ll_config["server"]["sources"]
|
||||
plugins = ll_config.setdefault("plugins", [])
|
||||
|
||||
enable_yt_plugin = sources["youtube"]
|
||||
if enable_yt_plugin:
|
||||
sources["youtube"] = False
|
||||
yt_plugin = {
|
||||
"dependency": f"dev.lavalink.youtube:youtube-plugin:{self.YT_PLUGIN_VERSION}",
|
||||
"repository": self.YT_PLUGIN_REPOSITORY,
|
||||
}
|
||||
plugins.append(yt_plugin)
|
||||
data = managed_node.generate_server_config(await self._config.yaml.all())
|
||||
|
||||
with open(self.lavalink_app_yml, "w", encoding="utf-8") as f:
|
||||
yaml.safe_dump(data, f)
|
||||
@@ -584,7 +377,8 @@ class ServerManager:
|
||||
# A 404 means our LAVALINK_DOWNLOAD_URL is invalid, so likely the jar version
|
||||
# hasn't been published yet
|
||||
raise LavalinkDownloadFailed(
|
||||
f"Lavalink jar version {self.JAR_VERSION} hasn't been published yet",
|
||||
f"Lavalink jar version {managed_node.JAR_VERSION}"
|
||||
" hasn't been published yet",
|
||||
response=response,
|
||||
should_retry=False,
|
||||
)
|
||||
@@ -670,7 +464,7 @@ class ServerManager:
|
||||
self._jvm = java["jvm"].decode()
|
||||
self._lavaplayer = lavaplayer["lavaplayer"].decode()
|
||||
self._buildtime = date
|
||||
self._up_to_date = self._lavalink_version >= self.JAR_VERSION
|
||||
self._up_to_date = self._lavalink_version >= managed_node.JAR_VERSION
|
||||
return self._up_to_date
|
||||
|
||||
async def maybe_download_jar(self):
|
||||
@@ -690,7 +484,7 @@ class ServerManager:
|
||||
log.info(
|
||||
"Lavalink version outdated, triggering update from %s to %s...",
|
||||
self._lavalink_version,
|
||||
self.JAR_VERSION,
|
||||
managed_node.JAR_VERSION,
|
||||
)
|
||||
await self._download_jar()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user