From 6042e71003710a7d8bac874cd21bab9bcc5fd869 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Mon, 12 Jun 2017 16:58:22 +0200 Subject: [PATCH] [fix] Don't attempt to change hostname in LXC (#301) * Don't attempt to change hostname in LXC * Detect any kind of container, not just lxc --- src/yunohost/tools.py | 59 +++++++++++++++++++++++++++++++++---------- 1 file changed, 45 insertions(+), 14 deletions(-) diff --git a/src/yunohost/tools.py b/src/yunohost/tools.py index b0e647238..d2c7d5fe6 100644 --- a/src/yunohost/tools.py +++ b/src/yunohost/tools.py @@ -164,14 +164,40 @@ def tools_maindomain(auth, new_domain=None): logger.warning("%s" % e, exc_info=1) raise MoulinetteError(errno.EPERM, m18n.n('maindomain_change_failed')) - # Clear nsswitch cache for hosts to make sure hostname is resolved ... + _set_hostname(new_domain) + + # Generate SSOwat configuration file + app_ssowatconf(auth) + + # Regen configurations + try: + with open('/etc/yunohost/installed', 'r') as f: + service_regen_conf() + except IOError: + pass + + logger.success(m18n.n('maindomain_changed')) + + +def _set_hostname(hostname, pretty_hostname=None): + """ + Change the machine hostname using hostnamectl + """ + + if _is_inside_container(): + logger.warning("You are inside a container and hostname cannot easily be changed") + return + + if not pretty_hostname: + pretty_hostname = "(YunoHost/%s)" % hostname + + # First clear nsswitch cache for hosts to make sure hostname is resolved... subprocess.call(['nscd', '-i', 'hosts']) - # Set hostname - pretty_hostname = "(YunoHost/%s)" % new_domain + # Then call hostnamectl commands = [ - "sudo hostnamectl --static set-hostname".split() + [new_domain], - "sudo hostnamectl --transient set-hostname".split() + [new_domain], + "sudo hostnamectl --static set-hostname".split() + [hostname], + "sudo hostnamectl --transient set-hostname".split() + [hostname], "sudo hostnamectl --pretty set-hostname".split() + [pretty_hostname] ] @@ -189,17 +215,22 @@ def tools_maindomain(auth, new_domain=None): else: logger.info(out) - # Generate SSOwat configuration file - app_ssowatconf(auth) - # Regen configurations - try: - with open('/etc/yunohost/installed', 'r') as f: - service_regen_conf() - except IOError: - pass +def _is_inside_container(): + """ + Check if we're inside a container (i.e. LXC) - logger.success(m18n.n('maindomain_changed')) + Returns True or False + """ + + # See https://stackoverflow.com/a/37016302 + p = subprocess.Popen("sudo cat /proc/1/sched".split(), + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT) + + out, _ = p.communicate() + + return out.split()[1] != "(1," def tools_postinstall(domain, password, ignore_dyndns=False):