Module: Chef::Sync::Utils

Defined in:
lib/chef-sync/utils.rb

Class Method Summary collapse

Class Method Details

.node_list(environment) ⇒ Object



4
5
6
# File 'lib/chef-sync/utils.rb', line 4

def self.node_list(environment)
  exec_knife_node_list.split
end

.public_hostnames_by_role(role, environment) ⇒ Object



8
9
10
11
12
# File 'lib/chef-sync/utils.rb', line 8

def self.public_hostnames_by_role(role, environment)
  results = JSON.parse(exec_knife_node_search(role, environment))
  hostnames = Array(results['rows']).map {|node| node['ec2.public_hostname'] }
  hostnames.sort
end

.update_mongo_from_chef(mongo_hash) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/chef-sync/utils.rb', line 26

def self.update_mongo_from_chef(mongo_hash)
  excluded_keys = %w(defaults test development)
  (mongo_hash.keys - excluded_keys).each do |environment|
    chef_server_list = public_hostnames_by_role('mongo', environment)

    yield(environment) if block_given?

    mongo_hash[environment]['hosts'] = chef_server_list.map do |hostname|
      ["#{hostname}:27017"]
    end if chef_server_list.any?
  end
end

.update_stages_from_chef(stages_hash, synchronized_roles, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/chef-sync/utils.rb', line 14

def self.update_stages_from_chef(stages_hash, synchronized_roles, &block)
  stages_hash.keys.each do |environment|
    synchronized_roles.each do |chef_role, cap_roles|
      cap_roles = Array(cap_roles)
      yield(cap_roles.join(' '), environment) if block_given?

      chef_server_list = public_hostnames_by_role(chef_role, environment).sort
      cap_roles.each {|cap_role| stages_hash[environment]['roles'][cap_role.to_s] = chef_server_list }
    end
  end
end