itamae-kitchen / itamae

Configuration management tool inspired by Chef, but simpler and lightweight. Formerly known as Lightchef.

Home Page:https://itamae.kitchen/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`not_if` / `only_if` don't work inside a `define`

smlx opened this issue · comments

If the not_if/only_if directives could be applied to all resources
contained within a define, it would help to DRY up my code.. right now I'm
using a workaround like this:

define :bootstrap_install do
  test_cmd = "dpkg-query -Wf'${db:Status-abbrev}' #{params[:name]} | grep '^i'"
  installed = run_command(test_cmd, error: false).exit_status == 0

  # run install if required
  (params[:name], params[:dependencies]).map do |pkg|
    "/tmp/#{pkg}_amd64.deb"
  end.each do |path|
    remote_file path do
      not_if installed
    end
    execute "dpkg -i #{path}" do
      not_if installed
    end
    file path do
      action :delete
      not_if installed
    end
  end
end

Whereas I'd like to be able to do:

define :bootstrap_install do
  not_if "dpkg-query -Wf'${db:Status-abbrev}' #{params[:name]} | grep '^i'"

  (params[:name], params[:dependencies]).map do |pkg|
    "/tmp/#{pkg}_amd64.deb"
  end.each do |path|
    remote_file path
    execute "dpkg -i #{path}"
    file path do
      action :delete
    end
  end
end

Thank you for your report.
This is a known problem and not fixed yet. I'll try to fix this.

I think the following workaround is better:

define :bootstrap_install do
  test_cmd = "dpkg-query -Wf'${db:Status-abbrev}' #{params[:name]} | grep '^i'"
  installed = run_command(test_cmd, error: false).exit_status == 0

  unless installed
    # run install if required
    (params[:name], params[:dependencies]).map do |pkg|
      "/tmp/#{pkg}_amd64.deb"
    end.each do |path|
      remote_file path
      execute "dpkg -i #{path}"
      file path do
        action :delete
      end
    end
  end
end

Released at v1.10.0
c.f. #271