sailesh1993 / rpms / cloud-init

Forked from rpms/cloud-init 10 months ago
Clone
088c30
From 01489fb91f64f6137ddf88c39feabe4296f3a156 Mon Sep 17 00:00:00 2001
088c30
From: Anh Vo <anhvo@microsoft.com>
088c30
Date: Fri, 23 Apr 2021 10:18:05 -0400
088c30
Subject: [PATCH 4/7] Azure: eject the provisioning iso before reporting ready
088c30
 (#861)
088c30
088c30
RH-Author: Eduardo Otubo <otubo@redhat.com>
088c30
RH-MergeRequest: 45: Add support for userdata on Azure from IMDS
088c30
RH-Commit: [4/7] ba830546a62ac5bea33b91d133d364a897b9f6c0
088c30
RH-Bugzilla: 2023940
088c30
RH-Acked-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
088c30
RH-Acked-by: Mohamed Gamal Morsy <mmorsy@redhat.com>
088c30
088c30
Due to hyper-v implementations, iso ejection is more efficient if performed
088c30
from within the guest. The code will attempt to perform a best-effort ejection.
088c30
Failure during ejection will not prevent reporting ready from happening. If iso
088c30
ejection is successful, later iso ejection from the platform will be a no-op.
088c30
In the event the iso ejection from the guest fails, iso ejection will still happen at
088c30
the platform level.
088c30
---
088c30
 cloudinit/sources/DataSourceAzure.py          | 22 +++++++++++++++---
088c30
 cloudinit/sources/helpers/azure.py            | 23 ++++++++++++++++---
088c30
 .../test_datasource/test_azure_helper.py      | 13 +++++++++--
088c30
 3 files changed, 50 insertions(+), 8 deletions(-)
088c30
088c30
diff --git a/cloudinit/sources/DataSourceAzure.py b/cloudinit/sources/DataSourceAzure.py
088c30
index 020b7006..39e67c4f 100755
088c30
--- a/cloudinit/sources/DataSourceAzure.py
088c30
+++ b/cloudinit/sources/DataSourceAzure.py
088c30
@@ -332,6 +332,7 @@ class DataSourceAzure(sources.DataSource):
088c30
     dsname = 'Azure'
088c30
     _negotiated = False
088c30
     _metadata_imds = sources.UNSET
088c30
+    _ci_pkl_version = 1
088c30
 
088c30
     def __init__(self, sys_cfg, distro, paths):
088c30
         sources.DataSource.__init__(self, sys_cfg, distro, paths)
088c30
@@ -346,8 +347,13 @@ class DataSourceAzure(sources.DataSource):
088c30
         # Regenerate network config new_instance boot and every boot
088c30
         self.update_events['network'].add(EventType.BOOT)
088c30
         self._ephemeral_dhcp_ctx = None
088c30
-
088c30
         self.failed_desired_api_version = False
088c30
+        self.iso_dev = None
088c30
+
088c30
+    def _unpickle(self, ci_pkl_version: int) -> None:
088c30
+        super()._unpickle(ci_pkl_version)
088c30
+        if "iso_dev" not in self.__dict__:
088c30
+            self.iso_dev = None
088c30
 
088c30
     def __str__(self):
088c30
         root = sources.DataSource.__str__(self)
088c30
@@ -459,6 +465,13 @@ class DataSourceAzure(sources.DataSource):
088c30
                     '%s was not mountable' % cdev, logger_func=LOG.warning)
088c30
                 continue
088c30
 
088c30
+            report_diagnostic_event("Found provisioning metadata in %s" % cdev,
088c30
+                                    logger_func=LOG.debug)
088c30
+
088c30
+            # save the iso device for ejection before reporting ready
088c30
+            if cdev.startswith("/dev"):
088c30
+                self.iso_dev = cdev
088c30
+
088c30
             perform_reprovision = reprovision or self._should_reprovision(ret)
