Class: ACE::PluginCache

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/plugin_cache.rb

Constant Summary collapse

PURGE_TIMEOUT =
60 * 60
PURGE_INTERVAL =
24 * PURGE_TIMEOUT
PURGE_TTL =
7 * PURGE_INTERVAL

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environments_cache_dir, purge_interval: PURGE_INTERVAL, purge_timeout: PURGE_TIMEOUT, purge_ttl: PURGE_TTL, cache_dir_mutex: Concurrent::ReadWriteLock.new, do_purge: true) ⇒ PluginCache

Returns a new instance of PluginCache.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ace/plugin_cache.rb', line 17

def initialize(environments_cache_dir,
               purge_interval: PURGE_INTERVAL,
               purge_timeout: PURGE_TIMEOUT,
               purge_ttl: PURGE_TTL,
               cache_dir_mutex: Concurrent::ReadWriteLock.new,
               do_purge: true)
  @cache_dir = environments_cache_dir
  @cache_dir_mutex = cache_dir_mutex

  if do_purge
    @purge = Concurrent::TimerTask.new(execution_interval: purge_interval,
                                       timeout_interval: purge_timeout,
                                       run_now: true) { expire(purge_ttl) }
    @purge.execute
  end
end

Instance Attribute Details

#cache_dirObject (readonly)

Returns the value of attribute cache_dir.



11
12
13
# File 'lib/ace/plugin_cache.rb', line 11

def cache_dir
  @cache_dir
end

#cache_dir_mutexObject (readonly)

Returns the value of attribute cache_dir_mutex.



11
12
13
# File 'lib/ace/plugin_cache.rb', line 11

def cache_dir_mutex
  @cache_dir_mutex
end

Instance Method Details

#environment_dir(environment) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/ace/plugin_cache.rb', line 77

def environment_dir(environment)
  environment_dir = File.join(cache_dir, environment)
  cache_dir_mutex.with_write_lock do
    FileUtils.mkdir_p(File.join(environment_dir, 'code', 'environments', environment))
    FileUtils.touch(environment_dir)
  end
  environment_dir
end

#expire(purge_ttl) ⇒ Object

the cache_dir will be the ‘cache-dir` from the ace config, with the appended environments, i.e. /opt/puppetlabs/server/data/ace-server/cache/environments then the directories within this path, which will be the puppet environments will be removed if they have not been modified in the last 7 days when the purge runs (every 24 hours)



102
103
104
105
106
107
108
109
110
111
# File 'lib/ace/plugin_cache.rb', line 102

def expire(purge_ttl)
  expired_time = Time.now - purge_ttl
  cache_dir_mutex.with_write_lock do
    Dir.glob(File.join(cache_dir, '*')).select { |f| File.directory?(f) }.each do |dir|
      if File.mtime(dir) < expired_time
        FileUtils.remove_dir(dir)
      end
    end
  end
end

#libdir(plugin_dest) ⇒ Object

the Puppet will point to a tmp location where the contents from the pluginsync dest is copied too.



68
69
70
71
72
73
74
75
# File 'lib/ace/plugin_cache.rb', line 68

def libdir(plugin_dest)
  tmpdir = Dir.mktmpdir(['plugins', plugin_dest])
  cache_dir_mutex.with_write_lock do
    FileUtils.cp_r(File.join(plugin_dest, '.'), tmpdir)
    FileUtils.touch(tmpdir)
  end
  tmpdir
end

#setupObject



34
35
36
37
# File 'lib/ace/plugin_cache.rb', line 34

def setup
  FileUtils.mkdir_p(cache_dir)
  self
end

#sync_core(environment) ⇒ Object

Puppet is referenced too



88
89
90
91
92
93
# File 'lib/ace/plugin_cache.rb', line 88

def sync_core(environment)
  env = Puppet::Node::Environment.remote(environment)
  environments_dir = environment_dir(environment)
  Puppet::Configurer::PluginHandler.new.download_plugins(env)
  libdir(File.join(environments_dir, 'plugins'))
end

#with_synced_libdir(environment, enforce_environment, certname, &block) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ace/plugin_cache.rb', line 39

def with_synced_libdir(environment, enforce_environment, certname, &block)
  ForkUtil.isolate do
    ACE::PuppetUtil.isolated_puppet_settings(
      certname,
      environment,
      enforce_environment,
      environment_dir(environment)
    )
    with_synced_libdir_core(environment, &block)
  end
end

#with_synced_libdir_core(environment) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ace/plugin_cache.rb', line 51

def with_synced_libdir_core(environment)
  pool = Puppet::Network::HTTP::Pool.new(Puppet[:http_keepalive_timeout])
  Puppet.push_context({
                        http_pool: pool
                      }, "Isolated HTTP Pool")
  libdir = sync_core(environment)
  Puppet.settings[:libdir] = libdir
  $LOAD_PATH << libdir
  yield
ensure
  FileUtils.remove_dir(libdir)
  pool.close
end