forked from unimatrix/web
Replace installed plugins by identity across legacy JAR filenames
This commit is contained in:
parent
eeee72a91e
commit
b204c2e978
2 changed files with 55 additions and 8 deletions
|
|
@ -1,6 +1,7 @@
|
|||
"""Install one recorded build into one backend, with bounded file/service recovery."""
|
||||
from pathlib import Path
|
||||
import datetime, hashlib, json, os, re, shutil, subprocess, sys, yaml
|
||||
from zipfile import ZipFile
|
||||
|
||||
ALLOWED = {'/opt/dunerealms/lobby': 'dune-lobby.service', **{
|
||||
f'/opt/dunerealms/servers/{x}': f'dune-{x}.service'
|
||||
|
|
@ -10,6 +11,13 @@ JARS = {'DuneMenus.jar', 'DuneRealms-Essentials.jar', 'DuneRealms-Lobby.jar',
|
|||
LEGACY = ('DunerealmsGUI.jar', 'DuneRealmsGUI.jar', 'DuneFullscreenPaper.jar', 'MCGUI.jar',
|
||||
'DunerealmsGUI', 'DuneRealmsGUI', 'DuneFullscreenPaper', 'MCGUI')
|
||||
PACK = 'DuneRealms-Network-Artwork-v7.zip'
|
||||
IDENTITIES = {'DuneMenus.jar': 'DuneMenus', 'DuneRealms-Essentials.jar': 'DuneEss',
|
||||
'DuneRealms-Lobby.jar': 'DuneLobby', 'DuneRealms-Core.jar': 'DuneCore',
|
||||
'jarvistranslator.jar': 'JarvisTranslator'}
|
||||
|
||||
def plugin_name(path):
|
||||
with ZipFile(path) as archive:
|
||||
return yaml.safe_load(archive.read('plugin.yml'))['name']
|
||||
|
||||
def require(ok, message):
|
||||
if not ok:
|
||||
|
|
@ -60,18 +68,37 @@ def install(bundle, root, unit, backup_base=Path('/root'), runner=run):
|
|||
config.setdefault('fullscreen', {})['enabled'] = True; config.pop('mcgui', None)
|
||||
pin = {'url': manifest['pack_url'], 'sha1': files[PACK]['sha1']}
|
||||
pack.update({'world': pin, 'lobby': pin})
|
||||
jars = sorted(name for name in JARS if (plugins/name).is_file())
|
||||
targets = [*[plugins/name for name in jars], *configs]
|
||||
installed = {}
|
||||
for jar in plugins.glob('*.jar'):
|
||||
try:
|
||||
identity = plugin_name(jar)
|
||||
except Exception:
|
||||
require(not any(jar.name.lower().startswith(prefix) for prefix in
|
||||
('dunemenus', 'dunecore', 'duneess', 'dunelobby', 'dunerealms-core',
|
||||
'dunerealms-essentials', 'dunerealms-lobby', 'jarvistranslator')),
|
||||
'Unreadable first-party plugin: '+jar.name)
|
||||
continue
|
||||
if identity in IDENTITIES.values():
|
||||
require(identity not in installed, 'Duplicate installed plugin: '+identity)
|
||||
installed[identity] = jar
|
||||
require('DuneMenus' in installed, 'DuneMenus identity missing')
|
||||
replacements = {}
|
||||
for name, identity in IDENTITIES.items():
|
||||
require(plugin_name(bundle/name) == identity, 'Unexpected artifact plugin identity: '+name)
|
||||
if identity in installed:
|
||||
replacements[name] = installed[identity]
|
||||
jars = sorted(replacements)
|
||||
targets = [*replacements.values(), *configs]
|
||||
require(all(not p.is_symlink() for p in targets), 'Symlinked replacement target')
|
||||
legacy = [plugins/name for name in LEGACY if (plugins/name).exists()]
|
||||
if (data/'mcgui').exists(): legacy.append(data/'mcgui')
|
||||
cache = plugins/'.paper-remapped'
|
||||
if cache.exists():
|
||||
require(not cache.is_symlink(), 'Symlinked remapped cache')
|
||||
legacy += [p for p in cache.glob('*.jar') if p.name in JARS or
|
||||
legacy += [p for p in cache.glob('*.jar') if p.name in JARS or p.name in {t.name for t in targets} or
|
||||
any(x in p.name.lower() for x in ('dunemenus', 'dunerealmsgui', 'dunefullscreen', 'mcgui'))]
|
||||
require(all(not p.is_symlink() and p.resolve().is_relative_to(plugins.resolve()) for p in legacy), 'Unsafe legacy path')
|
||||
excluded = set(jars) | set(LEGACY)
|
||||
excluded = {p.name for p in replacements.values()} | set(LEGACY)
|
||||
others = {p.name: digest(p) for p in plugins.glob('*.jar') if p.name not in excluded}
|
||||
runner(['systemctl', 'is-active', '--quiet', unit])
|
||||
stamp = datetime.datetime.now(datetime.timezone.utc).strftime('%Y%m%dT%H%M%S%fZ')
|
||||
|
|
@ -92,9 +119,9 @@ def install(bundle, root, unit, backup_base=Path('/root'), runner=run):
|
|||
saved = backup/'retired'/target.relative_to(plugins); saved.parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.move(str(target), str(saved)); moved.append((target, saved))
|
||||
for target in targets:
|
||||
source = bundle/target.name if target.suffix == '.jar' else staged/target.name
|
||||
source = next((bundle/name for name, path in replacements.items() if path == target), staged/target.name)
|
||||
changed.append(target); owned_replace(source, target)
|
||||
require(all(digest(plugins/name) == files[name]['sha256'] for name in jars), 'Installed checksum differs')
|
||||
require(all(digest(replacements[name]) == files[name]['sha256'] for name in jars), 'Installed checksum differs')
|
||||
require(others == {p.name: digest(p) for p in plugins.glob('*.jar') if p.name not in excluded}, 'Unrelated plugins changed')
|
||||
require(all(not p.exists() for p in legacy), 'Retired GUI remains active')
|
||||
runner(['systemctl', 'start', unit]); runner(['systemctl', 'is-active', '--quiet', unit])
|
||||
|
|
@ -118,7 +145,8 @@ def install(bundle, root, unit, backup_base=Path('/root'), runner=run):
|
|||
(backup/'failure.json').write_text(json.dumps(report, indent=2))
|
||||
raise RuntimeError(json.dumps(report)) from failure
|
||||
report = {'source': manifest['source'], 'server': str(root), 'unit': unit, 'backup': str(backup),
|
||||
'removed': [str(p.relative_to(plugins)) for p in legacy], 'jars': {name: files[name]['sha256'] for name in jars},
|
||||
'removed': [str(p.relative_to(plugins)) for p in legacy],
|
||||
'jars': {replacements[name].name: files[name]['sha256'] for name in jars},
|
||||
'pack': pin, 'other_plugins_unchanged': True, 'runtime_verified': False}
|
||||
(backup/'deployment.json').write_text(json.dumps(report, indent=2)); return report
|
||||
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import pathlib
|
|||
import tempfile
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
from zipfile import ZipFile
|
||||
|
||||
SPEC = importlib.util.spec_from_file_location('installer', pathlib.Path(__file__).with_name('install-dunemenus-release.py'))
|
||||
m = importlib.util.module_from_spec(SPEC)
|
||||
|
|
@ -32,9 +33,16 @@ class InstallerTests(unittest.TestCase):
|
|||
for name in sorted(m.JARS | {m.PACK}):
|
||||
payload = ('new-' + name).encode()
|
||||
(self.bundle / name).write_bytes(payload)
|
||||
if name in m.JARS:
|
||||
with ZipFile(self.bundle / name, 'w') as archive:
|
||||
archive.writestr('plugin.yml', 'name: ' + m.IDENTITIES[name])
|
||||
archive.writestr('build.txt', payload)
|
||||
payload = (self.bundle / name).read_bytes()
|
||||
files[name] = {'sha256': hashlib.sha256(payload).hexdigest(), 'sha1': hashlib.sha1(payload).hexdigest()}
|
||||
if name in m.JARS:
|
||||
(self.plugins / name).write_bytes(('old-' + name).encode())
|
||||
with ZipFile(self.plugins / name, 'w') as archive:
|
||||
archive.writestr('plugin.yml', 'name: ' + m.IDENTITIES[name])
|
||||
archive.writestr('build.txt', 'old-' + name)
|
||||
(self.bundle / 'release.json').write_text(json.dumps({
|
||||
'source': 'a' * 40, 'files': files,
|
||||
'pack_url': 'https://www.dunerealms.com/downloads/' + m.PACK}))
|
||||
|
|
@ -139,6 +147,17 @@ class InstallerTests(unittest.TestCase):
|
|||
m.install(self.bundle, self.root, 'wrong', self.backup, self.run_service)
|
||||
self.assertEqual(self.calls, [])
|
||||
|
||||
def test_legacy_versioned_filenames_are_replaced_without_duplicates(self):
|
||||
aliases = {'DuneRealms-Core.jar': 'DuneCore-1.0.10.jar',
|
||||
'DuneRealms-Essentials.jar': 'DuneEss-1.0.2.jar'}
|
||||
for source, target in aliases.items():
|
||||
(self.plugins / source).rename(self.plugins / target)
|
||||
result = self.invoke()
|
||||
for source, target in aliases.items():
|
||||
self.assertFalse((self.plugins / source).exists())
|
||||
self.assertEqual((self.plugins / target).read_bytes(), (self.bundle / source).read_bytes())
|
||||
self.assertIn(target, result['jars'])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
|
|
|||
Loading…
Reference in a new issue

Staff