Skip to content

Commit 5263d00

Browse files
authored
Merge pull request #20 from github/kpaulisse-modulepath
Use modulepath from environment.conf if available
2 parents 68766a4 + 583a419 commit 5263d00

14 files changed

Lines changed: 364 additions & 13 deletions

File tree

.version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.5.4
1+
0.5.6

doc/CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,22 @@
88
</tr>
99
</thead><tbody>
1010
<tr valign=top>
11+
<td>0.5.6</td>
12+
<td>2016-11-16</td>
13+
<td>
14+
<ul>
15+
<li><a href="https://github.com/github/octocatalog-diff/pull/20">https://github.com/github/octocatalog-diff/pull/20</a>: Use modulepath from environment.conf to inform lookup directories for <code>--compare-file-text</code> feature</li>
16+
</ul>
17+
</td>
18+
</tr>
19+
<tr valign=top>
20+
<td>0.5.5</td>
21+
<td>-</td>
22+
<td>
23+
Unreleased internal version
24+
</td>
25+
</tr>
26+
<tr valign=top>
1127
<td>0.5.4</td>
1228
<td>2016-11-07</td>
1329
<td>

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

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,45 @@ def self.convert_file_resources(obj)
2121
end
2222
end
2323

24+
# Internal method: Locate a file that is referenced at puppet:///modules/xxx/yyy using the
25+
# module path that is specified within the environment.conf file (assuming the default 'modules'
26+
# directory doesn't exist or the module isn't found in there). If the file can't be found then
27+
# this returns nil which may trigger an error.
28+
# @param src [String] A file reference: puppet:///modules/xxx/yyy
29+
# @param modulepaths [Array] Cached module path
30+
# @return [String] File system path to referenced file
31+
def self.file_path(src, modulepaths)
32+
unless src =~ %r{^puppet:///modules/([^/]+)/(.+)}
33+
raise ArgumentError, "Bad parameter source #{src}"
34+
end
35+
36+
path = File.join(Regexp.last_match(1), 'files', Regexp.last_match(2))
37+
modulepaths.each do |mp|
38+
file = File.join(mp, path)
39+
return file if File.exist?(file)
40+
end
41+
42+
nil
43+
end
44+
45+
# Internal method: Parse environment.conf to find the modulepath
46+
# @param compilation_dir [String] Compilation directory
47+
# @return [Array] Module paths
48+
def self.module_path(compilation_dir)
49+
environment_conf = File.join(compilation_dir, 'environment.conf')
50+
unless File.file?(environment_conf)
51+
return [File.join(compilation_dir, 'modules')]
52+
end
53+
54+
# This doesn't support multi-line, continuations with backslash, etc.
55+
# Does it need to??
56+
if File.read(environment_conf) =~ /^modulepath\s*=\s*(.+)/
57+
Regexp.last_match(1).split(/:/).map(&:strip).reject { |x| x =~ /^\$/ }.map { |x| File.join(compilation_dir, x) }
58+
else
59+
[File.join(compilation_dir, 'modules')]
60+
end
61+
end
62+
2463
# Internal method: Static method to convert file resources. The compilation directory is
2564
# required, or else this is a no-op. The passed-in array of resources is modified by this method.
2665
# @param resources [Array<Hash>] Array of catalog resources
@@ -34,18 +73,15 @@ def self._convert_file_resources(resources, compilation_dir)
3473
# that compilation_dir/environments/production is pointing at the right place). Otherwise, try to find
3574
# compilation_dir/modules. If neither of those exist, this code can't run.
3675
env_dir = File.join(compilation_dir, 'environments', 'production')
37-
unless File.directory?(File.join(env_dir, 'modules'))
38-
return unless File.directory?(File.join(compilation_dir, 'modules'))
39-
env_dir = compilation_dir
40-
end
76+
modulepaths = module_path(env_dir) + module_path(compilation_dir)
77+
modulepaths.select! { |x| File.directory?(x) }
78+
return if modulepaths.empty?
4179

42-
# Modify the resources
80+
# At least one existing module path was found! Run the code to modify the resources.
4381
resources.map! do |resource|
4482
if resource_convertible?(resource)
45-
# Parse the 'source' parameter into a file on disk
46-
src = resource['parameters']['source']
47-
raise "Bad parameter source #{src}" unless src =~ %r{^puppet:///modules/([^/]+)/(.+)}
48-
path = File.join(env_dir, 'modules', Regexp.last_match(1), 'files', Regexp.last_match(2))
83+
path = file_path(resource['parameters']['source'], modulepaths)
84+
raise Errno::ENOENT, "Unable to resolve '#{resource['parameters']['source']}'!" if path.nil?
4985

5086
if File.file?(path)
5187
# If the file is found, read its content. If the content is all ASCII, substitute it into
@@ -60,7 +96,10 @@ def self._convert_file_resources(resources, compilation_dir)
6096
# However, the fact that we found *something* at this location indicates that the catalog
6197
# is probably correct. Hence, the very general .exist? check.
6298
else
63-
raise Errno::ENOENT, "Unable to find '#{src}' at #{path}!"
99+
# This is probably a bug
100+
# :nocov:
101+
raise "Unable to find '#{resource['parameters']['source']}' at #{path}!"
102+
# :nocov:
64103
end
65104
end
66105
resource
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"document_type": "Catalog",
3+
"tags": ["settings","test"],
4+
"name": "my.rspec.node",
5+
"version": "production",
6+
"environment": "production",
7+
"resources": [
8+
{
9+
"type": "Stage",
10+
"title": "main",
11+
"tags": ["stage"],
12+
"exported": false,
13+
"parameters": {
14+
"name": "main"
15+
}
16+
},
17+
{
18+
"type": "Class",
19+
"title": "Settings",
20+
"tags": ["class","settings"],
21+
"exported": false
22+
},
23+
{
24+
"type": "File",
25+
"title": "/tmp/foo",
26+
"tags": ["file","class"],
27+
"file": "/x/modules/modulestest/manifests/init.pp",
28+
"line": 37,
29+
"exported": false,
30+
"parameters": {
31+
"backup": false,
32+
"mode": "0440",
33+
"owner": "root",
34+
"group": "root",
35+
"source": "puppet:///modules/modulestest/tmp/modulestest"
36+
}
37+
},
38+
{
39+
"type": "File",
40+
"title": "/tmp/foobaz",
41+
"tags": ["file","class"],
42+
"file": "/x/modules/modulestest/manifests/init.pp",
43+
"line": 37,
44+
"exported": false,
45+
"parameters": {
46+
"backup": false,
47+
"ensure": "directory",
48+
"mode": "0755",
49+
"owner": "root",
50+
"group": "root",
51+
"source": "puppet:///modules/modulestest/foo"
52+
}
53+
}
54+
],
55+
"classes": [
56+
"test"
57+
]
58+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
modulepath=modules:site:$basemodulepath
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node default {
2+
include modulestest
3+
include sitetest
4+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Hi
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Modules Test
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class modulestest {
2+
file { '/tmp/modulestest':
3+
source => 'puppet:///modules/modulestest/tmp/modulestest',
4+
}
5+
6+
file { '/tmp/foobaz':
7+
ensure => directory,
8+
source => 'puppet:///modules/modulestest/foo',
9+
recurse => true,
10+
}
11+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Site Test

0 commit comments

Comments
 (0)