]>
git.proxmox.com Git - mirror_ifupdown2.git/blob - ifupdown2/ifupdown/policymanager.py
3 # Copyright 2015-2017 Cumulus Networks, Inc. All rights reserved.
7 The PolicyManager should be subclassed by addon modules
8 to read a JSON policy config file that is later used to
11 Initialize: This module defines a list of config file location based
12 on module. There are defined in the __init__(): All the
13 addon modules need to do is import the policymanager module.
15 import ifupdown2.ifupdown.policymanager as policymanager
18 Provides: an API to retrieve link attributes based on addon module name,
19 interface name, and attribute.
21 The ifupdown.policymanager module provides a global object policymanager_api
22 that can be called like so:
24 speed_default = policymanager.policymanager_api.get_default(
25 module_name='ethtool',
36 class policymanager():
38 # we should check for these files in order
39 # so that customers can override the /var/lib file settings
40 self
.logger
= logging
.getLogger('ifupdown.' +
41 self
.__class
__.__name
__)
43 self
.logger
.info("policymanager init")
45 # we grab the json files from a known location and make sure that
46 # the defaults_policy is checked first
47 user_files
= glob
.glob('/etc/network/ifupdown2/policy.d/*.json')
48 # grab the default module files
49 default_files
= glob
.glob('/var/lib/ifupdown2/policy.d/*.json')
50 # keep an array of defaults indexed by module name
51 self
.system_policy_array
= {}
52 for filename
in default_files
:
55 with
open(filename
, 'r') as fd
:
56 system_array
= json
.load(fd
)
57 self
.logger
.debug('reading %s system policy defaults config' \
59 except Exception as e
:
60 self
.logger
.warning('could not read %s system policy defaults config' \
62 self
.logger
.warning(' exception is %s' % str(e
))
64 for module
in list(system_array
.keys()):
65 if module
in self
.system_policy_array
:
66 self
.logger
.debug("policymanager: merging system module %s policy with file %s" % (module
, filename
))
67 self
.system_policy_array
[module
].update(system_array
[module
])
69 json_dict
= system_array
[module
]
71 if isinstance(json_dict
, dict):
72 self
.system_policy_array
[module
] = system_array
[module
]
73 elif module
!= "README":
75 "file %s contains an invalid policy schema, key "
76 "\"%s\" contains %s when a dictionary is expected" %
77 (filename
, module
, type(json_dict
))
80 # take care of user defined policy defaults
81 self
.user_policy_array
= {}
82 for filename
in user_files
:
85 with
open(filename
, 'r') as fd
:
86 user_array
= json
.load(fd
)
87 self
.logger
.debug('reading %s policy user defaults config' \
89 except Exception as e
:
90 self
.logger
.warning('could not read %s user policy defaults config' \
92 self
.logger
.warning(' exception is %s' % str(e
))
93 # customer added module attributes
94 for module
in list(user_array
.keys()):
95 if module
in self
.system_policy_array
:
96 # warn user that we are overriding the system module setting
97 self
.logger
.debug('warning: overwriting system with user module %s from file %s' \
99 if module
in self
.user_policy_array
:
100 self
.logger
.debug("policymanager: merging user module %s policy with file %s" % (module
, filename
))
101 self
.user_policy_array
[module
].update(user_array
[module
])
103 self
.user_policy_array
[module
] = user_array
[module
]
106 def get_iface_default(self
,module_name
=None,ifname
=None,attr
=None):
108 get_iface_default: Addon modules must use one of two types of access methods to
109 the default configs. In this method, we expect the default to be
111 [module]['iface_defaults'][ifname][attr] or
112 [module]['defaults'][attr]
113 We first check the user_policy_array and return that value. But if
114 the user did not specify an override, we use the system_policy_array.
116 # make sure we have an index
117 if (not ifname
or not attr
or not module_name
):
121 # users can specify defaults to override the systemwide settings
122 # look for user specific interface attribute iface_defaults first
124 # looks for user specified value
125 val
= self
.user_policy_array
[module_name
]['iface_defaults'][ifname
][attr
]
127 except (TypeError, KeyError, IndexError):
130 # failing that, there may be a user default for all intefaces
131 val
= self
.user_policy_array
[module_name
]['defaults'][attr
]
133 except (TypeError, KeyError, IndexError):
136 # failing that, look for system setting for the interface
137 val
= self
.system_policy_array
[module_name
]['iface_defaults'][ifname
][attr
]
139 except (TypeError, KeyError, IndexError):
142 # failing that, look for system setting for all interfaces
143 val
= self
.system_policy_array
[module_name
]['defaults'][attr
]
145 except (TypeError, KeyError, IndexError):
148 # could not find any system or user default so return Non
151 def get_attr_default(self
,module_name
=None,attr
=None):
153 get_attr_default: Addon modules must use one of two types of access methods to
154 the default configs. In this method, we expect the default to be in
156 [module]['defaults'][attr]
158 We first check the user_policy_array and return that value. But if
159 the user did not specify an override, we use the system_policy_array.
161 if (not attr
or not module_name
):
163 # users can specify defaults to override the systemwide settings
164 # look for user specific attribute defaults first
167 # looks for user specified value
168 val
= self
.user_policy_array
[module_name
]['defaults'][attr
]
170 except (TypeError, KeyError, IndexError):
173 # failing that, look for system setting
174 val
= self
.system_policy_array
[module_name
]['defaults'][attr
]
176 except (TypeError, KeyError, IndexError):
181 def get_module_globals(self
,module_name
=None,attr
=None):
183 get_module_globals: Addon modules must use one of two types of access methods to
184 the default configs. In this method, we expect the default to be in
186 [module]['module_globals'][attr]
188 We first check the user_policy_array and return that value. But if
189 the user did not specify an override, we use the system_policy_array.
192 if (not attr
or not module_name
):
194 # users can specify defaults to override the systemwide settings
195 # look for user specific attribute defaults first
198 # looks for user specified value
199 val
= self
.user_policy_array
[module_name
]['module_globals'][attr
]
201 except (TypeError, KeyError, IndexError):
204 # failing that, look for system setting
205 val
= self
.system_policy_array
[module_name
]['module_globals'][attr
]
207 except (TypeError, KeyError, IndexError):
212 def get_module_defaults(self
, module_name
):
214 get_module_defaults: returns a merged dictionary of default values
215 specified in policy files. Users provided values override system
220 raise NotImplementedError('get_module_defaults: module name can\'t be None')
223 defaults
.update(self
.system_policy_array
.get(module_name
, {}).get('defaults', {}))
224 defaults
.update(self
.user_policy_array
.get(module_name
, {}).get('defaults', {}))
227 def get_iface_defaults(self
, module_name
):
231 self
.logger
.info('get_iface_defaults: module name can\'t be None')
233 defaults
.update(self
.system_policy_array
.get(module_name
, {}).get('iface_defaults', {}))
234 defaults
.update(self
.user_policy_array
.get(module_name
, {}).get('iface_defaults', {}))
238 policymanager_api
= policymanager()
241 global policymanager_api
242 policymanager_api
= policymanager()