让厨师来告知docker是否已安装在Linux上

问题描述 投票:0回答:2

我正在尝试在安装了防护的节点上找到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函数引发异常,但是我对红宝石和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
chef
2个回答
1
投票

您的问题不是“如何知道,是否已安装docker”,实际上是您使用Chef的方式。您正在尝试使用Chef资源编写一个普通的bash脚本。与bash脚本相比,这没有给您带来任何优势,但是使事情变得更加复杂。

看着您的代码,我看到您:

  1. 检查是否安装了Docker
  2. 如果尚未安装,请安装并配置。

厨师很聪明,所以您不必做第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会这样做。您只需要describe所需的计算机状态。

还请注意,您不需要更改模式或组的execute资源,您可以在directorycookbook_file资源本身内部设置模式和所有者/组。


1
投票

您可以测试是否以多种方式安装了Docker引擎,以下是一些。

  • 检查docker命令是否存在
command -v docker
  • 检查软件包管理器(假设您正在使用ubuntu)
dpkg -l | grep -i docker
  • 检查docker是否在路径中
which docker

并且您可以找到许多其他方法来检查docker是否存在...

© www.soinside.com 2019 - 2024. All rights reserved.