Skip to content

Commit 0ee6b2b

Browse files
author
Kevin Paulisse
committed
Allow different directory names
1 parent f74cebc commit 0ee6b2b

5 files changed

Lines changed: 34 additions & 19 deletions

File tree

lib/octocatalog-diff/catalog-diff/cli/catalogs.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def add_parallel_result(result, parallel_catalog_obj, key_task_tuple)
198198
# and remove the compilation directory (which is a tmpdir) to reveal only the relative
199199
# path to the files involved.
200200
dir = catalog.compilation_dir || ''
201-
dir_regex = Regexp.new(Regexp.escape(dir) + '/environments/production/')
201+
dir_regex = Regexp.new(Regexp.escape(dir) + '/environments/[^/]+/')
202202
error_display = catalog.error_message.split("\n").map do |line|
203203
line.sub(/^Error:/, '[Puppet Error]').gsub(dir_regex, '')
204204
end.join("\n")

lib/octocatalog-diff/catalog-util/enc/script.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def execute(logger)
8181
def script_path(enc, tempdir)
8282
return enc if enc.start_with? '/'
8383
raise ArgumentError, 'OctocatalogDiff::CatalogUtil::ENC::Script#new requires :tempdir' unless tempdir.is_a?(String)
84-
return File.join(tempdir, enc) if enc =~ %r{^environments/production/}
84+
return File.join(tempdir, enc) if enc =~ %r{^environments/[^/]+/}
8585
File.join(tempdir, 'environments', 'production', enc)
8686
end
8787
end

lib/octocatalog-diff/catalog-util/fileresources.rb

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ class FileResources
1313
# Public method: Convert file resources to text. See the description of the class
1414
# just above for details.
1515
# @param obj [OctocatalogDiff::Catalog] Catalog object (will be modified)
16-
def self.convert_file_resources(obj)
16+
def self.convert_file_resources(obj, environment = 'production')
1717
return unless obj.valid? && obj.compilation_dir.is_a?(String) && !obj.compilation_dir.empty?
18-
_convert_file_resources(obj.resources, obj.compilation_dir)
18+
_convert_file_resources(obj.resources, obj.compilation_dir, environment)
1919
begin
2020
obj.catalog_json = ::JSON.generate(obj.catalog)
2121
rescue ::JSON::GeneratorError => exc
@@ -45,36 +45,39 @@ def self.file_path(src, modulepaths)
4545
end
4646

4747
# Internal method: Parse environment.conf to find the modulepath
48-
# @param compilation_dir [String] Compilation directory
48+
# @param dir [String] Directory in which to look for environment.conf
4949
# @return [Array] Module paths
50-
def self.module_path(compilation_dir)
51-
environment_conf = File.join(compilation_dir, 'environment.conf')
52-
unless File.file?(environment_conf)
53-
return [File.join(compilation_dir, 'modules')]
54-
end
50+
def self.module_path(dir)
51+
environment_conf = File.join(dir, 'environment.conf')
52+
return [File.join(dir, 'modules')] unless File.file?(environment_conf)
5553

5654
# This doesn't support multi-line, continuations with backslash, etc.
5755
# Does it need to??
5856
if File.read(environment_conf) =~ /^modulepath\s*=\s*(.+)/
59-
Regexp.last_match(1).split(/:/).map(&:strip).reject { |x| x =~ /^\$/ }.map { |x| File.join(compilation_dir, x) }
57+
result = []
58+
Regexp.last_match(1).split(/:/).map(&:strip).each do |path|
59+
next if path.start_with?('$')
60+
result << File.expand_path(path, dir)
61+
end
62+
result
6063
else
61-
[File.join(compilation_dir, 'modules')]
64+
[File.join(dir, 'modules')]
6265
end
6366
end
6467

6568
# Internal method: Static method to convert file resources. The compilation directory is
6669
# required, or else this is a no-op. The passed-in array of resources is modified by this method.
6770
# @param resources [Array<Hash>] Array of catalog resources
6871
# @param compilation_dir [String] Compilation directory (so files can be looked up)
69-
def self._convert_file_resources(resources, compilation_dir)
72+
def self._convert_file_resources(resources, compilation_dir, environment = 'production')
7073
# Calculate compilation directory. There is not explicit error checking here because
7174
# there is on-demand, explicit error checking for each file within the modification loop.
7275
return unless compilation_dir.is_a?(String) && compilation_dir != ''
7376

74-
# Making sure that compilation_dir/environments/production/modules exists (and by inference,
75-
# that compilation_dir/environments/production is pointing at the right place). Otherwise, try to find
77+
# Making sure that compilation_dir/environments/<env>/modules exists (and by inference,
78+
# that compilation_dir/environments/<env> is pointing at the right place). Otherwise, try to find
7679
# compilation_dir/modules. If neither of those exist, this code can't run.
77-
env_dir = File.join(compilation_dir, 'environments', 'production')
80+
env_dir = File.join(compilation_dir, 'environments', environment)
7881
modulepaths = module_path(env_dir) + module_path(compilation_dir)
7982
modulepaths.select! { |x| File.directory?(x) }
8083
return if modulepaths.empty?

lib/octocatalog-diff/catalog.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,18 @@ def build(logger = Logger.new(StringIO.new))
7474
# Perform post-generation processing of the catalog
7575
return unless valid?
7676
unless @catalog_obj.respond_to?(:convert_file_resources) && @catalog_obj.convert_file_resources == false
77-
OctocatalogDiff::CatalogUtil::FileResources.convert_file_resources(self) if @options.fetch(:compare_file_text, false)
77+
if @options.fetch(:compare_file_text, false)
78+
OctocatalogDiff::CatalogUtil::FileResources.convert_file_resources(self, environment)
79+
end
7880
end
7981
end
8082

83+
# Compilation environment
84+
# @return [String] Compilation environment (if set), else 'production' by default
85+
def environment
86+
@catalog_obj.respond_to?(:environment) ? @catalog_obj.environment : 'production'
87+
end
88+
8189
# For logging we may wish to know the backend being used
8290
# @return [String] Class of backend used
8391
def builder

lib/octocatalog-diff/catalog/computed.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ def compilation_dir
7878
@builddir.tempdir
7979
end
8080

81+
# Environment used to compile catalog
82+
def environment
83+
@opts[:preserve_environments] ? @opts.fetch(:environment, 'production') : 'production'
84+
end
85+
8186
private
8287

8388
# Private method: Clean up a checkout directory, if it exists
@@ -183,8 +188,7 @@ def exec_puppet
183188

184189
# Private method: Make sure that the Puppet environment directory exists.
185190
def assert_that_puppet_environment_directory_exists
186-
environ = @opts[:preserve_environments] ? @opts.fetch(:environment, 'production') : 'production'
187-
target_dir = File.join(@builddir.tempdir, 'environments', environ)
191+
target_dir = File.join(@builddir.tempdir, 'environments', environment)
188192
return if File.directory?(target_dir)
189193
raise Errno::ENOENT, "Environment directory #{target_dir} does not exist"
190194
end

0 commit comments

Comments
 (0)