如何在Chef Ruby块中执行MySQL

时间:2015-01-26 18:20:16

标签: mysql ruby chef

好的,我有一个调用Opscode MySQL食谱的食谱。

我可以创建数据库,但我不确定如何在Chef Recipe中创建具有读/写权限的MySQL用户。

这是我的食谱:

include_recipe 'percona' #this is an example

mysql_secret = Chef::EncryptedDataBagItem.load_secret("#{node[:percona][:encrypted_data_bag_secret_file]}")
mysql_creds = Chef::EncryptedDataBagItem.load("passwords", "mysql", mysql_secret)
pdns_password='1H@t3D@taB@G$!'
mysql_connection_info = {
  :host => "127.0.0.1",
  :username => 'root',
  :password => mysql_creds["root"]
}

#
# Only write to master pdb01
#
if node.name =~ /pdb01/
    dbs=['serv']
    # Create application database
    dbs.each do |db|
        ruby_block "create_#{db}_db" do
          block do
            %x[mysql -uroot  -e "CREATE DATABASE #{db};"]
          end
          not_if "mysql -u root  -e \"SHOW DATABASES LIKE '#{db}'\" | grep #{db}";
          action :create
        end
    end
end
# Grant mysql privileges
dbs.each do |db|
    ruby_block "add_servo_#{db}permissions" do
        block do
          %x[mysql -u root  -e "GRANT SELECT,INSERT,UPDATE,DELETE \
            ON #{db}.* TO 'serv'@'127.0.0.1' IDENTIFIED BY '#{servo_password}';"]
            end
            not_if "mysql -u root  -e \"SELECT user, host FROM mysql.user\" | \
              grep serv | grep #{db}"
            action :create
        end
end
#
# Create pdb02 slave replication
#

if node.name =~ /pdb02/
    dbs=['serv','serv_app'
    # Create application database
    dbs.each do |db|
        ruby_block "create_#{db}_db" do
          block do
            %x[mysql -uroot  -e "CREATE DATABASE #{db};"]
          end
          not_if "mysql -u root  -e \"SHOW DATABASES LIKE '#{db}'\" | grep #{db}";
          action :create
        end
    end
    dbs.each do |db|
        ruby_block "add_serv_#{db}permissions" do
            block do
              %x[mysql -u root  -e "GRANT SELECT,INSERT,UPDATE,DELETE \
                ON #{db}.* TO 'repl'@'127.0.0.1' IDENTIFIED BY 'replpass';"]
               GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%' IDENTIFIED BY 'replpass';
                end
                not_if "mysql -u root  -e \"SELECT user, host FROM mysql.user\" | \
                  grep repl | grep #{db}"
                action :create
            end
    end
end

AND - 它在这里失败

   SyntaxError
   -----------
   /tmp/kitchen/cache/cookbooks/cb-dataBases/recipes/pdb.rb:55: syntax error, unexpected tIDENTIFIER, expecting ']'

       dbs.each do |db|
          ^
   /tmp/kitchen/cache/cookbooks/cb-dataBases/recipes/pdb.rb:69: syntax error, unexpected '.'
   ...  GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%' IDENTIFIED BY...
   ...                               ^
   /tmp/kitchen/cache/cookbooks/cb-dataBases/recipes/pdb.rb:69: `@'' is not allowed as an instance variable name
   /tmp/kitchen/cache/cookbooks/cb-dataBases/recipes/pdb.rb:69: syntax error, unexpected $end, expecting keyword_end
   ...ICATION SLAVE ON *.* TO 'repl'@'%' IDENTIFIED BY 'replpass'; 
   ...                               ^

我做错了什么?

1 个答案:

答案 0 :(得分:1)

您的]错误。 %x[]必须围绕整个命令字符串。