-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
118 lines (101 loc) · 4.73 KB
/
Copy pathparser.py
File metadata and controls
118 lines (101 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import inspect
import logging
from functools import partial
import os
import yaml
from ops_registry import OpsRegistry
REQUIRED_PARAMS = ['name']
class HostParser(object):
def __init__(self, host_config, host_properties_location='hosts', ops_registry=OpsRegistry()):
self.host_properties_location = host_properties_location
self.host_config = host_config
self.ops_registry = ops_registry
self.parsed_action = {}
self.logger = logging.getLogger('mgmt.' + __name__)
def execute(self, action):
func = self.ops_registry.operations_registry[action['action_name']]
# Positional arguments
for m in action['mandatory_params']:
func = partial(func, action['action_params'][m])
del action['action_params'][m]
# Keword arguments
for k, v in action['action_params'].items():
t = {k: v}
func = partial(func, **t)
try:
func()
except TypeError as e:
self.logger.error(e)
def parse(self):
try:
host_config_file = os.path.join(
self.host_properties_location, self.host_config)
with open(host_config_file, 'r') as f:
config = yaml.load(f)
for action in config['actions']:
"""
Check mandatory params are in place. This is required to
build ordered set of actions
"""
for required_param in REQUIRED_PARAMS:
if required_param not in action.keys():
return False
name = action['name']
dependencies = []
if 'dependencies' in action['meta'].keys():
dependencies = action['meta']['dependencies']
for action_name, action_params in action['action_name'].items():
"""
While checking we also return the mandatory
params in order to execute eventually the action.
"""
mandatory_params = self._check(
action_name, action_params)
if mandatory_params:
self.parsed_action[name] = {'dependencies': dependencies,
'action_name': action_name,
'action_params': action_params,
'mandatory_params': mandatory_params}
else:
self.logger.info(
'Action %s could not be added' % action_name)
self.logger.info("Finished parsing actions for %s" %
str(self.host_config))
except FileNotFoundError:
self.logger.error('host_config: %s not found. Abort' %
str(host_config_file))
return False
except yaml.parser.ParserError:
self.logger.error(
'host_config: %s is not valid yaml. Abort' % str(self.host_config))
return False
return self.parsed_action
def _check(self, action_name, action_params):
if action_name in self.ops_registry.operations_registry.keys():
self.logger.debug("Action: %s found" % action_name)
self.logger.debug(
"Retrieving mandatory arguments for %s action" % action_name)
signature = inspect.signature(
self.ops_registry.operations_registry[action_name])
mandatory_params = []
not_mandatory_params = []
"""
inspect.Paramenters is an ordered dictionary we can rely on that when we build the function back
"""
for param in signature.parameters.values():
if param.default == inspect.Parameter.empty:
mandatory_params.append(param.name)
else:
not_mandatory_params.append(param.name)
for mandatory_param in mandatory_params:
if mandatory_param not in action_params:
self.logger.error("mandatory argument %s for action %s not specified. Abort" % (
mandatory_param, action_name))
return False
self.logger.debug("Emitting %s with %s as argument" %
(action_name, action_params))
return mandatory_params
else:
self.logger.error(
"Action: `` %s `` not registered. Abort" % action_name)
return False