Skip to content

Commit

Permalink
Refactor download logic (#2651)
Browse files Browse the repository at this point in the history
* Refactor download logic

* Cleanup

Co-authored-by: narrieta <narrieta>
  • Loading branch information
narrieta authored Aug 17, 2022
1 parent e2b61fe commit 3aebcdd
Show file tree
Hide file tree
Showing 44 changed files with 511 additions and 616 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@

from collections import defaultdict

import azurelinuxagent.common.logger as logger
from azurelinuxagent.common import logger
from azurelinuxagent.common.event import add_event, WALAEventOperation
from azurelinuxagent.common.exception import ExtensionsConfigError
from azurelinuxagent.common.future import ustr
from azurelinuxagent.common.protocol.extensions_goal_state import ExtensionsGoalState, GoalStateChannel, GoalStateSource
from azurelinuxagent.common.protocol.restapi import ExtensionSettings, Extension, VMAgentManifest, ExtensionState, InVMGoalStateMetaData
from azurelinuxagent.common.utils import restutil
from azurelinuxagent.common.utils.textutil import parse_doc, parse_json, findall, find, findtext, getattrib, gettext, format_exception, \
is_str_none_or_whitespace, is_str_empty

Expand Down Expand Up @@ -81,51 +80,42 @@ def _parse_extensions_config(self, xml_text, wire_client):
self._status_upload_blob_type = getattrib(status_upload_node, "statusBlobType")
logger.verbose("Extension config shows status blob type as [{0}]", self._status_upload_blob_type)

self._on_hold = self._fetch_extensions_on_hold(xml_doc, wire_client)
self._on_hold = ExtensionsGoalStateFromExtensionsConfig._fetch_extensions_on_hold(xml_doc, wire_client)

in_vm_gs_metadata = InVMGoalStateMetaData(find(xml_doc, "InVMGoalStateMetaData"))
self._activity_id = self._string_to_id(in_vm_gs_metadata.activity_id)
self._correlation_id = self._string_to_id(in_vm_gs_metadata.correlation_id)
self._created_on_timestamp = self._ticks_to_utc_timestamp(in_vm_gs_metadata.created_on_ticks)

def _fetch_extensions_on_hold(self, xml_doc, wire_client):
@staticmethod
def _fetch_extensions_on_hold(xml_doc, wire_client):
def log_info(message):
logger.info(message)
add_event(op=WALAEventOperation.ArtifactsProfileBlob, message=message, is_success=True, log_event=False)

def log_warning(message):
logger.warn(message)
add_event(op=WALAEventOperation.ArtifactsProfileBlob, message=message, is_success=False, log_event=False)

artifacts_profile_blob = findtext(xml_doc, "InVMArtifactsProfileBlob")
if is_str_none_or_whitespace(artifacts_profile_blob):
log_info("ExtensionsConfig does not include a InVMArtifactsProfileBlob; will assume the VM is not on hold")
return False

def fetch_direct():
content, _ = wire_client.fetch(artifacts_profile_blob)
return content

def fetch_through_host():
host = wire_client.get_host_plugin()
uri, headers = host.get_artifact_request(artifacts_profile_blob)
content, _ = wire_client.fetch(uri, headers, use_proxy=False, retry_codes=restutil.HGAP_GET_EXTENSION_ARTIFACT_RETRY_CODES)
return content

logger.verbose("Retrieving the artifacts profile")

try:
profile = wire_client.send_request_using_appropriate_channel(fetch_direct, fetch_through_host)
if profile is None:
logger.warn("Failed to fetch artifacts profile from blob {0}", artifacts_profile_blob)
return False
profile = wire_client.fetch_artifacts_profile_blob(artifacts_profile_blob)
except Exception as error:
logger.warn("Exception retrieving artifacts profile from blob {0}. Error: {1}".format(artifacts_profile_blob, ustr(error)))
log_warning("Can't download the artifacts profile blob; will assume the VM is not on hold. {0}".format(ustr(error)))
return False

if is_str_empty(profile):
log_info("The artifacts profile blob is empty; will assume the VM is not on hold.")
return False

logger.verbose("Artifacts profile downloaded")

try:
artifacts_profile = _InVMArtifactsProfile(profile)
except Exception:
logger.warn("Could not parse artifacts profile blob")
msg = "Content: [{0}]".format(profile)
logger.verbose(msg)
add_event(op=WALAEventOperation.ArtifactsProfileBlob, is_success=False, message=msg, log_event=False)
except Exception as exception:
log_warning("Can't parse the artifacts profile blob; will assume the VM is not on hold. Error: {0}".format(ustr(exception)))
return False

return artifacts_profile.get_on_hold()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def _parse_extensions(self, vm_settings):
# "settingsSeqNo": 0,
# "settings": [
# {
# "protectedSettingsCertThumbprint": "4037FBF5F1F3014F99B5D6C7799E9B20E6871CB3",
# "protectedSettingsCertThumbprint": "BD447EF71C3ADDF7C837E84D630F3FAC22CCD22F",
# "protectedSettings": "MIIBsAYJKoZIhvcNAQcDoIIBoTCCAZ0CAQAxggFpMIIBZQIBADBNMDkxNzA1BgoJkiaJk/IsZAEZFidXaW5kb3dzIEF6dXJlIENSUCBDZXJ0aWZpY2F0ZSBHZW5lcmF0b3ICEFpB/HKM/7evRk+DBz754wUwDQYJKoZIhvcNAQEBBQAEggEADPJwniDeIUXzxNrZCloitFdscQ59Bz1dj9DLBREAiM8jmxM0LLicTJDUv272Qm/4ZQgdqpFYBFjGab/9MX+Ih2x47FkVY1woBkckMaC/QOFv84gbboeQCmJYZC/rZJdh8rCMS+CEPq3uH1PVrvtSdZ9uxnaJ+E4exTPPviIiLIPtqWafNlzdbBt8HZjYaVw+SSe+CGzD2pAQeNttq3Rt/6NjCzrjG8ufKwvRoqnrInMs4x6nnN5/xvobKIBSv4/726usfk8Ug+9Q6Benvfpmre2+1M5PnGTfq78cO3o6mI3cPoBUjp5M0iJjAMGeMt81tyHkimZrEZm6pLa4NQMOEjArBgkqhkiG9w0BBwEwFAYIKoZIhvcNAwcECC5nVaiJaWt+gAhgeYvxUOYHXw==",
# "publicSettings": "{\"GCS_AUTO_CONFIG\":true}"
# }
Expand Down
Loading

0 comments on commit 3aebcdd

Please sign in to comment.