100 lines
4.5 KiB
Python
100 lines
4.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Publish only the owned board helpers from the active, CI-promoted release."""
|
|
import argparse
|
|
import datetime
|
|
import hashlib
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import re
|
|
import shutil
|
|
import subprocess
|
|
import tempfile
|
|
|
|
EXPECTED = {
|
|
'system-board-membership.php': '8d9fc7126a229b2ca0f6033ec0647946e418558981af75eb731ef5526f63a15f',
|
|
}
|
|
FILES = ['system-board-provision.php', 'system-board-membership.php']
|
|
PROMOTION_STATE = Path('/mnt/HC_Volume_107481500/dunegit/ci/promotion')
|
|
|
|
|
|
def digest(data):
|
|
return hashlib.sha256(data).hexdigest()
|
|
|
|
|
|
def publication_changes(source, target):
|
|
changes = []
|
|
for name in FILES:
|
|
destination = target / name
|
|
content = (source / name).read_bytes()
|
|
if destination.is_symlink():
|
|
raise RuntimeError('Refusing a symlink destination: ' + name)
|
|
old = destination.read_bytes() if destination.exists() else None
|
|
if old == content:
|
|
continue
|
|
if old is not None and digest(old) != EXPECTED.get(name):
|
|
raise RuntimeError('Native helper has unreviewed changes: ' + name)
|
|
if old is None and name in EXPECTED:
|
|
raise RuntimeError('Expected existing helper missing: ' + name)
|
|
changes.append((name, content, old))
|
|
return changes
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('--apply', action='store_true')
|
|
args = parser.parse_args()
|
|
release = Path(__file__).resolve().parents[1]
|
|
app = Path('/home/dunerealms/apps/dunerealms')
|
|
if (app / 'current').resolve(strict=True) != release or not re.fullmatch(r'ci-[0-9a-f]{40}', release.name):
|
|
raise RuntimeError('Use this publisher from the active CI-promoted release')
|
|
if (PROMOTION_STATE / 'deployed').read_text().strip() != release.name[3:]:
|
|
raise RuntimeError('Publication state does not match the active release')
|
|
source = release / 'integrations/mybb'
|
|
target = Path('/home/dunerealms/apps/integrations/mybb').resolve(strict=True)
|
|
if not (target / 'global.php').is_file():
|
|
raise RuntimeError('Native MyBB installation missing')
|
|
changes = publication_changes(source, target)
|
|
for name in [*FILES, 'provision-system-boards.php']:
|
|
subprocess.run(['php', '-l', str(source / name)], check=True, capture_output=True)
|
|
for name in ['test-system-board-policy.php', 'test-system-board-provision.php']:
|
|
subprocess.run(['php', str(source / name)], check=True, capture_output=True)
|
|
if not args.apply:
|
|
print(json.dumps({'applied': False, 'files': [name for name, _, _ in changes]}))
|
|
return
|
|
backup = Path('/var/backups/dunerealms-system-boards') / datetime.datetime.now(datetime.timezone.utc).strftime('%Y%m%dT%H%M%SZ')
|
|
if changes:
|
|
backup.mkdir(parents=True, mode=0o700, exist_ok=False)
|
|
reference = (target / 'system-board-membership.php').stat()
|
|
for name, content, old in changes:
|
|
destination = target / name
|
|
current = destination.read_bytes() if destination.exists() else None
|
|
if current != old:
|
|
raise RuntimeError('Native helper changed during publication: ' + name)
|
|
if old is not None:
|
|
shutil.copy2(destination, backup / name)
|
|
fd, temporary = tempfile.mkstemp(prefix='.dune-board-', dir=target)
|
|
try:
|
|
with os.fdopen(fd, 'wb') as output:
|
|
output.write(content)
|
|
os.chmod(temporary, reference.st_mode & 0o777)
|
|
os.chown(temporary, reference.st_uid, reference.st_gid)
|
|
os.replace(temporary, destination)
|
|
finally:
|
|
if os.path.exists(temporary):
|
|
os.unlink(temporary)
|
|
node = '/home/dunerealms/.nvm/versions/node/v22.23.3/bin/node'
|
|
snapshot = subprocess.run(['runuser', '-u', 'dunerealms', '--', node,
|
|
'--env-file=/home/dunerealms/.config/dunerealms.env',
|
|
str(release / 'ops/system-board-snapshot.mjs')],
|
|
check=True, capture_output=True, text=True)
|
|
result = subprocess.run(['php', str(source / 'provision-system-boards.php')],
|
|
input=snapshot.stdout, check=True, capture_output=True, text=True)
|
|
# Existing native permission tests inspect real MyBB without editing users.
|
|
subprocess.run(['php', str(source / 'test-system-board-native.php')], check=True, capture_output=True)
|
|
print(json.dumps({'applied': True, 'files': [name for name, _, _ in changes],
|
|
'provisioning': json.loads(result.stdout)}))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|

Staff