-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from miraheze/RhinosF1-patch-1
Add some utils
- Loading branch information
Showing
10 changed files
with
137 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import argparse | ||
import sys | ||
import os | ||
from miraheze.salt.utils import generate_salt_command, execute_salt_command, get_db_cluster | ||
|
||
|
||
def reset_wiki(wiki: str) -> None: | ||
# Step 1: Get the db cluster for the wiki | ||
|
||
try: | ||
wiki_cluster = get_db_cluster(wiki) | ||
except (KeyError, IndexError): | ||
print(f'Error: Unable to determine the db cluster for {wiki}') | ||
sys.exit(1) | ||
|
||
# Step 2: Execute deleteWiki.php | ||
execute_salt_command(salt_command=generate_salt_command('mwtask181', f'mwscript extensions/CreateWiki/deleteWiki.php loginwiki --deletewiki {wiki} --delete {os.getlogin()}')) | ||
|
||
# Step 3: Backup and drop database | ||
execute_salt_command(salt_command=generate_salt_command(wiki_cluster, f"sudo -i mysqldump {wiki} > {wiki}.sql'")) | ||
execute_salt_command(salt_command=generate_salt_command(wiki_cluster, f"sudo -i mysql -e 'DROP DATABASE {wiki}'")) | ||
|
||
|
||
def main() -> None: | ||
parser = argparse.ArgumentParser(description='Executes the commands needed to reset wikis') | ||
parser.add_argument('--wiki', required=True, help='Old wiki database name') | ||
|
||
args = parser.parse_args() | ||
reset_wiki(args.wiki) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import argparse | ||
import sys | ||
from miraheze.salt.utils import generate_salt_command, execute_salt_command, get_db_cluster | ||
|
||
|
||
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: | ||
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'")) | ||
|
||
# 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')) | ||
|
||
|
||
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__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import subprocess | ||
import sys | ||
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: DbClusterMap = { | ||
'c1': 'db151', | ||
'c2': 'db161', | ||
'c3': 'db171', | ||
'c4': 'db181', | ||
} | ||
|
||
|
||
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 = 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']: | ||
try: | ||
return subprocess.run(salt_command, shell=shell, stdout=stdout, text=text, check=True) | ||
except subprocess.CalledProcessError as e: | ||
print(f"Command '{salt_command}' failed with return code {e.returncode}") | ||
except Exception as e: | ||
print(f'An error occurred: {e}') | ||
return None | ||
if response in ['s', 'skip']: | ||
return None | ||
sys.exit(1) # noqa: R503 | ||
|
||
|
||
def get_db_cluster(wiki: str) -> str: | ||
db_query = f"SELECT wiki_dbcluster FROM mhglobal.cw_wikis WHERE wiki_dbname = '{wiki}'" | ||
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() | ||
cluster_data = cluster_name.split('\n') | ||
cluster_data_b = cluster_data[1].split(' ') | ||
print(cluster_data_b) | ||
cluster_name = cluster_data_b[4] | ||
|
||
return db_clusters[cluster_name] # type: ignore[literal-required] | ||
raise KeyboardInterrupt('Impossible to skip. Aborted.') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,28 +6,18 @@ | |
with open('README.md') as readme_file: | ||
readme = readme_file.read() | ||
|
||
# with open('CHANGELOG.md') as history_file: | ||
# history = history_file.read() | ||
with open('requirements.txt') as requirements_file: | ||
requirements = list(requirements_file.readlines()) | ||
|
||
# with open('dev-requirements.txt') as dev_requirements_file: | ||
# dev_requirements = list(dev_requirements_file.readlines()) | ||
|
||
|
||
setup( | ||
name='Miraheze_PyUtils', | ||
version=VERSION, | ||
description='Python Utilities for Miraheze', | ||
long_description=readme, # + '\n\n' + history, | ||
long_description_content_type='text/markdown', # This is important! | ||
long_description=readme, | ||
long_description_content_type='text/markdown', # This is important! | ||
author='RhinosF1', | ||
author_email='[email protected]', | ||
url='https://github.com/FOSSBots/MirahezeBots', | ||
url='https://github.com/miraheze/python-functions', | ||
packages=find_packages('.'), | ||
include_package_data=True, | ||
install_requires=requirements, | ||
# tests_require=dev_requirements, | ||
test_suite='tests', | ||
license='GPL3', | ||
) |