diff options
author | Christian Poessinger <christian@poessinger.com> | 2017-12-08 18:54:15 +0100 |
---|---|---|
committer | Christian Poessinger <christian@poessinger.com> | 2017-12-09 10:35:54 +0100 |
commit | a545cd18f98ad747af26176f9b05cc325e4dd5ff (patch) | |
tree | 77783a4ec3a7fd1359bec6823c7c76cd54071797 /src | |
parent | f5c24a9e91f8ea5e0983697e4350cead206cea4e (diff) | |
download | vyos-1x-a545cd18f98ad747af26176f9b05cc325e4dd5ff.tar.gz vyos-1x-a545cd18f98ad747af26176f9b05cc325e4dd5ff.zip |
Add 'vyos-update-mdns-repeater.py' functionality
Diffstat (limited to 'src')
-rwxr-xr-x | src/conf-mode/vyos-config-mdns-repeater.py | 97 | ||||
-rwxr-xr-x | src/conf-mode/vyos-update-mdns-repeater.py | 24 |
2 files changed, 97 insertions, 24 deletions
diff --git a/src/conf-mode/vyos-config-mdns-repeater.py b/src/conf-mode/vyos-config-mdns-repeater.py new file mode 100755 index 000000000..3f9eb1b2b --- /dev/null +++ b/src/conf-mode/vyos-config-mdns-repeater.py @@ -0,0 +1,97 @@ +#!/usr/bin/env python3 +# +# Copyright (C) 2017 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 <http://www.gnu.org/licenses/>. +# +# + +import sys +import os +import netifaces +import time +import subprocess + +from vyos.config import Config +from vyos.util import ConfigError + +config_file = r'/etc/default/mdns-repeater' + +def get_config(): + interface_list = [] + + conf = Config() + conf.set_level('service mdns repeater') + if not conf.exists(''): + return interface_list + + if conf.exists('interfaces'): + intfs_names = conf.return_values('interfaces') + intfs_names=intfs_names.replace("'", "") + intfs_names=intfs_names.split() + + for name in intfs_names: + interface_list.append(name) + + return interface_list + +def verify(mdns): + # '0' interfaces are possible, think of service deletion. Only '1' is not supported! + if len(mdns) == 1: + raise ConfigError('At least 2 interfaces must be specified but %d given!' % len(mdns)) + + # For mdns-repeater to work it is essential that the interfaces + # have an IP address assigned + for intf in mdns: + try: + netifaces.ifaddresses(intf)[netifaces.AF_INET] + except KeyError as e: + raise ConfigError('No IP address configured for interface "%s"!' % intf) + + return None + +def generate(mdns): + config_header = '### Autogenerated by vyos-update-mdns-repeater.py on {tm} ###\n'.format(tm=time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())) + if len(mdns) > 0: + config_args = 'DAEMON_ARGS="' + ' '.join(str(e) for e in mdns) + '"\n' + else: + config_args = 'DAEMON_ARGS=""\n' + + # write new configuration file + f = open(config_file, 'w') + f.write(config_header) + f.write(config_args) + f.close() + + return None + +def apply(mdns): + systemd_cmd = ["sudo", "service", "mdns-repeater"] + + if len(mdns) == 0: + systemd_cmd.append("stop") + else: + systemd_cmd.append("restart") + + subprocess.call(systemd_cmd) + return None + +if __name__ == '__main__': + try: + c = get_config() + verify(c) + generate(c) + apply(c) + except ConfigError as e: + print(e) + sys.exit(1) diff --git a/src/conf-mode/vyos-update-mdns-repeater.py b/src/conf-mode/vyos-update-mdns-repeater.py deleted file mode 100755 index 56ccfb111..000000000 --- a/src/conf-mode/vyos-update-mdns-repeater.py +++ /dev/null @@ -1,24 +0,0 @@ -#!/usr/bin/env python3 -# -# Copyright (C) 2017 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 <http://www.gnu.org/licenses/>. -# -# - -import os -import sys - -from vyos.config import Config -from vyos.util import ConfigError - |