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 3 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 25 additions & 12 deletions modules/salt/files/bin/rename-wiki.py
@@ -1,6 +1,9 @@
import subprocess
import sys
import os
import argparse
import re
import json
from typing import Optional, TypedDict


Expand All @@ -9,6 +12,7 @@ class DbClusterMap(TypedDict):
c2: str
c3: str
c4: str
c5: str
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still no c5 CC @RhinosF1



# Define the mapping of db clusters to db names
Expand All @@ -21,10 +25,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}"'
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC we don't need -E if we aren't using * but isn't * required if the full hostname is IE db151.wikitide.net? I can't remember if salt uses that or just db151 though



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 +38,40 @@ 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.41/extensions/CreateWiki/maintenance/renameWiki.php --wiki=loginwiki --rename {oldwiki_db} {newwiki_db} $user'))
execute_salt_command(salt_command=generate_salt_command('mwtask181', f'mwscript extensions/CreateWiki/renameWiki.php loginwiki --no-log --rename {oldwiki_db} {newwiki_db} {os.getlogin()}'))
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think mwtask181 should be some sort of constant so it is easy to change if task server is changed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that makes sense, how exactly would I do that here?

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to

DEPLOYUSER = 'www-data'

execute_salt_command(salt_command=generate_salt_command('mwtask181', f"/usr/local/bin/logsalmsg '{os.getlogin()} renamed {oldwiki_db} to {newwiki_db} using renamewiki.py'"))



def main() -> None:
Expand Down