Postgresql 厨师如何可靠地测试和存储postgres数据库存在性检查的结果?

Postgresql 厨师如何可靠地测试和存储postgres数据库存在性检查的结果?,postgresql,chef-infra,conditional-statements,Postgresql,Chef Infra,Conditional Statements,对于以下3个数据库中的每一个,我想检查数据库是否存在 psql-d{dbname}-c'选择1作为复选框它本身按预期工作,如果存在则返回0,如果不存在则返回2 但是让厨师做一些测试是困难的 我不能使用guard not_,如果因为我真的需要控制存在的许多步骤: 加载数据库 创建用户 授权用户 等 所以我真正计划做的是做下一步,通过下一步迭代到循环中的下一个数据库(如果它存在的话) 营救似乎并不重要。尽管发生了“忽略”故障,我仍然会出错: 对于那些好奇的人,我正在使用Chef将服务器逐渐调整到所需

对于以下3个数据库中的每一个,我想检查数据库是否存在

psql-d{dbname}-c'选择1作为复选框它本身按预期工作,如果存在则返回0,如果不存在则返回2

但是让厨师做一些测试是困难的

我不能使用guard not_,如果因为我真的需要控制存在的许多步骤:

加载数据库 创建用户 授权用户 等 所以我真正计划做的是做下一步,通过下一步迭代到循环中的下一个数据库(如果它存在的话)

营救似乎并不重要。尽管发生了“忽略”故障,我仍然会出错:

对于那些好奇的人,我正在使用Chef将服务器逐渐调整到所需的状态。很多时候,代码和配置会发生一些变化,但数据库不需要任何更新。事实上,由于数据库是通过用户输入来更新的,所以我希望用户的更改在Chef运行期间保持不变。因此,我不想在没有充分理由的情况下删除并重新加载数据库

解决方案,改编自@coderanger的答案: execute是一种资源,当您希望从运行命令中获得某种结果时,它并没有真正的帮助。您需要的是使用shell_out帮助器方法:

shell_out("psql", "-d", dbname, "-c", "SELECT 1 as check_;").error?

或者类似的东西。与通常具有两次加载和执行过程的资源不同,shell_out和shell_out!是命令式Ruby代码,因此当该行运行时,它会运行命令并立即获得结果。查看有关加载模型的更多信息以及您的示例不起作用的原因。

txs。我会看看的。是的,两步模型是我知道我不太清楚的东西。另外值得注意的是,not_if foo和not_if{shell_outfoo.error?}的意思是一样的,除非我把布尔值倒过来,但你知道我的意思。因此,实际上您可能仍然需要一个not_if/only_if守卫,并且只使用const作为实际命令,这样您就不会在很多地方重复它。
==> default: STDERR: psql: FATAL:  database "pgfin92" does not exist
==> default: ---- End output of psql -d pgfin92 -c 'SELECT 1 as check_;' ----
==> default: Ran psql -d pgfin92 -c 'SELECT 1 as check_;' returned 2; ignore_failure is set, continuing
==> default:
==> default:     ================================================================================
==> default:     Error executing action `run` on resource 'execute[check_exist_db]'
==> default:     ================================================================================
==> default:
==> default:     Mixlib::ShellOut::ShellCommandFailed
==> default:     ------------------------------------
==> default:     Expected process to exit with [0], but received '2'
==> default:     ---- Begin output of psql -d pgfin92 -c 'SELECT 1 as check_;' ----
==> default:     STDOUT:
==> default:     STDERR: psql: FATAL:  database "pgfin92" does not exist
==> default:     ---- End output of psql -d pgfin92 -c 'SELECT 1 as check_;' ----
==> default:     Ran psql -d pgfin92 -c 'SELECT 1 as check_;' returned 2
#chef load: save list of databases that don't exist
dbtodo = []

%w{ hcm91dmo ksysdb pgfin92}.each do |dbname|
  
  #note that you need to specify a user that works for postgres
  if (shell_out("psql", "-d", dbname, "-c", "SELECT 1 as check_;",:user => "postgres").error?)
    dbtodo.push(dbname)
  end

end

#now, just loop thru databases that didn't exist
#I know I need to perform all the actions on each one of these.
dbtodo.each do |dbname|

  #execute phase, no conditional needed because we're running off
  #saved database list
  postgresql_database dbname do
    connection postgresql_connection_info
    action :create
  end

...
end
shell_out("psql", "-d", dbname, "-c", "SELECT 1 as check_;").error?