From 23f032f0fbd5275b318c88677617631cb005deed Mon Sep 17 00:00:00 2001 From: Tommaso Bailetti Date: Wed, 20 Mar 2024 17:28:33 +0100 Subject: [PATCH] feat: added active from-to timestamps --- README.md | 6 +++++- imageroot/actions/create-module/10env | 2 ++ imageroot/actions/get-configuration/10get | 12 ++++++++--- .../get-configuration/validate-output.json | 13 +++++++++++- .../events/default-instance-changed/10set | 21 +++++++++++++++++++ imageroot/update-module.d/10config | 4 ++++ 6 files changed, 53 insertions(+), 5 deletions(-) create mode 100755 imageroot/events/default-instance-changed/10set diff --git a/README.md b/README.md index 428e372..4056238 100644 --- a/README.md +++ b/README.md @@ -83,10 +83,14 @@ api-cli run module/loki1/get-configuration ```json { - "retention_days": 7 + "retention_days": 7, + "active_from": "2021-05-28T15:49:27Z+00:00", + "active_to": "2021-05-28T15:49:27Z+00:00" } ``` +Note: `active_to` field WILL miss if the instance is still active. + ## Uninstall To uninstall the instance: diff --git a/imageroot/actions/create-module/10env b/imageroot/actions/create-module/10env index c9d9a7e..afe67a7 100755 --- a/imageroot/actions/create-module/10env +++ b/imageroot/actions/create-module/10env @@ -7,6 +7,7 @@ import agent import os +import datetime def genuuid(): uuid = os.popen("uuidgen") @@ -25,3 +26,4 @@ agent.set_env('LOKI_API_AUTH_PASSWORD', genuuid()) agent.set_env('LOKI_LOGS_INGRESS_TOKEN', genuuid()) agent.set_env('LOKI_HTTP_PORT', port) agent.set_env('LOKI_RETENTION_PERIOD', '365') +agent.set_env('LOKI_ACTIVE_FROM', datetime.datetime.now().astimezone().isoformat()) diff --git a/imageroot/actions/get-configuration/10get b/imageroot/actions/get-configuration/10get index 391aaa7..834ca8b 100755 --- a/imageroot/actions/get-configuration/10get +++ b/imageroot/actions/get-configuration/10get @@ -9,6 +9,12 @@ import os import json import sys -json.dump({ - "retention_days": int(os.getenv('LOKI_RETENTION_PERIOD')) -}, sys.stdout) +response = { + "retention_days": int(os.getenv('LOKI_RETENTION_PERIOD')), + "active_from": os.getenv('LOKI_ACTIVE_FROM') +} + +if os.getenv('LOKI_ACTIVE_TO') is not None: + response['active_to'] = os.getenv('LOKI_ACTIVE_TO') + +json.dump(response, sys.stdout) diff --git a/imageroot/actions/get-configuration/validate-output.json b/imageroot/actions/get-configuration/validate-output.json index 84fa999..47e69a8 100644 --- a/imageroot/actions/get-configuration/validate-output.json +++ b/imageroot/actions/get-configuration/validate-output.json @@ -5,13 +5,24 @@ "description": "Retrieve the configuration of loki instance.", "type": "object", "required": [ - "retention_days" + "retention_days", + "active_from" ], "properties": { "retention_days": { "type": "integer", "description": "Retention period of logs, in days.", "minimum": 1 + }, + "active_from": { + "type": "string", + "format": "date-time", + "description": "The ISO 8601 date-time when the Loki instance was activated." + }, + "active_to": { + "type": "string", + "format": "date-time", + "description": "The ISO 8601 date-time when the Loki instance was deactivated." } } } diff --git a/imageroot/events/default-instance-changed/10set b/imageroot/events/default-instance-changed/10set new file mode 100755 index 0000000..168cf10 --- /dev/null +++ b/imageroot/events/default-instance-changed/10set @@ -0,0 +1,21 @@ +#!/usr/bin/env python3 + +# +# Copyright (C) 2024 Nethesis S.r.l. +# SPDX-License-Identifier: GPL-3.0-or-later +# + +import json +import os +import sys +import datetime + +import agent + +# Check if event comes from the cluster +if os.getenv("AGENT_EVENT_SOURCE") == 'cluster': + # parse data + data = json.load(sys.stdin) + if 'instance' in data and data['instance'] == 'loki': + if 'previous' in data and data['previous'] == os.getenv('MODULE_ID'): + agent.set_env('LOKI_ACTIVE_TO', datetime.datetime.now().astimezone().isoformat()) diff --git a/imageroot/update-module.d/10config b/imageroot/update-module.d/10config index 911f7be..e67ce59 100755 --- a/imageroot/update-module.d/10config +++ b/imageroot/update-module.d/10config @@ -5,10 +5,14 @@ # SPDX-License-Identifier: GPL-3.0-or-later # +import datetime import os import agent # Set environment variables +if os.getenv('LOKI_ACTIVE_FROM') is None: + # This is the most unorthodox way to get the file creation time, which is not even guaranteed to be the same as the time the file was written to disk + agent.set_env('LOKI_ACTIVE_FROM', datetime.datetime.fromtimestamp(os.stat('environment').st_mtime).astimezone().isoformat()) if os.getenv('LOKI_RETENTION_PERIOD') is None: agent.set_env('LOKI_RETENTION_PERIOD', '365')