Skip to content

Commit

Permalink
Merge pull request #1 from miraheze/RhinosF1-patch-1
Browse files Browse the repository at this point in the history
Add some utils
  • Loading branch information
RhinosF1 authored May 18, 2024
2 parents 068e16b + ff6e334 commit 4759a82
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 16 deletions.
1 change: 1 addition & 0 deletions .github/pythonchecks.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pytest-xdist==3.3.1
flake8-noqa==1.3.2
coverage==7.3.2
setuptools==68.2.2; python_version == '3.12'
build==1.2.1
## ACTUAL
requests==2.28.1; python_version == '3.11'
requests==2.31.0; python_version == '3.12'
Expand Down
8 changes: 5 additions & 3 deletions .github/workflows/python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ jobs:
sudo apt-get install -y libgnutls28-dev
pip install -U pip
pip install -r .github/pythonchecks.txt
pyproject-build --wheel --outdir dist .
find dist -name "*.whl" | xargs pip3 install
- name: Run tests
run: |
flake8 --ignore=E501,W503,SFS301,T003,PT009
coverage run --branch -m pytest tests
mypy --ignore-missing-imports --disallow-untyped-defs
flake8 --exclude=*/__init__.py,dist/*,build/* --ignore=E501,W503,SFS301,T003,PT009
#coverage run --branch -m pytest tests
mypy miraheze --ignore-missing-imports --disallow-untyped-defs
notify-irc:
needs: build
Expand Down
1 change: 1 addition & 0 deletions miraheze/salt/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions miraheze/salt/mwcli/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

33 changes: 33 additions & 0 deletions miraheze/salt/mwcli/partial_reset_wiki.py
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()
35 changes: 35 additions & 0 deletions miraheze/salt/mwcli/rename_wiki.py
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()
53 changes: 53 additions & 0 deletions miraheze/salt/utils.py
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.')
1 change: 1 addition & 0 deletions miraheze/swift/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

4 changes: 4 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ classifiers =
Programming Language :: Python :: 3.12
[options]
python_requires = >=3.11
[options.entry_points]
console_scripts =
partial-reset-wiki = miraheze.salt.mwcli.partial_reset_wiki:main
rename-wiki = miraheze.salt.mwcli.rename_wiki:main
16 changes: 3 additions & 13 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
)

0 comments on commit 4759a82

Please sign in to comment.