Class: Wakame::Triggers::MaintainSshKnownHosts::UpdateKnownHosts

Inherits:
Action
  • Object
show all
Defined in:
lib/wakame/triggers/maintain_ssh_known_hosts.rb

Constant Summary

Constants included from AttributeHelper

AttributeHelper::CLASS_TYPE_KEY, AttributeHelper::CONVERT_CLASSES, AttributeHelper::PRIMITIVE_CLASSES

Instance Attribute Summary

Attributes inherited from Action

#action_manager

Instance Method Summary collapse

Methods inherited from Action

#acquire_lock, #actor_request, #agent_monitor, #all_subactions_complete?, #flush_subactions, #master, #notes, #notify, #on_canceled, #on_failed, #service_cluster, #status=, #subactions, #sync_actor_request, #trigger_action, #walk_subactions

Methods included from ThreadImmutable

#bind_thread, included, #target_thread, #target_thread?, #thread_check

Methods included from AttributeHelper

#dump_attrs, #retrieve_attr_attribute

Instance Method Details

#runObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/wakame/triggers/maintain_ssh_known_hosts.rb', line 7

def run
  host_keys = []
  ['/etc/ssh/ssh_host_rsa_key.pub', '/etc/ssh/ssh_host_dsa_key.pub'].each { |k|
    next unless File.file? k
    host_keys << File.readlines(k).join('').chomp.sub(/ host$/, '')
  }
  return if host_keys.empty?

  basedir = File.dirname(Wakame.config.ssh_known_hosts)
  FileUtils.mkpath(basedir) unless File.exist? basedir

  tmpfile = File.expand_path(File.basename(Wakame.config.ssh_known_hosts) + '.tmp', basedir)
  File.open(tmpfile, 'w') { |f|
    StatusDB.barrier do
      agent_monitor.agent_pool.group_active.keys.each { |agent_id|
        agent = Service::Agent.find(agent_id)
        host_keys.each { |k|
          f << "#{Wakame::Util.ssh_known_hosts_hash(agent.agent_ip)} #{k}\n"
        }
      }
    end
  }

  FileUtils.move(tmpfile, Wakame.config.ssh_known_hosts, {:force=>true})
end