为什么不在幂等模式下运行我的LWRP?

时间:2014-03-20 15:09:04

标签: chef chef-recipe chef-solo lwrp

这是我自定义LWRP的action :create,但它始终运行。我想做出一个幂等的行动。

这是我的实施:

action :create do
  converge_by "Create [#{@new_resource}]" do

    USER_NAME     = @new_resource.username
    USER_PASSWORD = @new_resource.password unless (@new_resource.password.nil? ||
                                                        @new_resource.password.empty?)
    USER_PASSWORD = USER_NAME
    DATABASE_NAME = @new_resource.database_name unless (@new_resource.database_name.nil? ||
                                                              @new_resource.database_name.empty?)
    DATABASE_NAME = USER_NAME

    execute "create user [#{USER_NAME}]" do
      user "postgres"
      exists = <<-EOH
      PGPASSWORD='postgres' psql -U postgres -c "select * from pg_user where usename='#{USER_NAME}'" | grep -c #{USER_NAME}
      EOH
      command "PGPASSWORD='postgres' createuser -U postgres -sw #{USER_NAME}"
      not_if exists, :user => "postgres"
    end

    execute "set-password" do
      user "postgres"
      command "PGPASSWORD='postgres' psql -U postgres -c \"alter role #{USER_NAME} with password '#{USER_PASSWORD}'\""
    end

    execute "create-database" do
      user "postgres"
      exists = <<-EOH
      PGPASSWORD='postgres' psql -U postgres -c "select * from pg_database WHERE datname='#{DATABASE_NAME}'" | grep -c #{DATABASE_NAME}
      EOH
      command "PGPASSWORD='postgres' createdb -U postgres -O #{USER_NAME} -T template0 #{DATABASE_NAME}"
      not_if exists, :user => "postgres"
    end
  end
  @new_resource.updated_by_last_action(true)
end

为什么最后一行@new_resource.updated_by_last_action(true)无效?

1 个答案:

答案 0 :(得分:0)

虽然我没有直接回答您的问题,但您可能对ToASTER感兴趣,这是一个测试Chef脚本幂等性的框架。

http://cloud-toaster.github.io/

在隔离容器环境(Docker VM)中使用不同配置执行Chef配方,ToASTER报告各种指标,如系统状态更改,收敛属性和幂等性问题。