This commit is contained in:
Alexey Berezhok
2026-07-02 00:18:50 +03:00
parent bcd2ca49e1
commit 2fe3c0c5cb
3 changed files with 155 additions and 2 deletions

View File

@@ -21,14 +21,19 @@ mkdir -p %{buildroot}/usr/local/mgr5/etc/plugins/ispmgr
mkdir -p %{buildroot}/usr/local/mgr5/addon
cp -a ispmgr/* %{buildroot}/usr/local/mgr5/etc/plugins/ispmgr/
cp -a nginx_mod_rewrite_plugin.py %{buildroot}/usr/local/mgr5/addon/
mkdir -p %{buildroot}/usr/local/ispmanager-mod_rewrite
mkdir -p %{buildroot}/usr/local/ispmanager-mod_rewrite/utils
wget -O %{buildroot}/usr/local/ispmanager-mod_rewrite/mod_rewrite.zip https://github.com/bayrepo/ngx_http_apache_rewrite_module/archive/refs/heads/main.zip
cp utils/build_mod_rewrite.py %{buildroot}/usr/local/ispmanager-mod_rewrite/utils
%triggerin -- nginx
/usr/local/ispmanager-mod_rewrite/utils/build_mod_rewrite.py
%files
/usr/local/mgr5/etc/xml/ispmgr_mod_nginx_mod_rewrite_plugin.xml
/usr/local/mgr5/etc/plugins/ispmgr/nginx_mod_rewrite_plugin.xml
%attr(700, root, root) /usr/local/mgr5/addon/nginx_mod_rewrite_plugin.py
%attr(600, root, root) /usr/local/ispmanager-mod_rewrite/mod_rewrite.zip
%attr(700, root, root) /usr/local/ispmanager-mod_rewrite/utils/build_mod_rewrite.py
%changelog
* Mon Jun 29 2026 Alexey BayRepo <a@bayrepo.ru> - 0.0.1-1

View File

@@ -3,6 +3,8 @@
import os
import xml.etree.ElementTree as etree
from sys import stdin
import subprocess
import datetime
def CheckModuleExists():
@@ -11,8 +13,49 @@ def CheckModuleExists():
return 'mod_rewrite already installed'
return None
def BuildModule():
"""
Build and install the nginx mod_rewrite module by calling the utility script.
Returns:
str: Error message if the utility failed, None on success
"""
utils_dir = '/usr/local/ispmanager-mod_rewrite/utils'
build_script = os.path.join(utils_dir, 'build_mod_rewrite.py')
log_path = '/usr/local/mgr5/var/nginx-mod-rewrite-plugin.log'
try:
with open(log_path, 'a') as log_file:
log_file.write(f'=== Starting module build via utility: {datetime.datetime.now()} ===\n')
log_file.write(f'Executing: python3 {build_script}\n')
result = subprocess.run(
['python3', build_script],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True
)
# Log the result
log_file.write(f'Build script exit code: {result.returncode}\n')
if result.stdout:
log_file.write(f'Stdout:\n{result.stdout}')
if result.returncode != 0:
# Extract error message from stderr (already captured in stdout due to redirect)
return f'Build utility failed with exit code {result.returncode}'
except Exception as exc:
log_path_error = os.path.join(os.path.dirname(log_path), 'error.log')
with open(log_path_error, 'a') as error_file:
error_file.write(f'Unexpected error calling build utility: {exc}\n')
return f'Unexpected error: {exc}'
return None
def InstallModule():
#Todo func
result = BuildModule()
if result is not None:
return result
return None
# Обработка нажатия на кнопку "Сохранить"

105
utils/build_mod_rewrite.py Normal file
View File

@@ -0,0 +1,105 @@
#!/usr/bin/env python3
"""
Utility for building and installing nginx mod_rewrite module.
This script handles downloading, extracting, building, and installing the module.
Returns None on success, or error message string on failure.
"""
import os
import sys
import shutil
import subprocess
import zipfile
import datetime
def build_mod_rewrite():
"""
Build and install the nginx mod_rewrite module.
Returns:
str: Error message on failure, None on success
"""
tmp_dir = '/tmp/nginx-mod-rewrite'
log_path = '/usr/local/mgr5/var/nginx-mod-rewrite-plugin.log'
try:
# Ensure log directory exists
log_dir = os.path.dirname(log_path)
if log_dir and not os.path.exists(log_dir):
os.makedirs(log_dir, exist_ok=True)
# Setup temporary directory: create or clean
if os.path.isdir(tmp_dir):
shutil.rmtree(tmp_dir)
os.makedirs(tmp_dir, exist_ok=True)
with open(log_path, 'a') as log_file:
log_file.write(f'=== Starting module download and install: {datetime.datetime.now()} ===\n')
# Copy the ZIP file into the temporary directory
src_zip = '/usr/local/ispmanager-mod_rewrite/mod_rewrite.zip'
dst_zip = os.path.join(tmp_dir, 'mod_rewrite.zip')
try:
shutil.copy(src_zip, dst_zip)
log_file.write(f'Copied zip to {dst_zip}\n')
except Exception as e:
return f'Failed to copy zip: {e}'
# Extract the ZIP file
try:
with zipfile.ZipFile(dst_zip, 'r') as zip_ref:
zip_ref.extractall(tmp_dir)
log_file.write('Extracted zip contents.\n')
except Exception as e:
return f'Failed to extract zip: {e}'
# Locate the extracted root directory
extracted_root = None
for entry in os.listdir(tmp_dir):
full_path = os.path.join(tmp_dir, entry)
if os.path.isdir(full_path):
extracted_root = full_path
break
if not extracted_root:
return 'Extracted folder not found.'
log_file.write(f'Using extracted root: {extracted_root}\n')
# Define the sequence of commands to run
commands = [
['bash', 'package_preparer.sh', 'installdeps'],
['bash', 'package_preparer.sh', 'download', '.', 'system'],
['bash', 'package_preparer.sh', 'build'],
['bash', 'package_preparer.sh', 'installmod']
]
for cmd in commands:
log_file.write(f'Running command: {" ".join(cmd)}\n')
result = subprocess.run(cmd, cwd=extracted_root, stdout=log_file, stderr=log_file)
if result.returncode != 0:
return f'Command {" ".join(cmd)} failed with exit code {result.returncode}'
log_file.write('All commands executed successfully.\n')
except Exception as exc:
return f'Unexpected error: {exc}'
finally:
# Clean up temporary directory
try:
if os.path.isdir(tmp_dir):
shutil.rmtree(tmp_dir)
except Exception:
pass
return None
if __name__ == '__main__':
# Run the build process and output result to stdout
result = build_mod_rewrite()
if result is not None:
# On error, write error message to stderr
print(result, file=sys.stderr)
sys.exit(1)
else:
# On success, exit with 0 (no output)
sys.exit(0)