-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpuppet-missing-files
executable file
·131 lines (107 loc) · 4.24 KB
/
puppet-missing-files
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
#!/usr/bin/env python2
# vim: set softtabstop=2 ts=2 sw=2 expandtab:
"""Test puppet sourced files and templates for existence."""
import os.path,re
import subprocess
import sys
def main():
"""The main flow."""
manifests = get_manifests()
paths = get_paths(manifests)
check_paths(paths)
def check_paths(paths):
retval = 0
"""Check the set of paths for existence (or symlinked existence)."""
for path in paths:
if not os.path.exists(path) and not os.path.islink(path):
print "%s does not exist." % format(path)
retval = 1
if retval == 1:
sys.exit('Files missing - Fatal Error')
else:
print 'No missing files - test PASSED'
sys.exit(0)
def get_manifests():
"""Find all .pp files in the current working directory and subfolders."""
try:
manifests = subprocess.check_output(["find", ".", "-type", "f",
"-name", "*.pp"])
manifests = manifests.strip().splitlines()
return manifests
except subprocess.CalledProcessError, error:
sys.exit(1, error)
def get_paths(manifests):
"""Extract and construct paths to check."""
paths = set()
for line in manifests:
try:
results = subprocess.check_output(["grep", "puppet:\/", line])
hits = results.splitlines()
for hit in hits:
working_copy = hit.strip()
# Exclude files with variables and things that aren't files
if re.search('\${?',working_copy):
continue
if re.search('<%=',working_copy):
continue
if re.search('class puppet',working_copy):
continue
quotesplit = re.compile("puppet://(.*)['\"]")
working_copy = quotesplit.split(working_copy)
if len(working_copy) > 1:
working_copy = working_copy[1]
else:
working_copy = working_copy[0]
working_copy = working_copy.replace("puppet://", ".")
segments = working_copy.split("/", 3)
del segments[1]
# If this is just a module by itself, we need to delete differently
if len(sys.argv) == 1:
segments.insert(2, 'files')
else:
if sys.argv[1] == 'module':
del segments[1]
segments.insert(1,'files')
else:
segments.insert(2, 'files')
path = "/".join(segments)
paths.add('.'+path)
# we don't care if grep does not find any matches in a file
except subprocess.CalledProcessError:
pass
try:
results = subprocess.check_output(["grep", "template(", line])
hits = results.splitlines()
for hit in hits:
working_copy = hit.strip()
if re.search('\${?',working_copy):
continue
if re.search('<%=',working_copy):
continue
quotesplit = re.compile("[\"']")
working_copy = quotesplit.split(working_copy)
if len(working_copy) > 1:
working_copy = working_copy[1]
else:
working_copy = working_copy[0]
segments = working_copy.split("/", 1)
# If it's a solo module this needs to be handled differently
if len(sys.argv) == 1:
segments.insert(0, ".")
segments.insert(2, "templates")
else:
if sys.argv[1] == 'module':
del segments[0]
segments.insert(0, ".")
segments.insert(0, "templates")
else:
segments.insert(0, ".")
segments.insert(2, "templates")
path = "/".join(segments)
paths.add(path)
# we don't care if grep does not find any matches in a file
except subprocess.CalledProcessError:
pass
return paths
if __name__ == "__main__":
main()