Skip to content

Commit

Permalink
update rename wiki script with "good version"
Browse files Browse the repository at this point in the history
  • Loading branch information
Reception123 authored and Universal-Omega committed Apr 17, 2024
1 parent b4ea8db commit 7dad190
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions modules/salt/files/bin/rename-wiki.py
@@ -1,6 +1,8 @@
import subprocess
import sys
import argparse
import re
import json
from typing import Optional, TypedDict


Expand All @@ -9,6 +11,7 @@ class DbClusterMap(TypedDict):
c2: str
c3: str
c4: str
c5: str


# Define the mapping of db clusters to db names
Expand All @@ -21,10 +24,10 @@ class DbClusterMap(TypedDict):


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


def execute_salt_command(salt_command: str, shell: bool = False, stdout: Optional[int] = None, text: Optional[bool] = None) -> Optional[subprocess.CompletedProcess]:
def execute_salt_command(salt_command: str, shell: bool = True, 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)
Expand All @@ -34,17 +37,24 @@ def execute_salt_command(salt_command: str, shell: bool = False, stdout: Optiona


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)
db_query = f'SELECT wiki_dbcluster FROM mhglobal.cw_wikis WHERE wiki_dbname = \\"{oldwiki_db}\\"'
command = generate_salt_command("db171", f"sudo -i mysql --skip-column-names -e '{db_query}'")
result = execute_salt_command(salt_command=command, stdout=subprocess.PIPE, text=True)
if result:
cluster_name = result.stdout.strip()
return db_clusters[cluster_name] # type: ignore[literal-required]
cluster_name = result.stdout.strip()
#print(cluster_name)
cluster_data = cluster_name.split('\n')
cluster_data_b = cluster_data[1].split(' ')
print(cluster_data_b)
#print("Extracted cluster_name:", cluster_name) # Print cluster_name for debugging
cluster_name = cluster_data_b[4]

return db_clusters[cluster_name] # type: ignore[literal-required]
raise KeyboardInterrupt('Impossible to skip. Aborted.')


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)

try:
oldwiki_cluster = get_db_cluster(oldwiki_db)
Expand All @@ -53,12 +63,14 @@ def rename_wiki(oldwiki_db: str, newwiki_db: str) -> None:
sys.exit(1)

# Step 2: Execute SQL commands for rename
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'"))
execute_salt_command(salt_command=generate_salt_command(oldwiki_cluster, f'sudo -i mysqldump {oldwiki_db} > /home/reception/{oldwiki_db}.sql'))
execute_salt_command(salt_command=generate_salt_command(oldwiki_cluster, f"sudo -i mysql -e 'CREATE DATABASE {newwiki_db}'"))
execute_salt_command(salt_command=generate_salt_command(oldwiki_cluster, f"sudo -i mysql -e 'USE {newwiki_db}; SOURCE /home/reception/{oldwiki_db}.sql;'"))

# Step 3: Execute MediaWiki rename script
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'))
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} Reception123'))
execute_salt_command(salt_command=generate_salt_command('mwtask181', f"/usr/local/bin/logsalmsg 'Renamed {oldwiki_db} to {newwiki_db} using renamewiki.py'"))



def main() -> None:
Expand Down

0 comments on commit 7dad190

Please sign in to comment.