Skip to content

Commit

Permalink
feat: added active from-to timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
Tbaile committed Mar 20, 2024
1 parent e0947b6 commit 23f032f
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 5 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions imageroot/actions/create-module/10env
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import agent
import os
import datetime

def genuuid():
uuid = os.popen("uuidgen")
Expand All @@ -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())
12 changes: 9 additions & 3 deletions imageroot/actions/get-configuration/10get
Original file line number Diff line number Diff line change
Expand Up @@ -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)
13 changes: 12 additions & 1 deletion imageroot/actions/get-configuration/validate-output.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}
}
}
21 changes: 21 additions & 0 deletions imageroot/events/default-instance-changed/10set
Original file line number Diff line number Diff line change
@@ -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())
4 changes: 4 additions & 0 deletions imageroot/update-module.d/10config
Original file line number Diff line number Diff line change
Expand Up @@ -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')

0 comments on commit 23f032f

Please sign in to comment.