Skip to content

Commit

Permalink
New events
Browse files Browse the repository at this point in the history
  • Loading branch information
tommaso-ascani committed Jun 18, 2024
1 parent 439b8d9 commit 27f7585
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions imageroot/events/default-instance-changed/10set
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,47 @@ import json
import os
import sys
import datetime
import subprocess

import agent

# Check if event comes from the cluster
if os.getenv("AGENT_EVENT_SOURCE") == 'cluster':
# parse data

# 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') and os.getenv('LOKI_ACTIVE_TO') is None:
agent.set_env('LOKI_ACTIVE_TO', datetime.datetime.now().astimezone().isoformat())

# Old instance
if 'previous' in data and data['previous'] == os.getenv('MODULE_ID'):
# Set loki active to
if os.getenv('LOKI_ACTIVE_TO') is None:
agent.set_env('LOKI_ACTIVE_TO', datetime.datetime.now().astimezone().isoformat())

if os.system("systemctl is-active --user --quiet cloud-log-manager-forwarder.") == 0:
subprocess.run('systemctl --user disable --now cloud-log-manager-forwarder.service', shell=True, executable='/bin/bash')

if os.system("systemctl is-active --user --quiet syslog-forwarder") == 0:
subprocess.run('systemctl --user disable --now syslog-forwarder.service', shell=True, executable='/bin/bash')

# 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 all(key in previous_environment for key in ('CLOUD_LOG_MANAGER_ADDRESS', 'CLOUD_LOG_MANAGER_TENANT', 'CLOUD_LOG_MANAGER_UUID')) and bool(subscription):
agent.set_env('CLOUD_LOG_MANAGER_UUID', previous_environment['CLOUD_LOG_MANAGER_UUID'])
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'])

subprocess.run('systemctl --user enable --now cloud-log-manager-forwarder.service', shell=True, executable='/bin/bash')

if all(key in previous_environment for key in ('SYSLOG_ADDRESS', 'SYSLOG_PORT', 'SYSLOG_PROTOCOL', 'SYSLOG_FORMAT')):
agent.set_env('SYSLOG_ADDRESS', previous_environment['SYSLOG_ADDRESS'])
agent.set_env('SYSLOG_PORT', previous_environment['SYSLOG_PORT'])
agent.set_env('SYSLOG_PROTOCOL', previous_environment['SYSLOG_PROTOCOL'])
agent.set_env('SYSLOG_FORMAT', previous_environment['SYSLOG_FORMAT'])

subprocess.run('systemctl --user enable --now syslog-forwarder.service', shell=True, executable='/bin/bash')

0 comments on commit 27f7585

Please sign in to comment.