-
Notifications
You must be signed in to change notification settings - Fork 8
/
lint.py
164 lines (141 loc) · 4.68 KB
/
lint.py
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# -*- coding: utf-8 -*-
'''
Lint states and sls files
'''
from __future__ import absolute_import
# Import python libs
import logging
from types import NoneType
from collections import OrderedDict
from voluptuous import *
from inspect import getargspec
import importlib
# Import salt libs
import salt.config
import salt.utils
import salt.state
import salt.payload
from salt.exceptions import SaltInvocationError
__outputter__ = {
'validate_sls': 'highstate',
}
log = logging.getLogger(__name__)
def _getschema(state):
# Get argspec for state. Return False is not available.
(module, function) = state.split('.')
try:
package = importlib.import_module("salt.states.%s" % module)
except:
return False
try:
argspec = getargspec(getattr(package, function))
except Exception as e:
return False
# Default schema for common functions
# Add list of valid templates
schema = {
'context': OrderedDict,
'defaults': OrderedDict,
'name': Coerce(str),
'names': list,
'check_cmd': str,
'listen': list,
'listen_in': list,
'onchanges': list,
'onchanges_in': list,
'onfail': list,
'onfail_in': list,
'onlyif': Coerce(str),
'order': int,
'prereq': list,
'prereq_in': list,
'require': list,
'require_in': list,
'template': str,
'unless': Coerce(str),
'use': list,
'watch': list,
'watch_in': list,
'formatter': str
}
# pkgrepo doesn't have arguments defined in the state. define them here
pkgrepo_schema = {
'baseurl': str,
'comments': list,
'comps': str,
'consolidate': bool,
'disabled': bool,
'dist': str,
'file': str,
'gpgcheck': int,
'gpgkey': str,
'humanname': str,
'keyserver': str,
'key_urk': str,
'mirrorlist': str,
'ppa': str,
'ppa_auth': str,
'refresh_db': bool
}
if state in ['pkgrepo.managed', 'pkgrepo.absent']:
return Schema(dict(schema.items() + pkgrepo_schema.items()))
# Identify arguments and default value. Add to schema dict inheriting
# type from default value. If no default value, assume string.
for idx, arg in enumerate(argspec.args):
if arg not in schema:
if argspec.defaults == None:
default = 'string'
else:
nodefaults = (len(argspec.args) - len(argspec.defaults))
if idx < nodefaults:
default = 'string'
else:
didx = (idx - nodefaults)
default = argspec.defaults[didx]
if type(default) == bool:
stype = bool
elif type(default) == NoneType:
stype = Coerce(str)
else:
stype = Coerce(type(default))
schema[arg] = stype
return Schema(schema)
def validate_sls(mods, saltenv='base', test=None, queue=False, env=None, **kwargs):
schema = {}
ret = {}
errors = []
data = __salt__['state.show_sls'](mods, saltenv, test, queue, env, kwargs=kwargs)
# Errors returned from state
if type(data) == list:
return data
# iterate over ids
for id, resource in data.items():
ret[id] = {}
# iterate over states
for module, args in resource.items():
# Ignore dunder dicts
if module in ['__sls__', '__env__']:
continue
# find state name, i.e. cmd.run
function = [e for e in args if type(e) == str][0]
state = "%s.%s" % (module, function)
# add state to schema, and check state is valid
if state not in schema:
schema[state] = _getschema(state)
if schema[state] == False:
errors.append("%s: %s not available in schema" % (id, state))
continue
# iterate over arguments to make sure they're valid according to our schema
for arg in [e for e in args if type(e) != str]:
try:
schema[state](arg)
except Exception as e:
if e.msg == "extra keys not allowed":
errors.append("%s: %s is not a valid argument for %s" % (id, arg.iterkeys().next(), state))
else:
errors.append("%s %s: Got %s for %s but %s" % (id, state, arg.itervalues().next(), arg.iterkeys().next(), e.msg))
ret[id][state] = { 'result': True }
if len(errors) > 0:
__context__['retcode'] = 1
return errors
return ret