]>
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 fd
= open(filename
,'r')
53 system_array
= json
.load(fd
)
54 self
.logger
.debug('reading %s system policy defaults config' \
57 self
.logger
.debug('could not read %s system policy defaults config' \
59 self
.logger
.debug(' exception is %s' % str(e
))
60 for module
in system_array
.keys():
61 if self
.system_policy_array
.has_key(module
):
62 self
.logger
.debug('warning: overwriting system module %s from file %s' \
64 self
.system_policy_array
[module
] = system_array
[module
]
66 # take care of user defined policy defaults
67 self
.user_policy_array
= {}
68 for filename
in user_files
:
71 fd
= open(filename
,'r')
72 user_array
= json
.load(fd
)
73 self
.logger
.debug('reading %s policy user defaults config' \
76 self
.logger
.debug('could not read %s user policy defaults config' \
78 self
.logger
.debug(' exception is %s' % str(e
))
79 # customer added module attributes
80 for module
in user_array
.keys():
81 if self
.system_policy_array
.has_key(module
):
82 # warn user that we are overriding the system module setting
83 self
.logger
.debug('warning: overwriting system with user module %s from file %s' \
85 self
.user_policy_array
[module
] = user_array
[module
]
88 def get_iface_default(self
,module_name
=None,ifname
=None,attr
=None):
90 get_iface_default: Addon modules must use one of two types of access methods to
91 the default configs. In this method, we expect the default to be
93 [module]['iface_defaults'][ifname][attr] or
94 [module]['defaults'][attr]
95 We first check the user_policy_array and return that value. But if
96 the user did not specify an override, we use the system_policy_array.
98 # make sure we have an index
99 if (not ifname
or not attr
or not module_name
):
103 # users can specify defaults to override the systemwide settings
104 # look for user specific interface attribute iface_defaults first
106 # looks for user specified value
107 val
= self
.user_policy_array
[module_name
]['iface_defaults'][ifname
][attr
]
112 # failing that, there may be a user default for all intefaces
113 val
= self
.user_policy_array
[module_name
]['defaults'][attr
]
118 # failing that, look for system setting for the interface
119 val
= self
.system_policy_array
[module_name
]['iface_defaults'][ifname
][attr
]
124 # failing that, look for system setting for all interfaces
125 val
= self
.system_policy_array
[module_name
]['defaults'][attr
]
130 # could not find any system or user default so return Non
133 def get_attr_default(self
,module_name
=None,attr
=None):
135 get_attr_default: Addon modules must use one of two types of access methods to
136 the default configs. In this method, we expect the default to be in
140 We first check the user_policy_array and return that value. But if
141 the user did not specify an override, we use the system_policy_array.
143 if (not attr
or not module_name
):
145 # users can specify defaults to override the systemwide settings
146 # look for user specific interface attribute iface_defaults first
148 if self
.user_policy_array
.get(module_name
):
149 val
= self
.user_policy_array
[module_name
].get(attr
)
152 if self
.system_policy_array
.get(module_name
):
153 val
= self
.system_policy_array
[module_name
].get(attr
)
157 def get_module_default(self
,module_name
=None):
159 get_module_default: Addon modules can also access the entire config
160 This method returns indexed by "system" and "user": these are the
161 system-wide and user-defined policy arrays for a specific module.
165 if self
.system_policy_array
.get(module_name
) and \
166 self
.user_policy_array
.get(module_name
):
167 mod_array
= {"system":self
.system_policy_array
[module_name
],
168 "user":self
.user_policy_array
[module_name
]}
170 # the module must not have these defined, return None
175 policymanager_api
= policymanager()