-
-
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.
- Loading branch information
Showing
1 changed file
with
51 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
|
||
|
||
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.') |