diff options
Diffstat (limited to 'cloudinit/sources/azure')
-rw-r--r-- | cloudinit/sources/azure/errors.py | 38 | ||||
-rw-r--r-- | cloudinit/sources/azure/kvp.py | 57 |
2 files changed, 79 insertions, 16 deletions
diff --git a/cloudinit/sources/azure/errors.py b/cloudinit/sources/azure/errors.py index 1a452635..5c4ad7db 100644 --- a/cloudinit/sources/azure/errors.py +++ b/cloudinit/sources/azure/errors.py @@ -8,7 +8,7 @@ import logging import traceback from datetime import datetime from io import StringIO -from typing import Any, Dict, Optional +from typing import Any, Dict, List, Optional from cloudinit import version from cloudinit.sources.azure import identity @@ -16,6 +16,22 @@ from cloudinit.sources.azure import identity LOG = logging.getLogger(__name__) +def encode_report( + data: List[str], delimiter: str = "|", quotechar: str = "'" +) -> str: + """Encode report data with csv.""" + with StringIO() as io: + csv.writer( + io, + delimiter=delimiter, + quotechar=quotechar, + quoting=csv.QUOTE_MINIMAL, + ).writerow(data) + + # strip trailing \r\n + return io.getvalue().rstrip() + + class ReportableError(Exception): def __init__( self, @@ -39,10 +55,11 @@ class ReportableError(Exception): except Exception as id_error: self.vm_id = f"failed to read vm id: {id_error!r}" - def as_description( - self, *, delimiter: str = "|", quotechar: str = "'" + def as_encoded_report( + self, ) -> str: data = [ + "result=error", f"reason={self.reason}", f"agent={self.agent}", ] @@ -53,18 +70,7 @@ class ReportableError(Exception): f"documentation_url={self.documentation_url}", ] - with StringIO() as io: - csv.writer( - io, - delimiter=delimiter, - quotechar=quotechar, - quoting=csv.QUOTE_MINIMAL, - ).writerow(data) - - # strip trailing \r\n - csv_data = io.getvalue().rstrip() - - return f"PROVISIONING_ERROR: {csv_data}" + return encode_report(data) def __eq__(self, other) -> bool: return ( @@ -75,7 +81,7 @@ class ReportableError(Exception): ) def __repr__(self) -> str: - return self.as_description() + return self.as_encoded_report() class ReportableErrorUnhandledException(ReportableError): diff --git a/cloudinit/sources/azure/kvp.py b/cloudinit/sources/azure/kvp.py new file mode 100644 index 00000000..735c4616 --- /dev/null +++ b/cloudinit/sources/azure/kvp.py @@ -0,0 +1,57 @@ +# Copyright (C) 2022 Microsoft Corporation. +# +# This file is part of cloud-init. See LICENSE file for license information. + +import logging +from datetime import datetime +from typing import Optional + +from cloudinit import version +from cloudinit.reporting import handlers, instantiated_handler_registry +from cloudinit.sources.azure import errors, identity + +LOG = logging.getLogger(__name__) + + +def get_kvp_handler() -> Optional[handlers.HyperVKvpReportingHandler]: + """Get instantiated KVP telemetry handler.""" + kvp_handler = instantiated_handler_registry.registered_items.get( + "telemetry" + ) + if not isinstance(kvp_handler, handlers.HyperVKvpReportingHandler): + return None + + return kvp_handler + + +def report_via_kvp(report: str) -> bool: + """Report to host via PROVISIONING_REPORT KVP key.""" + kvp_handler = get_kvp_handler() + if kvp_handler is None: + LOG.debug("KVP handler not enabled, skipping host report.") + return False + + kvp_handler.write_key("PROVISIONING_REPORT", report) + return True + + +def report_failure_to_host(error: errors.ReportableError) -> bool: + return report_via_kvp(error.as_encoded_report()) + + +def report_success_to_host() -> bool: + try: + vm_id = identity.query_vm_id() + except Exception as id_error: + vm_id = f"failed to read vm id: {id_error!r}" + + report = errors.encode_report( + [ + "result=success", + f"agent=Cloud-Init/{version.version_string()}", + f"timestamp={datetime.utcnow().isoformat()}", + f"vm_id={vm_id}", + ] + ) + + return report_via_kvp(report) |