【问题标题】:Get puppet build to fail when the contained SQL script fails execution当包含的 SQL 脚本执行失败时,使 puppet 构建失败
【发布时间】:2014-04-05 19:47:51
【问题描述】:

我正在尝试运行一个 vagrant build,它在 Ubuntu Virtualbox VM 中安装 Oracle XE,然后运行 ​​SQL 脚本来初始化 Oracle Schema。

vagrant build 在这里:https://github.com/ajorpheus/vagrant-ubuntu-oracle-xe setup.sql 作为 oracle 模块的 init.pp 的一部分运行(查看该文件的末尾附近或搜索“oracle-script”)。

我在运行这个 vagrant build 时遇到了一个错误,只是被告知“oracle-script”位在没有更多细节的情况下不成功。为了弄清楚“oracle-script”位是否有问题,我将其提取到test-sql.pp:

file {
    '/tmp/setup.sql':
      ensure => file,
      source => '/tmp/setup.sql';
  }

  exec { 'oracle-script':
    path => ['/bin', '/u01/app/oracle/product/11.2.0/xe/bin'],
    command => 'sqlplus system/manager@xe < /tmp/setup.sql',
    require => [ File['/tmp/setup.sql']],
    timeout => '0',
  }

现在我有另一个问题(这就是这个问题的意义所在)。 setup.sql 创建一个表空间,在随后的运行中,表空间创建应该会失败,puppet 脚本也会失败。

我正在运行以下命令

puppet apply --verbose --debug test-sql.pp

而且输出总是表明一个成功的结果:

debug: Exec[oracle-script](provider=posix): Executing 'sqlplus system/manager@xe < /tmp/setup.sql'
debug: Executing 'sqlplus system/manager@xe < /tmp/setup.sql'
notice: /Stage[main]//Exec[oracle-script]/returns: executed successfully

但是,如果我手动运行相同的 SQL 脚本:


问题

  1. 为什么即使包含的 SQL 脚本失败,Puppet 构建也不会失败?我应该查看 SQL Plus 的退出代码吗?

  2. 如何让 puppet 发出更详细的调试信息?我找到了this,并尝试了那里的建议。

  3. 在 RTFM 之后...我看到 Puppet 的 exec 必须是幂等的。但是,问题仍然存在,如果表空间已经存在,为什么 puppet 构建不会失败?

谢谢!

更新

添加 logoutput 后​​,按照 TheQ 下面的建议,我看到以下输出:

debug: Executing 'sqlplus system/manager@xe < /tmp/setup.sql'
notice: /Stage[main]//Exec[oracle-script]/returns:
notice: /Stage[main]//Exec[oracle-script]/returns: SQL*Plus: Release 11.2.0.2.0 Production on Sat Apr 5 16:02:37 2014
notice: /Stage[main]//Exec[oracle-script]/returns:
notice: /Stage[main]//Exec[oracle-script]/returns: Copyright (c) 1982, 2011, Oracle.  All rights reserved.
notice: /Stage[main]//Exec[oracle-script]/returns:
notice: /Stage[main]//Exec[oracle-script]/returns:
notice: /Stage[main]//Exec[oracle-script]/returns: Connected to:
notice: /Stage[main]//Exec[oracle-script]/returns: Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
notice: /Stage[main]//Exec[oracle-script]/returns:
notice: /Stage[main]//Exec[oracle-script]/returns: create tablespace some_INDEX_50M datafile '~\u01\some_INDEX_50M.dbf' size 50m
notice: /Stage[main]//Exec[oracle-script]/returns: *
notice: /Stage[main]//Exec[oracle-script]/returns: ERROR at line 1:
notice: /Stage[main]//Exec[oracle-script]/returns: ORA-01543: tablespace 'SOME_INDEX_50M' already exists
notice: /Stage[main]//Exec[oracle-script]/returns:
notice: /Stage[main]//Exec[oracle-script]/returns:
notice: /Stage[main]//Exec[oracle-script]/returns: Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
notice: /Stage[main]//Exec[oracle-script]/returns: executed successfully

【问题讨论】:

  • 根据调试输出,即使出现错误,sqlplus 似乎也返回“执行成功”。这里的问题是 sqlplus 报告成功。

标签: sql oracle puppet


【解决方案1】:

这并不能回答问题的所有部分,但如果您使用logoutput,您应该会看到来自 exec 命令的更多输出。默认情况下,仅当 exec 检测到失败时才会显示输出,而您的情况似乎没有发生这种情况。

因此,关于#2 尝试添加:

logoutput => 'true',

【讨论】:

  • 谢谢,这很有帮助。我现在可以看到脚本失败(构建仍然没有失败),但这是最有帮助的。 (自我注意:RTFM)。干杯!
  • 我将此标记为已接受的答案,因为它引导我诊断并修复问题的根本原因(即未设置环境变量)
【解决方案2】:

我相信 puppet 根据被调用程序的返回码检测脚本是否成功。默认情况下,当您关闭 sqlplus 时,它会返回 0,而不管会话期间可能运行了什么。

[oracle@bbdb2 ~]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.3.0 Production on Thu Apr 17 08:47:08 2014

Copyright (c) 1982, 2011, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> select asdjkhasd from sadbjaksd;
select asdjkhasd from sadbjaksd
                      *
ERROR at line 1:
ORA-00942: table or view does not exist


SQL> quit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
[oracle@bbdb2 ~]$ echo $?
0

如果你想让 sqlplus 以错误状态退出,你可以使用只要命令,例如

[oracle@bbdb2 ~]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.3.0 Production on Thu Apr 17 08:48:17 2014

Copyright (c) 1982, 2011, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> whenever sqlerror exit failure;
SQL> select bogus from nowhere;
select bogus from nowhere
                  *
ERROR at line 1:
ORA-00942: table or view does not exist


Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
[oracle@bbdb2 ~]$ echo $?
1

请注意后一种情况下的不同返回码。这应该足以让 puppet 知道命令失败了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-19
    • 1970-01-01
    • 2022-12-13
    • 1970-01-01
    • 2013-05-23
    • 2011-06-28
    相关资源
    最近更新 更多