mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
Run black
& revert misguidedly cosmetic changes
An obscur plugin must have done this...
This commit is contained in:
parent
e40f8fb861
commit
ced4da4171
1 changed files with 102 additions and 57 deletions
|
@ -43,7 +43,7 @@ from yunohost.app import (
|
|||
_installed_apps,
|
||||
_get_app_settings,
|
||||
_get_conflicting_apps,
|
||||
_parse_args_in_yunohost_format
|
||||
_parse_args_in_yunohost_format,
|
||||
)
|
||||
from yunohost.regenconf import regen_conf, _force_clear_hashes, _process_regen_conf
|
||||
from yunohost.utils.network import get_public_ip
|
||||
|
@ -56,6 +56,7 @@ logger = getActionLogger("yunohost.domain")
|
|||
DOMAIN_SETTINGS_PATH = "/etc/yunohost/domains.yml"
|
||||
REGISTRAR_LIST_PATH = "/usr/share/yunohost/other/providers_list.yml"
|
||||
|
||||
|
||||
def domain_list(exclude_subdomains=False):
|
||||
"""
|
||||
List domains
|
||||
|
@ -109,6 +110,7 @@ def domain_add(operation_logger, domain, dyndns=False):
|
|||
from yunohost.hook import hook_callback
|
||||
from yunohost.app import app_ssowatconf
|
||||
from yunohost.utils.ldap import _get_ldap_interface
|
||||
from yunohost.certificate import _certificate_install_selfsigned
|
||||
|
||||
if domain.startswith("xmpp-upload."):
|
||||
raise YunohostValidationError("domain_cannot_add_xmpp_upload")
|
||||
|
@ -142,14 +144,13 @@ def domain_add(operation_logger, domain, dyndns=False):
|
|||
|
||||
if dyndns:
|
||||
from yunohost.dyndns import dyndns_subscribe
|
||||
|
||||
# Actually subscribe
|
||||
dyndns_subscribe(domain=domain)
|
||||
|
||||
_certificate_install_selfsigned([domain], False)
|
||||
|
||||
try:
|
||||
import yunohost.certificate
|
||||
|
||||
yunohost.certificate._certificate_install_selfsigned([domain], False)
|
||||
|
||||
attr_dict = {
|
||||
"objectClass": ["mailDomain", "top"],
|
||||
"virtualdomain": domain,
|
||||
|
@ -176,13 +177,13 @@ def domain_add(operation_logger, domain, dyndns=False):
|
|||
regen_conf(names=["nginx", "metronome", "dnsmasq", "postfix", "rspamd"])
|
||||
app_ssowatconf()
|
||||
|
||||
except Exception:
|
||||
except Exception as e:
|
||||
# Force domain removal silently
|
||||
try:
|
||||
domain_remove(domain, force=True)
|
||||
except Exception:
|
||||
pass
|
||||
raise
|
||||
raise e
|
||||
|
||||
hook_callback("post_domain_add", args=[domain])
|
||||
|
||||
|
@ -234,21 +235,37 @@ def domain_remove(operation_logger, domain, remove_apps=False, force=False):
|
|||
settings = _get_app_settings(app)
|
||||
label = app_info(app)["name"]
|
||||
if settings.get("domain") == domain:
|
||||
apps_on_that_domain.append((app, " - %s \"%s\" on https://%s%s" % (app, label, domain, settings["path"]) if "path" in settings else app))
|
||||
apps_on_that_domain.append(
|
||||
(
|
||||
app,
|
||||
' - %s "%s" on https://%s%s'
|
||||
% (app, label, domain, settings["path"])
|
||||
if "path" in settings
|
||||
else app,
|
||||
)
|
||||
)
|
||||
|
||||
if apps_on_that_domain:
|
||||
if remove_apps:
|
||||
if msettings.get('interface') == "cli" and not force:
|
||||
answer = msignals.prompt(m18n.n('domain_remove_confirm_apps_removal',
|
||||
apps="\n".join([x[1] for x in apps_on_that_domain]),
|
||||
answers='y/N'), color="yellow")
|
||||
if msettings.get("interface") == "cli" and not force:
|
||||
answer = msignals.prompt(
|
||||
m18n.n(
|
||||
"domain_remove_confirm_apps_removal",
|
||||
apps="\n".join([x[1] for x in apps_on_that_domain]),
|
||||
answers="y/N",
|
||||
),
|
||||
color="yellow",
|
||||
)
|
||||
if answer.upper() != "Y":
|
||||
raise YunohostError("aborting")
|
||||
|
||||
for app, _ in apps_on_that_domain:
|
||||
app_remove(app)
|
||||
else:
|
||||
raise YunohostValidationError('domain_uninstall_app_first', apps="\n".join([x[1] for x in apps_on_that_domain]))
|
||||
raise YunohostValidationError(
|
||||
"domain_uninstall_app_first",
|
||||
apps="\n".join([x[1] for x in apps_on_that_domain]),
|
||||
)
|
||||
|
||||
operation_logger.start()
|
||||
|
||||
|
@ -261,7 +278,7 @@ def domain_remove(operation_logger, domain, remove_apps=False, force=False):
|
|||
os.system("rm -rf /etc/yunohost/certs/%s" % domain)
|
||||
|
||||
# Delete dyndns keys for this domain (if any)
|
||||
os.system('rm -rf /etc/yunohost/dyndns/K%s.+*' % domain)
|
||||
os.system("rm -rf /etc/yunohost/dyndns/K%s.+*" % domain)
|
||||
|
||||
# Sometime we have weird issues with the regenconf where some files
|
||||
# appears as manually modified even though they weren't touched ...
|
||||
|
@ -475,7 +492,9 @@ def _build_dns_conf(domains):
|
|||
extra = []
|
||||
ipv4 = get_public_ip()
|
||||
ipv6 = get_public_ip(6)
|
||||
owned_dns_zone = "owned_dns_zone" in domains[root] and domains[root]["owned_dns_zone"]
|
||||
owned_dns_zone = (
|
||||
"owned_dns_zone" in domains[root] and domains[root]["owned_dns_zone"]
|
||||
)
|
||||
|
||||
root_prefix = root.partition(".")[0]
|
||||
child_domain_suffix = ""
|
||||
|
@ -484,15 +503,14 @@ def _build_dns_conf(domains):
|
|||
ttl = domain["ttl"]
|
||||
|
||||
if domain_name == root:
|
||||
name = root_prefix if not owned_dns_zone else "@"
|
||||
name = root_prefix if not owned_dns_zone else "@"
|
||||
else:
|
||||
name = domain_name[0:-(1 + len(root))]
|
||||
name = domain_name[0 : -(1 + len(root))]
|
||||
if not owned_dns_zone:
|
||||
name += "." + root_prefix
|
||||
|
||||
|
||||
if name != "@":
|
||||
child_domain_suffix = "." + name
|
||||
|
||||
|
||||
###########################
|
||||
# Basic ipv4/ipv6 records #
|
||||
|
@ -530,8 +548,18 @@ def _build_dns_conf(domains):
|
|||
########
|
||||
if domain["xmpp"]:
|
||||
xmpp += [
|
||||
[f"_xmpp-client._tcp{child_domain_suffix}", ttl, "SRV", f"0 5 5222 {domain_name}."],
|
||||
[f"_xmpp-server._tcp{child_domain_suffix}", ttl, "SRV", f"0 5 5269 {domain_name}."],
|
||||
[
|
||||
f"_xmpp-client._tcp{child_domain_suffix}",
|
||||
ttl,
|
||||
"SRV",
|
||||
f"0 5 5222 {domain_name}.",
|
||||
],
|
||||
[
|
||||
f"_xmpp-server._tcp{child_domain_suffix}",
|
||||
ttl,
|
||||
"SRV",
|
||||
f"0 5 5269 {domain_name}.",
|
||||
],
|
||||
["muc" + child_domain_suffix, ttl, "CNAME", name],
|
||||
["pubsub" + child_domain_suffix, ttl, "CNAME", name],
|
||||
["vjud" + child_domain_suffix, ttl, "CNAME", name],
|
||||
|
@ -542,7 +570,6 @@ def _build_dns_conf(domains):
|
|||
# Extra #
|
||||
#########
|
||||
|
||||
|
||||
if ipv4:
|
||||
extra.append([f"*{child_domain_suffix}", ttl, "A", ipv4])
|
||||
|
||||
|
@ -729,7 +756,13 @@ def _load_domain_settings():
|
|||
new_domains[domain] = {}
|
||||
# new_domains[domain] = { "main": is_maindomain }
|
||||
# Set other values (default value if missing)
|
||||
for setting, default in [ ("xmpp", is_maindomain), ("mail", True), ("owned_dns_zone", default_owned_dns_zone), ("ttl", 3600), ("provider", False)]:
|
||||
for setting, default in [
|
||||
("xmpp", is_maindomain),
|
||||
("mail", True),
|
||||
("owned_dns_zone", default_owned_dns_zone),
|
||||
("ttl", 3600),
|
||||
("provider", False),
|
||||
]:
|
||||
if domain_in_old_domains and setting in old_domains[domain].keys():
|
||||
new_domains[domain][setting] = old_domains[domain][setting]
|
||||
else:
|
||||
|
@ -737,6 +770,7 @@ def _load_domain_settings():
|
|||
|
||||
return new_domains
|
||||
|
||||
|
||||
def domain_setting(domain, key, value=None, delete=False):
|
||||
"""
|
||||
Set or get an app setting value
|
||||
|
@ -753,7 +787,7 @@ def domain_setting(domain, key, value=None, delete=False):
|
|||
if not domain in domains.keys():
|
||||
# TODO add locales
|
||||
raise YunohostError("domain_name_unknown", domain=domain)
|
||||
|
||||
|
||||
domain_settings = domains[domain]
|
||||
|
||||
# GET
|
||||
|
@ -771,7 +805,7 @@ def domain_setting(domain, key, value=None, delete=False):
|
|||
|
||||
# SET
|
||||
else:
|
||||
|
||||
|
||||
if "ttl" == key:
|
||||
try:
|
||||
ttl = int(value)
|
||||
|
@ -785,6 +819,7 @@ def domain_setting(domain, key, value=None, delete=False):
|
|||
domain_settings[key] = value
|
||||
_set_domain_settings(domain, domain_settings)
|
||||
|
||||
|
||||
def _get_domain_settings(domain, subdomains):
|
||||
"""
|
||||
Get settings of a domain
|
||||
|
@ -826,12 +861,13 @@ def _set_domain_settings(domain, domain_settings):
|
|||
domains[domain] = domain_settings
|
||||
|
||||
# Save the settings to the .yaml file
|
||||
with open(DOMAIN_SETTINGS_PATH, 'w') as file:
|
||||
with open(DOMAIN_SETTINGS_PATH, "w") as file:
|
||||
yaml.dump(domains, file, default_flow_style=False)
|
||||
|
||||
|
||||
# def domain_get_registrar():
|
||||
def domain_registrar_set(domain, registrar, args):
|
||||
|
||||
|
||||
domains = _load_domain_settings()
|
||||
if not domain in domains.keys():
|
||||
raise YunohostError("domain_name_unknown", domain=domain)
|
||||
|
@ -840,39 +876,33 @@ def domain_registrar_set(domain, registrar, args):
|
|||
if not registrar in registrars.keys():
|
||||
# FIXME créer l'erreur
|
||||
raise YunohostError("registrar_unknown")
|
||||
|
||||
|
||||
parameters = registrars[registrar]
|
||||
ask_args = []
|
||||
for parameter in parameters:
|
||||
ask_args.append({
|
||||
'name' : parameter,
|
||||
'type': 'string',
|
||||
'example': '',
|
||||
'default': '',
|
||||
})
|
||||
ask_args.append(
|
||||
{
|
||||
"name": parameter,
|
||||
"type": "string",
|
||||
"example": "",
|
||||
"default": "",
|
||||
}
|
||||
)
|
||||
args_dict = (
|
||||
{} if not args else dict(urllib.parse.parse_qsl(args, keep_blank_values=True))
|
||||
)
|
||||
parsed_answer_dict = _parse_args_in_yunohost_format(args_dict, ask_args)
|
||||
|
||||
domain_provider = {
|
||||
'name': registrar,
|
||||
'options': {
|
||||
|
||||
}
|
||||
}
|
||||
domain_provider = {"name": registrar, "options": {}}
|
||||
for arg_name, arg_value_and_type in parsed_answer_dict.items():
|
||||
domain_provider['options'][arg_name] = arg_value_and_type[0]
|
||||
|
||||
domain_provider["options"][arg_name] = arg_value_and_type[0]
|
||||
|
||||
domain_settings = domains[domain]
|
||||
domain_settings["provider"] = domain_provider
|
||||
|
||||
# Save the settings to the .yaml file
|
||||
with open(DOMAIN_SETTINGS_PATH, 'w') as file:
|
||||
with open(DOMAIN_SETTINGS_PATH, "w") as file:
|
||||
yaml.dump(domains, file, default_flow_style=False)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def domain_push_config(domain):
|
||||
|
@ -893,7 +923,6 @@ def domain_push_config(domain):
|
|||
# FIXME add locales
|
||||
raise YunohostValidationError("registrar_is_not_set", domain=domain)
|
||||
|
||||
|
||||
# Flatten the DNS conf
|
||||
flatten_dns_conf = []
|
||||
for key in dns_conf:
|
||||
|
@ -911,7 +940,7 @@ def domain_push_config(domain):
|
|||
# Construct the base data structure to use lexicon's API.
|
||||
base_config = {
|
||||
"provider_name": provider["name"],
|
||||
"domain": domain, # domain name
|
||||
"domain": domain, # domain name
|
||||
}
|
||||
base_config[provider["name"]] = provider["options"]
|
||||
|
||||
|
@ -929,16 +958,20 @@ def domain_push_config(domain):
|
|||
"action": "list",
|
||||
"type": key,
|
||||
}
|
||||
final_lexicon = ConfigResolver().with_dict(dict_object=base_config).with_dict(dict_object=record_config)
|
||||
final_lexicon = (
|
||||
ConfigResolver()
|
||||
.with_dict(dict_object=base_config)
|
||||
.with_dict(dict_object=record_config)
|
||||
)
|
||||
# print('final_lexicon:', final_lexicon);
|
||||
client = Client(final_lexicon)
|
||||
distant_records[key] = client.execute()
|
||||
|
||||
for key in types:
|
||||
for distant_record in distant_records[key]:
|
||||
logger.debug(f"distant_record: {distant_record}");
|
||||
logger.debug(f"distant_record: {distant_record}")
|
||||
for local_record in flatten_dns_conf:
|
||||
print('local_record:', local_record);
|
||||
print("local_record:", local_record)
|
||||
|
||||
# Push the records
|
||||
for record in flatten_dns_conf:
|
||||
|
@ -948,7 +981,10 @@ def domain_push_config(domain):
|
|||
# is_the_same_record = False
|
||||
|
||||
for distant_record in distant_records[record["type"]]:
|
||||
if distant_record["type"] == record["type"] and distant_record["name"] == record["name"]:
|
||||
if (
|
||||
distant_record["type"] == record["type"]
|
||||
and distant_record["name"] == record["name"]
|
||||
):
|
||||
it_exists = True
|
||||
# see previous TODO
|
||||
# if distant_record["ttl"] = ... and distant_record["name"] ...
|
||||
|
@ -956,8 +992,12 @@ def domain_push_config(domain):
|
|||
|
||||
# Finally, push the new record or update the existing one
|
||||
record_config = {
|
||||
"action": "update" if it_exists else "create", # create, list, update, delete
|
||||
"type": record["type"], # specify a type for record filtering, case sensitive in some cases.
|
||||
"action": "update"
|
||||
if it_exists
|
||||
else "create", # create, list, update, delete
|
||||
"type": record[
|
||||
"type"
|
||||
], # specify a type for record filtering, case sensitive in some cases.
|
||||
"name": record["name"],
|
||||
"content": record["value"],
|
||||
# FIXME Removed TTL, because it doesn't work with Gandi.
|
||||
|
@ -965,11 +1005,16 @@ def domain_push_config(domain):
|
|||
# But I think there is another issue with Gandi. Or I'm misusing the API...
|
||||
# "ttl": record["ttl"],
|
||||
}
|
||||
final_lexicon = ConfigResolver().with_dict(dict_object=base_config).with_dict(dict_object=record_config)
|
||||
final_lexicon = (
|
||||
ConfigResolver()
|
||||
.with_dict(dict_object=base_config)
|
||||
.with_dict(dict_object=record_config)
|
||||
)
|
||||
client = Client(final_lexicon)
|
||||
print('pushed_record:', record_config, "→", end=' ')
|
||||
print("pushed_record:", record_config, "→", end=" ")
|
||||
results = client.execute()
|
||||
print('results:', results);
|
||||
print("results:", results)
|
||||
# print("Failed" if results == False else "Ok")
|
||||
|
||||
|
||||
# def domain_config_fetch(domain, key, value):
|
||||
|
|
Loading…
Add table
Reference in a new issue