From: Tom Hughes Date: Tue, 31 Jan 2017 11:08:15 +0000 (+0000) Subject: Replace run_command and output_of_command with shell_out! X-Git-Url: https://git.openstreetmap.org./chef.git/commitdiff_plain/a439230240ac55952f5bbf40312c3116674a9cd5?ds=sidebyside Replace run_command and output_of_command with shell_out! --- diff --git a/cookbooks/chef/libraries/subversion.rb b/cookbooks/chef/libraries/subversion.rb index 4f2b1cd00..dc84788a9 100644 --- a/cookbooks/chef/libraries/subversion.rb +++ b/cookbooks/chef/libraries/subversion.rb @@ -1,6 +1,10 @@ +require "chef/mixin/shell_out" + class Chef class Provider class Subversion + extend Chef::Mixin::ShellOut + def sync_command if current_repository_matches_target_repository? c = scm :update, @new_resource.svn_arguments, verbose, authentication, "-r#{revision_int}", @new_resource.destination @@ -34,13 +38,7 @@ class Chef def svn_info command = scm(:info) - status, svn_info, error_message = output_of_command(command, run_options(:cwd => cwd)) - - unless [0, 1].include?(status.exitstatus) - handle_command_failures(status, "STDOUT: #{svn_info}\nSTDERR: #{error_message}") - end - - svn_info + shell_out!(command, run_options(:cwd => cwd)).stdout end end end diff --git a/cookbooks/mysql/libraries/mysql.rb b/cookbooks/mysql/libraries/mysql.rb index 61d600f1d..dc692a780 100644 --- a/cookbooks/mysql/libraries/mysql.rb +++ b/cookbooks/mysql/libraries/mysql.rb @@ -1,9 +1,9 @@ -require "chef/mixin/command" +require "chef/mixin/shell_out" require "rexml/document" class Chef class MySQL - include Chef::Mixin::Command + include Chef::Mixin::ShellOut USER_PRIVILEGES = [ :select, :insert, :update, :delete, :create, :drop, :reload, @@ -26,41 +26,39 @@ class Chef # Work out how to authenticate if options[:user] - args.push("--username=#{options[:user]}") - args.push("--password=#{options[:password]}") if options[:password] + args.push("--username") + args.push(options[:user]) + + if options[:password] + args.push("--password") + args.push(options[:password]) + end else args.push("--defaults-file=/etc/mysql/debian.cnf") end - # Build the other arguments - args.push("--execute=\"#{options[:command]}\"") if options[:command] - - # Get the database to use - database = options[:database] || "mysql" + # Set output format + args.push("--xml") if options[:xml] - # Build the command to run - command = "/usr/bin/mysql #{args.join(' ')} #{database}" + # Add any SQL command to execute + if options[:command] + args.push("--execute") + args.push(options[:command]) + end - # Escape backticks in the command - command.gsub!(/`/, "\\\\`") + # Add the database name + args.push(options[:database] || "mysql") # Run the command - run_command(:command => command, :user => "root", :group => "root") + shell_out!("/usr/bin/mysql", *args, :user => "root", :group => "root") end def query(sql, options = {}) - # Get the database to use - database = options[:database] || "mysql" - - # Construct the command string - command = "/usr/bin/mysql --defaults-file=/etc/mysql/debian.cnf --xml --execute='#{sql}' #{database}" - # Run the query - status, stdout, stderr = output_of_command(command, :user => "root", :group => "root") - handle_command_failures(status, "STDOUT: #{stdout}\nSTDERR: #{stderr}", :output_on_failure => true) + result = execute(options.merge(:command => sql, :xml => :true)) # Parse the output - document = REXML::Document.new(stdout) + document = REXML::Document.new(result.stdout) # Create records = [] diff --git a/cookbooks/postgresql/libraries/postgresql.rb b/cookbooks/postgresql/libraries/postgresql.rb index 5d2007424..70d38135b 100644 --- a/cookbooks/postgresql/libraries/postgresql.rb +++ b/cookbooks/postgresql/libraries/postgresql.rb @@ -1,8 +1,8 @@ -require "chef/mixin/command" +require "chef/mixin/shell_out" class Chef class PostgreSQL - include Chef::Mixin::Command + include Chef::Mixin::ShellOut TABLE_PRIVILEGES = [ :select, :insert, :update, :delete, :truncate, :references, :trigger @@ -16,37 +16,42 @@ class Chef # Create argument array args = [] - # Build the arguments - args.push("--command=\"#{options[:command].gsub('"', '\\"')}\"") if options[:command] - args.push("--file=#{options[:file]}") if options[:file] + # Add the cluster + args.push("--cluster") + args.push(@cluster) - # Get the database to use - database = options[:database] || "template1" + # Set output format + args.push("--no-align") unless options.fetch(:align, true) - # Build the command to run - command = "/usr/bin/psql --cluster #{@cluster} #{args.join(' ')} #{database}" + # Add any SQL command to execute + if options[:command] + args.push("--command") + args.push(options[:command]) + end + + # Add any file to execute SQL commands from + if options[:file] + args.push("--file") + args.push(options[:file]) + end + + # Add the database name + args.push(options[:database] || "template1") # Get the user and group to run as user = options[:user] || "postgres" group = options[:group] || "postgres" # Run the command - run_command(:command => command, :user => user, :group => group) + shell_out!("/usr/bin/psql", *args, :user => user, :group => group) end def query(sql, options = {}) - # Get the database to use - database = options[:database] || "template1" - - # Construct the command string - command = "/usr/bin/psql --cluster #{@cluster} --no-align --command='#{sql}' #{database}" - # Run the query - status, stdout, stderr = output_of_command(command, :user => "postgres", :group => "postgres") - handle_command_failures(status, "STDOUT: #{stdout}\nSTDERR: #{stderr}", :output_on_failure => true) + result = execute(options.merge(:command => sql, :align => false)) # Split the output into lines - lines = stdout.split("\n") + lines = result.stdout.split("\n") # Remove the "(N rows)" line from the end lines.pop diff --git a/cookbooks/wordpress/libraries/wordpress.rb b/cookbooks/wordpress/libraries/wordpress.rb index e83ba0417..64c5c5531 100644 --- a/cookbooks/wordpress/libraries/wordpress.rb +++ b/cookbooks/wordpress/libraries/wordpress.rb @@ -1,11 +1,11 @@ -require "chef/mixin/command" +require "chef/mixin/shell_out" require "httpclient" require "php_serialize" class Chef module Wordpress - extend Chef::Mixin::Command + extend Chef::Mixin::ShellOut @api_responses = {} @svn_responses = {} @@ -35,10 +35,9 @@ class Chef def svn_cat(url) unless @svn_responses[url] - status, stdout, stderr = output_of_command("svn cat #{url}", {}) - handle_command_failures(status, "STDOUT: #{stdout}\nSTDERR: #{stderr}", :output_on_failure => true) + result = shell_out!("svn", "cat", url) - @svn_responses[url] = stdout.force_encoding("UTF-8") + @svn_responses[url] = result.stdout.force_encoding("UTF-8") end @svn_responses[url]