class Pantry::Chef::SyncCookbooks

Client syncs up it’s local list of Chef Cookbooks with what the Server says the Client should have.

Public Instance Methods

perform(message) click to toggle source
# File lib/pantry/chef/sync_cookbooks.rb, line 8
def perform(message)
  cookbooks_to_download = ask_server_for_cookbook_list
  Pantry.logger.debug("[#{client.identity}] Downloading cookbooks #{cookbooks_to_download}")

  recievers = build_cookbook_receivers(cookbooks_to_download)
  send_receiver_information_to_server(recievers)
  wait_for_receivers_to_finish(recievers)
  true
end

Protected Instance Methods

ask_server_for_cookbook_list() click to toggle source
# File lib/pantry/chef/sync_cookbooks.rb, line 35
def ask_server_for_cookbook_list
  send_request!(Pantry::Chef::ListCookbooks.new.to_message).body
end
build_cookbook_receivers(cookbook_list) click to toggle source
# File lib/pantry/chef/sync_cookbooks.rb, line 39
def build_cookbook_receivers(cookbook_list)
  cookbook_list.map do |(name, size, checksum)|
    receive_info = FileAndReceiverInfo.new(name, client.receive_file(size, checksum))
    receive_info.on_complete(&unpack_received_file(receive_info))
    receive_info
  end
end
send_receiver_information_to_server(receivers) click to toggle source
# File lib/pantry/chef/sync_cookbooks.rb, line 59
def send_receiver_information_to_server(receivers)
  download_message = Pantry::Chef::SendCookbooks.new.to_message

  receivers.each do |receiver_info|
    download_message << [
      receiver_info.name,
      receiver_info.receiver_uuid,
      receiver_info.file_uuid
    ]
  end

  if receivers.any?
    send_request(download_message)
  end
end
unpack_received_file(receiver_info) click to toggle source
# File lib/pantry/chef/sync_cookbooks.rb, line 47
def unpack_received_file(receiver_info)
  lambda do
    stdout, stderr = Open3.capture2e(
      "tar",
      "-xzC", Pantry.root.join("chef", "cookbooks").to_s,
      "-f", receiver_info.uploaded_path
    )

    Pantry.logger.debug("[#{client.identity}] Unpack cookbook #{stdout.inspect}, #{stderr.inspect}")
  end
end
wait_for_receivers_to_finish(receivers) click to toggle source
# File lib/pantry/chef/sync_cookbooks.rb, line 75
def wait_for_receivers_to_finish(receivers)
  receivers.each do |receive_info|
    receive_info.wait_for_finish
  end
end