Repository: Azure/WALinuxAgent
Pull request #778 (2017-06-21 23:12:34)

[#777] -- Deprovisioning when VM identifier changes is too aggressive
Signed-off-by: Brendan Dixon <brendandixon@me.com>


Patch:
diff --git a/azurelinuxagent/pa/deprovision/default.py b/azurelinuxagent/pa/deprovision/default.py
index bdd108f4..e2c56132 100644
--- a/azurelinuxagent/pa/deprovision/default.py
+++ b/azurelinuxagent/pa/deprovision/default.py
@@ -155,23 +155,28 @@ class DeprovisionHandler(object):
             ]
         return dirs
     
-    def cloud_init_files(self, include_once=True):
-        files = [
-            "/etc/sudoers.d/90-cloud-init-users"
-        ]
+    def cloud_init_files(self, include_once=True, deluser=False):
+        files = []
+        if deluser:
+            files += [
+                "/etc/sudoers.d/90-cloud-init-users"
+            ]
         if include_once:
             files += [
                 "/var/lib/cloud/sem/config_scripts_per_once.once"
             ]
         return files
 
-    def del_cloud_init(self, warnings, actions, include_once=True):
+    def del_cloud_init(self, warnings, actions,
+            include_once=True, deluser=False):
         dirs = [d for d in self.cloud_init_dirs(include_once=include_once) \
                     if os.path.isdir(d)]
         if len(dirs) > 0:
             actions.append(DeprovisionAction(fileutil.rm_dirs, dirs))
 
-        files = [f for f in self.cloud_init_files(include_once=include_once) \
+        files = [f for f in self.cloud_init_files(
+                                    include_once=include_once,
+                                    deluser=deluser) \
                     if os.path.isfile(f)]
         if len(files) > 0:
             actions.append(DeprovisionAction(fileutil.rm_files, files))
@@ -197,7 +202,7 @@ class DeprovisionHandler(object):
         if conf.get_delete_root_password():
             self.del_root_password(warnings, actions)
 
-        self.del_cloud_init(warnings, actions)
+        self.del_cloud_init(warnings, actions, deluser=deluser)
         self.del_dirs(warnings, actions)
         self.del_files(warnings, actions)
         self.del_resolv(warnings, actions)
@@ -211,7 +216,8 @@ class DeprovisionHandler(object):
         warnings = []
         actions = []
 
-        self.del_cloud_init(warnings, actions, include_once=False)
+        self.del_cloud_init(warnings, actions,
+                    include_once=False, deluser=False)
         self.del_dhcp_lease(warnings, actions)
         self.del_lib_dir_files(warnings, actions)
 


Test patch:
diff --git a/tests/pa/test_deprovision.py b/tests/pa/test_deprovision.py
index 24456786..b2f7f0c0 100644
--- a/tests/pa/test_deprovision.py
+++ b/tests/pa/test_deprovision.py
@@ -59,10 +59,11 @@ class TestDeprovision(AgentTestCase):
                 mock_files,
                 mock_dirs):
         deprovision_handler = get_deprovision_handler("","","")
-        deprovision_handler.del_cloud_init([], [], include_once=False)
+        deprovision_handler.del_cloud_init([], [],
+                            include_once=False, deluser=False)
 
         mock_dirs.assert_called_with(include_once=False)
-        mock_files.assert_called_with(include_once=False)
+        mock_files.assert_called_with(include_once=False, deluser=False)
 
     @patch("signal.signal")
     @patch("azurelinuxagent.common.protocol.get_protocol_util")
@@ -87,10 +88,11 @@ class TestDeprovision(AgentTestCase):
                 mock_files.return_value = files
 
                 deprovision_handler = get_deprovision_handler("","","")
-                deprovision_handler.del_cloud_init(warnings, actions)
+                deprovision_handler.del_cloud_init(warnings, actions,
+                        deluser=True)
 
                 mock_dirs.assert_called_with(include_once=True)
-                mock_files.assert_called_with(include_once=True)
+                mock_files.assert_called_with(include_once=True, deluser=True)
 
                 self.assertEqual(len(warnings), 0)
                 self.assertEqual(len(actions), 2)