-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapache_welcome.py
More file actions
178 lines (150 loc) · 5.08 KB
/
Copy pathapache_welcome.py
File metadata and controls
178 lines (150 loc) · 5.08 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
from __future__ import annotations
import re
import subprocess
import tempfile
from pathlib import Path
import privileged
WELCOME_VERSION = "1"
WELCOME_MARKER = 'content="Amper Linux Control Panel 1"'
WELCOME_TEMPLATE = Path(__file__).resolve().parent / "assets" / "welcome" / "index.html"
DOCUMENT_ROOT_CANDIDATES = [
"/var/www/html",
"/var/www",
"/srv/www/htdocs",
"/usr/local/apache2/htdocs",
]
APACHE_CONFIG_CANDIDATES = [
"/etc/apache2/sites-enabled/000-default.conf",
"/etc/apache2/sites-enabled/default.conf",
"/etc/apache2/sites-available/000-default.conf",
"/etc/httpd/conf/httpd.conf",
"/etc/httpd/conf.d/000-default.conf",
]
DEFAULT_PAGE_MARKERS = [
"Apache2 Ubuntu Default Page",
"Apache2 Debian Default Page",
"It works!",
"Test Page for the Apache HTTP Server",
]
def _run_command(args: list[str]) -> subprocess.CompletedProcess[str]:
return subprocess.run(
args,
capture_output=True,
text=True,
check=False,
)
def apache_is_available() -> bool:
result = _run_command(
[
"systemctl",
"list-unit-files",
"--type=service",
"--no-pager",
"--no-legend",
]
)
for line in result.stdout.splitlines():
parts = line.split()
if not parts:
continue
name = parts[0].removesuffix(".service")
if name in {"apache2", "httpd"}:
return True
for path in APACHE_CONFIG_CANDIDATES:
if Path(path).exists():
return True
return False
def _parse_document_root_from_text(text: str) -> str | None:
match = re.search(r"^\s*DocumentRoot\s+(\S+)", text, re.MULTILINE)
if not match:
return None
return match.group(1).strip().strip('"').strip("'")
def find_document_root() -> str | None:
for config_path in APACHE_CONFIG_CANDIDATES:
path = Path(config_path)
if not path.is_file():
continue
root = _parse_document_root_from_text(path.read_text(encoding="utf-8", errors="ignore"))
if root and Path(root).is_dir():
return root
config_dir = Path("/etc/apache2/sites-enabled")
if config_dir.is_dir():
for config_path in sorted(config_dir.iterdir()):
if not config_path.is_file():
continue
root = _parse_document_root_from_text(
config_path.read_text(encoding="utf-8", errors="ignore")
)
if root and Path(root).is_dir():
return root
for candidate in DOCUMENT_ROOT_CANDIDATES:
if Path(candidate).is_dir():
return candidate
return None
def load_welcome_html() -> str:
if WELCOME_TEMPLATE.is_file():
return WELCOME_TEMPLATE.read_text(encoding="utf-8")
return ""
def _is_default_apache_page(content: str) -> bool:
return any(marker in content for marker in DEFAULT_PAGE_MARKERS)
def _is_amper_page(content: str) -> bool:
return WELCOME_MARKER in content
def should_deploy(document_root: str) -> bool:
index_path = Path(document_root) / "index.html"
html = load_welcome_html()
if not html:
return False
if not index_path.is_file():
return True
try:
current = index_path.read_text(encoding="utf-8", errors="ignore")
except OSError:
return True
if _is_amper_page(current):
return current.strip() != html.strip()
if _is_default_apache_page(current):
return True
return False
def _write_privileged(source: Path, target: Path) -> bool:
if privileged.run(["cp", str(source), str(target)]).returncode != 0:
return False
privileged.run(["chown", "www-data:www-data", str(target)])
privileged.run(["chown", "apache:apache", str(target)])
return True
def deploy_welcome(document_root: str) -> tuple[bool, str]:
html = load_welcome_html()
if not html:
return False, "welcome template missing"
target = Path(document_root) / "index.html"
if not target.parent.is_dir():
return False, "document root not found"
with tempfile.NamedTemporaryFile(
mode="w",
encoding="utf-8",
suffix=".html",
delete=False,
) as handle:
handle.write(html)
temp_path = Path(handle.name)
try:
try:
target.write_text(html, encoding="utf-8")
return True, str(target)
except PermissionError:
if _write_privileged(temp_path, target):
return True, str(target)
return False, "permission denied"
finally:
temp_path.unlink(missing_ok=True)
def sync_welcome_if_needed() -> tuple[bool, str, str]:
if not apache_is_available():
return False, "", "apache not installed"
document_root = find_document_root()
if not document_root:
return False, "", "document root not found"
if not should_deploy(document_root):
return False, document_root, "welcome page already up to date"
ok, detail = deploy_welcome(document_root)
if ok:
return True, document_root, detail
return False, document_root, detail