Puppet::Environments::Cached#list returns environments that exist on disk and either caches the entry or updates its ttl. However, if an environment is in the cache, but is no longer on disk, then it is not removed from the cache. This shouldn't be an issue with r10k/code manager, because they explicitly delete the environment or all environments after purging the directory. But it could lead to consistency problems if the REST request never occurs or fails. The following shows how after the environment is deleted, it is not returned from the list method, yet it is still cached:
require 'puppet'
|
Puppet.initialize_settings(['--environment_timeout', 'unlimited'])
|
Puppet::Util::Log.newdestination(:console)
|
|
name = "test#{Process.pid}"
|
testdir = File.join(Puppet[:environmentpath], name)
|
FileUtils.mkdir_p(testdir)
|
|
Puppet::ApplicationSupport.push_application_context(Puppet::Util::RunMode[:user])
|
|
envs = Puppet.lookup(:environments)
|
envs.get!(:production)
|
envs.get!(name)
|
|
puts "Listed #{envs.list.map(&:name).join(', ')} environments"
|
|
FileUtils.rm_rf(testdir)
|
puts "Deleted #{name} environment"
|
|
puts "Listed #{envs.list.map(&:name).join(', ')} environments"
|
puts "Cached #{envs.instance_variable_get(:@cache).keys.join(', ')} environments"
|
❯ ls ~/.puppetlabs/etc/code/environments/
|
production/
|
|
❯ bx ruby envs.rb
|
Listed test2597598, production environments
|
Deleted test2597598 environment
|
Listed production environments
|
Cached production, test2597598 environments
|