Trash ugly hack that merge services.yml every regenconf

This commit is contained in:
Alexandre Aubin 2021-08-18 23:07:38 +02:00
parent 1f7faabc30
commit a29940d8f4
2 changed files with 49 additions and 98 deletions

View file

@ -2,8 +2,6 @@
set -e set -e
services_path="/etc/yunohost/services.yml"
do_init_regen() { do_init_regen() {
if [[ $EUID -ne 0 ]]; then if [[ $EUID -ne 0 ]]; then
echo "You must be root to run this script" 1>&2 echo "You must be root to run this script" 1>&2
@ -19,8 +17,6 @@ do_init_regen() {
|| echo "yunohost.org" > /etc/yunohost/current_host || echo "yunohost.org" > /etc/yunohost/current_host
# copy default services and firewall # copy default services and firewall
[[ -f $services_path ]] \
|| cp services.yml "$services_path"
[[ -f /etc/yunohost/firewall.yml ]] \ [[ -f /etc/yunohost/firewall.yml ]] \
|| cp firewall.yml /etc/yunohost/firewall.yml || cp firewall.yml /etc/yunohost/firewall.yml
@ -49,6 +45,9 @@ do_init_regen() {
chmod 644 /etc/ssowat/conf.json.persistent chmod 644 /etc/ssowat/conf.json.persistent
chown root:root /etc/ssowat/conf.json.persistent chown root:root /etc/ssowat/conf.json.persistent
# Empty service conf
touch /etc/yunohost/services.yml
mkdir -p /var/cache/yunohost/repo mkdir -p /var/cache/yunohost/repo
chown root:root /var/cache/yunohost chown root:root /var/cache/yunohost
chmod 700 /var/cache/yunohost chmod 700 /var/cache/yunohost
@ -59,25 +58,9 @@ do_pre_regen() {
cd /usr/share/yunohost/templates/yunohost cd /usr/share/yunohost/templates/yunohost
# update services.yml # Legacy code that can be removed once on bullseye
if [[ -f $services_path ]]; then touch /etc/yunohost/services.yml
tmp_services_path="${services_path}-tmp" yunohost tools shell -c "from yunohost.service import _get_services, _save_services; _save_services(_get_services())"
new_services_path="${services_path}-new"
cp "$services_path" "$tmp_services_path"
_update_services "$new_services_path" || {
mv "$tmp_services_path" "$services_path"
exit 1
}
if [[ -f $new_services_path ]]; then
# replace services.yml with new one
mv "$new_services_path" "$services_path"
mv "$tmp_services_path" "${services_path}-old"
else
rm -f "$tmp_services_path"
fi
else
cp services.yml /etc/yunohost/services.yml
fi
mkdir -p $pending_dir/etc/cron.d/ mkdir -p $pending_dir/etc/cron.d/
mkdir -p $pending_dir/etc/cron.daily/ mkdir -p $pending_dir/etc/cron.daily/
@ -206,65 +189,6 @@ do_post_regen() {
[[ ! "$regen_conf_files" =~ "nftables.service.d/ynh-override.conf" ]] || systemctl daemon-reload [[ ! "$regen_conf_files" =~ "nftables.service.d/ynh-override.conf" ]] || systemctl daemon-reload
} }
_update_services() {
python3 - << EOF
import yaml
with open('services.yml') as f:
new_services = yaml.safe_load(f)
with open('/etc/yunohost/services.yml') as f:
services = yaml.safe_load(f) or {}
updated = False
for service, conf in new_services.items():
# remove service with empty conf
if conf is None:
if service in services:
print("removing '{0}' from services".format(service))
del services[service]
updated = True
# add new service
elif not services.get(service, None):
print("adding '{0}' to services".format(service))
services[service] = conf
updated = True
# update service conf
else:
conffiles = services[service].pop('conffiles', {})
# status need to be removed
if "status" not in conf and "status" in services[service]:
print("update '{0}' service status access".format(service))
del services[service]["status"]
updated = True
if services[service] != conf:
print("update '{0}' service".format(service))
services[service].update(conf)
updated = True
if conffiles:
services[service]['conffiles'] = conffiles
# Remove legacy /var/log/daemon.log and /var/log/syslog from log entries
# because they are too general. Instead, now the journalctl log is
# returned by default which is more relevant.
if "log" in services[service]:
if services[service]["log"] in ["/var/log/syslog", "/var/log/daemon.log"]:
del services[service]["log"]
if updated:
with open('/etc/yunohost/services.yml-new', 'w') as f:
yaml.safe_dump(services, f, default_flow_style=False)
EOF
}
FORCE=${2:-0} FORCE=${2:-0}
DRY_RUN=${3:-0} DRY_RUN=${3:-0}

View file

@ -37,10 +37,13 @@ from moulinette import m18n
from yunohost.utils.error import YunohostError, YunohostValidationError from yunohost.utils.error import YunohostError, YunohostValidationError
from moulinette.utils.process import check_output from moulinette.utils.process import check_output
from moulinette.utils.log import getActionLogger from moulinette.utils.log import getActionLogger
from moulinette.utils.filesystem import read_file, append_to_file, write_to_file from moulinette.utils.filesystem import read_file, append_to_file, write_to_file, read_yaml, write_to_yaml
MOULINETTE_LOCK = "/var/run/moulinette_yunohost.lock" MOULINETTE_LOCK = "/var/run/moulinette_yunohost.lock"
SERVICES_CONF = "/etc/yunohost/services.yml"
SERVICES_CONF_BASE = "/usr/share/yunohost/templates/yunohost/services.yml"
logger = getActionLogger("yunohost.service") logger = getActionLogger("yunohost.service")
@ -127,7 +130,8 @@ def service_add(
try: try:
_save_services(services) _save_services(services)
except Exception: except Exception as e:
logger.warning(e)
# we'll get a logger.warning with more details in _save_services # we'll get a logger.warning with more details in _save_services
raise YunohostError("service_add_failed", service=name) raise YunohostError("service_add_failed", service=name)
@ -669,17 +673,19 @@ def _get_services():
""" """
try: try:
with open("/etc/yunohost/services.yml", "r") as f: services = read_yaml(SERVICES_CONF_BASE) or {}
services = yaml.safe_load(f) or {}
# These are keys flagged 'null' in the base conf
legacy_keys_to_delete = [k for k, v in services.items() if v is None]
services.update(read_yaml(SERVICES_CONF) or {})
services = {name: infos
for name, infos in services.items()
if name not in legacy_keys_to_delete}
except Exception: except Exception:
return {} return {}
# some services are marked as None to remove them from YunoHost
# filter this
for key, value in list(services.items()):
if value is None:
del services[key]
# Dirty hack to automatically find custom SSH port ... # Dirty hack to automatically find custom SSH port ...
ssh_port_line = re.findall( ssh_port_line = re.findall(
r"\bPort *([0-9]{2,5})\b", read_file("/etc/ssh/sshd_config") r"\bPort *([0-9]{2,5})\b", read_file("/etc/ssh/sshd_config")
@ -703,6 +709,13 @@ def _get_services():
del services["postgresql"]["description"] del services["postgresql"]["description"]
services["postgresql"]["actual_systemd_service"] = "postgresql@11-main" services["postgresql"]["actual_systemd_service"] = "postgresql@11-main"
# Remove legacy /var/log/daemon.log and /var/log/syslog from log entries
# because they are too general. Instead, now the journalctl log is
# returned by default which is more relevant.
for infos in services.values():
if infos.get("log") in ["/var/log/syslog", "/var/log/daemon.log"]:
del infos["log"]
return services return services
@ -714,12 +727,26 @@ def _save_services(services):
services -- A dict of managed services with their parameters services -- A dict of managed services with their parameters
""" """
try:
with open("/etc/yunohost/services.yml", "w") as f: # Compute the diff with the base file
yaml.safe_dump(services, f, default_flow_style=False) # such that /etc/yunohost/services.yml contains the minimal
except Exception as e: # changes with respect to the base conf
logger.warning("Error while saving services, exception: %s", e, exc_info=1)
raise conf_base = yaml.safe_load(open(SERVICES_CONF_BASE)) or {}
diff = {}
for service_name, service_infos in services.items():
service_conf_base = conf_base.get(service_name, {})
diff[service_name] = {}
for key, value in service_infos.items():
if service_conf_base.get(key) != value:
diff[service_name][key] = value
diff = {name: infos for name, infos in diff.items() if infos}
write_to_yaml(SERVICES_CONF, diff)
def _tail(file, n): def _tail(file, n):