sailesh1993 / rpms / cloud-init

Forked from rpms/cloud-init a year ago
Clone
da04ad
From ddfd2eba79b4849309c37472dfb5852811b03391 Mon Sep 17 00:00:00 2001
da04ad
From: Emanuele Giuseppe Esposito <eesposit@redhat.com>
da04ad
Date: Thu, 19 Jan 2023 09:46:10 +0100
da04ad
Subject: [PATCH] cc_set_hostname: ignore /var/lib/cloud/data/set-hostname if
da04ad
 it's empty (#1967)
da04ad
da04ad
RH-Author: Emanuele Giuseppe Esposito <eesposit@redhat.com>
da04ad
RH-MergeRequest: 88: cc_set_hostname: ignore /var/lib/cloud/data/set-hostname if it's empty (#1967)
da04ad
RH-Bugzilla: 2162258
da04ad
RH-Acked-by: Mohamed Gamal Morsy <mmorsy@redhat.com>
da04ad
RH-Acked-by: Jon Maloy <jmaloy@redhat.com>
da04ad
RH-Commit: [1/1] 04aaaf46290c4488dd46c9c2673b0bf038b7d311
da04ad
da04ad
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2162258
da04ad
da04ad
commit 9c7502a801763520639c66125eb373123d1e4f44
da04ad
Author: Emanuele Giuseppe Esposito <eesposit@redhat.com>
da04ad
Date:   Wed Jan 18 17:55:16 2023 +0100
da04ad
da04ad
    cc_set_hostname: ignore /var/lib/cloud/data/set-hostname if it's empty (#1967)
da04ad
da04ad
    If the file exists but is empty, do nothing.
da04ad
    Otherwise cloud-init will crash because it does not handle the empty file.
da04ad
da04ad
    RHBZ: 2140893
da04ad
da04ad
    Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
da04ad
da04ad
Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
da04ad
---
da04ad
 cloudinit/config/cc_set_hostname.py            |  2 +-
da04ad
 tests/unittests/config/test_cc_set_hostname.py | 17 +++++++++++++++++
da04ad
 2 files changed, 18 insertions(+), 1 deletion(-)
da04ad
da04ad
diff --git a/cloudinit/config/cc_set_hostname.py b/cloudinit/config/cc_set_hostname.py
da04ad
index eb0ca328..9d78f6ad 100644
da04ad
--- a/cloudinit/config/cc_set_hostname.py
da04ad
+++ b/cloudinit/config/cc_set_hostname.py
da04ad
@@ -86,7 +86,7 @@ def handle(name, cfg, cloud, log, _args):
da04ad
     # distro._read_hostname implementation so we only validate  one artifact.
da04ad
     prev_fn = os.path.join(cloud.get_cpath("data"), "set-hostname")
da04ad
     prev_hostname = {}
da04ad
-    if os.path.exists(prev_fn):
da04ad
+    if os.path.exists(prev_fn) and os.stat(prev_fn).st_size > 0:
da04ad
         prev_hostname = util.load_json(util.load_file(prev_fn))
da04ad
     hostname_changed = hostname != prev_hostname.get(
da04ad
         "hostname"
da04ad
diff --git a/tests/unittests/config/test_cc_set_hostname.py b/tests/unittests/config/test_cc_set_hostname.py
da04ad
index fd994c4e..a819039b 100644
da04ad
--- a/tests/unittests/config/test_cc_set_hostname.py
da04ad
+++ b/tests/unittests/config/test_cc_set_hostname.py
da04ad
@@ -5,6 +5,7 @@ import os
da04ad
 import shutil
da04ad
 import tempfile
da04ad
 from io import BytesIO
da04ad
+from pathlib import Path
da04ad
 from unittest import mock
da04ad
 
da04ad
 from configobj import ConfigObj
da04ad
@@ -204,5 +205,21 @@ class TestHostname(t_help.FilesystemMockingTestCase):
da04ad
             str(ctx_mgr.exception),
da04ad
         )
da04ad
 
da04ad
+    def test_ignore_empty_previous_artifact_file(self):
da04ad
+        cfg = {
da04ad
+            "hostname": "blah",
da04ad
+            "fqdn": "blah.blah.blah.yahoo.com",
da04ad
+        }
da04ad
+        distro = self._fetch_distro("debian")
da04ad
+        paths = helpers.Paths({"cloud_dir": self.tmp})
da04ad
+        ds = None
da04ad
+        cc = cloud.Cloud(ds, paths, {}, distro, None)
da04ad
+        self.patchUtils(self.tmp)
da04ad
+        prev_fn = Path(cc.get_cpath("data")) / "set-hostname"
da04ad
+        prev_fn.touch()
da04ad
+        cc_set_hostname.handle("cc_set_hostname", cfg, cc, LOG, [])
da04ad
+        contents = util.load_file("/etc/hostname")
da04ad
+        self.assertEqual("blah", contents.strip())
da04ad
+
da04ad
 
da04ad
 # vi: ts=4 expandtab
da04ad
-- 
da04ad
2.39.1
da04ad