]>
git.proxmox.com Git - mirror_ifupdown2.git/blob - ifupdown/policymanager.py
3 # Copyright 2015 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 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',
35 class policymanager():
37 # we should check for these files in order
38 # so that customers can override the /var/lib file settings
39 self
.logger
= logging
.getLogger('ifupdown.' +
40 self
.__class
__.__name
__)
42 # we grab the json files from a known location and make sure that
43 # the defaults_policy is checked first
44 user_files
= glob
.glob('/etc/network/ifupdown2/policy.d/*.json')
45 # grab the default module files
46 default_files
= glob
.glob('/var/lib/ifupdown2/policy.d/*.json')
47 # keep an array of defaults indexed by module name
48 self
.system_policy_array
= {}
49 for filename
in default_files
:
52 with
open(filename
, 'r') as fd
:
53 system_array
= json
.load(fd
)
54 self
.logger
.debug('reading %s system policy defaults config' \
57 self
.logger
.info('could not read %s system policy defaults config' \
59 self
.logger
.info(' exception is %s' % str(e
))
61 for module
in system_array
.keys():
62 if self
.system_policy_array
.has_key(module
):
63 self
.logger
.debug('warning: overwriting system module %s from file %s' \
65 self
.system_policy_array
[module
] = system_array
[module
]
67 # take care of user defined policy defaults
68 self
.user_policy_array
= {}
69 for filename
in user_files
:
72 with
open(filename
, 'r') as fd
:
73 user_array
= json
.load(fd
)
74 self
.logger
.debug('reading %s policy user defaults config' \
77 self
.logger
.debug('could not read %s user policy defaults config' \
79 self
.logger
.debug(' exception is %s' % str(e
))
80 # customer added module attributes
81 for module
in user_array
.keys():
82 if self
.system_policy_array
.has_key(module
):
83 # warn user that we are overriding the system module setting
84 self
.logger
.debug('warning: overwriting system with user module %s from file %s' \
86 self
.user_policy_array
[module
] = user_array
[module
]
89 def get_iface_default(self
,module_name
=None,ifname
=None,attr
=None):
91 get_iface_default: Addon modules must use one of two types of access methods to
92 the default configs. In this method, we expect the default to be
94 [module]['iface_defaults'][ifname][attr] or
95 [module]['defaults'][attr]
96 We first check the user_policy_array and return that value. But if
97 the user did not specify an override, we use the system_policy_array.
99 # make sure we have an index
100 if (not ifname
or not attr
or not module_name
):
104 # users can specify defaults to override the systemwide settings
105 # look for user specific interface attribute iface_defaults first
107 # looks for user specified value
108 val
= self
.user_policy_array
[module_name
]['iface_defaults'][ifname
][attr
]
110 except (TypeError, KeyError, IndexError):
113 # failing that, there may be a user default for all intefaces
114 val
= self
.user_policy_array
[module_name
]['defaults'][attr
]
116 except (TypeError, KeyError, IndexError):
119 # failing that, look for system setting for the interface
120 val
= self
.system_policy_array
[module_name
]['iface_defaults'][ifname
][attr
]
122 except (TypeError, KeyError, IndexError):
125 # failing that, look for system setting for all interfaces
126 val
= self
.system_policy_array
[module_name
]['defaults'][attr
]
128 except (TypeError, KeyError, IndexError):
131 # could not find any system or user default so return Non
134 def get_attr_default(self
,module_name
=None,attr
=None):
136 get_attr_default: Addon modules must use one of two types of access methods to
137 the default configs. In this method, we expect the default to be in
139 [module]['defaults'][attr]
141 We first check the user_policy_array and return that value. But if
142 the user did not specify an override, we use the system_policy_array.
144 if (not attr
or not module_name
):
146 # users can specify defaults to override the systemwide settings
147 # look for user specific attribute defaults first
150 # looks for user specified value
151 val
= self
.user_policy_array
[module_name
]['defaults'][attr
]
153 except (TypeError, KeyError, IndexError):
156 # failing that, look for system setting
157 val
= self
.system_policy_array
[module_name
]['defaults'][attr
]
159 except (TypeError, KeyError, IndexError):
164 def get_module_globals(self
,module_name
=None,attr
=None):
166 get_module_globals: Addon modules must use one of two types of access methods to
167 the default configs. In this method, we expect the default to be in
169 [module]['module_globals'][attr]
171 We first check the user_policy_array and return that value. But if
172 the user did not specify an override, we use the system_policy_array.
175 if (not attr
or not module_name
):
177 # users can specify defaults to override the systemwide settings
178 # look for user specific attribute defaults first
181 # looks for user specified value
182 val
= self
.user_policy_array
[module_name
]['module_globals'][attr
]
184 except (TypeError, KeyError, IndexError):
187 # failing that, look for system setting
188 val
= self
.system_policy_array
[module_name
]['module_globals'][attr
]
190 except (TypeError, KeyError, IndexError):
195 def get_module_default(self
,module_name
=None):
197 get_module_default: Addon modules can also access the entire config
198 This method returns indexed by "system" and "user": these are the
199 system-wide and user-defined policy arrays for a specific module.
203 if self
.system_policy_array
.get(module_name
) and \
204 self
.user_policy_array
.get(module_name
):
205 mod_array
= {"system":self
.system_policy_array
[module_name
],
206 "user":self
.user_policy_array
[module_name
]}
208 # the module must not have these defined, return None
213 policymanager_api
= policymanager()