From af97e7d09644cd14b7cd0c2aab42d329dcbd9ae1 Mon Sep 17 00:00:00 2001
From: Daniil Baturin <daniil@baturin.org>
Date: Thu, 17 May 2018 06:50:40 +0200
Subject: T574: fix setting the hostname on boot.

---
 src/conf_mode/host_name.py | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

(limited to 'src/conf_mode')

diff --git a/src/conf_mode/host_name.py b/src/conf_mode/host_name.py
index f6d091ac2..11612637c 100755
--- a/src/conf_mode/host_name.py
+++ b/src/conf_mode/host_name.py
@@ -81,8 +81,14 @@ def generate(config):
     old_hostname = subprocess.check_output(['hostname']).decode().strip()
 
     # replace the local host line
-    hosts = re.sub(r"({}\s+{}.*)".format(local_addr, old_hostname),
-                   r"{}\t{} # VyOS entry\n".format(local_addr, config["fqdn"]), hosts)
+    vyos_host_line_re = re.compile(r"({}\s+{}.*)".format(local_addr, old_hostname))
+    vyos_host_line = "{}\t{} # VyOS entry\n".format(local_addr, config["fqdn"])
+    if re.search(vyos_host_line_re, hosts):
+        hosts = re.sub(vyos_host_line_re, vyos_host_line, hosts)
+    else:
+        # On boot (or after errors), the /etc/hosts file has no line for vyos hostname,
+        # so we have to add it
+        hosts = "{0}\n{1}".format(hosts, vyos_host_line)
 
     with open(hosts_file, 'w') as f:
         f.write(hosts)
-- 
cgit v1.2.3