Merge pull request #1125 from YunoHost/domain-remove-remove-apps

Add '--remove-apps' (and '--force') options to "yunohost domain remove"
This commit is contained in:
Alexandre Aubin 2021-02-13 20:43:59 +01:00 committed by GitHub
commit 0f7c0b039a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 15 deletions

View file

@ -451,6 +451,14 @@ domain:
help: Domain to delete help: Domain to delete
extra: extra:
pattern: *pattern_domain pattern: *pattern_domain
-r:
full: --remove-apps
help: Remove apps installed on the domain
action: store_true
-f:
full: --force
help: Do not ask confirmation to remove apps
action: store_true
### domain_dns_conf() ### domain_dns_conf()
dns-conf: dns-conf:

View file

@ -280,6 +280,7 @@
"domain_dyndns_root_unknown": "Unknown DynDNS root domain", "domain_dyndns_root_unknown": "Unknown DynDNS root domain",
"domain_exists": "The domain already exists", "domain_exists": "The domain already exists",
"domain_hostname_failed": "Unable to set new hostname. This might cause an issue later (it might be fine).", "domain_hostname_failed": "Unable to set new hostname. This might cause an issue later (it might be fine).",
"domain_remove_confirm_apps_removal": "Removing this domain will remove those applications:\n{apps}\n\nAre you sure you want to do that? [{answers}]",
"domain_uninstall_app_first": "Those applications are still installed on your domain:\n{apps}\n\nPlease uninstall them using 'yunohost app remove the_app_id' or move them to another domain using 'yunohost app change-url the_app_id' before proceeding to domain removal", "domain_uninstall_app_first": "Those applications are still installed on your domain:\n{apps}\n\nPlease uninstall them using 'yunohost app remove the_app_id' or move them to another domain using 'yunohost app change-url the_app_id' before proceeding to domain removal",
"domain_name_unknown": "Domain '{domain}' unknown", "domain_name_unknown": "Domain '{domain}' unknown",
"domain_unknown": "Unknown domain", "domain_unknown": "Unknown domain",

View file

@ -26,7 +26,7 @@
import os import os
import re import re
from moulinette import m18n, msettings from moulinette import m18n, msettings, msignals
from moulinette.core import MoulinetteError from moulinette.core import MoulinetteError
from yunohost.utils.error import YunohostError from yunohost.utils.error import YunohostError
from moulinette.utils.log import getActionLogger from moulinette.utils.log import getActionLogger
@ -167,7 +167,7 @@ def domain_add(operation_logger, domain, dyndns=False):
except Exception: except Exception:
# Force domain removal silently # Force domain removal silently
try: try:
domain_remove(domain, True) domain_remove(domain, force=True)
except Exception: except Exception:
pass pass
raise raise
@ -178,21 +178,26 @@ def domain_add(operation_logger, domain, dyndns=False):
@is_unit_operation() @is_unit_operation()
def domain_remove(operation_logger, domain, force=False): def domain_remove(operation_logger, domain, remove_apps=False, force=False):
""" """
Delete domains Delete domains
Keyword argument: Keyword argument:
domain -- Domain to delete domain -- Domain to delete
force -- Force the domain removal remove_apps -- Remove applications installed on the domain
force -- Force the domain removal and don't not ask confirmation to
remove apps if remove_apps is specified
""" """
from yunohost.hook import hook_callback from yunohost.hook import hook_callback
from yunohost.app import app_ssowatconf, app_info from yunohost.app import app_ssowatconf, app_info, app_remove
from yunohost.utils.ldap import _get_ldap_interface from yunohost.utils.ldap import _get_ldap_interface
if not force and domain not in domain_list()["domains"]: # the 'force' here is related to the exception happening in domain_add ...
raise YunohostError("domain_name_unknown", domain=domain) # we don't want to check the domain exists because the ldap add may have
# failed
if not force and domain not in domain_list()['domains']:
raise YunohostError('domain_name_unknown', domain=domain)
# Check domain is not the main domain # Check domain is not the main domain
if domain == _get_maindomain(): if domain == _get_maindomain():
@ -215,16 +220,21 @@ def domain_remove(operation_logger, domain, force=False):
settings = _get_app_settings(app) settings = _get_app_settings(app)
label = app_info(app)["name"] label = app_info(app)["name"]
if settings.get("domain") == domain: if settings.get("domain") == domain:
apps_on_that_domain.append( apps_on_that_domain.append((app, " - %s \"%s\" on https://%s%s" % (app, label, domain, settings["path"]) if "path" in settings else app))
' - %s "%s" on https://%s%s' % (app, label, domain, settings["path"])
if "path" in settings
else app
)
if apps_on_that_domain: if apps_on_that_domain:
raise YunohostError( if remove_apps:
"domain_uninstall_app_first", apps="\n".join(apps_on_that_domain) 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 YunohostError('domain_uninstall_app_first', apps="\n".join([x[1] for x in apps_on_that_domain]))
operation_logger.start() operation_logger.start()
ldap = _get_ldap_interface() ldap = _get_ldap_interface()