088c30
             perform_reprovision_after_nic_attach = (
088c30
                 reprovision_after_nic_attach or
088c30
@@ -1226,7 +1239,9 @@ class DataSourceAzure(sources.DataSource):
088c30
         @return: The success status of sending the ready signal.
088c30
         """
088c30
         try:
088c30
-            get_metadata_from_fabric(None, lease['unknown-245'])
088c30
+            get_metadata_from_fabric(fallback_lease_file=None,
088c30
+                                     dhcp_opts=lease['unknown-245'],
088c30
+                                     iso_dev=self.iso_dev)
088c30
             return True
088c30
         except Exception as e:
088c30
             report_diagnostic_event(
088c30
@@ -1332,7 +1347,8 @@ class DataSourceAzure(sources.DataSource):
088c30
         metadata_func = partial(get_metadata_from_fabric,
088c30
                                 fallback_lease_file=self.
088c30
                                 dhclient_lease_file,
088c30
-                                pubkey_info=pubkey_info)
088c30
+                                pubkey_info=pubkey_info,
088c30
+                                iso_dev=self.iso_dev)
088c30
 
088c30
         LOG.debug("negotiating with fabric via agent command %s",
088c30
                   self.ds_cfg['agent_command'])
088c30
diff --git a/cloudinit/sources/helpers/azure.py b/cloudinit/sources/helpers/azure.py
088c30
index 03e7156b..ad476076 100755
088c30
--- a/cloudinit/sources/helpers/azure.py
088c30
+++ b/cloudinit/sources/helpers/azure.py
088c30
@@ -865,7 +865,19 @@ class WALinuxAgentShim:
088c30
         return endpoint_ip_address
088c30
 
088c30
     @azure_ds_telemetry_reporter
088c30
-    def register_with_azure_and_fetch_data(self, pubkey_info=None) -> dict:
088c30
+    def eject_iso(self, iso_dev) -> None:
088c30
+        try:
088c30
+            LOG.debug("Ejecting the provisioning iso")
088c30
+            subp.subp(['eject', iso_dev])
088c30
+        except Exception as e:
088c30
+            report_diagnostic_event(
088c30
+                "Failed ejecting the provisioning iso: %s" % e,
088c30
+                logger_func=LOG.debug)
088c30
+
088c30
+    @azure_ds_telemetry_reporter
088c30
+    def register_with_azure_and_fetch_data(self,
088c30
+                                           pubkey_info=None,
088c30
+                                           iso_dev=None) -> dict:
088c30
         """Gets the VM's GoalState from Azure, uses the GoalState information
088c30
         to report ready/send the ready signal/provisioning complete signal to
088c30
         Azure, and then uses pubkey_info to filter and obtain the user's
088c30
@@ -891,6 +903,10 @@ class WALinuxAgentShim:
088c30
             ssh_keys = self._get_user_pubkeys(goal_state, pubkey_info)
088c30
         health_reporter = GoalStateHealthReporter(
088c30
             goal_state, self.azure_endpoint_client, self.endpoint)
088c30
+
088c30
+        if iso_dev is not None:
088c30
+            self.eject_iso(iso_dev)
088c30
+
088c30
         health_reporter.send_ready_signal()
088c30
         return {'public-keys': ssh_keys}
088c30
 
088c30
@@ -1046,11 +1062,12 @@ class WALinuxAgentShim:
088c30
 
088c30
 @azure_ds_telemetry_reporter
088c30
 def get_metadata_from_fabric(fallback_lease_file=None, dhcp_opts=None,
088c30
-                             pubkey_info=None):
088c30
+                             pubkey_info=None, iso_dev=None):
088c30
     shim = WALinuxAgentShim(fallback_lease_file=fallback_lease_file,
088c30
                             dhcp_options=dhcp_opts)
088c30
     try:
088c30
-        return shim.register_with_azure_and_fetch_data(pubkey_info=pubkey_info)
088c30
+        return shim.register_with_azure_and_fetch_data(
088c30
+            pubkey_info=pubkey_info, iso_dev=iso_dev)
088c30
     finally:
088c30
         shim.clean_up()
088c30
 
088c30
diff --git a/tests/unittests/test_datasource/test_azure_helper.py b/tests/unittests/test_datasource/test_azure_helper.py
088c30
index 63482c6c..552c7905 100644
088c30
--- a/tests/unittests/test_datasource/test_azure_helper.py
088c30
+++ b/tests/unittests/test_datasource/test_azure_helper.py
088c30
@@ -1009,6 +1009,14 @@ class TestWALinuxAgentShim(CiTestCase):
088c30
         self.GoalState.return_value.container_id = self.test_container_id
088c30
         self.GoalState.return_value.instance_id = self.test_instance_id
088c30
 
088c30
+    def test_eject_iso_is_called(self):
088c30
+        shim = wa_shim()
088c30
+        with mock.patch.object(
088c30
+            shim, 'eject_iso', autospec=True
088c30
+        ) as m_eject_iso:
088c30
+            shim.register_with_azure_and_fetch_data(iso_dev="/dev/sr0")
088c30
+            m_eject_iso.assert_called_once_with("/dev/sr0")
088c30
+
088c30
     def test_http_client_does_not_use_certificate_for_report_ready(self):
088c30
         shim = wa_shim()
088c30
         shim.register_with_azure_and_fetch_data()
088c30
@@ -1283,13 +1291,14 @@ class TestGetMetadataGoalStateXMLAndReportReadyToFabric(CiTestCase):
088c30
 
088c30
     def test_calls_shim_register_with_azure_and_fetch_data(self):
088c30
         m_pubkey_info = mock.MagicMock()
088c30
-        azure_helper.get_metadata_from_fabric(pubkey_info=m_pubkey_info)
088c30
+        azure_helper.get_metadata_from_fabric(
088c30
+            pubkey_info=m_pubkey_info, iso_dev="/dev/sr0")
088c30
         self.assertEqual(
088c30
             1,
088c30
             self.m_shim.return_value
088c30
                 .register_with_azure_and_fetch_data.call_count)
088c30
         self.assertEqual(
088c30
-            mock.call(pubkey_info=m_pubkey_info),
088c30
+            mock.call(iso_dev="/dev/sr0", pubkey_info=m_pubkey_info),
088c30
             self.m_shim.return_value
088c30
                 .register_with_azure_and_fetch_data.call_args)
088c30
 
088c30
-- 
088c30
2.27.0
088c30