Skip to content

Commit

Permalink
Create python script to rename wikis with one command (T11621) (#3589)
Browse files Browse the repository at this point in the history
Co-authored-by: CosmicAlpha <[email protected]>
  • Loading branch information
RhinosF1 and Universal-Omega authored Mar 17, 2024
1 parent 9937496 commit 27a7331
Showing 1 changed file with 49 additions and 28 deletions.
77 changes: 49 additions & 28 deletions modules/mediawiki/files/cookbooks/rename-wiki.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,73 @@
import subprocess
import sys
import argparse
from typing import Optional, TypedDict


class DbClusterMap(TypedDict):
c1: str
c2: str
c3: str
c4: str


# Define the mapping of db clusters to db names
db_clusters = {
'c1': 'db131',
'c2': 'db101',
'c3': 'db142',
'c4': 'db121',
'c5': 'db131',
db_clusters: DbClusterMap = {
'c1': 'db151',
'c2': 'db161',
'c3': 'db171',
'c4': 'db181',
}


def get_db_cluster(oldwiki_db):
command = f"salt-ssh -E 'db131*' cmd.run 'mysql -e "USE mhglobal; SELECT wiki_dbcluster FROM cw_wikis WHERE wiki_dbname = \"{oldwiki_db}\"'"
result = subprocess.run(command, shell=True, stdout=subprocess.PIPE, text=True)
cluster_name = result.stdout.strip()
return db_clusters.get(cluster_name)

def generate_salt_command(cluster: str, command: str) -> str:
return f'salt-ssh -E "{cluster}*" cmd.run "{command}"'

def execute_salt_command(cluster, command):
salt_command = f"salt-ssh -E '{cluster}*' cmd.run '{command}'"
subprocess.run(salt_command, shell=True)

def execute_salt_command(salt_command: str, shell: bool = False, stdout: Optional[int] = None, text: Optional[bool] = None) -> Optional[subprocess.CompletedProcess]:
response = input(f'EXECUTE (type c(continue), s(kip), a(bort): {salt_command}')
if response in ['c', 'continue']:
return subprocess.run(salt_command, shell=shell, stdout=stdout, text=text)
if response in ['s', 'skip']:
return None
sys.exit(1) # noqa: R503

def main():
parser = argparse.ArgumentParser(description='Executes the commands needed to rename wikis')
parser.add_argument('--oldwiki', required=True, help='Old wiki database name')
parser.add_argument('--newwiki', required=True, help='New wiki database name')

args = parser.parse_args()
def get_db_cluster(oldwiki_db: str) -> str:
command = generate_salt_command('db171*', f'cmd.run "mysql -e \'SELECT wiki_dbcluster FROM mhglobal.cw_wikis WHERE wiki_dbname = "{oldwiki_db}" \' "')
result = execute_salt_command(salt_command=command, shell=True, stdout=subprocess.PIPE, text=True)
if result:
cluster_name = result.stdout.strip()
return db_clusters[cluster_name] # type: ignore[literal-required]
raise KeyboardInterrupt('Impossible to skip. Aborted.')

oldwiki_db = args.oldwiki
newwiki_db = args.newwiki

def rename_wiki(oldwiki_db: str, newwiki_db: str) -> None:
# Step 1: Get the db cluster for the old wiki dbname
oldwiki_cluster = get_db_cluster(oldwiki_db)

if not oldwiki_cluster:
try:
oldwiki_cluster = get_db_cluster(oldwiki_db)
except KeyError:
print(f'Error: Unable to determine the db cluster for {oldwiki_db}')
return
sys.exit(1)

# Step 2: Execute SQL commands for rename
execute_salt_command(oldwiki_cluster, f'mysqldump {oldwiki_db} > oldwikidb.sql')
execute_salt_command(oldwiki_cluster, f"mysql -e 'CREATE DATABASE {newwiki_db}'")
execute_salt_command(oldwiki_cluster, f"mysql -e 'USE {newwiki_db}; SOURCE /home/$user/oldwikidb.sql'")
execute_salt_command(salt_command=generate_salt_command(oldwiki_cluster, f'mysqldump {oldwiki_db} > oldwikidb.sql'))
execute_salt_command(salt_command=generate_salt_command(oldwiki_cluster, f"mysql -e 'CREATE DATABASE {newwiki_db}'"))
execute_salt_command(salt_command=generate_salt_command(oldwiki_cluster, f"mysql -e 'USE {newwiki_db}; SOURCE /home/$user/oldwikidb.sql'"))

# Step 3: Execute MediaWiki rename script
execute_salt_command('mwtask141', f'sudo -u www-data php /srv/mediawiki/w/extensions/CreateWiki/maintenance/renameWiki.php --wiki=loginwiki --rename {oldwiki_db} {newwiki_db} $user')
execute_salt_command(salt_command=generate_salt_command('mwtask181', f'sudo -u www-data php /srv/mediawiki/1.41/extensions/CreateWiki/maintenance/renameWiki.php --wiki=loginwiki --rename {oldwiki_db} {newwiki_db} $user'))


def main() -> None:
parser = argparse.ArgumentParser(description='Executes the commands needed to rename wikis')
parser.add_argument('--oldwiki', required=True, help='Old wiki database name')
parser.add_argument('--newwiki', required=True, help='New wiki database name')

args = parser.parse_args()
rename_wiki(args.oldwiki, args.newwiki)


if __name__ == '__main__':
Expand Down

0 comments on commit 27a7331

Please sign in to comment.