Skip to content

Commit 40f3098

Browse files
(maint) Merge up f9496c6 to main
Generated by CI * commit 'f9496c6fb1fdf6635c81b5587498ca7250399dfc': (PUP-11763) Converts ctime/mtime to string (packaging) Updating manpage file for 7.x (PUP-11691) Report configured environmentpath when a symlink
2 parents 977a019 + f9496c6 commit 40f3098

File tree

11 files changed

+127
-4
lines changed

11 files changed

+127
-4
lines changed

lib/puppet/defaults.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,19 @@ def self.initialize_default_settings!(settings)
421421
<https://puppet.com/docs/puppet/latest/environments_about.html>",
422422
:type => :path,
423423
},
424+
:report_configured_environmentpath => {
425+
:type => :boolean,
426+
:default => true,
427+
:desc => <<-'EOT'
428+
When versioned_environment_dirs is `true` Puppet will readlink the environmentpath
429+
when constructing the environment's modulepath. The full readlinked path is referred
430+
to as the "resolved path" and the configured path potentially containing symlinks is
431+
the "configured path". When reporting where resources come from users may choose
432+
between the configured or resolved path.
433+
434+
When set to false, the resolved paths are reported instead of the configured paths.
435+
EOT
436+
},
424437
:use_last_environment => {
425438
:type => :boolean,
426439
:default => true,

lib/puppet/environments.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,15 @@ def create_environment(name)
243243
setting_values.interpolate(:manifest),
244244
setting_values.interpolate(:config_version)
245245
)
246+
247+
configured_path = File.join(@environment_dir, name.to_s)
248+
env.configured_path = configured_path
249+
if Puppet.settings[:report_configured_environmentpath]
250+
env.resolved_path = validated_directory(configured_path)
251+
else
252+
env.resolved_path = configured_path
253+
end
254+
246255
env
247256
end
248257

lib/puppet/node/environment.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,44 @@ def full_modulepath
166166
# @api private
167167
attr_reader :lock
168168

169+
# For use with versioned dirs
170+
# our environment path may contain symlinks, while we want to resolve the
171+
# path while reading the manifests we may want to report the resources as
172+
# coming from the configured path.
173+
attr_accessor :configured_path
174+
175+
# See :configured_path above
176+
attr_accessor :resolved_path
177+
178+
# Ensure the path given is of the format we want in the catalog/report.
179+
#
180+
# Intended for use with versioned symlinked environments. If this
181+
# environment is configured with "/etc/puppetlabs/code/environments/production"
182+
# but the resolved path is
183+
#
184+
# "/opt/puppetlabs/server/puppetserver/filesync/client/puppet-code/production_abcdef1234"
185+
#
186+
# this changes the filepath
187+
#
188+
# "/opt/puppetlabs/server/puppetserver/filesync/client/puppet-code/production_abcdef1234/modules/foo/manifests/init.pp"
189+
#
190+
# to
191+
#
192+
# "/etc/puppetlabs/code/environments/production/modules/foo/manifests/init.pp"
193+
def externalize_path(filepath)
194+
paths_set = configured_path && resolved_path
195+
munging_possible = paths_set && configured_path != resolved_path
196+
munging_desired = munging_possible &&
197+
Puppet[:report_configured_environmentpath] &&
198+
filepath.to_s.start_with?(resolved_path)
199+
200+
if munging_desired
201+
File.join(configured_path, filepath.delete_prefix(resolved_path))
202+
else
203+
filepath
204+
end
205+
end
206+
169207
# Checks to make sure that this environment did not have a manifest set in
170208
# its original environment.conf if Puppet is configured with
171209
# +disable_per_environment_manifest+ set true. If it did, the environment's

lib/puppet/resource.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,10 @@ def initialize(type, title = nil, attributes = EMPTY_HASH)
323323
send(attr.to_s + "=", value)
324324
end
325325

326+
if environment.is_a?(Puppet::Node::Environment) && environment != Puppet::Node::Environment::NONE
327+
self.file = environment.externalize_path(attributes[:file])
328+
end
329+
326330
@type, @title = self.class.type_and_title(type, title)
327331

328332
rt = resource_type

lib/puppet/type/file/ctime.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def retrieve
1010
if stat
1111
current_value = stat.ctime
1212
end
13-
current_value
13+
current_value.to_s
1414
end
1515

1616
validate do |val|

lib/puppet/type/file/mtime.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def retrieve
99
if stat
1010
current_value = stat.mtime
1111
end
12-
current_value
12+
current_value.to_s
1313
end
1414

