Repository navigation
Expand file tree
/
Copy pathshared.py
More file actions
216 lines (182 loc) · 7.6 KB
/
Copy pathshared.py
File metadata and controls
216 lines (182 loc) · 7.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import logging
import os
import platform
import subprocess
import json
import importlib
import sys
# Configure logging
logging.basicConfig(
filename="debug.log",
level=logging.DEBUG,
format="%(asctime)s [%(levelname)s] %(message)s"
)
logger = logging.getLogger(__name__)
# Shared constants
SUPPORTED_DISTROS = {
"Percona Server for MySQL": "pdps-",
"Percona Distribution for MySQL (PXC)": "pdpxc-",
"Percona Distribution for MongoDB": "pdmdb-",
"Percona Distribution for PostgreSQL": "ppg-"
}
REPO_TYPES = ["release", "testing", "experimental"]
# Shared functions
def detect_os():
"""
Detect the operating system and return the appropriate package manager.
Supports popular Linux distributions like Ubuntu, Debian, CentOS, Rocky, AlmaLinux, Fedora, etc.
"""
try:
if os.path.exists("/etc/os-release"):
with open("/etc/os-release", "r") as file:
os_release = file.read().lower()
if "ubuntu" in os_release or "debian" in os_release:
return "apt-get"
elif "centos" in os_release or "red hat" in os_release or "rocky" in os_release or "alma" in os_release:
return "yum"
elif "fedora" in os_release:
return "dnf"
else:
raise Exception("Unsupported Linux distribution detected.")
os_id = platform.system().lower()
if os_id == "linux":
raise Exception("Minimal Linux distribution detected. Unable to determine package manager.")
elif os_id == "windows":
raise Exception("Windows is not supported.")
elif os_id == "darwin":
raise Exception("MacOS is not supported.")
else:
raise Exception("Unsupported operating system.")
except Exception as e:
logger.error(f"Error detecting OS: {str(e)}")
raise Exception(f"Unsupported OS: {str(e)}")
def ensure_percona_release(output_callback):
"""
Ensures the Percona Release package is downloaded and installed.
Provides real-time feedback via the provided callback.
"""
try:
output_callback("Ensuring Percona Release package is installed...\n")
# Check if the percona-release command exists
result = subprocess.run(
["which", "percona-release"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True
)
if result.returncode == 0:
output_callback("percona-release is already installed.\n")
logger.info("percona-release is already installed.")
return
# Detect the host OS
package_manager = detect_os()
if package_manager == "apt-get":
# Update package list
output_callback("Updating package list...\n")
subprocess.run(["sudo", "apt-get", "update"], check=True)
# Install wget if not present
output_callback("Checking for wget...\n")
subprocess.run(["sudo", "apt-get", "install", "-y", "wget"], check=True)
# Get the codename of the OS
output_callback("Determining OS codename...\n")
codename = subprocess.check_output(["lsb_release", "-sc"], text=True).strip()
# Check if the Percona Release package is already downloaded
packagename = f"percona-release_latest.{codename}_all.deb"
if not os.path.exists(packagename):
output_callback("Downloading Percona Release package...\n")
url = f"https://repo.percona.com/apt/percona-release_latest.{codename}_all.deb"
subprocess.run(["wget", url], check=True)
else:
output_callback("Percona Release package already downloaded.\n")
# Install the downloaded package
output_callback("Installing Percona Release package...\n")
subprocess.run(["sudo", "dpkg", "-i", packagename], check=True)
# Update package list again
output_callback("Updating package list after installation...\n")
subprocess.run(["sudo", "apt-get", "update"], check=True)
elif package_manager in ["yum", "dnf"]:
# Install the percona-release package
output_callback("Installing Percona Release package...\n")
subprocess.run(["sudo", package_manager, "install", "-y", "https://repo.percona.com/yum/percona-release-latest.noarch.rpm"], check=True)
else:
output_callback(f"Unsupported package manager: {package_manager}\n")
return
output_callback("Percona Release package successfully installed.\n")
except subprocess.CalledProcessError as e:
output_callback(f"Error during installation: {str(e)}\n")
except Exception as e:
output_callback(f"Unexpected error: {str(e)}\n")
# New Shared Utilities
def build_repo_command(distribution, version, repo_type):
"""
Build the repository enable command.
Args:
distribution (str): The selected distribution.
version (str): The selected version.
repo_type (str): The repository type.
Returns:
str: The constructed command.
"""
repo_name = f"{SUPPORTED_DISTROS[distribution]}{version}"
return f"sudo percona-release enable {repo_name} {repo_type}"
def get_available_solutions():
"""
Retrieves a list of available solutions (Python files) in the defined directory.
:param directory: The directory to search for Python solution files.
:return: A list of solution names (without the .py extension).
"""
directory = os.path.join(os.path.dirname(__file__), "solution")
try:
# List all .py files in the directory, excluding __init__.py
files = [
f[:-3] for f in os.listdir(directory)
if f.endswith('.py') and f != '__init__.py'
]
return files
except Exception as e:
print(f"Error accessing directory {directory}: {e}")
sys.exit(1)
def load_solutions_functions(directory, output_callback=print):
"""
Dynamically load all Python modules and functions from the given directory.
"""
directory = 'solution'
solutions = {}
directory_path = os.path.join(os.path.dirname(__file__), directory)
# Ensure the directory exists
if not os.path.isdir(directory_path):
output_callback(f"Directory not found: {directory_path}")
return solutions
# Add directory to sys.path
sys.path.insert(0, directory_path)
# Import each .py file in the directory
for filename in os.listdir(directory_path):
if filename.endswith(".py") and filename != "__init__.py":
module_name = filename[:-3] # Strip .py extension
try:
# Import the module dynamically
module = importlib.import_module(module_name)
# Collect all callable functions from the module
for attr_name in dir(module):
attr = getattr(module, attr_name)
if callable(attr) and not attr_name.startswith("_"):
solutions[attr_name] = attr
except Exception as e:
output_callback(f"Error loading {module_name}: {e}")
return solutions
# Platform Constants
SUPPORTED_PLATFORMS = {
"Ubuntu": ["20.04", "22.04", "24.04"],
"Debian": ["11", "12"],
"Oracle Linux": ["8", "9"],
"Rocky Linux": ["8", "9"],
"AlmaLinux": ["8", "9"]
}
NORMALIZED_DISTROS = {
"oracle linux server": "Oracle Linux",
"oracle linux": "Oracle Linux",
"ubuntu": "Ubuntu",
"debian": "Debian",
"rocky": "Rocky Linux",
"almalinux": "AlmaLinux"
}