#!/usr/bin/env python3 """Upload overlay + nginx only. Does not clone phpNuxBill or reset the admin password.""" from __future__ import annotations import tarfile import tempfile from pathlib import Path import sys sys.path.insert(0, str(Path(__file__).resolve().parent)) import vps_ssh ROOT = Path(r"c:\laragon\www\light-otantik-nux") SAAS = ROOT / "deploy" / "saas" REMOTE_SH = r"""#!/bin/bash set -euo pipefail cd /tmp rm -rf /tmp/wifi-otantik-overlay tar -xzf /tmp/wifi-otantik-overlay.tgz sed -i 's/\r$//' /tmp/nginx-wifi-otantik.conf /tmp/apply_overlay.sh cp -a /tmp/overlay/. /var/www/wifi-otantik/ install -m 644 /tmp/nginx-wifi-otantik.conf /etc/nginx/sites-available/wifi-otantik ln -sfn /etc/nginx/sites-available/wifi-otantik /etc/nginx/sites-enabled/wifi-otantik nginx -t systemctl reload nginx sed -i 's|^\$protocol = .*|\$protocol = "https://";|' /var/www/wifi-otantik/config.php grep -n protocol /var/www/wifi-otantik/config.php | head -5 find /var/www/wifi-otantik/ui/compiled -type f -delete || true mysql wifi_otantik -e "DELETE FROM tbl_appconfig WHERE setting IN ('otantik_host_wifi','otantik_host_business','otantik_host_portal'); INSERT INTO tbl_appconfig (setting,value) VALUES ('otantik_host_wifi','wifi.otantik.cm'),('otantik_host_business','business.otantik.cm'),('otantik_host_portal','portal.otantik.cm');" chown -R www-data:www-data /var/www/wifi-otantik/ui/cache /var/www/wifi-otantik/ui/compiled /var/www/wifi-otantik/system/cache mkdir -p /var/www/wifi-otantik/ui/ui_custom/images/avatars /var/www/wifi-otantik/ui/ui_custom/images/portals /var/www/wifi-otantik/system/storage/kyc chown -R www-data:www-data /var/www/wifi-otantik/ui/ui_custom/images/avatars /var/www/wifi-otantik/ui/ui_custom/images/portals /var/www/wifi-otantik/system/storage/kyc cat >/etc/cron.d/wifi-otantik <<'CRON' SHELL=/bin/bash PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin */5 * * * * www-data cd /var/www/wifi-otantik && /usr/bin/php /var/www/wifi-otantik/system/otantik_cron.php >> /tmp/otantik-cron.log 2>&1 CRON chmod 644 /etc/cron.d/wifi-otantik PHP_POOL=/etc/php/8.3/fpm/pool.d/www.conf if [ -f "$PHP_POOL" ]; then sed -i 's/^pm.max_children = .*/pm.max_children = 12/' "$PHP_POOL" sed -i 's/^pm.start_servers = .*/pm.start_servers = 3/' "$PHP_POOL" sed -i 's/^pm.min_spare_servers = .*/pm.min_spare_servers = 2/' "$PHP_POOL" sed -i 's/^pm.max_spare_servers = .*/pm.max_spare_servers = 6/' "$PHP_POOL" if grep -q '^request_terminate_timeout' "$PHP_POOL"; then sed -i 's/^request_terminate_timeout = .*/request_terminate_timeout = 90/' "$PHP_POOL" else echo 'request_terminate_timeout = 90' >> "$PHP_POOL" fi systemctl reload php8.3-fpm || systemctl restart php8.3-fpm fi echo ---cron--- cat /etc/cron.d/wifi-otantik echo ---nginx--- grep server_name /etc/nginx/sites-available/wifi-otantik echo ---hosts--- curl -s -o /dev/null -w 'wifi=%{http_code} ' -H 'Host: wifi.otantik.cm' http://127.0.0.1/ curl -s -o /dev/null -w 'business=%{http_code} ' -H 'Host: business.otantik.cm' http://127.0.0.1/ curl -s -o /dev/null -w 'portal=%{http_code} ' -H 'Host: portal.otantik.cm' http://127.0.0.1/ echo curl -s -o /dev/null -w 'wifi_login=%{http_code} loc=%{redirect_url}\n' -H 'Host: wifi.otantik.cm' http://127.0.0.1/connexion echo OVERLAY_DONE """ def to_lf(path: Path) -> None: data = path.read_bytes().replace(b"\r\n", b"\n").replace(b"\r", b"\n") path.write_bytes(data) def main() -> None: overlay = SAAS / "nux-custom" nginx = SAAS / "nginx-wifi-otantik.conf" to_lf(nginx) tmp = Path(tempfile.mkdtemp()) sh_path = tmp / "apply_overlay.sh" sh_path.write_bytes(REMOTE_SH.replace("\r\n", "\n").encode("utf-8")) tar_path = tmp / "overlay.tgz" with tarfile.open(tar_path, "w:gz") as tar: tar.add(overlay, arcname="overlay") tar.add(nginx, arcname="nginx-wifi-otantik.conf") tar.add(sh_path, arcname="apply_overlay.sh") print("uploading overlay...") vps_ssh.put(str(tar_path), "/tmp/wifi-otantik-overlay.tgz") code, out = vps_ssh.run( "rm -rf /tmp/wifi-otantik-overlay /tmp/overlay /tmp/apply_overlay.sh /tmp/nginx-wifi-otantik.conf", timeout=30, sudo=True, ) print(out) if code != 0: raise SystemExit(code) code, out = vps_ssh.run( "tar -xzf /tmp/wifi-otantik-overlay.tgz -C /tmp && sed -i 's/\\r$//' /tmp/apply_overlay.sh && chmod +x /tmp/apply_overlay.sh", timeout=60, sudo=False, ) print(out) if code != 0: raise SystemExit(code) print("applying overlay...") code, out = vps_ssh.run("bash /tmp/apply_overlay.sh", timeout=180, sudo=True) print(out) raise SystemExit(code) if __name__ == "__main__": main()