1 """
2 @copyright: 2011 Mark LaPerriere
3
4 @license:
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9 U{http://www.apache.org/licenses/LICENSE-2.0}
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16
17 @summary:
18 pyHai auditor plugin base class
19
20 @author: Mark LaPerriere
21 @contact: pyhai@mindmind.com
22 @organization: Mind Squared Design / www.mindmind.com
23 @version: 0.1.3
24 @date: Jan 19, 2012
25 """
26 import abc
27
28 import logging
29
30 _logger = logging.getLogger(__name__)
31 _logger.addHandler(logging.NullHandler())
35 """
36 An ABC class to enforce a common plugin interface
37 """
38 __metaclass__ = abc.ABCMeta
39
40 __profile = None
41 __results = None
42 _running_audit = None
43
44
45 - def __init__(self, profile, running_audit_results, *args, **kwargs):
46 """
47 You probably shouldn't overwrite this method unless you know what you're doing and
48 even then you should be careful and call Plugin.__init__.py at some point.
49 I{Swim at your own risk}
50
51 @param profile: A dictionary containing this host's profile
52 @type profile: C{dict}
53 @param running_audit_results: A dictionary of the results of all the plugins that have run up to "now"
54 @type running_audit_results: C{dict}
55 @return: A dictionary of results from the plugin's run
56 @rtype: C{dict}
57 """
58 self.__profile = profile
59 self._running_audit = running_audit_results
60 try:
61 before_results = self.before(*args, **kwargs)
62 self.__set_results(self.run(*args, before_results=before_results, **kwargs))
63 self.__set_results(self.after(*args, **kwargs))
64 except Exception as exc:
65 self.fail(exc)
66
67 @abc.abstractmethod
68 - def run(self, *args, **kwargs):
69 """
70 Will be called after the before hook.
71 This is the only "required" method for plugins
72 """
73 pass
74
75 - def before(self, *args, **kwargs):
76 """
77 Overwrite in subclass as necessary
78 Will be called first during init
79 """
80 _logger.debug('Plugin hook "before" is not overwritten')
81 return None
82
83 - def after(self, *args, **kwargs):
84 """
85 Overwrite in subclass as necessary
86 Will be called first during init
87 """
88 _logger.debug('Plugin hook "after" is not overwritten')
89 return self._get_results()
90
91 - def fail(self, exc, msg=None, *args):
92 """
93 Overwrite in subclass as necessary
94 Will be called for any exceptions raised for before, run, or after methods
95 """
96 _logger.debug('Current state of results in %s: %s',)
97 _logger.exception(msg, *args)
98
100 """
101 Gets the entire profile dictionary, a list of items or a single key from the profile dictionary
102
103 @param key: Providing a string returns a single value from the profile dictionary,
104 providing an array, returns a slice of the profile dictionary, omitting this param
105 returns the entire profile dictionary
106 @type key: C{list | str}
107 @return: Mixed... part or all of the profile dictionary
108 @rtype: C{dict | str}
109 """
110 if key is not None:
111 if type(key) is list:
112 return dict([(k, self.__profile[k]) for k in key if k in self.__profile])
113 elif type(key) is str and key in self.__profile:
114 return self.__profile[key]
115 else:
116 return None
117 else:
118 return self.__profile
119
121 """
122 Ensures that only dict types are stored in the __results instance var
123
124 @param results: The results from the plugin_hooks or the run
125 B{**MUST} be a dict or will raise an exception
126 @type results: C{dict}
127 """
128 result_type = type(results)
129 if result_type is dict or result_type is tuple or result_type is list:
130 self.__results = results
131
133 """
134 Returns results
135
136 @return: Results of the run
137 @rtype: C{dict}
138 """
139 return self.__results
140