def update()

in src/extension/src/ActionHandler.py [0:0]


    def update(self):
        """ as per the extension user guide, upon update request, Azure agent calls
         1. disable on the prev version
         2. update on the new version
         3. uninstall on the prev version
         4. install (if updateMode is UpdateWithInstall)
         5. enable on the new version
         on uninstall the agent deletes removes configuration files"""

        # config folder path is usually something like: /var/lib/waagent/Microsoft.CPlat.Core.LinuxPatchExtension-<version>/config
        try:
            self.setup(action=Constants.UPDATE, log_message="Extension is being updated to the latest version. Copying the required extension artifacts from preceding version to the current one")

            # fetch all earlier extension versions available on the machine
            new_version_config_folder = self.ext_env_handler.config_folder
            extension_pardir = os.path.abspath(os.path.join(new_version_config_folder, os.path.pardir, os.path.pardir))
            self.logger.log("Parent directory for all extension version artifacts [Directory={0}]".format(str(extension_pardir)))
            paths_to_all_versions = self.filter_files_from_versions(self.get_all_versions(extension_pardir))
            self.logger.log("List of all extension versions found on the machine. [All Versions={0}]".format(paths_to_all_versions))
            if len(paths_to_all_versions) <= 1:
                # Extension Update action called when
                # a) artifacts for the preceding version do not exist on the machine, or
                # b) after all artifacts from the preceding versions have been deleted
                error_msg = "No earlier versions for the extension found on the machine. So, could not copy any references to the current version."
                self.logger.log_error(error_msg)
                self.ext_output_status_handler.write_status_file("", self.seq_no, status=Constants.Status.Error.lower(), message=error_msg, code=Constants.ExitCode.HandlerFailed)
                return Constants.ExitCode.HandlerFailed

            # identify the version preceding current
            self.logger.log("Fetching the extension version preceding current from all available versions...")

            # use custom sort logic to sort path based on version numbers
            sorted_versions = self.ext_version_comparator.sort_ext_paths_desc_order(paths_to_all_versions)
            self.logger.log_debug("List of extension versions in descending order: [SortedVersion={0}]".format(sorted_versions))

            preceding_version_path = sorted_versions[1]

            if preceding_version_path is None or preceding_version_path == "" or not os.path.exists(preceding_version_path):
                error_msg = "Could not find path where preceding extension version artifacts are stored. Hence, cannot copy the required artifacts to the latest version. "\
                            "[Preceding extension version path={0}]".format(str(preceding_version_path))
                self.logger.log_error(error_msg)
                self.ext_output_status_handler.write_status_file("", self.seq_no, status=Constants.Status.Error.lower(), message=error_msg, code=Constants.ExitCode.HandlerFailed)
                return Constants.ExitCode.HandlerFailed

            self.logger.log("Preceding version path. [Path={0}]".format(str(preceding_version_path)))

            # copy all required files from preceding version to current
            self.copy_config_files(preceding_version_path, new_version_config_folder)

            # Delete temp_folder
            self.ext_env_handler.delete_temp_folder()

            self.logger.log("All update actions from extension handler completed.")
            self.ext_output_status_handler.write_status_file("", self.seq_no, status=Constants.Status.Success.lower())
            return Constants.ExitCode.Okay

        except Exception as error:
            self.logger.log_error("Error occurred during extension update. [Error={0}]".format(repr(error)))
            self.ext_output_status_handler.write_status_file("", self.seq_no, status=Constants.Status.Error.lower(), message="Error occurred during extension update", code=Constants.ExitCode.HandlerFailed)
            return Constants.ExitCode.HandlerFailed

        finally:
            self.tear_down()