Skip to content

Commit

Permalink
Update rename-wiki.py
Browse files Browse the repository at this point in the history
  • Loading branch information
RhinosF1 authored Jan 7, 2024
1 parent 9c5c2d2 commit 0747d67
Showing 1 changed file with 32 additions and 18 deletions.
50 changes: 32 additions & 18 deletions modules/mediawiki/files/cookbooks/rename-wiki.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import subprocess
import argparse
from typing import TypedDict

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

# Define the mapping of db clusters to db names
db_clusters = {
db_clusters: DbClusterMap = {
'c1': 'db131',
'c2': 'db101',
'c3': 'db142',
Expand All @@ -11,25 +19,22 @@
}


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: bool = subprocess.PIPE, text: bool = False) -> None:
return subprocess.run(salt_command, shell=shell, stdout, text)


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')
def get_db_cluster(oldwiki_db: str) -> str:
command = generate_salt_command('db131*', 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)
cluster_name = result.stdout.strip()
return db_clusters.get(cluster_name)

args = parser.parse_args()

def rename_wiki(oldwiki: str, newwiki: str) -> None:
oldwiki_db = args.oldwiki
newwiki_db = args.newwiki

Expand All @@ -41,12 +46,21 @@ def main():
return

# 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=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'))


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 0747d67

Please sign in to comment.