Class: Chef::Win32::Registry
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#create_key(key_path, recursive) ⇒ Object
-
#data_exists!(key_path, value) ⇒ Object
-
#data_exists?(key_path, value) ⇒ Boolean
-
#delete_key(key_path, recursive) ⇒ Object
-
#delete_value(key_path, value) ⇒ Object
-
#get_name_from_type(val_class) ⇒ Object
-
#get_subkeys(key_path) ⇒ Object
-
#get_type_from_name(val_type) ⇒ Object
-
#get_values(key_path) ⇒ Object
-
#has_subkeys?(key_path) ⇒ Boolean
-
#hive_exists?(key_path) ⇒ Boolean
-
#initialize(run_context = nil, user_architecture = :machine) ⇒ Registry
constructor
A new instance of Registry.
-
#key_exists!(key_path) ⇒ Object
-
#key_exists?(key_path) ⇒ Boolean
-
#keys_missing?(key_path) ⇒ Boolean
-
#registry_system_architecture ⇒ Object
32-bit chef clients running on 64-bit machines will default to reading the 64-bit registry.
-
#set_value(key_path, value) ⇒ Object
-
#type_matches!(key_path, value) ⇒ Object
-
#type_matches?(key_path, value) ⇒ Boolean
-
#value_exists!(key_path, value) ⇒ Object
-
#value_exists?(key_path, value) ⇒ Boolean
utf8_to_wide, wide_to_utf8, wstring
Constructor Details
#initialize(run_context = nil, user_architecture = :machine) ⇒ Registry
Returns a new instance of Registry.
45
46
47
48
|
# File 'lib/chef/win32/registry.rb', line 45
def initialize(run_context = nil, user_architecture = :machine)
@run_context = run_context
self.architecture = user_architecture
end
|
Instance Attribute Details
#architecture ⇒ Object
Returns the value of attribute architecture.
43
44
45
|
# File 'lib/chef/win32/registry.rb', line 43
def architecture
@architecture
end
|
#run_context ⇒ Object
Returns the value of attribute run_context.
42
43
44
|
# File 'lib/chef/win32/registry.rb', line 42
def run_context
@run_context
end
|
Instance Method Details
#create_key(key_path, recursive) ⇒ Object
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/chef/win32/registry.rb', line 106
def create_key(key_path, recursive)
Chef::Log.trace("Creating registry key #{key_path}")
if keys_missing?(key_path)
if recursive == true
Chef::Log.trace("Registry key #{key_path} has missing subkeys, and recursive specified, creating them....")
create_missing(key_path)
else
raise Chef::Exceptions::Win32RegNoRecursive, "Registry key #{key_path} has missing subkeys, and recursive not specified"
end
end
if key_exists?(key_path)
Chef::Log.trace("Registry key #{key_path} already exists, doing nothing")
else
hive, key = get_hive_and_key(key_path)
hive.create(key, ::Win32::Registry::KEY_WRITE | registry_system_architecture)
Chef::Log.trace("Registry key #{key_path} created")
end
true
end
|
#data_exists!(key_path, value) ⇒ Object
235
236
237
238
239
240
241
|
# File 'lib/chef/win32/registry.rb', line 235
def data_exists!(key_path, value)
unless data_exists?(key_path, value)
raise Chef::Exceptions::Win32RegDataMissing, "Registry key #{key_path} has no value named #{value[:name]}, containing type #{value[:type]} and data #{value[:data]}"
end
true
end
|
#data_exists?(key_path, value) ⇒ Boolean
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
# File 'lib/chef/win32/registry.rb', line 212
def data_exists?(key_path, value)
key_exists!(key_path)
hive, key = get_hive_and_key(key_path)
hive.open(key, ::Win32::Registry::KEY_READ | registry_system_architecture) do |reg|
reg.each do |val_name, val_type, val_data|
if safely_downcase(val_name) == safely_downcase(value[:name]) &&
val_type == get_type_from_name(value[:type]) &&
val_data == value[:data]
return true
end
end
end
false
end
|
#delete_key(key_path, recursive) ⇒ Object
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
# File 'lib/chef/win32/registry.rb', line 126
def delete_key(key_path, recursive)
Chef::Log.trace("Deleting registry key #{key_path}")
unless key_exists?(key_path)
Chef::Log.trace("Registry key #{key_path}, does not exist, not deleting")
return true
end
if has_subkeys?(key_path) && !recursive
raise Chef::Exceptions::Win32RegNoRecursive, "Registry key #{key_path} has subkeys, and recursive not specified"
end
hive, key_including_parent = get_hive_and_key(key_path)
key_parts = key_including_parent.split("\\")
key = key_parts.pop
key_parent = key_parts.join("\\")
hive.open(key_parent, ::Win32::Registry::KEY_WRITE | registry_system_architecture) do |reg|
reg.delete_key(key, recursive)
end
Chef::Log.trace("Registry key #{key_path} deleted")
true
end
|
#delete_value(key_path, value) ⇒ Object
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/chef/win32/registry.rb', line 88
def delete_value(key_path, value)
Chef::Log.trace("Deleting value #{value[:name]} from registry key #{key_path}")
if value_exists?(key_path, value)
begin
hive, key = get_hive_and_key(key_path)
rescue Chef::Exceptions::Win32RegKeyMissing
return true
end
hive.open(key, ::Win32::Registry::KEY_SET_VALUE | registry_system_architecture) do |reg|
reg.delete_value(value[:name])
Chef::Log.trace("Deleted value #{value[:name]} from registry key #{key_path}")
end
else
Chef::Log.trace("Value #{value[:name]} in registry key #{key_path} does not exist, not updated")
end
true
end
|
#get_name_from_type(val_class) ⇒ Object
276
277
278
|
# File 'lib/chef/win32/registry.rb', line 276
def get_name_from_type(val_class)
_name_type_map[val_class]
end
|
#get_subkeys(key_path) ⇒ Object
187
188
189
190
191
192
193
194
195
|
# File 'lib/chef/win32/registry.rb', line 187
def get_subkeys(key_path)
subkeys = []
key_exists!(key_path)
hive, key = get_hive_and_key(key_path)
hive.open(key, ::Win32::Registry::KEY_READ | registry_system_architecture) do |reg|
reg.each_key { |current_key| subkeys << current_key }
end
subkeys
end
|
#get_type_from_name(val_type) ⇒ Object
272
273
274
|
# File 'lib/chef/win32/registry.rb', line 272
def get_type_from_name(val_type)
_type_name_map[val_type]
end
|
#get_values(key_path) ⇒ Object
55
56
57
58
59
60
61
|
# File 'lib/chef/win32/registry.rb', line 55
def get_values(key_path)
hive, key = get_hive_and_key(key_path)
key_exists!(key_path)
values = hive.open(key, ::Win32::Registry::KEY_READ | registry_system_architecture) do |reg|
reg.map { |name, type, data| { name: name, type: get_name_from_type(type), data: data } }
end
end
|
#has_subkeys?(key_path) ⇒ Boolean
178
179
180
181
182
183
184
185
|
# File 'lib/chef/win32/registry.rb', line 178
def has_subkeys?(key_path)
key_exists!(key_path)
hive, key = get_hive_and_key(key_path)
hive.open(key, ::Win32::Registry::KEY_READ | registry_system_architecture) do |reg|
reg.each_key { |key| return true }
end
false
end
|
#hive_exists?(key_path) ⇒ Boolean
169
170
171
172
173
174
175
176
|
# File 'lib/chef/win32/registry.rb', line 169
def hive_exists?(key_path)
begin
hive, key = get_hive_and_key(key_path)
rescue Chef::Exceptions::Win32RegHiveMissing => e
return false
end
true
end
|
#key_exists!(key_path) ⇒ Object
161
162
163
164
165
166
167
|
# File 'lib/chef/win32/registry.rb', line 161
def key_exists!(key_path)
unless key_exists?(key_path)
raise Chef::Exceptions::Win32RegKeyMissing, "Registry key #{key_path} does not exist"
end
true
end
|
#key_exists?(key_path) ⇒ Boolean
150
151
152
153
154
155
156
157
158
159
|
# File 'lib/chef/win32/registry.rb', line 150
def key_exists?(key_path)
hive, key = get_hive_and_key(key_path)
begin
hive.open(key, ::Win32::Registry::KEY_READ | registry_system_architecture) do |current_key|
return true
end
rescue ::Win32::Registry::Error => e
return false
end
end
|
#keys_missing?(key_path) ⇒ Boolean
265
266
267
268
269
270
|
# File 'lib/chef/win32/registry.rb', line 265
def keys_missing?(key_path)
missing_key_arr = key_path.split("\\")
missing_key_arr.pop
key = missing_key_arr.join("\\")
!key_exists?(key)
end
|
#registry_system_architecture ⇒ Object
32-bit chef clients running on 64-bit machines will default to reading the 64-bit registry
198
199
200
201
|
# File 'lib/chef/win32/registry.rb', line 198
def registry_system_architecture
applied_arch = ( architecture == :machine ) ? machine_architecture : architecture
( applied_arch == :x86_64 ) ? 0x0100 : 0x0200
end
|
#set_value(key_path, value) ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/chef/win32/registry.rb', line 63
def set_value(key_path, value)
data = value[:data]
data = data.to_s if value[:type] == :string
Chef::Log.trace("Updating value #{value[:name]} in registry key #{key_path} with type #{value[:type]} and data #{data}")
key_exists!(key_path)
hive, key = get_hive_and_key(key_path)
if value_exists?(key_path, value)
if data_exists?(key_path, value)
Chef::Log.trace("Value #{value[:name]} in registry key #{key_path} already had those values, not updated")
return false
else
hive.open(key, ::Win32::Registry::KEY_SET_VALUE | ::Win32::Registry::KEY_QUERY_VALUE | registry_system_architecture) do |reg|
reg.write(value[:name], get_type_from_name(value[:type]), data)
end
Chef::Log.trace("Value #{value[:name]} in registry key #{key_path} updated")
end
else
hive.open(key, ::Win32::Registry::KEY_SET_VALUE | ::Win32::Registry::KEY_QUERY_VALUE | registry_system_architecture) do |reg|
reg.write(value[:name], get_type_from_name(value[:type]), data)
end
Chef::Log.trace("Value #{value[:name]} in registry key #{key_path} created")
end
true
end
|
#type_matches!(key_path, value) ⇒ Object
259
260
261
262
263
|
# File 'lib/chef/win32/registry.rb', line 259
def type_matches!(key_path, value)
unless type_matches?(key_path, value)
raise Chef::Exceptions::Win32RegTypesMismatch, "Registry key #{key_path} has a value #{value[:name]} with a type that is not #{value[:type]}"
end
end
|
#type_matches?(key_path, value) ⇒ Boolean
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
# File 'lib/chef/win32/registry.rb', line 243
def type_matches?(key_path, value)
value_exists!(key_path, value)
hive, key = get_hive_and_key(key_path)
hive.open(key, ::Win32::Registry::KEY_READ | registry_system_architecture) do |reg|
reg.each do |val_name, val_type|
if val_name == value[:name]
type_new = get_type_from_name(value[:type])
if val_type == type_new
return true
end
end
end
end
false
end
|
#value_exists!(key_path, value) ⇒ Object
227
228
229
230
231
232
233
|
# File 'lib/chef/win32/registry.rb', line 227
def value_exists!(key_path, value)
unless value_exists?(key_path, value)
raise Chef::Exceptions::Win32RegValueMissing, "Registry key #{key_path} has no value named #{value[:name]}"
end
true
end
|
#value_exists?(key_path, value) ⇒ Boolean
203
204
205
206
207
208
209
210
|
# File 'lib/chef/win32/registry.rb', line 203
def value_exists?(key_path, value)
key_exists!(key_path)
hive, key = get_hive_and_key(key_path)
hive.open(key, ::Win32::Registry::KEY_READ | registry_system_architecture) do |reg|
return true if reg.any? { |val| safely_downcase(val) == safely_downcase(value[:name]) }
end
false
end
|