使用Chef运行所有SQL文件 [英] Run all sql files with Chef

查看:83
本文介绍了使用Chef运行所有SQL文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我需要帮助,我做了一个命令,该命令应该读取sql_dumps文件夹内的所有sql文件,但是它不起作用..这就是我得到的。

 执行 Run_SQL_Files执行
dirResults = Dir.glob( / tmp / sql_dumps / *。sql)
var = 0
而var< 15 do
var = var + 1
命令 mysql --user = root --password = toomba源 + dirResults [var]
#已经尝试过
#命令 mysql --user = root --password = toomba< dirResults [var]
将dirResults [var]
结束
结束

我对红宝石不是很熟悉。这是我收到的错误

 默认值:Errno :: ENOENT 
默认值:-------- -----
默认值:无此类文件或目录-Run_SQL_Files
默认值:
默认值:资源声明:
默认值:----------- ----------
默认值:#在/ tmp / vagrant-chef-3 / chef-solo-1 / cookbooks / main / recip中
默认值.rb
默认值:
缺省值:214:
缺省值:215:执行 Run_SQL_Files执行
缺省值:216:dirResults = Dir.glob( / tmp / sql_dumps / *。sql)
默认值:217:var = 0
默认值:218:而var< 15 do
默认值:219:var = var + 1
默认值:220:将`mysql --user = root --password = toomba source
{dirResults [var]}`
默认值:221:puts dirResults [var]
默认值:222:end
默认值:223:#command mysql --user = root --password = toomba<
iles
默认值:224:结束

预先感谢!

解决方案

对于Chef如何编译资源存在误解。您期望Chef执行15次命令,但这不是Chef的操作方式。 Chef在两个阶段中运行-执行阶段和编译阶段。在编译阶段(首先运行),将评估Ruby并将资源添加到资源集合中。除了某些例外,此阶段不会更改系统状态。因此,考虑到您的配方:

 执行 Run_SQL_Files即可执行
dirResults = Dir.glob ( /tmp/sql_dumps/*.sql)
var = 0
而var< 15 do
var = var + 1
命令 mysql --user = root --password = toomba源 + dirResults [var]
#已经尝试过
#命令 mysql --user = root --password = toomba< dirResults [var]
将dirResults [var]
结束
结束

在功能上等效于这样编写的食谱(在编译阶段完成之后)



< pre class = lang-rb prettyprint-override> 执行 Run_SQL_Files执行
命令 mysql --user = root --password = toomba源/tmp/sql_dumps/15.sql
结束

请注意,厨师只会使用 last



使用 command 属性的值是因为Chef分两个阶段执行(如上所述)。资源定义中的条件逻辑和循环几乎总是会引起问题,在此示例中,您需要编译命令执行资源的外部。您要执行的每个SQL命令都必须具有自己的 execute 块。这是一个简单的重构示例:

  Dir [ / tmp / sql_dumps / *。sql ] .each做| path | 
执行 run_sql _#{path}做
命令 mysql --user = root --password = toomba<#{path}
结束
结束

这会将15个(从OP中假定)执行资源放入资源集合中,并按顺序执行。


Hello i need help i made a command that should read all the sql files inside of the sql_dumps folder but it isnt working.. here is what i got.

execute "Run_SQL_Files" do
  dirResults = Dir.glob("/tmp/sql_dumps/*.sql") 
  var = 0
  while var < 15 do
    var = var + 1 
    command "mysql --user=root --password=toomba source" + dirResults[var]
    # Already tried this also
    # command "mysql --user=root --password=toomba < "  dirResults[var]
    puts dirResults[var]
  end
end

I'am not very familiar with ruby. This is the error i received

 default: Errno::ENOENT
 default: -------------
 default: No such file or directory - Run_SQL_Files
 default:
 default: Resource Declaration:
 default: ---------------------
 default: # In /tmp/vagrant-chef-3/chef-solo-1/cookbooks/main/recip
 default.rb
 default:
 default: 214:
 default: 215: execute "Run_SQL_Files" do
 default: 216:   dirResults = Dir.glob("/tmp/sql_dumps/*.sql")
 default: 217:   var = 0
 default: 218:   while var < 15 do
 default: 219:     var = var + 1
 default: 220:     puts `mysql --user=root --password=toomba source
 {dirResults[var]}`
 default: 221:     puts dirResults[var]
 default: 222:   end
 default: 223:   #command "mysql --user=root --password=toomba < "
 iles
 default: 224: end

Thanks in advance!

解决方案

There is a misconception here about how Chef compiles resources. You are expecting Chef to execute the command 15 times, but that's not how Chef operates. Chef runs in two phases - the execution phase, and the compilation phase. During the compilation phase (which runs first), the Ruby is evaluated and resources are added to the resource collection. With some exceptions, this phase does not alter the state of the system. So given your recipe:

execute "Run_SQL_Files" do
  dirResults = Dir.glob("/tmp/sql_dumps/*.sql") 
  var = 0
  while var < 15 do
    var = var + 1 
    command "mysql --user=root --password=toomba source" + dirResults[var]
    # Already tried this also
    # command "mysql --user=root --password=toomba < "  dirResults[var]
    puts dirResults[var]
  end
end

That is functionally equivalent to a recipe that was written like this (after the compilation phase completes)"

execute "Run_SQL_Files" do
  command "mysql --user=root --password=toomba source /tmp/sql_dumps/15.sql"
end

Notice that Chef is only going to utilize the last value for the command attribute. That is because Chef executes in two phases (as already mentioned).

Using conditional logic and loops inside of a resource definition is almost always going to cause problems. In this example, you need to compile the command outside of the execute resource. Each SQL command you want to execute needs to have it's own execute block. Here's a simple, refactored example:

Dir["/tmp/sql_dumps/*.sql"].each do |path|
  execute "run_sql_#{path}" do
    command "mysql --user=root --password=toomba < #{path}"
  end
end

This will put 15 (assumption from OP) execute resources in the resource collection, and execute them in order.

这篇关于使用Chef运行所有SQL文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