perf: lazyloading settings_get + pass all the global settings as an env variable to the regenconf to prevent having to 'yunohost settings get' like ten times ... reduces the regenconf runtime from 35s to 23s on my machine

This commit is contained in:
Alexandre Aubin 2023-11-28 21:12:03 +01:00
parent 9a4b0e422d
commit 16391d7374
9 changed files with 33 additions and 26 deletions

View file

@ -1084,7 +1084,7 @@ _ynh_apply_default_permissions() {
} }
int_to_bool() { int_to_bool() {
sed -e 's/^1$/True/g' -e 's/^0$/False/g' sed -e 's/^1$/True/g' -e 's/^0$/False/g' -e 's/^true$/True/g' -e 's/^false$/False/g'
} }
toml_to_json() { toml_to_json() {

View file

@ -9,17 +9,16 @@ do_pre_regen() {
cd /usr/share/yunohost/conf/ssh cd /usr/share/yunohost/conf/ssh
# Support different strategy for security configurations
export compatibility="$(jq -r '.ssh_compatibility' <<< "$YNH_SETTINGS")"
export port="$(jq -r '.ssh_port' <<< "$YNH_SETTINGS")"
export password_authentication="$(jq -r '.ssh_password_authentication' <<< "$YNH_SETTINGS" | int_to_bool)"
export ssh_keys=$(ls /etc/ssh/ssh_host_{ed25519,rsa,ecdsa}_key 2>/dev/null || true)
# do not listen to IPv6 if unavailable # do not listen to IPv6 if unavailable
[[ -f /proc/net/if_inet6 ]] && ipv6_enabled=true || ipv6_enabled=false [[ -f /proc/net/if_inet6 ]] && ipv6_enabled=true || ipv6_enabled=false
ssh_keys=$(ls /etc/ssh/ssh_host_{ed25519,rsa,ecdsa}_key 2>/dev/null || true)
# Support different strategy for security configurations
export compatibility="$(yunohost settings get 'security.ssh.ssh_compatibility')"
export port="$(yunohost settings get 'security.ssh.ssh_port')"
export password_authentication="$(yunohost settings get 'security.ssh.ssh_password_authentication' | int_to_bool)"
export ssh_keys
export ipv6_enabled export ipv6_enabled
ynh_render_template "sshd_config" "${pending_dir}/etc/ssh/sshd_config" ynh_render_template "sshd_config" "${pending_dir}/etc/ssh/sshd_config"
} }

View file

@ -56,7 +56,7 @@ do_pre_regen() {
# install / update plain conf files # install / update plain conf files
cp plain/* "$nginx_conf_dir" cp plain/* "$nginx_conf_dir"
# remove the panel overlay if this is specified in settings # remove the panel overlay if this is specified in settings
panel_overlay=$(yunohost settings get 'misc.portal.ssowat_panel_overlay_enabled' | int_to_bool) panel_overlay="$(jq -r '.ssowat_panel_overlay_enabled' <<< "$YNH_SETTINGS" | int_to_bool)"
if [ "$panel_overlay" == "False" ]; then if [ "$panel_overlay" == "False" ]; then
echo "#" >"${nginx_conf_dir}/yunohost_panel.conf.inc" echo "#" >"${nginx_conf_dir}/yunohost_panel.conf.inc"
fi fi
@ -65,9 +65,9 @@ do_pre_regen() {
main_domain=$(cat /etc/yunohost/current_host) main_domain=$(cat /etc/yunohost/current_host)
# Support different strategy for security configurations # Support different strategy for security configurations
export redirect_to_https="$(yunohost settings get 'security.nginx.nginx_redirect_to_https' | int_to_bool)" export redirect_to_https="$(jq -r '.nginx_redirect_to_https' <<< "$YNH_SETTINGS" | int_to_bool)"
export compatibility="$(yunohost settings get 'security.nginx.nginx_compatibility')" export compatibility="$(jq -r '.nginx_compatibility' <<< "$YNH_SETTINGS" | int_to_bool)"
export experimental="$(yunohost settings get 'security.experimental.security_experimental_enabled' | int_to_bool)" export experimental="$(jq -r '.security_experimental_enabled' <<< "$YNH_SETTINGS" | int_to_bool)"
ynh_render_template "security.conf.inc" "${nginx_conf_dir}/security.conf.inc" ynh_render_template "security.conf.inc" "${nginx_conf_dir}/security.conf.inc"
cert_status=$(yunohost domain cert status --json) cert_status=$(yunohost domain cert status --json)
@ -109,9 +109,9 @@ do_pre_regen() {
done done
export webadmin_allowlist_enabled=$(yunohost settings get security.webadmin.webadmin_allowlist_enabled | int_to_bool) export webadmin_allowlist_enabled="$(jq -r '.webadmin_allowlist_enabled' <<< "$YNH_SETTINGS" | int_to_bool)"
if [ "$webadmin_allowlist_enabled" == "True" ]; then if [ "$webadmin_allowlist_enabled" == "True" ]; then
export webadmin_allowlist=$(yunohost settings get security.webadmin.webadmin_allowlist) export webadmin_allowlist="$(jq -r '.webadmin_allowlist' <<< "$YNH_SETTINGS" | sed 's/^null$//g')"
fi fi
ynh_render_template "yunohost_admin.conf.inc" "${nginx_conf_dir}/yunohost_admin.conf.inc" ynh_render_template "yunohost_admin.conf.inc" "${nginx_conf_dir}/yunohost_admin.conf.inc"
ynh_render_template "yunohost_api.conf.inc" "${nginx_conf_dir}/yunohost_api.conf.inc" ynh_render_template "yunohost_api.conf.inc" "${nginx_conf_dir}/yunohost_api.conf.inc"

View file

@ -22,19 +22,19 @@ do_pre_regen() {
main_domain=$(cat /etc/yunohost/current_host) main_domain=$(cat /etc/yunohost/current_host)
# Support different strategy for security configurations # Support different strategy for security configurations
export compatibility="$(yunohost settings get 'security.postfix.postfix_compatibility')" export compatibility="$(jq -r '.postfix_compatibility' <<< "$YNH_SETTINGS")"
# Add possibility to specify a relay # Add possibility to specify a relay
# Could be useful with some isp with no 25 port open or more complex setup # Could be useful with some isp with no 25 port open or more complex setup
export relay_port="" export relay_port=""
export relay_user="" export relay_user=""
export relay_host="" export relay_host=""
export relay_enabled="$(yunohost settings get 'email.smtp.smtp_relay_enabled' | int_to_bool)" export relay_enabled="$(jq -r '.smtp_relay_enabled' <<< "$YNH_SETTINGS" | int_to_bool)"
if [ "${relay_enabled}" == "True" ]; then if [ "${relay_enabled}" == "True" ]; then
relay_host="$(yunohost settings get 'email.smtp.smtp_relay_host')" relay_host="$(jq -r '.smtp_relay_host' <<< "$YNH_SETTINGS")"
relay_port="$(yunohost settings get 'email.smtp.smtp_relay_port')" relay_port="$(jq -r '.smtp_relay_port' <<< "$YNH_SETTINGS")"
relay_user="$(yunohost settings get 'email.smtp.smtp_relay_user')" relay_user="$(jq -r '.smtp_relay_user' <<< "$YNH_SETTINGS")"
relay_password="$(yunohost settings get 'email.smtp.smtp_relay_password')" relay_password="$(jq -r '.smtp_relay_password' <<< "$YNH_SETTINGS")"
# Avoid to display "Relay account paswword" to other users # Avoid to display "Relay account paswword" to other users
touch ${postfix_dir}/sasl_passwd touch ${postfix_dir}/sasl_passwd
@ -56,7 +56,7 @@ do_pre_regen() {
>"${default_dir}/postsrsd" >"${default_dir}/postsrsd"
# adapt it for IPv4-only hosts # adapt it for IPv4-only hosts
ipv6="$(yunohost settings get 'email.smtp.smtp_allow_ipv6' | int_to_bool)" ipv6="$(jq -r '.smtp_allow_ipv6' <<< "$YNH_SETTINGS" | int_to_bool)"
if [ "$ipv6" == "False" ] || [ ! -f /proc/net/if_inet6 ]; then if [ "$ipv6" == "False" ] || [ ! -f /proc/net/if_inet6 ]; then
sed -i \ sed -i \
's/ \[::ffff:127.0.0.0\]\/104 \[::1\]\/128//g' \ 's/ \[::ffff:127.0.0.0\]\/104 \[::1\]\/128//g' \

View file

@ -16,7 +16,7 @@ do_pre_regen() {
cp dovecot-ldap.conf "${dovecot_dir}/dovecot-ldap.conf" cp dovecot-ldap.conf "${dovecot_dir}/dovecot-ldap.conf"
cp dovecot.sieve "${dovecot_dir}/global_script/dovecot.sieve" cp dovecot.sieve "${dovecot_dir}/global_script/dovecot.sieve"
export pop3_enabled="$(yunohost settings get 'email.pop3.pop3_enabled' | int_to_bool)" export pop3_enabled="$(jq -r '.pop3_enabled' <<< "$YNH_SETTINGS" | int_to_bool)"
export main_domain=$(cat /etc/yunohost/current_host) export main_domain=$(cat /etc/yunohost/current_host)
export domain_list="$(yunohost domain list --features mail_in mail_out --output-as json | jq -r ".domains[]" | tr '\n' ' ')" export domain_list="$(yunohost domain list --features mail_in mail_out --output-as json | jq -r ".domains[]" | tr '\n' ' ')"

View file

@ -18,7 +18,7 @@ do_pre_regen() {
cp postfix-sasl.conf "${fail2ban_dir}/filter.d/postfix-sasl.conf" cp postfix-sasl.conf "${fail2ban_dir}/filter.d/postfix-sasl.conf"
cp jail.conf "${fail2ban_dir}/jail.conf" cp jail.conf "${fail2ban_dir}/jail.conf"
export ssh_port="$(yunohost settings get 'security.ssh.ssh_port')" export ssh_port="$(jq -r '.ssh_port' <<< "$YNH_SETTINGS")"
ynh_render_template "yunohost-jails.conf" "${fail2ban_dir}/jail.d/yunohost-jails.conf" ynh_render_template "yunohost-jails.conf" "${fail2ban_dir}/jail.d/yunohost-jails.conf"
} }

View file

@ -76,7 +76,6 @@ from yunohost.utils.system import (
binary_to_human, binary_to_human,
space_used_by_directory, space_used_by_directory,
) )
from yunohost.settings import settings_get
BACKUP_PATH = "/home/yunohost.backup" BACKUP_PATH = "/home/yunohost.backup"
ARCHIVES_PATH = f"{BACKUP_PATH}/archives" ARCHIVES_PATH = f"{BACKUP_PATH}/archives"
@ -1926,6 +1925,7 @@ class TarBackupMethod(BackupMethod):
@property @property
def _archive_file(self): def _archive_file(self):
from yunohost.settings import settings_get
if isinstance(self.manager, BackupManager) and settings_get( if isinstance(self.manager, BackupManager) and settings_get(
"misc.backup.backup_compress_tar_archives" "misc.backup.backup_compress_tar_archives"
): ):

View file

@ -37,7 +37,6 @@ from yunohost.domain import (
from yunohost.utils.dns import dig, is_yunohost_dyndns_domain, is_special_use_tld from yunohost.utils.dns import dig, is_yunohost_dyndns_domain, is_special_use_tld
from yunohost.utils.error import YunohostValidationError, YunohostError from yunohost.utils.error import YunohostValidationError, YunohostError
from yunohost.utils.network import get_public_ip from yunohost.utils.network import get_public_ip
from yunohost.settings import settings_get
from yunohost.log import is_unit_operation from yunohost.log import is_unit_operation
from yunohost.hook import hook_callback from yunohost.hook import hook_callback
@ -145,6 +144,8 @@ def _build_dns_conf(base_domain, include_empty_AAAA_if_no_ipv6=False):
} }
""" """
from yunohost.settings import settings_get
basic = [] basic = []
mail = [] mail = []
xmpp = [] xmpp = []

View file

@ -20,6 +20,7 @@ import os
import yaml import yaml
import shutil import shutil
import hashlib import hashlib
import json
from logging import getLogger from logging import getLogger
from difflib import unified_diff from difflib import unified_diff
from datetime import datetime from datetime import datetime
@ -63,6 +64,8 @@ def regen_conf(
""" """
from yunohost.settings import settings_get
if names is None: if names is None:
names = [] names = []
@ -140,6 +143,10 @@ def regen_conf(
domain_list(exclude_subdomains=True)["domains"] domain_list(exclude_subdomains=True)["domains"]
) )
env["YNH_CONTEXT"] = "regenconf" env["YNH_CONTEXT"] = "regenconf"
# perf: Export all global settings as a environment variable
# so that scripts dont have to call 'yunohost settings get' manually
# which is painful performance-wise
env["YNH_SETTINGS"] = json.dumps(settings_get("", export=True))
pre_result = hook_callback("conf_regen", names, pre_callback=_pre_call, env=env) pre_result = hook_callback("conf_regen", names, pre_callback=_pre_call, env=env)