diff --git a/imageroot/events/default-instance-changed/10set b/imageroot/events/default-instance-changed/10set index ebdb671..869df20 100755 --- a/imageroot/events/default-instance-changed/10set +++ b/imageroot/events/default-instance-changed/10set @@ -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') \ No newline at end of file