Skip to content

Commit

Permalink
Cloud Log Manager UX improvement (#14)
Browse files Browse the repository at this point in the history
- Assign the Cloud Log Manager UUID and HOSTNAME when the instance is created and never change their values. In this way, even the first time CLM is configured, its HOSTNAME is visible in the UI. This avoids to reopen the configuration form again at a later time, just to obtain the HOSTNAME.
- Upgrade versions <1.2 by setting new variables

Refs NethServer/dev#6932
  • Loading branch information
DavidePrincipi authored Jul 22, 2024
1 parent 2dd3765 commit 742b701
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 16 deletions.
8 changes: 6 additions & 2 deletions imageroot/actions/create-module/10env
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@
import agent
import os
import datetime
import uuid

def genuuid():
uuid = os.popen("uuidgen")
return uuid.read()
return str(uuid.uuid4()) # random UUID generation

node_id = os.environ['NODE_ID']
port = os.environ['TCP_PORT']

rdb = agent.redis_connect()
rkey = f'node/{node_id}/vpn'
ip_address = rdb.hget(rkey, 'ip_address')
# Keep the cluster UUID private, use an hash of it instead:
clm_uuid = str(uuid.uuid5(uuid.NAMESPACE_DNS, rdb.get('cluster/uuid') or genuuid()))

# Discover the currently active Loki instance
active_agent = agent.resolve_agent_id("loki@cluster") or os.environ["AGENT_ID"]
Expand All @@ -33,3 +35,5 @@ agent.set_env('LOKI_LOGS_INGRESS_TOKEN', genuuid())
agent.set_env('LOKI_HTTP_PORT', port)
agent.set_env('LOKI_RETENTION_PERIOD', initial_retention)
agent.set_env('LOKI_ACTIVE_FROM', datetime.datetime.now().astimezone().isoformat())
agent.set_env('CLOUD_LOG_MANAGER_UUID', clm_uuid)
agent.set_env('CLOUD_LOG_MANAGER_HOSTNAME', 'cluster-' + clm_uuid[:8])
4 changes: 0 additions & 4 deletions imageroot/actions/set-clm-forwarder/10set
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@ import sys
request = json.load(sys.stdin)

rdb = agent.redis_connect()
cluster_uuid = rdb.get('cluster/uuid')
subscription = rdb.hgetall('cluster/subscription')

if not bool(subscription):
print("No subscription found !")
sys.exit(1)

if (request['active']):
agent.set_env('CLOUD_LOG_MANAGER_UUID', uuid.uuid5(uuid.NAMESPACE_DNS, cluster_uuid))
agent.set_env('CLOUD_LOG_MANAGER_HOSTNAME', 'cluster-' + hashlib.sha256(cluster_uuid.encode("utf-8")).hexdigest()[:8])
agent.set_env('CLOUD_LOG_MANAGER_ADDRESS', f"{request['address']}")
agent.set_env('CLOUD_LOG_MANAGER_TENANT', f"{request['tenant']}")

Expand All @@ -38,7 +35,6 @@ if (request['active']):
else:
action = 'enable'
else:
agent.unset_env('CLOUD_LOG_MANAGER_UUID')
agent.unset_env('CLOUD_LOG_MANAGER_START_TIME')
agent.unset_env('CLOUD_LOG_MANAGER_ADDRESS')
agent.unset_env('CLOUD_LOG_MANAGER_TENANT')
Expand Down
10 changes: 2 additions & 8 deletions imageroot/events/default-instance-changed/10set
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,15 @@ if os.getenv("AGENT_EVENT_SOURCE") == 'cluster':
stdout=sys.stderr,
stderr=sys.stderr,
check=True)

agent.unset_env('CLOUD_LOG_MANAGER_HOSTNAME')


# New instance
if 'current' in data and data['current'] == os.getenv('MODULE_ID'):
rdb = agent.redis_connect()
previous_environment = rdb.hgetall(f"module/{data['previous']}/environment")
subscription = rdb.hgetall('cluster/subscription')

# If cloud log manager service was active in old instance, start on new instacnce
if all(key in previous_environment for key in ('CLOUD_LOG_MANAGER_ADDRESS', 'CLOUD_LOG_MANAGER_TENANT', 'CLOUD_LOG_MANAGER_UUID')) and bool(subscription):
cluster_uuid = rdb.get('cluster/uuid')

agent.set_env('CLOUD_LOG_MANAGER_UUID', previous_environment['CLOUD_LOG_MANAGER_UUID'])
agent.set_env('CLOUD_LOG_MANAGER_HOSTNAME', 'cluster-' + hashlib.sha256(cluster_uuid.encode("utf-8")).hexdigest()[:8])
if all(key in previous_environment for key in ('CLOUD_LOG_MANAGER_ADDRESS', 'CLOUD_LOG_MANAGER_TENANT')) and bool(subscription):
agent.set_env('CLOUD_LOG_MANAGER_ADDRESS', previous_environment['CLOUD_LOG_MANAGER_ADDRESS'])
agent.set_env('CLOUD_LOG_MANAGER_TENANT', previous_environment['CLOUD_LOG_MANAGER_TENANT'])

Expand Down
1 change: 0 additions & 1 deletion imageroot/events/subscription-changed/10check_forwarder
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import subprocess
data = json.load(sys.stdin)

if data['action'] == 'terminated':
agent.unset_env('CLOUD_LOG_MANAGER_UUID')
agent.unset_env('CLOUD_LOG_MANAGER_START_TIME')
agent.unset_env('CLOUD_LOG_MANAGER_ADDRESS')
agent.unset_env('CLOUD_LOG_MANAGER_TENANT')
Expand Down
10 changes: 9 additions & 1 deletion imageroot/update-module.d/10config
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@

import datetime
import os

import uuid
import agent

rdb = agent.redis_connect()

# 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')

# Versions before 1.2 need to set new variables for Cloud Log Manager:
if os.getenv('CLOUD_LOG_MANAGER_UUID') is None:
clm_uuid = str(uuid.uuid5(uuid.NAMESPACE_DNS, rdb.get('cluster/uuid') or str(uuid.uuid4())))
agent.set_env('CLOUD_LOG_MANAGER_UUID', clm_uuid)
agent.set_env('CLOUD_LOG_MANAGER_HOSTNAME', 'cluster-' + clm_uuid[:8])

0 comments on commit 742b701

Please sign in to comment.