[SOLVED] Run chef-client inside ruby script

Hello All,

So I am trying to run chef-client and want to pass it a recipe along with a JSON file to input attributes, all inside a ruby script. I am able to run the command directly in cmd or Powershell. And I am running the command inside the ruby script as:
system("chef-client -o ‘recipe’ -j ‘path-to-JSON’ -L ‘log-file-path’ ")

It does run chef-client but since my recipe contains a require ‘chef/provisioning/aws’ statement, it gives the following error:
Generated at 2016-11-03 06:54:40 +0000
LoadError: cannot load such file – chef/provisioning/aws_driver
C:/opscode/chef/embedded/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in require' C:/opscode/chef/embedded/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:inrequire’
c:/chef/cache/cookbooks/network_stack/providers/route.rb:5:in class_from_file' C:/opscode/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/mixin/from_file.rb:42:inclass_eval’
C:/opscode/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/mixin/from_file.rb:42:in class_from_file' C:/opscode/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/provider/lwrp_base.rb:64:inbuild_from_file’
C:/opscode/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/run_context/cookbook_compiler.rb:213:in load_lwrp_provider' C:/opscode/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/run_context/cookbook_compiler.rb:204:inblock in load_lwrps_from_cookbook’
C:/opscode/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/run_context/cookbook_compiler.rb:203:in each' C:/opscode/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/run_context/cookbook_compiler.rb:203:inload_lwrps_from_cookbook’
C:/opscode/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/run_context/cookbook_compiler.rb:121:in block in compile_lwrps' C:/opscode/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/run_context/cookbook_compiler.rb:120:ineach’
C:/opscode/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/run_context/cookbook_compiler.rb:120:in compile_lwrps' C:/opscode/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/run_context/cookbook_compiler.rb:73:incompile’
C:/opscode/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/run_context.rb:176:in load' C:/opscode/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/policy_builder/expand_node_object.rb:97:insetup_run_context’
C:/opscode/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/client.rb:510:in setup_run_context' C:/opscode/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/client.rb:280:inrun’
C:/opscode/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/application.rb:284:in run_with_graceful_exit_option' C:/opscode/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/application.rb:260:inblock in run_chef_client’
C:/opscode/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/local_mode.rb:44:in with_server_connectivity' C:/opscode/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/application.rb:243:inrun_chef_client’
C:/opscode/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/application/client.rb:464:in sleep_then_run_chef_client' C:/opscode/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/application/client.rb:451:inblock in interval_run_chef_client’
C:/opscode/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/application/client.rb:450:in loop' C:/opscode/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/application/client.rb:450:ininterval_run_chef_client’
C:/opscode/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/application/client.rb:434:in run_application' C:/opscode/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/application.rb:60:inrun’
C:/opscode/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/bin/chef-client:26:in <top (required)>' C:/opscode/chef/bin/chef-client:65:inload’
C:/opscode/chef/bin/chef-client:65:in `’

I don’t know why would it give such an error when it runs directly through powershell or cmd. Is there something else that I need to do to let my script run the same way as I run the command manually?

I tried to google it but could not find any reason on why this particular error shows up.

Also open to any suggestions on alternate ways of running chef-client command on provisioner node inside a script.

Thanks,
Rahul

Nevermind, was my silly mistake :slight_smile: