]> git.proxmox.com Git - mirror_ifupdown2.git/blob - ifupdown/policymanager.py
policymanager: fix default policy file paths
[mirror_ifupdown2.git] / ifupdown / policymanager.py
1 #!/usr/bin/python
2 #
3 # Copyright 2015 Cumulus Networks, Inc. All rights reserved.
4 #
5 #
6 '''
7 The PolicyManager should be subclassed by addon modules
8 to read a JSON policy config file that is later used to
9 set defaults:
10
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.
14
15 import ifupdown.policymanager as policymanager
16
17
18 Provides: an API to retrieve link attributes based on addon module name,
19 interface name, and attribute.
20
21 The ifupdown.policymanager module provides a global object policymanager_api
22 that can be called like so:
23
24 speed_default = policymanager.policymanager_api.get_default(
25 module_name='ethtool',
26 ifname=ifaceobj.name,
27 attr='link-speed'
28 )
29 '''
30
31 import json
32 import logging
33 import glob
34
35 class policymanager():
36 def __init__(self):
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__)
41
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:
50 system_array = {}
51 try:
52 fd = open(filename,'r')
53 system_array = json.load(fd)
54 self.logger.debug('reading %s system policy defaults config' \
55 % filename)
56 except Exception, e:
57 self.logger.debug('could not read %s system policy defaults config' \
58 % filename)
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' \
63 % (module,filename))
64 self.system_policy_array[module] = system_array[module]
65
66 # take care of user defined policy defaults
67 self.user_policy_array = {}
68 for filename in user_files:
69 user_array = {}
70 try:
71 fd = open(filename,'r')
72 user_array = json.load(fd)
73 self.logger.debug('reading %s policy user defaults config' \
74 % filename)
75 except Exception, e:
76 self.logger.debug('could not read %s user policy defaults config' \
77 % filename)
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' \
84 % (module,filename))
85 self.user_policy_array[module] = user_array[module]
86 return
87
88 def get_iface_default(self,module_name=None,ifname=None,attr=None):
89 '''
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
92 either in
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.
97 '''
98 # make sure we have an index
99 if (not ifname or not attr or not module_name):
100 return None
101
102 val = None
103 # users can specify defaults to override the systemwide settings
104 # look for user specific interface attribute iface_defaults first
105 try:
106 # looks for user specified value
107 val = self.user_policy_array[module_name]['iface_defaults'][ifname][attr]
108 return val
109 except:
110 pass
111 try:
112 # failing that, there may be a user default for all intefaces
113 val = self.user_policy_array[module_name]['defaults'][attr]
114 return val
115 except:
116 pass
117 try:
118 # failing that, look for system setting for the interface
119 val = self.system_policy_array[module_name]['iface_defaults'][ifname][attr]
120 return val
121 except:
122 pass
123 try:
124 # failing that, look for system setting for all interfaces
125 val = self.system_policy_array[module_name]['defaults'][attr]
126 return val
127 except:
128 pass
129
130 # could not find any system or user default so return Non
131 return val
132
133 def get_attr_default(self,module_name=None,attr=None):
134 '''
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
137
138 [module][attr]
139
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.
142 '''
143 if (not attr or not module_name):
144 return None
145 # users can specify defaults to override the systemwide settings
146 # look for user specific interface attribute iface_defaults first
147 val = None
148 if self.user_policy_array.get(module_name):
149 val = self.user_policy_array[module_name].get(attr)
150
151 if not val:
152 if self.system_policy_array.get(module_name):
153 val = self.system_policy_array[module_name].get(attr)
154
155 return val
156
157 def get_module_default(self,module_name=None):
158 '''
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.
162 '''
163 if not module_name:
164 return None
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]}
169 else:
170 # the module must not have these defined, return None
171 mod_array = None
172
173 return mod_array
174
175 policymanager_api = policymanager()