1515
validate do |val|

spec/unit/environments_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,15 @@ def remove
115115
end
116116
end
117117

118+
it "sets the environment's configured and resolved paths set when symlinked" do
119+
loader_from(:filesystem => [directory_tree],
120+
:directory => directory_tree.children.first) do |loader|
121+
env = loader.get("symlinked_environment")
122+
expect(env.resolved_path).to eq("#{FS.path_string(directory_tree)}/versioned_env")
123+
expect(env.configured_path).to eq("#{FS.path_string(directory_tree)}/envdir/symlinked_environment")
124+
end
125+
end
126+
118127
it "ignores symlinked environments when `:versioned_environment_dirs` is false" do
119128
Puppet[:versioned_environment_dirs] = false
120129
loader_from(:filesystem => [directory_tree],

spec/unit/node/environment_spec.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,47 @@
4040
expect(e.inspect).to match(%r{<Puppet::Node::Environment:\w* @name="test" @manifest="#{File.expand_path('/manifests/path')}" @modulepath="#{File.expand_path('/modules/path')}:#{File.expand_path('/other/modules')}" >})
4141
end
4242

43+
describe "externalizing filepaths" do
44+
before(:each) do
45+
env.resolved_path = "/opt/puppetlabs/envs/prod_123"
46+
env.configured_path = "/etc/puppetlabs/envs/prod"
47+
48+
@vendored_manifest = "/opt/puppetlabs/vendored/modules/foo/manifests/init.pp"
49+
@production_manifest = "/opt/puppetlabs/envs/prod_123/modules/foo/manifests/init.pp"
50+
end
51+
52+
it "leaves paths alone if they do not match the resolved path" do
53+
expect(env.externalize_path(@vendored_manifest)).to eq(@vendored_manifest)
54+
end
55+
56+
it "leaves paths alone if resolved or configured paths are not set" do
57+
env.resolved_path = nil
58+
env.configured_path = nil
59+
expect(env.externalize_path(@production_manifest)).to eq(@production_manifest)
60+
end
61+
62+
it "replaces resolved paths with configured paths" do
63+
externalized_path = env.externalize_path(@production_manifest)
64+
expect(externalized_path).to eq("/etc/puppetlabs/envs/prod/modules/foo/manifests/init.pp")
65+
end
66+
67+
it "handles nil" do
68+
externalized_path = env.externalize_path(nil)
69+
expect(externalized_path).to eq(nil)
70+
end
71+
72+
it "appropriately handles mismatched trailing slashes" do
73+
env.resolved_path = "/opt/puppetlabs/envs/prod_123/"
74+
externalized_path = env.externalize_path(@production_manifest)
75+
expect(externalized_path).to eq("/etc/puppetlabs/envs/prod/modules/foo/manifests/init.pp")
76+
end
77+
78+
it "can be disabled with the `report_configured_environmentpath` setting" do
79+
Puppet[:report_configured_environmentpath] = false
80+
expect(env.externalize_path(@production_manifest)).to eq(@production_manifest)
81+
end
82+
end
83+
4384
describe "equality" do
4485
it "works as a hash key" do
4586
base = Puppet::Node::Environment.create(:first, ["modules"], "manifests")

spec/unit/parser/resource_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ def newnode(name)
108108
}.to raise_error(ArgumentError, /Resources require a hash as last argument/)
109109
end
110110

111+
it "should attempt to externalize filepaths via the environment" do
112+
environment = Puppet::Node::Environment.create(:testing, [])
113+
expect(environment).to receive(:externalize_path).at_least(:once).and_return("foo")
114+
Puppet[:code] = "notify { 'hello': }"
115+
catalog = Puppet::Parser::Compiler.compile(Puppet::Node.new 'anyone', environment: environment)
116+
notify = catalog.resource('Notify[hello]')
117+
expect(notify.file).to eq("foo")
118+
end
119+
111120
it "should set the reference correctly" do
112121
res = Puppet::Parser::Resource.new("resource", "testing", @arguments)
113122
expect(res.ref).to eq("Resource[testing]")

spec/unit/type/file/ctime_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
@resource[:audit] = [:ctime]
1616

1717
# this .to_resource audit behavior is magical :-(
18-
expect(@resource.to_resource[:ctime]).to eq(Puppet::FileSystem.stat(@filename).ctime)
18+
expect(@resource.to_resource[:ctime]).to eq(Puppet::FileSystem.stat(@filename).ctime.to_s)
1919
end
2020

2121
it "should return absent if auditing an absent file" do

0 commit comments

Comments
 (0)