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

Add some utils #1

Merged
merged 29 commits into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
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
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()}'))
RhinosF1 marked this conversation as resolved.
Show resolved Hide resolved

# 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'"))
RhinosF1 marked this conversation as resolved.
Show resolved Hide resolved

# 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):
RhinosF1 marked this conversation as resolved.
Show resolved Hide resolved
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',
)
Loading