Skip to content

Commit

Permalink
Merge pull request #1951 from YunoHost/enh-auto-dns
Browse files Browse the repository at this point in the history
Enh: Make auto dns feature optional
  • Loading branch information
alexAubin authored Sep 16, 2024
2 parents d2481c8 + 59b7cd6 commit c05bdbc
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 10 deletions.
1 change: 1 addition & 0 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@
"domain_dns_registrar_managed_in_parent_domain": "This domain is a subdomain of {parent_domain_link}. DNS registrar configuration should be managed in {parent_domain}'s configuration panel.",
"domain_dns_registrar_not_supported": "YunoHost could not automatically detect the registrar handling this domain. You should manually configure your DNS records following the documentation at https://yunohost.org/dns.",
"domain_dns_registrar_supported": "YunoHost automatically detected that this domain is handled by the registrar **{registrar}**. If you want, YunoHost will automatically configure this DNS zone, if you provide it with the appropriate API credentials. You can find documentation on how to obtain your API credentials on this page: https://yunohost.org/registar_api_{registrar}. (You can also manually configure your DNS records following the documentation at https://yunohost.org/dns )",
"domain_dns_registrar_use_auto": "Use automatic DNS feature",
"domain_dns_registrar_yunohost": "This domain is a nohost.me / nohost.st / ynh.fr and its DNS configuration is therefore automatically handled by YunoHost without any further configuration. (see the 'yunohost dyndns update' command)",
"domain_dyndns_already_subscribed": "You have already subscribed to a DynDNS domain",
"domain_exists": "The domain already exists",
Expand Down
3 changes: 2 additions & 1 deletion src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
if TYPE_CHECKING:
from pydantic.typing import AbstractSetIntStr, MappingIntStrAny

from yunohost.utils.configpanel import RawSettings
from yunohost.utils.configpanel import RawSettings, ConfigPanelModel
from yunohost.utils.form import FormModel

logger = getLogger("yunohost.app")
Expand Down Expand Up @@ -1860,6 +1860,7 @@ def _get_raw_settings(self) -> "RawSettings":
def _apply(
self,
form: "FormModel",
config: "ConfigPanelModel",
previous_settings: dict[str, Any],
exclude: Union["AbstractSetIntStr", "MappingIntStrAny", None] = None,
) -> None:
Expand Down
13 changes: 10 additions & 3 deletions src/dns.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,8 @@ def _get_registrar_config_section(domain):
elif is_special_use_tld(dns_zone):
registrar_infos["infos"]["ask"] = m18n.n("domain_dns_conf_special_use_tld")

return registrar_infos

try:
registrar = _relevant_provider_for_domain(dns_zone)[0]
except ValueError:
Expand Down Expand Up @@ -554,9 +556,15 @@ def _get_registrar_config_section(domain):
f"Registrar {registrar} unknown / Should be added to YunoHost's registrar_list.toml by the development team!"
)
registrar_credentials = {}
else:
registrar_infos["use_auto_dns"] = {
"type": "boolean",
"ask": m18n.n("domain_dns_registrar_use_auto"),
"default": True
}
for credential, infos in registrar_credentials.items():
infos["default"] = infos.get("default", "")
infos["optional"] = infos.get("optional", "False")
infos["visible"] = "use_auto_dns == true"
registrar_infos.update(registrar_credentials)

