From d542bf338aa1638b082a141163db5437ce7a33a9 Mon Sep 17 00:00:00 2001 From: Viacheslav Hletenko Date: Tue, 7 Jun 2022 06:54:39 +0000 Subject: event-handler: T3083: Move system to service event-handler Move 'system event-handler' to 'service event-handler' --- src/conf_mode/service_event_handler.py | 84 ++++++++++++++++++++++++++++++++++ src/conf_mode/system_event_handler.py | 84 ---------------------------------- 2 files changed, 84 insertions(+), 84 deletions(-) create mode 100755 src/conf_mode/service_event_handler.py delete mode 100755 src/conf_mode/system_event_handler.py (limited to 'src/conf_mode') diff --git a/src/conf_mode/service_event_handler.py b/src/conf_mode/service_event_handler.py new file mode 100755 index 000000000..cf4aba49b --- /dev/null +++ b/src/conf_mode/service_event_handler.py @@ -0,0 +1,84 @@ +#!/usr/bin/env python3 +# +# Copyright (C) 2022 VyOS maintainers and contributors +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 or later as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +import json +from pathlib import Path + +from vyos.config import Config +from vyos.util import call +from vyos import ConfigError +from vyos import airbag + +airbag.enable() + +service_name = 'vyos-event-handler' +service_conf = Path(f'/run/{service_name}.conf') + + +def get_config(config=None): + if config: + conf = config + else: + conf = Config() + + base = ['service', 'event-handler'] + config = conf.get_config_dict(base, + get_first_key=True, + no_tag_node_value_mangle=True) + + return config + + +def verify(config): + # bail out early - looks like removal from running config + if not config: + return None + + for name, event_config in config.items(): + if 'pattern' not in event_config or 'script' not in event_config: + raise ConfigError( + 'Event-handler: both pattern and script items are mandatory!') + + +def generate(config): + if not config: + # Remove old config and return + service_conf.unlink(missing_ok=True) + return None + + # Write configuration file + conf_json = json.dumps(config, indent=4) + service_conf.write_text(conf_json) + + return None + + +def apply(config): + if config: + call(f'systemctl restart {service_name}.service') + else: + call(f'systemctl stop {service_name}.service') + + +if __name__ == '__main__': + try: + c = get_config() + verify(c) + generate(c) + apply(c) + except ConfigError as e: + print(e) + exit(1) diff --git a/src/conf_mode/system_event_handler.py b/src/conf_mode/system_event_handler.py deleted file mode 100755 index 9f91e99fc..000000000 --- a/src/conf_mode/system_event_handler.py +++ /dev/null @@ -1,84 +0,0 @@ -#!/usr/bin/env python3 -# -# Copyright (C) 2022 VyOS maintainers and contributors -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License version 2 or later as -# published by the Free Software Foundation. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -import json -from pathlib import Path - -from vyos.config import Config -from vyos.util import call -from vyos import ConfigError -from vyos import airbag - -airbag.enable() - -service_name = 'vyos-event-handler' -service_conf = Path(f'/run/{service_name}.conf') - - -def get_config(config=None): - if config: - conf = config - else: - conf = Config() - - base = ['system', 'event-handler'] - config = conf.get_config_dict(base, - get_first_key=True, - no_tag_node_value_mangle=True) - - return config - - -def verify(config): - # bail out early - looks like removal from running config - if not config: - return None - - for name, event_config in config.items(): - if 'pattern' not in event_config or 'script' not in event_config: - raise ConfigError( - 'Event-handler: both pattern and script items are mandatory!') - - -def generate(config): - if not config: - # Remove old config and return - service_conf.unlink(missing_ok=True) - return None - - # Write configuration file - conf_json = json.dumps(config, indent=4) - service_conf.write_text(conf_json) - - return None - - -def apply(config): - if config: - call(f'systemctl restart {service_name}.service') - else: - call(f'systemctl stop {service_name}.service') - - -if __name__ == '__main__': - try: - c = get_config() - verify(c) - generate(c) - apply(c) - except ConfigError as e: - print(e) - exit(1) -- cgit v1.2.3