Files
ispmanager-mod_rewrite/usr/local/mgr5/addon/nginx_mod_rewrite_plugin.py

101 lines
3.2 KiB
Python
Raw Normal View History

2026-07-01 23:00:40 +03:00
#!/usr/bin/env python3
import os
import xml.etree.ElementTree as etree
from sys import stdin
2026-07-02 00:18:50 +03:00
import subprocess
import datetime
2026-07-01 23:00:40 +03:00
def CheckModuleExists():
module_path = '/usr/share/nginx/modules/ngx_http_apache_rewrite_module.conf'
if os.path.isfile(module_path):
return 'mod_rewrite already installed'
return None
2026-07-02 00:18:50 +03:00
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
2026-07-01 23:00:40 +03:00
def InstallModule():
2026-07-02 00:18:50 +03:00
result = BuildModule()
if result is not None:
return result
2026-07-01 23:00:40 +03:00
return None
# Обработка нажатия на кнопку "Сохранить"
def Handle(root):
result = True
result_error = CheckModuleExists()
if result_error is None:
result_error = InstallModule()
result = result_error is None
if not result:
# Формирование сообщения об ошибке согласно структуре в metadata
error = etree.SubElement(root, "error")
error.set('code', '1')
error.set('type', 'nginx_mod_rewrite_plugin')
error.set('object', 'wrong_value')
param = etree.SubElement(error, 'param')
param.text = 'wrong_value'
param.set('name', 'object')
param.set('type', 'msg')
param = etree.SubElement(error, 'param')
param.set('name', 'value')
param.text = result_error
if __name__ == "__main__":
func = os.getenv("PARAM_func")
correct_function = False
if func == "nginx_mod_rewrite_plugin.settings":
correct_function = True
elif func == "plugin.settings":
if os.getenv("PARAM_plugin_settings") == "nginx_mod_rewrite_plugin":
correct_function = True
root = etree.parse(stdin).getroot()
if correct_function:
if os.getenv("PARAM_clicked_button") == "ok":
Handle(root)
etree.dump(root)
quit(0)