我试图在安装护罩的节点上找到Docker的存在。
资源块如下所示:
ruby_block "install_docker" do
not_if {shell_out('docker -v').stdout =~ /Docker version/i}
block do
#command_out = shell_out('docker version --format \'{{.Server.Version}}\'')
end
action :create
notifies :write, 'log[installing_docker_log]', :immediately
notifies :install, 'package[install_docker_ce]', :immediately
notifies :install, 'yum_package[install_docker_ce_cli]', :immediately
notifies :install, 'yum_package[install_containerd_io]', :immediately
notifies :create, "directory[create_docker_dir]", :immediately
notifies :create, "directory[create_etc_docker_dir]", :immediately
notifies :create, "cookbook_file[create_docker_daemon_json]", :immediately
notifies :create, "cookbook_file[create_docker_99_docker_config]", :immediately
notifies :run, "execute[chgrp docker_dir]", :immediately
notifies :run, "execute[chmod docker_dir]", :immediately
notifies :run, "execute[chgrp /etc/docker]", :immediately
notifies :run, "execute[chmod /etc/docker]", :immediately end
在厨房里跑的时候,我得到这个输出,说守卫失败了。如果未安装docker,则失败。所以我假设shell_out函数抛出异常,但是我对ruby和Chef还是陌生的,所以不确定如何使其静音。
我是否要检查docker是否以其他方式安装?非常感谢您提供的任何帮助。
Compiled Resource:
------------------
# Declared in /tmp/kitchen/cache/cookbooks/kpmi_linux_base/recipes/install_docker.rb:54:in `from_file'
ruby_block("install_docker") do
action [:create]
default_guard_interpreter :default
declared_type :ruby_block
cookbook_name "kpmi_linux_base"
recipe_name "install_docker"
block #<Proc:0x000000000470c8b8 /tmp/kitchen/cache/cookbooks/kpmi_linux_base/recipes/install_docker.rb:56>
not_if { #code block }
end
System Info:
------------
chef_version=16.1.16
platform=centos
platform_version=7.6.1810
ruby=ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-linux]
program_name=/opt/chef/bin/chef-client
executable=/opt/chef/bin/chef-client
Running handlers:
[2020-06-05T18:30:39+00:00] ERROR: Running exception handlers
Running handlers complete
[2020-06-05T18:30:39+00:00] ERROR: Exception handlers complete
Chef Infra Client failed. 0 resources updated in 10 seconds
[2020-06-05T18:30:39+00:00] FATAL: Stacktrace dumped to /tmp/kitchen/cache/chef-stacktrace.out
[2020-06-05T18:30:39+00:00] FATAL: Please provide the contents of the stacktrace.out file if you file a bug report
[2020-06-05T18:30:39+00:00] FATAL: Errno::ENOENT: ruby_block[install_docker] (kpmi_linux_base::install_docker line 54) had an error: Errno::ENOENT: No such file or directory - docker
您的问题不是“如何知道,是否已安装docker”,实际上是您使用Chef的方式。您正在尝试使用Chef资源编写一个普通的bash脚本。与bash脚本相比,这没有给您带来任何优势,但是使事情变得更加复杂。
查看您的代码,我看到您:
Chef很聪明,所以您不必做第1点。立即转到第2点。您的食谱应如下所示:
package 'docker_ce' # install docker_ce package
yum_package 'docker_ce_cli' # install docker_ce_cli package
yum_package 'containerd_io' # install containerd_io package
directory '/etc/docker' do # create /etc/docker directory
owner 'root' # set owner for /etc/docker
group 'root' # set group for /etc/docker
mode '0755' # set mode for /etc/docker
end
cookbook_file [...] do
owner 'root'
group 'root'
mode '0755'
end
[...]
大部分Chef内置资源(如程序包,目录,文件等),除了execute,ruby_block和仅运行任意代码的其他资源外,都是幂等的。这意味着,如果已经安装了该软件包,则不会尝试再次安装它;如果该目录存在,它将不会尝试创建它,等等。因此,您不必自己进行检查,Chef会这样做。您只需要描述计算机的所需状态。
另请注意,您不需要execute
更改模式或组的资源,可以在directory
或cookbook_file
资源本身内部设置模式和所有者/组。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句