【问题标题】:Error in running python script using chef使用 Chef 运行 python 脚本时出错
【发布时间】:2016-04-22 07:40:04
【问题描述】:

所以,我正在尝试使用 chef-cookbook 的 python 资源运行一个简单的 python 脚本。这是我的食谱的样子:

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

但是当我运行食谱时,我收到以下错误:

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)

【问题讨论】:

    标签: python chef-infra chef-solo


    【解决方案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
    

    【讨论】:

    • 您好,我尝试了您的解决方案,但仍然收到错误消息。这是:pastebin.com/hdmmLUWH
    • 虽然当我尝试执行资源时,它工作得很好。我想知道为什么脚本不起作用?
    【解决方案2】:

    我建议确保 py 脚本独立运行,然后使用 execute 和命令重写您的厨师食谱:

    文件名 = "test.py"

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

    我正在测试这种基本方法,它对我有用。 不要忘记编辑指向脚本路径的 cwd(当前工作目录)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-07
      • 2018-01-27
      • 2016-09-26
      相关资源
      最近更新 更多