return registrar_infos
Expand Down Expand Up @@ -589,8 +597,7 @@ def domain_dns_push(operation_logger, domain, dry_run=False, force=False, purge=
_assert_domain_exists(domain)

if is_special_use_tld(domain):
logger.info(m18n.n("domain_dns_conf_special_use_tld"))
return {}
raise YunohostValidationError("domain_dns_conf_special_use_tld")

if not registrar or registrar == "None": # yes it's None as a string
raise YunohostValidationError("domain_dns_push_not_applicable", domain=domain)
Expand Down
26 changes: 24 additions & 2 deletions src/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
from pydantic.typing import AbstractSetIntStr, MappingIntStrAny

from yunohost.utils.configpanel import RawConfig
from yunohost.utils.form import FormModel
from yunohost.utils.form import FormModel, ConfigPanelModel
from yunohost.utils.configpanel import RawSettings

logger = getLogger("yunohost.domain")
Expand Down Expand Up @@ -678,6 +678,7 @@ def domain_config_set(

def _get_DomainConfigPanel():
from yunohost.utils.configpanel import ConfigPanel
from yunohost.dns import _set_managed_dns_records_hashes

class DomainConfigPanel(ConfigPanel):
entity_type = "domain"
Expand Down Expand Up @@ -755,6 +756,7 @@ def _get_raw_settings(self) -> "RawSettings":
def _apply(
self,
form: "FormModel",
config: "ConfigPanelModel",
previous_settings: dict[str, Any],
exclude: Union["AbstractSetIntStr", "MappingIntStrAny", None] = None,
) -> None:
Expand All @@ -778,6 +780,20 @@ def _apply(
self.entity, next_settings["recovery_password"]
)

# NB: this is subtlely different from just checking `next_settings.get("use_auto_dns") since we want to find the exact situation where the admin *disables* the autodns`
remove_auto_dns_feature = "use_auto_dns" in next_settings and not next_settings["use_auto_dns"]
if remove_auto_dns_feature:
# disable auto dns by reseting every registrar form values
options = [
option
for option in config.get_section("registrar").options
if not option.readonly
and option.id != "use_auto_dns"
and hasattr(form, option.id)
]
for option in options:
setattr(form, option.id, option.default)

custom_css = next_settings.pop("custom_css", "").strip()
if custom_css:
write_to_file(f"/usr/share/yunohost/portal/customassets/{self.entity}.custom.css", custom_css)
Expand Down Expand Up @@ -837,7 +853,13 @@ def _apply(
str(portal_settings_path), portal_settings, sort_keys=True, indent=4
)

super()._apply(form, previous_settings, exclude={"recovery_password"})
super()._apply(
form, config, previous_settings, exclude={"recovery_password"}
)

# Also remove `managed_dns_records_hashes` in settings which are not handled by the config panel
if remove_auto_dns_feature:
_set_managed_dns_records_hashes(self.entity, [])

# Reload ssowat if default app changed
if "default_app" in next_settings or "enable_public_apps_page" in next_settings:
Expand Down
6 changes: 4 additions & 2 deletions src/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from yunohost.log import OperationLogger
from yunohost.utils.configpanel import (
ConfigPanelGetMode,
ConfigPanelModel,
RawSettings,
)
from yunohost.utils.form import FormModel
Expand Down Expand Up @@ -174,7 +175,7 @@ def reset(
if operation_logger:
operation_logger.start()
try:
self._apply(self.form, previous_settings)
self._apply(self.form, self.config, previous_settings)
except YunohostError:
raise
# Script got manually interrupted ...
Expand Down Expand Up @@ -220,6 +221,7 @@ def _get_raw_settings(self) -> "RawSettings":
def _apply(
self,
form: "FormModel",
config: "ConfigPanelModel",
previous_settings: dict[str, Any],
exclude: Union["AbstractSetIntStr", "MappingIntStrAny", None] = None,
) -> None:
Expand All @@ -245,7 +247,7 @@ def _apply(
)

# First save settings except virtual + default ones
super()._apply(form, previous_settings, exclude=self.virtual_settings)
super()._apply(form, config, previous_settings, exclude=self.virtual_settings)
next_settings = {
k: v
for k, v in form.dict(exclude=self.virtual_settings).items()
Expand Down
17 changes: 15 additions & 2 deletions src/utils/configpanel.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,19 @@ def options(self) -> Iterator[AnyOption]:
for option in section.options:
yield option

def get_option(self, option_id) -> Union[AnyOption, None]:
def get_panel(self, panel_id: str) -> Union[PanelModel, None]:
for panel in self.panels:
if panel.id == panel_id:
return panel
return None

def get_section(self, section_id: str) -> Union[SectionModel, None]:
for section in self.sections:
if section.id == section_id:
return section
return None

def get_option(self, option_id: str) -> Union[AnyOption, None]:
for option in self.options:
if option.id == option_id:
return option
Expand Down Expand Up @@ -573,7 +585,7 @@ def set(
operation_logger.start()

try:
self._apply(self.form, previous_settings)
self._apply(self.form, self.config, previous_settings)
except YunohostError:
raise
# Script got manually interrupted ...
Expand Down Expand Up @@ -866,6 +878,7 @@ def _ask(
def _apply(
self,
form: "FormModel",
config: ConfigPanelModel,
previous_settings: dict[str, Any],
exclude: Union["AbstractSetIntStr", "MappingIntStrAny", None] = None,
) -> None:
Expand Down

0 comments on commit c05bdbc

Please sign in to comment.