From 75a9878162dc35f8b60904e4f9cf5a8a7397f1eb Mon Sep 17 00:00:00 2001 From: RhinosF1 Date: Sat, 18 May 2024 15:48:01 +0100 Subject: [PATCH] Update fix_container_permissions.py (#2) * Update fix_container_permissions.py * Update setup.cfg * Update fix_container_permissions.py * Apply suggestions from code review Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update fix_container_permissions.py Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update fix_container_permissions.py --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- miraheze/swift/fix_container_permissions.py | 28 +++++++++++++-------- setup.cfg | 1 + 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/miraheze/swift/fix_container_permissions.py b/miraheze/swift/fix_container_permissions.py index 289e040..abe95b0 100644 --- a/miraheze/swift/fix_container_permissions.py +++ b/miraheze/swift/fix_container_permissions.py @@ -1,18 +1,24 @@ -import os import re import subprocess -import sys +import argparse -if len(sys.argv) != 2: - print("Please use in form of 'python fix_containers.py foowiki'") - sys.exit(1) -wiki = sys.argv[1] +def fix_container_perms(wiki: str) -> None: + out = subprocess.run(['sudo', '-u', 'www-data', 'php', '/srv/mediawiki/1.41/maintenance/run.php', '/srv/mediawiki/1.41/extensions/CreateWiki/maintenance/setContainersAccess.php', '--wiki', wiki], capture_output=True, text=True) + matches = re.findall(r"Making sure 'mwstore:\/\/miraheze-swift\/([^']+)' [^\n]+\.failed\.", out.stdout) + for match in matches: + subprocess.run(['swift', 'post', '--read-acl', 'mw:media', '--write-acl', 'mw:media', f'miraheze-{wiki}-{match}'], check=True) -out = subprocess.run(['sudo', '-u', 'www-data', 'php', '/srv/mediawiki/1.41/maintenance/run.php', '/srv/mediawiki/1.41/extensions/CreateWiki/maintenance/setContainersAccess.php', '--wiki', wiki], capture_output=True, text=True) + subprocess.run(['sudo', '-u', 'www-data', 'php', '/srv/mediawiki/1.41/maintenance/run.php', '/srv/mediawiki/1.41/extensions/CreateWiki/maintenance/setContainersAccess.php', '--wiki', wiki]) -matches = re.findall(r"Making sure 'mwstore:\/\/miraheze-swift\/([^']+)' [^\n]+\.failed\.", out.stdout) -for match in matches: - os.system(f"swift post --read-acl 'mw:media' --write-acl 'mw:media' miraheze-{wiki}-{match}") -os.system(f'sudo -u www-data php /srv/mediawiki/1.41/maintenance/run.php /srv/mediawiki/1.41/extensions/CreateWiki/maintenance/setContainersAccess.php --wiki {wiki}') +def main() -> None: + parser = argparse.ArgumentParser(description='Fix container permissions for a specified wiki') + parser.add_argument('--wiki', required=True, help='wiki database name') + + args = parser.parse_args() + fix_container_perms(args.wiki) + + +if __name__ == '__main__': + main() diff --git a/setup.cfg b/setup.cfg index f12093f..6c681d9 100644 --- a/setup.cfg +++ b/setup.cfg @@ -13,3 +13,4 @@ python_requires = >=3.11 console_scripts = partial-reset-wiki = miraheze.salt.mwcli.partial_reset_wiki:main rename-wiki = miraheze.salt.mwcli.rename_wiki:main + fix-container-perms = miraheze.swift.fix_container_permissions:main