Pensu
Pensu

Reputation: 3619

Error in running python script using chef

So, I am trying to run a simple python script using python resource of chef-cookbook. Here is what my recipe looks like:

python "excute_file" do
  cwd '/home/peeyush/'
  code <<-EOH
    python #{filename}
  EOH
end

But when I run the cookbook, I get the following error:

Recipe: hellochef::default
  * python[excute_file] action run
================================================================================
Error executing action `run` on resource 'python[excute_file]'
================================================================================


Mixlib::ShellOut::ShellCommandFailed
------------------------------------
Expected process to exit with [0], but received '1'
---- Begin output of "python"  "/tmp/chef-script20140306-18792-iqfp89" ----
STDOUT: 
STDERR: File "/tmp/chef-script20140306-18792-iqfp89", line 1
    python test.py
    ^
IndentationError: unexpected indent
---- End output of "python"  "/tmp/chef-script20140306-18792-iqfp89" ----
Ran "python"  "/tmp/chef-script20140306-18792-iqfp89" returned 1


Resource Declaration:
---------------------
# In /home/peeyush/chef/cookbook/hellochef/recipes/default.rb

 17: python "excute_file" do
 18:   cwd '/home/peeyush/'
 19:   code <<-EOH
 20:     python #{filename}
 21:   EOH
 22: end



Compiled Resource:
------------------
# Declared in /home/peeyush/chef/cookbook/hellochef/recipes/default.rb:17:in `from_file'

python("excute_file") do
  action "run"
  retries 0
  retry_delay 2
  command "\"python\"  \"/tmp/chef-script20140306-18792-iqfp89\""
  backup 5
  cwd "/home/peeyush/"
  returns 0
  code "    python test.py\n"
  interpreter "python"
  cookbook_name :hellochef
  recipe_name "default"
end




Running handlers:
[2014-03-06T17:30:21+05:30] ERROR: Running exception handlers
Running handlers complete

[2014-03-06T17:30:21+05:30] ERROR: Exception handlers complete
[2014-03-06T17:30:21+05:30] FATAL: Stacktrace dumped to /home/peeyush/chef/chef-stacktrace.out
Chef Client failed. 0 resources updated in 6.727546421 seconds
[2014-03-06T17:30:21+05:30] ERROR: python[excute_file] (hellochef::default line 17) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '1'
---- Begin output of "python"  "/tmp/chef-script20140306-18792-iqfp89" ----
STDOUT: 
STDERR: File "/tmp/chef-script20140306-18792-iqfp89", line 1
    python test.py
    ^
IndentationError: unexpected indent
---- End output of "python"  "/tmp/chef-script20140306-18792-iqfp89" ----
Ran "python"  "/tmp/chef-script20140306-18792-iqfp89" returned 1
[2014-03-06T17:30:21+05:30] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)

I am using chef-solo. Can you please help me figure out the error.

Update: I added "action :run" to my script. Now the error is:

Recipe: hellochef::default
  * python[excute_file] action run
================================================================================
Error executing action `run` on resource 'python[excute_file]'
================================================================================


Mixlib::ShellOut::ShellCommandFailed
------------------------------------
Expected process to exit with [0], but received '1'
---- Begin output of "python"  "/tmp/chef-script20140306-19760-hs5acr" ----
STDOUT: 
STDERR: File "/tmp/chef-script20140306-19760-hs5acr", line 1
    python test.py
              ^
SyntaxError: invalid syntax
---- End output of "python"  "/tmp/chef-script20140306-19760-hs5acr" ----
Ran "python"  "/tmp/chef-script20140306-19760-hs5acr" returned 1


Resource Declaration:
---------------------
# In /home/peeyush/chef/cookbook/hellochef/recipes/default.rb

 17: python "excute_file" do
 18:   cwd '/home/peeyush/'
 19:   code <<-EOH
 20: python #{filename}
 21:   EOH
 22:   action :run
 23: end



Compiled Resource:
------------------
# Declared in /home/peeyush/chef/cookbook/hellochef/recipes/default.rb:17:in `from_file'

python("excute_file") do
  action [:run]
  retries 0
  retry_delay 2
  command "\"python\"  \"/tmp/chef-script20140306-19760-hs5acr\""
  backup 5
  cwd "/home/peeyush/"
  returns 0
  code "python test.py\n"
  interpreter "python"
  cookbook_name :hellochef
  recipe_name "default"
end




Running handlers:
[2014-03-06T17:43:33+05:30] ERROR: Running exception handlers
Running handlers complete

[2014-03-06T17:43:33+05:30] ERROR: Exception handlers complete
[2014-03-06T17:43:33+05:30] FATAL: Stacktrace dumped to /home/peeyush/chef/chef-stacktrace.out
Chef Client failed. 0 resources updated in 1.204350962 seconds
[2014-03-06T17:43:33+05:30] ERROR: python[excute_file] (hellochef::default line 17) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '1'
---- Begin output of "python"  "/tmp/chef-script20140306-19760-hs5acr" ----
STDOUT: 
STDERR: File "/tmp/chef-script20140306-19760-hs5acr", line 1
    python test.py
              ^
SyntaxError: invalid syntax
---- End output of "python"  "/tmp/chef-script20140306-19760-hs5acr" ----
Ran "python"  "/tmp/chef-script20140306-19760-hs5acr" returned 1
[2014-03-06T17:43:33+05:30] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)

Upvotes: 1

Views: 1690

Answers (2)

Juanjo Marron
Juanjo Marron

Reputation: 479

I suggest to make sure the py script runs properly standalone and after that rewrite your chef recipe using execute and command:

filename = "test.py"

execute 'execute_file' do
 cwd '/tmp/test.py'
 command "python #{filename}"
end

I was testing this basic approach and it worked for me. Don't forget to edit your cwd (current working dir) pointing to the script path

Upvotes: 0

sethvargo
sethvargo

Reputation: 26997

The python resource runs the code as python. You are trying to run a python script - they are different things. In your case, you should use the script resource:

script 'execute_file' do
  cwd '/home/peeyush'
  code "python #{filename}"
end

Or you can execute the contents of the file using python:

python 'execute' do
  code <<-EOH.gsub(/^ {4}/, '')
    some.magical.python:
      code.in.here
  EOH
end

Upvotes: 1

Related Questions