-
Notifications
You must be signed in to change notification settings - Fork 9
/
setup_pbl.py
executable file
·137 lines (115 loc) · 3.87 KB
/
setup_pbl.py
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
#!/usr/bin/env python3
import dbus
import os
import re
import shutil
from subprocess import run, PIPE, STDOUT
import sys
import xml.etree.ElementTree as ET
name_short = "pbl"
server_root = "/etc/httpd"
document_root = "/var/www/html"
httpd_conf = "/conf/".join((server_root, "httpd.conf"))
httpd_welcome_file = "/conf.d/".join((server_root, "welcome.conf"))
httpd_welcome_conf = "# disable welcome page"
httpd_listen = "Listen 8080"
httpd_custom_file = "99-publish_behave_logs.conf"
httpd_custom_file_full = "/conf.d/".join((server_root, httpd_custom_file))
firewalld_etc = "/etc/firewalld"
source_dir = os.path.dirname(__file__)
os.makedirs(document_root, exist_ok=True)
# set up apache
try:
with open(httpd_welcome_file, "r+") as f:
welcome_orig = f.read()
f.truncate(0)
f.seek(0)
f.write(httpd_welcome_conf)
with open(httpd_conf, "r+", encoding="UTF-8") as f:
conf_orig = f.read()
conf_new = re.sub("(?m)^Listen\s+.*$", httpd_listen, conf_orig)
f.truncate(0)
f.seek(0)
f.write(conf_new)
shutil.copy("/".join((source_dir, httpd_custom_file)), httpd_custom_file_full)
# raise exception if configuration test fails
configtest = run(
("apachectl", "configtest"),
check=True,
stdout=PIPE,
stderr=STDOUT,
encoding="UTF-8",
)
except:
with open(httpd_welcome_file, "w") as f:
f.truncate(0)
f.write(welcome_orig)
with open(httpd_conf) as f:
f.truncate(0)
f.write(conf_orig)
os.remove(httpd_custom_file_full)
raise SystemExit(
f"apachectl configtest failed! configtest output was:\n\n{configtest.stdout}"
)
sys.stderr.write("httpd configured\n")
shutil.copy("/".join((source_dir, "publish_behave_logs")), "/usr/local/bin/")
if not os.path.isfile("/etc/os-release"):
shutil.copy(
"/".join((source_dir, "publish_behave_logs.service.el7")),
"/etc/systemd/system/publish_behave_logs.service",
)
else:
shutil.copy(
"/".join((source_dir, "publish_behave_logs.service")), "/etc/systemd/system/"
)
os.symlink(
"/etc/systemd/system/publish_behave_logs.service",
"/etc/systemd/system/multi-user.target.wants/publish_behave_logs.service",
)
os.symlink(
"/usr/lib/systemd/system/httpd.service",
"/etc/systemd/system/multi-user.target.wants/httpd.service",
)
sys.stderr.write("service files installed\n")
shutil.copy(
"/".join([source_dir, "service.xml"]),
"/".join([firewalld_etc, "services", name_short + ".xml"]),
)
shutil.copy(
"/".join([source_dir, "policy.xml"]),
"/".join([firewalld_etc, "policies", name_short + ".xml"]),
)
# load and restart systemd services
have_dbus = False
try:
dbus_system_bus = dbus.SystemBus()
have_dbus = True
except dbus.exceptions.DBusException:
pass
if have_dbus:
dbus_systemd = dbus_system_bus.get_object(
"org.freedesktop.systemd1", "/org/freedesktop/systemd1"
)
dbus_systemd_manager = dbus.Interface(
dbus_systemd, "org.freedesktop.systemd1.Manager"
)
dbus_systemd_manager.EnableUnitFiles(
["httpd.service", "publish_behave_logs.service"], False, True
)
dbus_systemd_manager.Reload()
dbus_systemd_manager.RestartUnit("publish_behave_logs.service", "replace")
dbus_systemd_manager.RestartUnit("httpd.service", "replace")
try:
dbus_systemd_manager.ReloadOrTryRestartUnit("firewalld.service", "replace")
except dbus.exceptions.DBusException:
pass
sys.stderr.write("services enabled and started, all should be set now\n")
else:
msg = [
"systemd not found on dbus. Please do the following:",
"- reload units: systemctl daemon-reload",
"- enable pbl: systemctl enable --now publish_behave_logs.service"
"- if firewalld is active, restart it: systemctl restart firewalld",
"",
]
sys.stderr.write("\n".join(msg))