26
26
from vyos .utils .file import wait_for_file_write_complete
27
27
from vyos .utils .process import process_running
28
28
29
+
29
30
class VRRPError (Exception ):
30
31
pass
31
32
33
+
32
34
class VRRPNoData (VRRPError ):
33
35
pass
34
36
37
+
35
38
class VRRP (object ):
36
39
_vrrp_prefix = '00:00:5E:00:01:'
37
40
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' ,
45
48
}
46
49
47
50
_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 ,
51
54
}
52
55
53
56
_name = {
54
57
'state' : 'information' ,
55
58
'stats' : 'statistics' ,
56
- 'json' : 'data' ,
59
+ 'json' : 'data' ,
57
60
}
58
61
59
62
state = {
@@ -64,7 +67,7 @@ class VRRP(object):
64
67
# UNKNOWN
65
68
}
66
69
67
- def __init__ (self ,ifname ):
70
+ def __init__ (self , ifname ):
68
71
self .ifname = ifname
69
72
70
73
def enabled (self ):
@@ -79,7 +82,7 @@ def active_interfaces(cls):
79
82
80
83
@classmethod
81
84
def decode_state (cls , code ):
82
- return cls .state .get (code ,'UNKNOWN' )
85
+ return cls .state .get (code , 'UNKNOWN' )
83
86
84
87
# used in conf mode
85
88
@classmethod
@@ -94,16 +97,20 @@ def collect(cls, what):
94
97
try :
95
98
# send signal to generate the configuration file
96
99
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
+ )
100
105
101
106
return read_file (fname )
107
+ except FileNotFoundError :
108
+ raise VRRPNoData (
109
+ 'VRRP data is not available (process not running or no active groups)'
110
+ )
102
111
except OSError :
103
112
# 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)' )
107
114
except Exception :
108
115
name = cls ._name [what ]
109
116
raise VRRPError (f'VRRP { name } is not available' )
@@ -118,32 +125,41 @@ def disabled(cls):
118
125
conf = ConfigTreeQuery ()
119
126
if conf .exists (base ):
120
127
# 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
+ )
123
131
124
132
# add disabled groups to the list
125
133
if 'group' in vrrp_config_dict :
126
134
for group , group_config in vrrp_config_dict ['group' ].items ():
127
135
if 'disable' not in group_config :
128
136
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
+ )
130
146
131
147
# return list with disabled instances
132
148
return disabled
133
149
134
150
@classmethod
135
151
def format (cls , data ):
136
- headers = [" Name" , " Interface" , " VRID" , " State" , " Priority" , " Last Transition" ]
152
+ headers = [' Name' , ' Interface' , ' VRID' , ' State' , ' Priority' , ' Last Transition' ]
137
153
groups = []
138
154
139
- data = json .loads (data )
155
+ data = json .loads (data ) if isinstance ( data , str ) else data
140
156
for group in data :
141
157
data = group ['data' ]
142
158
143
159
name = data ['iname' ]
144
160
intf = data ['ifp_ifname' ]
145
161
vrid = data ['vrid' ]
146
- state = cls .decode_state (data [" state" ])
162
+ state = cls .decode_state (data [' state' ])
147
163
priority = data ['effective_priority' ]
148
164
149
165
since = int (time () - float (data ['last_transition' ]))
@@ -153,4 +169,4 @@ def format(cls, data):
153
169
154
170
# add to the active list disabled instances
155
171
groups .extend (cls .disabled ())
156
- return ( tabulate (groups , headers ) )
172
+ return tabulate (groups , headers )
0 commit comments