Class: Controlrepo
- Inherits:
-
Object
show all
- Defined in:
- lib/controlrepo.rb,
lib/controlrepo/node.rb,
lib/controlrepo/test.rb,
lib/controlrepo/class.rb,
lib/controlrepo/group.rb,
lib/controlrepo/beaker.rb,
lib/controlrepo/testconfig.rb
Defined Under Namespace
Classes: Beaker, Class, Group, Node, Test, TestConfig
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(search_path = Dir.pwd) ⇒ Controlrepo
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
|
# File 'lib/controlrepo.rb', line 77
def initialize(search_path = Dir.pwd)
begin
until File.exist?(File.expand_path('./Puppetfile',search_path)) do
search_path = File.expand_path('..',search_path)
end
rescue => e
raise " Could not find Puppetfile"
raise e
end
@root = search_path
@environmentpath = 'etc/puppetlabs/code/environments'
@puppetfile = File.expand_path('./Puppetfile',@root)
@environment_conf = File.expand_path('./environment.conf',@root)
@facts_dir = File.expand_path('./spec/factsets',@root)
@spec_dir = File.expand_path('./spec',@root)
@facts_files = [Dir["#{@facts_dir}/*.json"],Dir["#{File.expand_path('../../factsets',__FILE__)}/*.json"]].flatten
@nodeset_file = File.expand_path('./spec/acceptance/nodesets/controlrepo-nodes.yml',@root)
@role_regex = /role[s]?:{2}/
@profile_regex = /profile[s]?:{2}/
@tempdir = nil || ENV['CONTROLREPO_temp']
$temp_modulepath = nil
@manifest = config['manifest'] ? File.expand_path(config['manifest'],@root) : nil
end
|
Instance Attribute Details
#environmentpath ⇒ Object
Returns the value of attribute environmentpath.
19
20
21
|
# File 'lib/controlrepo.rb', line 19
def environmentpath
@environmentpath
end
|
#facts_files ⇒ Object
Returns the value of attribute facts_files.
18
19
20
|
# File 'lib/controlrepo.rb', line 18
def facts_files
@facts_files
end
|
#manifest ⇒ Object
Returns the value of attribute manifest.
25
26
27
|
# File 'lib/controlrepo.rb', line 25
def manifest
@manifest
end
|
#nodeset_file ⇒ Object
Returns the value of attribute nodeset_file.
24
25
26
|
# File 'lib/controlrepo.rb', line 24
def nodeset_file
@nodeset_file
end
|
#profile_regex ⇒ Object
Returns the value of attribute profile_regex.
21
22
23
|
# File 'lib/controlrepo.rb', line 21
def profile_regex
@profile_regex
end
|
#puppetfile ⇒ Object
Returns the value of attribute puppetfile.
17
18
19
|
# File 'lib/controlrepo.rb', line 17
def puppetfile
@puppetfile
end
|
#role_regex ⇒ Object
Returns the value of attribute role_regex.
20
21
22
|
# File 'lib/controlrepo.rb', line 20
def role_regex
@role_regex
end
|
#root ⇒ Object
Returns the value of attribute root.
16
17
18
|
# File 'lib/controlrepo.rb', line 16
def root
@root
end
|
#spec_dir ⇒ Object
Returns the value of attribute spec_dir.
22
23
24
|
# File 'lib/controlrepo.rb', line 22
def spec_dir
@spec_dir
end
|
#temp_modulepath ⇒ Object
Returns the value of attribute temp_modulepath.
23
24
25
|
# File 'lib/controlrepo.rb', line 23
def temp_modulepath
@temp_modulepath
end
|
Class Method Details
.classes ⇒ Object
50
51
52
|
# File 'lib/controlrepo.rb', line 50
def self.classes
Controlrepo.new.classes
end
|
.config ⇒ Object
62
63
64
|
# File 'lib/controlrepo.rb', line 62
def self.config
Controlrepo.new.config
end
|
.facts(filter = nil) ⇒ Object
66
67
68
|
# File 'lib/controlrepo.rb', line 66
def self.facts(filter = nil)
Controlrepo.new.facts(filter)
end
|
.facts_files ⇒ Object
46
47
48
|
# File 'lib/controlrepo.rb', line 46
def self.facts_files
Controlrepo.new.facts_files
end
|
.hiera_config_file ⇒ Object
70
71
72
|
# File 'lib/controlrepo.rb', line 70
def self.hiera_config_file
Controlrepo.new.hiera_config_file
end
|
.profiles ⇒ Object
58
59
60
|
# File 'lib/controlrepo.rb', line 58
def self.profiles
Controlrepo.new.profiles
end
|
.puppetfile ⇒ Object
42
43
44
|
# File 'lib/controlrepo.rb', line 42
def self.puppetfile
Controlrepo.new.puppetfile
end
|
.roles ⇒ Object
54
55
56
|
# File 'lib/controlrepo.rb', line 54
def self.roles
Controlrepo.new.roles
end
|
.root ⇒ Object
Create methods on self so that we can access these basic things without having to actually instantiate the class, I’m debating how much stuff I should be putting in here, we don’t reeeally need to instantiate the object unless we want to modify it’s parameters, so maybe everything. We shall see…
And yeah I know this makes little sense, but it will look nicer to type, promise
Also it’s probably pretty memory hungry, but let’s be honest, how many times would be be calling this? If we call it over and over you can just instantiate it anyway
Instance Method Details
#classes ⇒ Object
138
139
140
141
142
143
144
145
146
147
148
149
150
|
# File 'lib/controlrepo.rb', line 138
def classes
code_dirs = self.config['modulepath']
code_dirs.delete_if { |dir| dir[0] == '$'}
classes = []
code_dirs.each do |dir|
classes << get_classes(dir)
end
classes.flatten
end
|
#config ⇒ Object
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
# File 'lib/controlrepo.rb', line 266
def config
env_conf = File.read(@environment_conf)
env_conf = env_conf.split("\n")
env_conf.delete_if { |l| l =~ /^\s*#/}
environment_config = {}
env_conf.each do |line|
environment_config.merge!(Hash[*line.split('=').map { |s| s.strip}])
end
begin
environment_config['modulepath'] = environment_config['modulepath'].split(':')
rescue
raise "modulepath was not found in environment.conf, don't know where to look for roles & profiles"
end
return environment_config
end
|
#facts(filter = nil) ⇒ Object
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
# File 'lib/controlrepo.rb', line 152
def facts(filter = nil)
all_facts = []
@facts_files.each do |file|
all_facts << read_facts(file)['values']
end
if filter
raise "Filter param must be a hash" unless filter.is_a?(Hash)
all_facts.keep_if do |hash|
matches = []
filter.each do |filter_fact,value|
matches << keypair_is_in_hash(hash,filter_fact,value)
end
if matches.include? false
false
else
true
end
end
end
return all_facts
end
|
#fixtures ⇒ Object
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
# File 'lib/controlrepo.rb', line 176
def fixtures
puppetfile = R10K::Puppetfile.new(@root)
modules = puppetfile.load
symlinks = []
forge_modules = []
repositories = []
modules.each do |mod|
if mod.is_a? R10K::Module::Forge
if mod.expected_version.is_a?(Hash)
symlinks << {
'name' => mod.name,
'dir' => mod.expected_version[:path]
}
elsif mod.expected_version.is_a?(String)
forge_modules << {
'name' => mod.name,
'repo' => mod.title,
'ref' => mod.expected_version
}
end
elsif mod.is_a? R10K::Module::Git
repositories << {
'name' => mod.name,
'repo' => mod.instance_variable_get(:@remote),
'ref' => mod.version
}
end
end
code_dirs = self.config['modulepath']
code_dirs.delete_if { |dir| dir[0] == '$'}
code_dirs.each do |dir|
Dir["#{dir}/*"].each do |mod|
symlinks << {
'name' => File.basename(mod),
'dir' => Pathname.new(File.expand_path(mod)).relative_path_from(Pathname.new(@root)) }
end
end
template_dir = File.expand_path('../templates',File.dirname(__FILE__))
fixtures_template = File.read(File.expand_path('./.fixtures.yml.erb',template_dir))
fixtures_yaml = ERB.new(fixtures_template, nil, '-').result(binding)
return fixtures_yaml
end
|
#hiera_config ⇒ Object
244
245
246
247
248
249
250
251
|
# File 'lib/controlrepo.rb', line 244
def hiera_config
begin
YAML.load_file(hiera_config_file)
rescue TypeError
puts "WARNING: Could not find hiera config file, continuing"
nil
end
end
|
#hiera_config=(data) ⇒ Object
253
254
255
|
# File 'lib/controlrepo.rb', line 253
def hiera_config=(data)
File.write(hiera_config_file,data.to_yaml)
end
|
#hiera_config_file ⇒ Object
237
238
239
240
241
242
|
# File 'lib/controlrepo.rb', line 237
def hiera_config_file
hiera_config_file = File.expand_path('./hiera.yaml',@spec_dir) if File.exist?(File.expand_path('./hiera.yaml',@spec_dir))
hiera_config_file = File.expand_path('./hiera.yaml',@root) if File.exist?(File.expand_path('./hiera.yaml',@root))
hiera_config_file
end
|
#hiera_data ⇒ Object
257
258
259
260
261
262
263
264
|
# File 'lib/controlrepo.rb', line 257
def hiera_data
possibe_datadirs = Dir["#{@root}/*/"]
possibe_datadirs.keep_if { |dir| dir =~ /hiera(?:.*data)?/i }
raise "There were too many directories that looked like hiera data: #{possibe_datadirs}" if possibe_datadirs.count > 1
File.expand_path(possibe_datadirs[0])
end
|
#profiles ⇒ Object
134
135
136
|
# File 'lib/controlrepo.rb', line 134
def profiles
classes.keep_if { |c| c =~ @profile_regex }
end
|
#r10k_config ⇒ Object
295
296
297
|
# File 'lib/controlrepo.rb', line 295
def r10k_config
YAML.load_file(r10k_config_file)
end
|
#r10k_config=(data) ⇒ Object
299
300
301
|
# File 'lib/controlrepo.rb', line 299
def r10k_config=(data)
File.write(r10k_config_file,data.to_yaml)
end
|
#r10k_config_file ⇒ Object
289
290
291
292
293
|
# File 'lib/controlrepo.rb', line 289
def r10k_config_file
r10k_config_file = File.expand_path('./r10k.yaml',@spec_dir) if File.exist?(File.expand_path('./r10k.yaml',@spec_dir))
r10k_config_file = File.expand_path('./r10k.yaml',@root) if File.exist?(File.expand_path('./r10k.yaml',@root))
r10k_config_file
end
|
#roles ⇒ Object
130
131
132
|
# File 'lib/controlrepo.rb', line 130
def roles
classes.keep_if { |c| c =~ @role_regex }
end
|
#temp_manifest ⇒ Object
303
304
305
|
# File 'lib/controlrepo.rb', line 303
def temp_manifest
config['manifest'] ? File.expand_path(config['manifest'],@tempdir) : nil
end
|
#tempdir ⇒ Object
116
117
118
119
120
121
122
123
124
|
# File 'lib/controlrepo.rb', line 116
def tempdir
if ENV['CONTROLREPO_temp'] == ''
return nil
elsif ENV['CONTROLREPO_temp'] == nil
return nil
else
return File.absolute_path(ENV['CONTROLREPO_temp'])
end
end
|
#tempdir=(dir) ⇒ Object
126
127
128
|
# File 'lib/controlrepo.rb', line 126
def tempdir=(dir)
ENV['CONTROLREPO_temp'] = dir
end
|
#to_s ⇒ Object
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/controlrepo.rb', line 103
def to_s
<<-END.gsub(/^\s{4}/,'')
puppetfile: #{@puppetfile}
environment_conf: #{@environment_conf}
facts_dir: #{@facts_dir}
spec_dir: #{@spec_dir}
facts_files: #{@facts_files}
nodeset_file: #{@nodeset_file}
roles: #{roles}
profiles: #{profiles}
END
end
|