mysql - Chef : How to require ruby/mysql API in ruby_block?

标签 mysql ruby chef-infra cookbook

我是 ruby​​ 和 chef 的新手。我建立了一个 Chef 服务器并成功引导了一个节点。我想在没有 Mysql 和数据库说明书的情况下通过我的说明书访问节点上的 Mysql。我使用了 ruby​​/mysql API。当在单个 ruby​​ 文件中编程时,它运行良好。代码如下。

require "rubygems"
require "mysql"

begin
    con = Mysql.new("localhost", "root", "123")
    res = con.query("create database abc")
rescue Mysql::Error => e
    puts e.errno
    puts e.error
ensure
    con.close if con
end

但是,当我将此代码添加到我的 Recipe 中的 ruby​​_block 和 chef-client 时,我在节点上遇到了错误。

================================================================================
Error executing action `run` on resource 'ruby_block[test1]'
================================================================================

LoadError
---------
cannot load such file -- mysql

Cookbook Trace:
---------------
/var/chef/cache/cookbooks/db_test/recipes/default.rb:14:in `block (2 levels) in from_file'

Resource Declaration:
---------------------
# In /var/chef/cache/cookbooks/db_test/recipes/default.rb

 11: ruby_block "test1" do
 12:        block do
 13:                require "rubygems"
 14:                require "mysql"
 15:
 16:                begin
 17:                        con = Mysql.new("localhost", "root", "123")
 18:                        res = con.query("create database abc")
 19:                rescue Mysql::Error => e
 20:                        puts e.errno
 21:                        puts e.error
 22:                ensure
 23:                        con.close if con
 24:                end
 25:        end
 26: #      action :run

Compiled Resource:
------------------
# Declared in /var/chef/cache/cookbooks/db_test/recipes/default.rb:11:in `from_file'

ruby_block("test1") do
  action "run"
  retries 0
  retry_delay 2
  default_guard_interpreter :default
  block_name "test1"
  declared_type :ruby_block
  cookbook_name "db_test"
  recipe_name "default"
  block #<Proc:0x000000051907a8@/var/chef/cache/cookbooks/db_test/recipes/default.rb:12>
end


Running handlers:
[2015-04-07T22:14:12-04:00] ERROR: Running exception handlers
Running handlers complete
[2015-04-07T22:14:12-04:00] ERROR: Exception handlers complete
[2015-04-07T22:14:12-04:00] FATAL: Stacktrace dumped to /var/chef/cache/chef-stacktrace.out
Chef Client failed. 0 resources updated in 0.962999999 seconds
[2015-04-07T22:14:12-04:00] ERROR: ruby_block[test1] (db_test::default line 11) had an error: LoadError: cannot load such file -- mysql
[2015-04-07T22:14:12-04:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)

我不知道什么时候出了问题。有人可以帮忙吗?

最佳答案

Chef 在其自己的 ruby​​ 实例中运行(假设您已使用 omnibus 安装)。您需要使用 chef_gem 将 mysql gem 安装到 Chef ruby​​ 中。资源。然后你应该能够加载 mysql ruby​​

关于mysql - Chef : How to require ruby/mysql API in ruby_block?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29505082/

相关文章:

chef-infra - 无法使用knife命令删除节点属性

php - Laravel 5.1 如果记录存在于另一个表中则插入

javascript - php/mysql 时间数据检索并创建倒计时

Java JDBC UPDATE 添加号码?

php - 如何在 Ruby 中复制此 PHP 代码?

ruby-on-rails - 在这部分代码中设置 klass = self 的动机是什么

ruby - 我可以在方法签名中指定鸭子类型吗?

amazon-web-services - 当我们不提及节点名称时,Chef 如何获取 aws ec2 的实例 ID

javascript - 如何将 Url 变量与 php 变量匹配

ruby - 如何更改 Vagrant (VDD) VM 的 php 和 xdebug 版本?