blob: 85e3bb42c9d0e3880e6ae9e08c46c7baa6f6ba3a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
#!/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('interface'):
intfs_names = conf.return_values('interface')
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", "systemctl"]
if len(mdns) == 0:
systemd_cmd.append("stop")
else:
systemd_cmd.append("restart")
systemd_cmd.append("mdns-repeater.service")
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)
|