2016-08-02 76 views
-1

我想写在厨师的食谱,但我坚持我如何可以在Linux中执行多行执行命令行。首先是配方,下面是我收到的错误输出。厨师多线命令

node['freeswitch']['source']['dependencies'].each { |d| package d } 

execute "apt_update" do 
    command "wget -O - https://files.freeswitch.org/repo/deb/debian/freeswitch_archive_g0.pub | apt-key add -&&" 
    "echo "deb http://files.freeswitch.org/repo/deb/freeswitch-1.6/ jessie main" > /etc/apt/sources.list.d/freeswitch.list &&" 
    "apt-get update &&" 
    "apt-get install -y --force-yes freeswitch-video-deps-most &&" 

    # because we're in a branch that will go through many rebases it's 
    # better to set this one, or you'll get CONFLICTS when pulling (update) 
    git config --global pull.rebase true 
end 

下面是误差输出

NoMethodError 
------------- 
No resource or method named `command' for `Chef::Recipe "source"' 

Cookbook Trace: 
--------------- 
/var/chef/cache/cookbooks/freeswitch/recipes/source.rb:6:in `from_file' 
/var/chef/cache/cookbooks/freeswitch/recipes/default.rb:5:in `from_file' 
Relevant File Content: 
---------------------- 
/var/chef/cache/cookbooks/freeswitch/recipes/source.rb: 

1: #include_recipe 'apt' 
2: 
3: node['freeswitch']['source']['dependencies'].each { |d| package d } 
4: 
5: execute "apt_update" 
6>> command 'wget -O - https://files.freeswitch.org/repo/deb/debian/freeswitch_archive_g0.pub | apt-key add -'&& 
7: 'echo "deb http://files.freeswitch.org/repo/deb/freeswitch-1.6/ jessie main" > /etc/apt/sources.list.d/freeswitch.list' && 
8:   'apt-get update' && 
9:   'apt-get install -y --force-yes freeswitch-video-deps-most' && 
10: 
11: # because we're in a branch that will go through many rebases it's 
12: # better to set this one, or you'll get CONFLICTS when pulling (update) 
13:   'git config --global pull.rebase true' 
14: 
15: execute "git_clone" do 

Platform: 
--------- 
x86_64-linux 


Running handlers: 
[2016-08-02T09:19:35+01:00] ERROR: Running exception handlers 
Running handlers complete 
[2016-08-02T09:19:35+01:00] ERROR: Exception handlers complete 
Chef Client failed. 0 resources updated in 01 seconds 
[2016-08-02T09:19:35+01:00] FATAL: Stacktrace dumped to /var/chef/cache/chef-stacktrace.out 
[2016-08-02T09:19:35+01:00] FATAL: Please provide the contents of the stacktrace.out file if you file a bug report 
[2016-08-02T09:19:35+01:00] ERROR: No resource or method named `command' for `Chef::Recipe "source"' 
[2016-08-02T09:19:35+01:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1) 
+1

习惯厨师和我们一致的资源,例如[remote_file](https://docs.chef.io/resource_remote_file.html),[apt cookbook](https://supermarket.chef.io/ cookbooks/apt)和[package](https://docs.chef.io/resource_package.html)资源。不要只使用Chef作为Shell脚本的运行者。你不会有那么开心。 – StephenKing

+0

在这种情况下,从源代码运行是首选选项。 – jaseUK

回答

0

&&壳的逻辑and运算符。要么你明确地在shell启动的命令,如:

execute 'Execute a shell' do 
    command "bash -c 'cmd1 && cmd2 && ..'" 
end 

或使用bash resource

bash 'Execute bash script' 
    code <<-EOH 
    cmd1 \ 
    && cmd2 \ 
    && ... 
    EOH 
end 
+0

请不要让他这样做.. – StephenKing

+0

@StephenKing我看到,使用'apt'资源似乎更适合于此。当我找到时间我会准备这样一个例子。手上没有厨师atm。 – hek2mgl

2

因为我不想反对票两个问题,并没有显示出唯一的答案你如何做到这一点(至少多一点),在这里。你编写代码猴子创造了一个食谱,增加了容易回购和安装包:

# recipes/default.rb 

# the package resource can handle multiple packages at once 
package node['freeswitch']['source']['dependencies'] 

# set up a new apt repo, import the key, automatically triggers `apt-get update` 
apt_repository "freeswitch" do 
    key "https://files.freeswitch.org/repo/deb/debian/freeswitch_archive_g0.pub" 
    uri "http://files.freeswitch.org/repo/deb/freeswitch-1.6/" 
    components ['main'] 
    distribution node['lsb']['codename'] 
end 

# finally, install this package. You could even merge the first line with this one. 
package "freeswitch-video-deps-most" 

你只需要申报apt菜谱作为依赖于你的metadata.rb

# metadata.rb 
name 'whatever' 
... 

depends "apt" 

当你习惯了它时,这很容易。奖励:它是幂等的。