class Chef::Knife::HitoriCook

Public Instance Methods

fetch_server_info() click to toggle source
# File lib/chef/knife/hitori_cook.rb, line 116
def fetch_server_info
  servers = server_info_list

  if config[:tag]
    tag_key, tag_value = config[:tag].to_s.split(/=/, 2)
    servers.select {|x| x.tags[tag_key] == tag_value.to_s}
  elsif config[:groups]
    g = Set.new(config[:groups].split(/,/))
    servers.select {|x| (Set.new(x.groups) & g).size > 0}
  elsif config[:name]
    servers.select {|x| x.tags['Name'] == config[:name]}
  elsif config[:public_ip_address]
    servers.select {|x| x.public_ip_address == config[:public_ip_address]}
  end
end
find_ec2_servers() click to toggle source
# File lib/chef/knife/hitori_cook.rb, line 104
def find_ec2_servers
  server = nil
  3.times do
    server = fetch_server_info
    break if server && !server.empty?
    print '.'
    sleep 5
  end
  puts
  server
end
new_run_list() click to toggle source
@return [Hash]

def json_attrs

attrs = load_settings || {}
# Chef::Mixin::DeepMerge.merge(normal_attrs,normal_attrs_to_merge)

# Update run_list
run_list = new_run_list
unless run_list.empty?
  attrs['run_list'] = run_list
end
#
attrs

end

def load_settings

path = Chef::Config[:settings_path]
if ::File.exists?(path)
  return JSON.parse(::File.read(path))
end
ui.warn "#{path} is not found!!" if path
{}

end

# File lib/chef/knife/hitori_cook.rb, line 72
def new_run_list
  run_list = []
  run_list += config[:roles].split(/,/).map{|r| "role[#{r.strip}]"} if config[:roles]
  run_list += config[:recipes].split(/,/).map{|r| "recipe[#{r.strip}]"} if config[:recipes]
  run_list
end
run() click to toggle source
# File lib/chef/knife/hitori_cook.rb, line 44
def run
  update_environment(config[:environment]) if config[:environment]
  run_remote
end
run_remote(server_list=nil) click to toggle source
# File lib/chef/knife/hitori_cook.rb, line 81
def run_remote(server_list=nil)
  if server_list
    servers = server_list
  else
    servers = find_ec2_servers
    unless servers && !servers.empty?
      ui.warn ui.color('no server found', :red)
      return
    end
  end

  run_list = new_run_list
  threads = []
  servers.each_with_index do |server, idx|
    threads << Thread.new {
      KnifeHitori::RunRemoteCook.new(Chef::Config, config, server, :idx => idx, :ui => ui, :run_list => run_list).run
    }
  end
  threads.each do |t|
    t.join
  end
end
server_info_list() click to toggle source
# File lib/chef/knife/hitori_cook.rb, line 132
def server_info_list
  connection = Fog::Compute.new(
      :provider => 'AWS',
      :aws_access_key_id => Chef::Config[:knife][:aws_access_key_id],
      :aws_secret_access_key => Chef::Config[:knife][:aws_secret_access_key],
      :region => Chef::Config[:knife][:region]
  )
  connection.servers.all.to_a
end