Skip to content

Commit ceb64f3

Browse files
authored
Merge pull request #4155 from HollyGurza/T4583
T4583: Rewrite VRRP op-mode to vyos.opmode format
2 parents 3710c58 + a5a484a commit ceb64f3

File tree

3 files changed

+380
-68
lines changed

3 files changed

+380
-68
lines changed

op-mode-definitions/vrrp.xml.in

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,42 @@
22
<interfaceDefinition>
33
<node name="show">
44
<children>
5+
<tagNode name="vrrp">
6+
<properties>
7+
<help>Show specified VRRP (Virtual Router Redundancy Protocol) group information</help>
8+
</properties>
9+
<children>
10+
<node name="statistics">
11+
<properties>
12+
<help>Show VRRP statistics</help>
13+
</properties>
14+
<command>sudo ${vyos_op_scripts_dir}/vrrp.py show_statistics --group-name="$3"</command>
15+
</node>
16+
<node name="detail">
17+
<properties>
18+
<help>Show detailed VRRP state information</help>
19+
</properties>
20+
<command>sudo ${vyos_op_scripts_dir}/vrrp.py show_detail --group-name="$3"</command>
21+
</node>
22+
</children>
23+
</tagNode>
524
<node name="vrrp">
625
<properties>
726
<help>Show VRRP (Virtual Router Redundancy Protocol) information</help>
827
</properties>
9-
<command>sudo ${vyos_op_scripts_dir}/vrrp.py --summary</command>
28+
<command>sudo ${vyos_op_scripts_dir}/vrrp.py show_summary</command>
1029
<children>
1130
<node name="statistics">
1231
<properties>
1332
<help>Show VRRP statistics</help>
1433
</properties>
15-
<command>sudo ${vyos_op_scripts_dir}/vrrp.py --statistics</command>
34+
<command>sudo ${vyos_op_scripts_dir}/vrrp.py show_statistics</command>
1635
</node>
1736
<node name="detail">
1837
<properties>
1938
<help>Show detailed VRRP state information</help>
2039
</properties>
21-
<command>sudo ${vyos_op_scripts_dir}/vrrp.py --data</command>
40+
<command>sudo ${vyos_op_scripts_dir}/vrrp.py show_detail</command>
2241
</node>
2342
</children>
2443
</node>

python/vyos/ifconfig/vrrp.py

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,37 @@
2626
from vyos.utils.file import wait_for_file_write_complete
2727
from vyos.utils.process import process_running
2828

29+
2930
class VRRPError(Exception):
3031
pass
3132

33+
3234
class VRRPNoData(VRRPError):
3335
pass
3436

37+
3538
class VRRP(object):
3639
_vrrp_prefix = '00:00:5E:00:01:'
3740
location = {
38-
'pid': '/run/keepalived/keepalived.pid',
39-
'fifo': '/run/keepalived/keepalived_notify_fifo',
40-
'state': '/tmp/keepalived.data',
41-
'stats': '/tmp/keepalived.stats',
42-
'json': '/tmp/keepalived.json',
43-
'daemon': '/etc/default/keepalived',
44-
'config': '/run/keepalived/keepalived.conf',
41+
'pid': '/run/keepalived/keepalived.pid',
42+
'fifo': '/run/keepalived/keepalived_notify_fifo',
43+
'state': '/tmp/keepalived.data',
44+
'stats': '/tmp/keepalived.stats',
45+
'json': '/tmp/keepalived.json',
46+
'daemon': '/etc/default/keepalived',
47+
'config': '/run/keepalived/keepalived.conf',
4548
}
4649

4750
_signal = {
48-
'state': signal.SIGUSR1,
49-
'stats': signal.SIGUSR2,
50-
'json': signal.SIGRTMIN + 2,
51+
'state': signal.SIGUSR1,
52+
'stats': signal.SIGUSR2,
53+
'json': signal.SIGRTMIN + 2,
5154
}
5255

