厨师资源通知会影响通知资源

时间:2019-06-20 21:19:01

标签: chef chef-recipe

我对Chef来说还比较陌生,因此我尝试第一次使用:before计时器接收一些通知。这个想法很简单:我有一个自定义资源,可以从存储库中提取工件,将其保存到暂存目录,然后将其部署到部署目录。如果自定义资源发现必须部署工件,则将当前部署备份到备份目录,并部署新工件。如果不需要部署工件(由于下面的not_if { some_condition }行),将不运行任何备份步骤(这是使用通知的全部要点,因为备份步骤仅通过通知触发)。

为了实现此功能,我使用action :nothing创建了多个资源,并使用通知和:before计时器从自定义资源中运行它们(请参见下文)。我尝试的每次运行都将some_condition评估为false。

# SNIPPET 1 - This is my intended code, whose behavior I can't understand. 
# All backup actions are set to :nothing and will be triggered
# only if some_condition is false.

directory "delete #{backup_dir}" do
  recursive true
  path backup_dir
  action :nothing
end

directory "create #{backup_dir}" do
  owner node['my_cookbook']['owner']
  group node['my_cookbook']['group']
  path backup_dir
  mode '0755'
  recursive true
  action :nothing
end

execute 'backup current deployment' do
  command "mv #{deployments_dir} #{backup_dir}"
  action :nothing
  only_if { ::Dir.exists?(deployments_dir) }
end

directory "create #{deployments_dir}" do
  path deployments_dir
  owner node['my_cookbook']['owner']
  group node['my_cookbook']['group']
  mode  "0755"
  recursive true
  action :nothing
end

node['my_cookbook']['artifacts'].each do |artifact_name|
  config = my_environment(artifact_name, node)
  # This custom resource downloads the artifact to a staging directory
  # and then moves it to deployments_dir.
  my_artifact_cache "my_cookbook::deploy_artifacts - Pull cached artifact #{artifact_name}" do
    snapshot      config['repository'] == 'snapshot'
    group_id      config['group_id']
    artifact_id   config['artifact_id']
    version       config['version']
    filetype      config['filename_extension']
    path          deployments_dir
    owner         node['my_cookbook']['owner']
    group         node['my_cookbook']['group']
    mode          "0755"
    action :pull
    not_if { some_condition }
    notifies :delete, "directory[delete #{backup_dir}]", :before
    notifies :create, "directory[create #{backup_dir}]", :before
    notifies :run, 'execute[backup current deployment]', :before
    notifies :create, "directory[create #{deployments_dir}]", :before
  end
end

但是,每次我运行上面的代码时,my_artifact_cache自定义资源中的某些动作 都会被跳过,我也不知道为什么。这导致运行失败,因为不是从自定义资源中创建暂存目录(与我实际上通过通知操作的目录无关)。没有通知,日志显示- created directory /staging/dir,有通知我显示- Would create new directory /staging/dir

当我运行下面的第二个代码段时,一切正常。我希望两个摘要在some_condition为假的情况下是等效的。为什么通知似乎会影响通知资源的行为?我想念什么吗?

# SNIPPET 2 - Commented out the :nothing actions, replaced them with the actions
# from the notifications - this snippet works as expected. I expected
# snippet 1 to be equivalent during runtime if some_condition is false

directory "delete #{backup_dir}" do
  recursive true
  path backup_dir
  #action :nothing
  action :delete
end

directory "create #{backup_dir}" do
  owner node['my_cookbook']['owner']
  group node['my_cookbook']['group']
  path backup_dir
  mode '0755'
  recursive true
  #action :nothing
end

execute 'backup current deployment' do
  command "mv #{deployments_dir} #{backup_dir}"
  #action :nothing
  only_if { ::Dir.exists?(deployments_dir) }
end

directory "create #{deployments_dir}" do
  path deployments_dir
  owner node['my_cookbook']['owner']
  group node['my_cookbook']['group']
  mode  "0755"
  recursive true
  #action :nothing
end

node['my_cookbook']['artifacts'].each do |artifact_name|
  config = my_environment(artifact_name, node)
  # This custom resource downloads the artifact to a staging directory
  # and then moves it to deployments_dir.
  my_artifact_cache "my_cookbook::deploy_artifacts - Pull cached artifact #{artifact_name}" do
    snapshot      config['repository'] == 'snapshot'
    group_id      config['group_id']
    artifact_id   config['artifact_id']
    version       config['version']
    filetype      config['filename_extension']
    path          deployments_dir
    owner         node['my_cookbook']['owner']
    group         node['my_cookbook']['group']
    mode          "0755"
    action :pull
    not_if { some_condition }
    #notifies :delete, "directory[delete #{backup_dir}]", :before
    #notifies :create, "directory[create #{backup_dir}]", :before
    #notifies :run, 'execute[backup current deployment]', :before
    #notifies :create, "directory[create #{deployments_dir}]", :before
  end
end

1 个答案:

答案 0 :(得分:-1)

首席资源按其写入顺序运行。这意味着您可以按顺序实现逻辑。

在我看来,您正在使用notifications来实现代码的重用,但是通知并不是要走的路,因为每个厨师资源都必须具有唯一的名称,以避免任何资源冲突或错误的通知,这就是resource cloning has been deprecated的原因。

如果是这种情况,那么有更好的方法可以在Chef中实现。例如,写custom light weight resource provider (lwrp)(也read this)或写library(也read this)。