Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions buildpack/telemetry/metering.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,31 @@ def _is_usage_metering_enabled():
return True


def _is_sap_metering_configured():
use_license_server = os.environ.get("MXRUNTIME_License.UseLicenseServer", "false").lower()
if use_license_server == "true":
return False

endpoint = _get_sap_metering_endpoint()
token = _get_sap_metering_token()

if not endpoint or not token:
logging.warning(
"Missing configuration for SAP metering sidecar."
)
return False

return True


def _get_sap_metering_endpoint():
return os.environ.get("MXRUNTIME_License.MeteringEndpoint", "").strip() or None


def _get_sap_metering_token():
return os.environ.get("MXRUNTIME_License.MeteringToken", "").strip() or None


def _get_project_id(file_path):
try:
with open(file_path) as file_handle:
Expand Down Expand Up @@ -89,9 +114,38 @@ def _is_sidecar_installed():
return False


def _copy_sap_metering_sidecar(build_path, endpoint, token):
"""Download SAP metering sidecar binary from HTTPS endpoint."""
import requests

sidecar_dir = os.path.join(build_path, NAMESPACE)
destination = os.path.join(sidecar_dir, BINARY)
util.mkdir_p(sidecar_dir)

# Download binary file via HTTPS with auth-token header
response = requests.get(
endpoint,
headers={"auth-token": token},
stream=True,
timeout=60,
)
response.raise_for_status()

# Stream binary content to disk
with open(destination, "wb") as file_handle:
for chunk in response.iter_content(chunk_size=8192):
if chunk:
file_handle.write(chunk)

logging.info("SAP metering sidecar downloaded successfully")
util.set_executable(destination)
return destination


def stage(buildpack_path, build_path, cache_dir):
try:
if _is_usage_metering_enabled():
# Original Mendix metering flow
logging.info("Usage metering is enabled")
_download(buildpack_path, build_path, cache_dir)

Expand All @@ -105,6 +159,19 @@ def stage(buildpack_path, build_path, cache_dir):
os.path.join(build_path, NAMESPACE, SIDECAR_CONFIG_FILE),
config,
)
elif _is_sap_metering_configured():
# UseLicenseServer = false with valid SAP endpoint and token
endpoint = _get_sap_metering_endpoint()
token = _get_sap_metering_token()
try:
_copy_sap_metering_sidecar(build_path, endpoint, token)
logging.info("SAP metering sidecar staged successfully")
except Exception:
logging.error(
"Encountered an exception while staging the SAP metering sidecar. "
"Continuing buildpack execution."
)
logging.debug("SAP metering sidecar staging exception details:", exc_info=True)
else:
logging.info("Usage metering is NOT enabled")
except Exception:
Expand Down