@@ -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
0 commit comments