-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
augeas.rb
163 lines (143 loc) · 4.8 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
# coding: utf-8
# Alternative Augeas-based providers for Puppet
#
# Copyright (c) 2012 Raphaël Pinson
# Licensed under the Apache License, Version 2.0
raise("Missing augeasproviders_core dependency") if Puppet::Type.type(:augeasprovider).nil?
Puppet::Type.type(:ssh_config).provide(:augeas, :parent => Puppet::Type.type(:augeasprovider).provider(:default)) do
desc "Uses Augeas API to update an ssh_config parameter"
default_file { '/etc/ssh/ssh_config' }
lens { 'Ssh.lns' }
confine :feature => :augeas
resource_path do |resource|
base = self.base_path(resource)
key = resource[:key] ? resource[:key] : resource[:name]
"#{base}/*[label()=~regexp('#{key}', 'i')]"
end
def self.base_path(resource)
"$target/Host[.='#{resource[:host]}']"
end
def self.instances
augopen do |aug,path|
resources = []
aug.match('$target/Host').each do |hpath|
aug.match("#{hpath}/*").each do |kpath|
label = path_label(aug, kpath)
next if label.start_with?("#")
host = aug.get(hpath)
value = self.get_value(aug, kpath)
resources << new({
:ensure => :present,
:name => label,
:key => label,
:value => value,
:host => host
})
end
end
resources
end
end
def self.get_value(aug, pathx)
aug.match(pathx).map do |vp|
# Augeas lens does transparent multi-node (no counte reset) so check for any int
if aug.match("#{vp}/*[label()=~regexp('[0-9]*')]").empty?
aug.get(vp)
else
aug.match("#{vp}/*").map do |svp|
aug.get(svp)
end
end
end.flatten
end
def self.set_value(aug, base, path, label, value)
if label =~ /Ciphers|SendEnv|MACs|(HostKey|Kex)Algorithms|GlobalKnownHostsFile/i
aug.rm("#{path}/*")
# In case there is more than one entry, keep only the first one
aug.rm("#{path}[position() != 1]")
count = 0
value.each do |v|
count += 1
aug.set("#{path}/#{count}", v)
end
else
# Normal setting: one value per entry
value = value.clone
# Change any existing settings with this name
lastsp = nil
aug.match(path).each do |sp|
val = value.shift
if val.nil?
aug.rm(sp)
else
aug.set(sp, val)
lastsp = sp
end
end
# Insert new values for the rest
value.each do |v|
if lastsp
# After the most recent same setting (lastsp)
aug.insert(lastsp, label, false)
aug.set("#{path}[last()]", v)
else
# Prefer to create the node next to a commented out entry
commented = aug.match("#{base}/#comment[.=~regexp('#{label}([^a-z\.].*)?')]")
unless commented.empty?
aug.insert(commented.first, label, false)
end
aug.set("#{path}[last()]", v)
end
lastsp = aug.match("#{path}[last()]")[0]
end
aug.defvar('resource', path)
end
end
def create
base_path = self.class.base_path(resource)
augopen! do |aug|
key = resource[:key] ? resource[:key] : resource[:name]
# create base_path
aug.set(base_path, resource[:host])
self.class.set_value(aug, base_path, "#{base_path}/#{key}", key, resource[:value])
self.class.set_comment(aug, base_path, resource[:name], resource[:comment]) if resource[:comment]
end
end
def value
augopen do |aug|
self.class.get_value(aug, '$resource')
end
end
def value=(value)
augopen! do |aug|
key = resource[:key] ? resource[:key] : resource[:name]
self.class.set_value(aug, self.class.base_path(resource), resource_path, key, value)
end
end
def comment
base_path = self.class.base_path(resource)
augopen do |aug|
comment = aug.get("#{base_path}/#comment[following-sibling::*[1][label() =~ regexp('#{resource[:name]}', 'i')]][. =~ regexp('#{resource[:name]}:.*', 'i')]")
comment.sub!(/^#{resource[:name]}:\s*/i, "") if comment
comment || ""
end
end
def comment=(value)
base_path = self.class.base_path(resource)
augopen! do |aug|
self.class.set_comment(aug, base_path, resource[:name], value)
end
end
def self.set_comment(aug, base, name, value)
cmtnode = "#{base}/#comment[following-sibling::*[1][label() =~ regexp('#{name}', 'i')]][. =~ regexp('#{name}:.*', 'i')]"
if value.empty?
aug.rm(cmtnode)
else
if aug.match(cmtnode).empty?
aug.insert('$resource', "#comment", true)
end
aug.set("#{base}/#comment[following-sibling::*[1][label() =~ regexp('#{name}', 'i')]]",
"#{name}: #{value}")
end
end
end