简体   繁体   中英

catch mysql STDOUT error from ruby system method or backticks `` system execution (Chef)

This question kind of involves many things and i don't know from where to make a workaround. I found a solution, but i think it is a nasty one.

i have this LWRP that installs mysql and as the last step i update the password if it is provided and it is not defined before:

ruby_block "Changing password for root" do
        block do
            if !(root_password.nil? || system("\"#{installation_path}\\bin\\mysql.exe\" -u root -p#{root_password} --execute \"exit\""))
                # In order to allow the service to completely start and then change the passowrd
                # 
                sleep 30
                change_pass_str = "\"#{installation_path}\\bin\\mysql.exe\" -u root --execute \"UPDATE mysql.user SET Password=PASSWORD('#{root_password}') WHERE User='root';FLUSH PRIVILEGES;\""
                password_set_result = system(change_pass_str)

            else
                puts !password_set_result ? "Password wasn't changed since root already have a password defined. Maybe there's still data from a previous installation." : "Password has been set!"
                `dir`
            end

        end
end

when this resource is executed in chef it prints or throws or STDOUT or i don't know how to call it, the next lines:

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: Yes)

that line is not captured if i execute the mysql call with `` (backticks), i mean, something like this:

irb(main):002:0> response = `mysql.exe -u root -pdevtest --execute \"exit\"`
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
=> ""
irb(main):003:0> puts response
=> ""

only returns "", instead of returning the error string itself.

the thing is that when i later (in the recipe cookbook that implements mysql installation and does other stuff with subversion) execute subversion resource with action :export (i think it would fail with any other action), it fails telling that it returns exit status 1 , but it doesn't is the exit status that mysql throw.

if i add a dir system call (as you can see in the ruby block), subversion resource passes without any problem as the dir system call (i think) resets the exit status to 0.

The dir system call, i guess is a really nasty way to work around this, that's why i'm asking for your help, how do avoid mysql to throw (or STDOUT) that thing, or how do i catch it or reset the exit status ?

A lot of things here.

First I would bet the ERROR from mysql is sent to stderr and not stdout, it is why its not captured, you may redirect it to stdout with the classic 2&>1 redirection to capture the error.

The exit status is the last command status, mysql had an error and so set its exit status to something different from 0.

All in all, you should avoid system call in chef recipe and use Mixin::ShellOut instead wich give you more control over what you run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM