forked from voxpupuli/puppet-augeasproviders
/
augeas.rb
192 lines (165 loc) · 4.98 KB
/
augeas.rb
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# Alternative Augeas-based provider for sysctl type
#
# Copyright (c) 2012 Dominic Cleal
# Licensed under the Apache License, Version 2.0
require File.dirname(__FILE__) + '/../../../augeasproviders/provider'
Puppet::Type.type(:sysctl).provide(:augeas) do
desc "Uses Augeas API to update sysctl settings"
include AugeasProviders::Provider
default_file { '/etc/sysctl.conf' }
lens { 'Sysctl.lns' }
optional_commands :sysctl => 'sysctl'
def self.sysctl_set(key, value)
if Facter.value(:kernel) == :openbsd
sysctl("#{key}=#{value}")
else
sysctl('-w', %Q{#{key}="#{value}"})
end
end
def self.sysctl_get(key)
sysctl('-n', key).chomp
end
confine :feature => :augeas
confine :exists => target
def self.instances
aug = nil
file = target
path = "/files#{file}"
begin
resources = []
aug = augopen
aug.match("#{path}/*").each do |spath|
resource = {:ensure => :present}
basename = spath.split("/")[-1]
resource[:name] = basename.split("[")[0]
next unless resource[:name]
next if resource[:name] == "#comment"
resource[:value] = aug.get("#{spath}")
# Only match comments immediately before the entry and prefixed with
# the sysctl name
cmtnode = aug.match("#{path}/#comment[following-sibling::*[1][self::#{basename}]]")
unless cmtnode.empty?
comment = aug.get(cmtnode[0])
if comment.match(/#{resource[:name]}:/)
resource[:comment] = comment.sub(/^#{resource[:name]}:\s*/, "")
end
end
resources << new(resource)
end
resources
ensure
aug.close if aug
end
end
def exists?
aug = nil
path = "/files#{self.class.target(resource)}"
begin
aug = self.class.augopen(resource)
not aug.match("#{path}/#{resource[:name]}").empty?
ensure
aug.close if aug
end
end
def create
aug = nil
path = "/files#{self.class.target(resource)}"
# the value to pass to augeas can come either from the 'value' or the
# 'val' type parameter.
value = resource[:value] || resource[:val]
begin
aug = self.class.augopen(resource)
# Prefer to create the node next to a commented out entry
commented = aug.match("#{path}/#comment[.=~regexp('#{resource[:name]}([^a-z\.].*)?')]")
aug.insert(commented.first, resource[:name], false) unless commented.empty?
aug.set("#{path}/#{resource[:name]}", value)
if resource[:comment]
aug.insert("#{path}/#{resource[:name]}", "#comment", true)
aug.set("#{path}/#comment[following-sibling::*[1][self::#{resource[:name]}]]",
"#{resource[:name]}: #{resource[:comment]}")
end
augsave!(aug)
if resource[:apply]
self.class.sysctl_set(resource[:name], value)
end
ensure
aug.close if aug
end
end
def destroy
aug = nil
path = "/files#{self.class.target(resource)}"
begin
aug = self.class.augopen(resource)
aug.rm("#{path}/#comment[following-sibling::*[1][self::#{resource[:name]}]][. =~ regexp('#{resource[:name]}:.*')]")
aug.rm("#{path}/#{resource[:name]}")
augsave!(aug)
ensure
aug.close if aug
end
end
def target
self.class.target(resource)
end
def live_value
self.class.sysctl_get(resource[:name])
end
def value
aug = nil
path = "/files#{self.class.target(resource)}"
begin
aug = self.class.augopen(resource)
aug.get("#{path}/#{resource[:name]}")
ensure
aug.close if aug
end
end
def value=(value)
aug = nil
path = "/files#{self.class.target(resource)}"
begin
aug = self.class.augopen(resource)
aug.set("#{path}/#{resource[:name]}", value)
augsave!(aug)
if resource[:apply] == :true
self.class.sysctl_set(resource[:name], value)
end
ensure
aug.close if aug
end
end
alias_method :val, :value
alias_method :val=, :value=
def comment
aug = nil
path = "/files#{self.class.target(resource)}"
begin
aug = self.class.augopen(resource)
comment = aug.get("#{path}/#comment[following-sibling::*[1][self::#{resource[:name]}]][. =~ regexp('#{resource[:name]}:.*')]")
comment.sub!(/^#{resource[:name]}:\s*/, "") if comment
comment || ""
ensure
aug.close if aug
end
end
def comment=(value)
aug = nil
path = "/files#{self.class.target(resource)}"
begin
aug = self.class.augopen(resource)
cmtnode = "#{path}/#comment[following-sibling::*[1][self::#{resource[:name]}]][. =~ regexp('#{resource[:name]}:.*')]"
if value.empty?
aug.rm(cmtnode)
else
if aug.match(cmtnode).empty?
aug.insert("#{path}/#{resource[:name]}", "#comment", true)
end
aug.set("#{path}/#comment[following-sibling::*[1][self::#{resource[:name]}]]",
"#{resource[:name]}: #{resource[:comment]}")
end
augsave!(aug)
ensure
aug.close if aug
end
end
end