How to check if a database is populated(in work state) for idempotent recipes - best practic question


#1

I write redmine cookbook for our server.

I populate database for redmine (I’m doing it this way):

script “populate_db” do
interpreter "bash"
user "root"
cwd "/tmp"
code <<-EOF

   #!/bin/bash
   var=$(mysql -s -u root 

–password=#{node[‘mysql’][‘server_root_password’]} -e “SELECT COUNT(*)
FROM information_schema.tables WHERE table_schema = ‘redmine’” | sed ‘2p’)

   if [[ $var -lt 59 ]]; then
           mysql -u root 

–password=#{node[‘mysql’][‘server_root_password’]} <
/etc/custom/redmine/mysql/latest/redmine.sql
if [[ $? -eq 0 ]]; then exit 0; else exit 6666; fi
else
exit 0;
fi

EOF
end


I think that this way is not true. First of all I am newbee in Ruby (.
But I know some of bash scripting.

What do you think for:

  1. Most safely method to populate database in my recipe. For example
    bestest method to check if the database is in working state?
    Of course I can add some code to save database before this code -
    for secure my data.
  2. Most perspective methods for doing this
  3. What do you think about populate database in automated recipe. May be
    this is not true and i must do it manual after all ?

Thank you.


Best regards,

CVisionLab System Administrator
Vladmir Skubriev


#2

I think you’ve got the right idea. In order to be convergent, you do need to run a test to determine whether the schema is present in the database before attempting to write it.

However, instead of trying to do it in bash, you might try using the primitives in the database cookbook.

Thanks,
–Charles

Charles Johnson — Solutions Engineer
(510) 454-9485 – charles@opscode.com – my: Linkedin Twitter

OPSCODE
CODE CAN
opscode.com Blog Facebook Twitter YouTube

On September 16, 2013 at 5:06:54 AM, Vladimir Skubriev (skubriev@cvisionlab.com) wrote:

I write redmine cookbook for our server.

I populate database for redmine (I’m doing it this way):

script “populate_db” do
interpreter "bash"
user "root"
cwd "/tmp"
code <<-EOF

#!/bin/bash
var=$(mysql -s -u root
–password=#{node[‘mysql’][‘server_root_password’]} -e “SELECT COUNT(*)
FROM information_schema.tables WHERE table_schema = ‘redmine’” | sed ‘2p’)

if [[ var -lt 59 ]]; then mysql -u root --password=#{node['mysql']['server_root_password']} < /etc/custom/redmine/mysql/latest/redmine.sql if [[ ? -eq 0 ]]; then exit 0; else exit 6666; fi
else
exit 0;
fi

EOF
end


I think that this way is not true. First of all I am newbee in Ruby (.
But I know some of bash scripting.

What do you think for:

  1. Most safely method to populate database in my recipe. For example
    bestest method to check if the database is in working state?
    Of course I can add some code to save database before this code -
    for secure my data.
  2. Most perspective methods for doing this
  3. What do you think about populate database in automated recipe. May be
    this is not true and i must do it manual after all ?

Thank you.


Best regards,

CVisionLab System Administrator
Vladmir Skubriev