Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update rename wiki script with "good version" #3814

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions modules/salt/files/bin/rename-wiki.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import subprocess
import sys
import os
import argparse
import re
import json
from typing import Optional, TypedDict


Expand All @@ -19,12 +22,14 @@ class DbClusterMap(TypedDict):
'c4': 'db181',
}

TASK_SERVER = 'mwtask181'


def generate_salt_command(cluster: str, command: str) -> str:
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,31 +39,39 @@ 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)
except KeyError:
except (KeyError, IndexError):
print(f'Error: Unable to determine the db cluster for {oldwiki_db}')
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/{os.getlogin()}/{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/{os.getlogin()}/{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.42/extensions/CreateWiki/maintenance/renameWiki.php --wiki=loginwiki --rename {oldwiki_db} {newwiki_db} $user'))
execute_salt_command(salt_command=generate_salt_command(TASK_SERVER, f'mwscript extensions/CreateWiki/renameWiki.php loginwiki --no-log --rename {oldwiki_db} {newwiki_db} {os.getlogin()}'))
execute_salt_command(salt_command=generate_salt_command(TASK_SERVER, f"/usr/local/bin/logsalmsg '{os.getlogin()} renamed {oldwiki_db} to {newwiki_db} using renamewiki.py'"))


def main() -> None:
Expand Down
Loading