使用Chef运行Python脚本时出错

1 投票
2 回答
1672 浏览
提问于 2025-04-17 21:06

我正在尝试使用chef-cookbook中的python资源来运行一个简单的python脚本。我的配方(recipe)看起来是这样的:

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

但是当我运行这个cookbook时,我遇到了以下错误:

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)

我正在使用chef-solo。你能帮我找出这个错误吗?

更新:我在我的脚本中添加了“action :run”。现在错误变成了:

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)

2 个回答

0

我建议你先确保这个 Python 脚本可以独立运行正常,然后再用 execute 和命令来重写你的 Chef 配方:

文件名 = "test.py"

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

我测试了这个基本的方法,结果对我来说是有效的。别忘了修改你的当前工作目录(cwd),让它指向脚本所在的路径。

1

这个python资源是用来运行代码的,它是以python的方式运行的。你现在想运行一个python脚本,但这两者是不同的。在你的情况下,你应该使用script资源:

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

或者你可以用python来执行文件的内容

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

撰写回答