Hi guys,

recently for some work tasks I needed to interact with Chef Push Jobs not from knife command line tool but through ruby code…

Screen Shot 2017-08-14 at 11.43.02

For this reason, I decided to write a Ruby gem and I hope it will be useful for the Chef community 🙂

You can find code and instructions here https://github.com/lucky-sideburn/foodpusher.

Below some code, just to have a look about how to use of this gem.

Let’s say you want push chef-client execution on client01.local and chef-server.

require 'chef/knife'
require 'chef/knife/foodpusher'

job = { 'command' => 'chef-client',
        'nodes' => ['client01.local', 'chef-server'],
        'quorum' => 1,
        'capture_output' => false,
        'env' => {} }

Chef::Config.chef_server_url = 'https://chef.devops.foobar.lan/organizations/chef'
Chef::Config.client_key = '/vagrant/chef-server.local/.chef/chef.pem'
Chef::Config.node_name = 'chef'
Chef::Config.ssl_verify_mode = :verify_none
pusher = FoodPusher.new(job)

# Start Job
myjob = pusher.start
puts "Job uri #{myjob}"
status = (pusher.status myjob)['status']

# Check Job Status
while status == 'running'
  status = (pusher.status myjob)['status']
  puts status
  sleep 1
end

# Check Node Status
(pusher.node_status job['nodes']).each do |result|
  puts result
end

Launch the script and that’s the output

Job uri {"uri"=>"https://chef.devops.foobar.lan/organizations/chef/pushy/jobs/336d0bf2dc771d3127dffead469381b5"}
running
running
running
running
running
complete
{"node_name"=>"client01.local", "status"=>"online", "availability"=>"available"}
{"node_name"=>"chef-server", "status"=>"online", "availability"=>"available"}

Thanks!