1 | Puppet::Type.type(:file_line).provide(:ruby) do
|
---|
2 | def exists?
|
---|
3 | if resource[:replace].to_s != 'true' and count_matches(match_regex) > 0
|
---|
4 | true
|
---|
5 | else
|
---|
6 | lines.find do |line|
|
---|
7 | if resource[:ensure].to_s == 'absent' and resource[:match_for_absence].to_s == 'true'
|
---|
8 | line.chomp =~ Regexp.new(resource[:match])
|
---|
9 | else
|
---|
10 | line.chomp == resource[:line].chomp
|
---|
11 | end
|
---|
12 | end
|
---|
13 | end
|
---|
14 | end
|
---|
15 |
|
---|
16 | def create
|
---|
17 | unless resource[:replace].to_s != 'true' and count_matches(match_regex) > 0
|
---|
18 | if resource[:match]
|
---|
19 | handle_create_with_match
|
---|
20 | elsif resource[:after]
|
---|
21 | handle_create_with_after
|
---|
22 | else
|
---|
23 | append_line
|
---|
24 | end
|
---|
25 | end
|
---|
26 | end
|
---|
27 |
|
---|
28 | def destroy
|
---|
29 | if resource[:match_for_absence].to_s == 'true' and resource[:match]
|
---|
30 | handle_destroy_with_match
|
---|
31 | else
|
---|
32 | handle_destroy_line
|
---|
33 | end
|
---|
34 | end
|
---|
35 |
|
---|
36 | private
|
---|
37 | def lines
|
---|
38 | # If this type is ever used with very large files, we should
|
---|
39 | # write this in a different way, using a temp
|
---|
40 | # file; for now assuming that this type is only used on
|
---|
41 | # small-ish config files that can fit into memory without
|
---|
42 | # too much trouble.
|
---|
43 | @lines ||= File.readlines(resource[:path])
|
---|
44 | end
|
---|
45 |
|
---|
46 | def match_regex
|
---|
47 | resource[:match] ? Regexp.new(resource[:match]) : nil
|
---|
48 | end
|
---|
49 |
|
---|
50 | def handle_create_with_match()
|
---|
51 | regex_after = resource[:after] ? Regexp.new(resource[:after]) : nil
|
---|
52 | match_count = count_matches(match_regex)
|
---|
53 |
|
---|
54 | if match_count > 1 && resource[:multiple].to_s != 'true'
|
---|
55 | raise Puppet::Error, "More than one line in file '#{resource[:path]}' matches pattern '#{resource[:match]}'"
|
---|
56 | end
|
---|
57 |
|
---|
58 | File.open(resource[:path], 'w') do |fh|
|
---|
59 | lines.each do |l|
|
---|
60 | fh.puts(match_regex.match(l) ? resource[:line] : l)
|
---|
61 | if (match_count == 0 and regex_after)
|
---|
62 | if regex_after.match(l)
|
---|
63 | fh.puts(resource[:line])
|
---|
64 | match_count += 1 #Increment match_count to indicate that the new line has been inserted.
|
---|
65 | end
|
---|
66 | end
|
---|
67 | end
|
---|
68 |
|
---|
69 | if (match_count == 0)
|
---|
70 | fh.puts(resource[:line])
|
---|
71 | end
|
---|
72 | end
|
---|
73 | end
|
---|
74 |
|
---|
75 | def handle_create_with_after
|
---|
76 | regex = Regexp.new(resource[:after])
|
---|
77 | count = count_matches(regex)
|
---|
78 |
|
---|
79 | if count > 1 && resource[:multiple].to_s != 'true'
|
---|
80 | raise Puppet::Error, "#{count} lines match pattern '#{resource[:after]}' in file '#{resource[:path]}'. One or no line must match the pattern."
|
---|
81 | end
|
---|
82 |
|
---|
83 | File.open(resource[:path], 'w') do |fh|
|
---|
84 | lines.each do |l|
|
---|
85 | fh.puts(l)
|
---|
86 | if regex.match(l) then
|
---|
87 | fh.puts(resource[:line])
|
---|
88 | end
|
---|
89 | end
|
---|
90 | end
|
---|
91 |
|
---|
92 | if (count == 0) # append the line to the end of the file
|
---|
93 | append_line
|
---|
94 | end
|
---|
95 | end
|
---|
96 |
|
---|
97 | def count_matches(regex)
|
---|
98 | lines.select{|l| l.match(regex)}.size
|
---|
99 | end
|
---|
100 |
|
---|
101 | def handle_destroy_with_match
|
---|
102 | match_count = count_matches(match_regex)
|
---|
103 | if match_count > 1 && resource[:multiple].to_s != 'true'
|
---|
104 | raise Puppet::Error, "More than one line in file '#{resource[:path]}' matches pattern '#{resource[:match]}'"
|
---|
105 | end
|
---|
106 |
|
---|
107 | local_lines = lines
|
---|
108 | File.open(resource[:path],'w') do |fh|
|
---|
109 | fh.write(local_lines.reject{|l| match_regex.match(l) }.join(''))
|
---|
110 | end
|
---|
111 | end
|
---|
112 |
|
---|
113 | def handle_destroy_line
|
---|
114 | local_lines = lines
|
---|
115 | File.open(resource[:path],'w') do |fh|
|
---|
116 | fh.write(local_lines.reject{|l| l.chomp == resource[:line] }.join(''))
|
---|
117 | end
|
---|
118 | end
|
---|
119 |
|
---|
120 | ##
|
---|
121 | # append the line to the file.
|
---|
122 | #
|
---|
123 | # @api private
|
---|
124 | def append_line
|
---|
125 | File.open(resource[:path], 'w') do |fh|
|
---|
126 | lines.each do |l|
|
---|
127 | fh.puts(l)
|
---|
128 | end
|
---|
129 | fh.puts resource[:line]
|
---|
130 | end
|
---|
131 | end
|
---|
132 | end
|
---|