5356
_name = {
5457
'state': 'information',
5558
'stats': 'statistics',
56-
'json': 'data',
59+
'json': 'data',
5760
}
5861

5962
state = {
@@ -64,7 +67,7 @@ class VRRP(object):
6467
# UNKNOWN
6568
}
6669

67-
def __init__(self,ifname):
70+
def __init__(self, ifname):
6871
self.ifname = ifname
6972

7073
def enabled(self):
@@ -79,7 +82,7 @@ def active_interfaces(cls):
7982

8083
@classmethod
8184
def decode_state(cls, code):
82-
return cls.state.get(code,'UNKNOWN')
85+
return cls.state.get(code, 'UNKNOWN')
8386

8487
# used in conf mode
8588
@classmethod
@@ -94,16 +97,20 @@ def collect(cls, what):
9497
try:
9598
# send signal to generate the configuration file
9699
pid = read_file(cls.location['pid'])
97-
wait_for_file_write_complete(fname,
98-
pre_hook=(lambda: os.kill(int(pid), cls._signal[what])),
99-
timeout=30)
100+
wait_for_file_write_complete(
101+
fname,
102+
pre_hook=(lambda: os.kill(int(pid), cls._signal[what])),
103+
timeout=30,
104+
)
100105

101106
return read_file(fname)
107+
except FileNotFoundError:
108+
raise VRRPNoData(
109+
'VRRP data is not available (process not running or no active groups)'
110+
)
102111
except OSError:
103112
# raised by vyos.utils.file.read_file
104-
raise VRRPNoData("VRRP data is not available (wait time exceeded)")
105-
except FileNotFoundError:
106-
raise VRRPNoData("VRRP data is not available (process not running or no active groups)")
113+
raise VRRPNoData('VRRP data is not available (wait time exceeded)')
107114
except Exception:
108115
name = cls._name[what]
109116
raise VRRPError(f'VRRP {name} is not available')
@@ -118,32 +125,41 @@ def disabled(cls):
118125
conf = ConfigTreeQuery()
119126
if conf.exists(base):
120127
# Read VRRP configuration directly from CLI
121-
vrrp_config_dict = conf.get_config_dict(base, key_mangling=('-', '_'),
122-
get_first_key=True)
128+
vrrp_config_dict = conf.get_config_dict(
129+
base, key_mangling=('-', '_'), get_first_key=True
130+
)
123131

124132
# add disabled groups to the list
125133
if 'group' in vrrp_config_dict:
126134
for group, group_config in vrrp_config_dict['group'].items():
127135
if 'disable' not in group_config:
128136
continue
129-
disabled.append([group, group_config['interface'], group_config['vrid'], 'DISABLED', ''])
137+
disabled.append(
138+
[
139+
group,
140+
group_config['interface'],
141+
group_config['vrid'],
142+
'DISABLED',
143+
'',
144+
]
145+
)
130146

131147
# return list with disabled instances
132148
return disabled
133149

134150
@classmethod
135151
def format(cls, data):
136-
headers = ["Name", "Interface", "VRID", "State", "Priority", "Last Transition"]
152+
headers = ['Name', 'Interface', 'VRID', 'State', 'Priority', 'Last Transition']
137153
groups = []
138154

139-
data = json.loads(data)
155+
data = json.loads(data) if isinstance(data, str) else data
140156
for group in data:
141157
data = group['data']
142158

143159
name = data['iname']
144160
intf = data['ifp_ifname']
145161
vrid = data['vrid']
146-
state = cls.decode_state(data["state"])
162+
state = cls.decode_state(data['state'])
147163
priority = data['effective_priority']
148164

149165
since = int(time() - float(data['last_transition']))
@@ -153,4 +169,4 @@ def format(cls, data):
153169

154170
# add to the active list disabled instances
155171
groups.extend(cls.disabled())
156-
return(tabulate(groups, headers))
172+
return tabulate(groups, headers)

0 commit comments

Comments
 (0)