From 2f4177068bf449bd1f59c479f780d8ab2705966b Mon Sep 17 00:00:00 2001 From: Robert Date: Sun, 21 Apr 2013 22:51:55 +0900 Subject: [PATCH] replace 'execute' with 'Mixlib::ShellOut' library. --- providers/clone.rb | 15 +++++++++------ providers/colocation.rb | 20 +++++++++----------- providers/location.rb | 14 +++++++++----- providers/ms.rb | 14 +++++++++----- providers/node.rb | 14 +++++++++----- providers/order.rb | 22 +++++++++++++--------- providers/primitive.rb | 22 +++++++++++----------- 7 files changed, 69 insertions(+), 52 deletions(-) diff --git a/providers/clone.rb b/providers/clone.rb index 5e73b90..5b5fc73 100644 --- a/providers/clone.rb +++ b/providers/clone.rb @@ -33,12 +33,16 @@ action :create do end end - e = execute "configure clone #{name}" do - command cmd + cmd_ = Mixlib::ShellOut.new(cmd) + cmd_.environment['HOME'] = ENV.fetch('HOME', '/root') + cmd_.run_command + begin + cmd.error! + new_resource.updated_by_last_action(true) + Chef::Log.info "Successfully configured clone '#{name}'." + rescue + Chef::Log.error "Failed to configure clone #{name}." end - - new_resource.updated_by_last_action(true) - Chef::Log.info "configured clone '#{name}'." end end @@ -53,5 +57,4 @@ action :delete do new_resource.updated_by_last_action(true) Chef::Log.info "Deleted clone '#{name}'." - end diff --git a/providers/colocation.rb b/providers/colocation.rb index 24ee83a..674e7e5 100644 --- a/providers/colocation.rb +++ b/providers/colocation.rb @@ -32,24 +32,22 @@ action :create do multiple_rscs.each do |rsc| cmd << " #{rsc}" end - - e = execute "configure colocation #{name}" do - command cmd - end - - new_resource.updated_by_last_action(true) - Chef::Log.info "Configured colocation '#{name}'." else rsc = new_resource.rsc with_rsc = new_resource.with_rsc cmd = "crm configure colocation #{name} #{priority}: #{rsc} #{with_rsc}" - e = execute "configure colocation #{name}" do - command cmd - end + end + cmd_ = Mixlib::ShellOut.new(cmd) + cmd_.environment['HOME'] = ENV.fetch('HOME', '/root') + cmd_.run_command + begin + cmd.error! new_resource.updated_by_last_action(true) - Chef::Log.info "Configured colocation '#{name}'." + Chef::Log.info "Successfully configured colocation '#{name}'." + rescue + Chef::Log.error "Failed to configure colocation #{name}." end end end diff --git a/providers/location.rb b/providers/location.rb index 4b29a24..2b068ec 100644 --- a/providers/location.rb +++ b/providers/location.rb @@ -28,12 +28,16 @@ action :create do unless resource_exists?(name) cmd = "crm configure location #{name} #{rsc} #{priority}: #{loc}" - e = execute "configure location #{name}" do - command cmd + cmd_ = Mixlib::ShellOut.new(cmd) + cmd_.environment['HOME'] = ENV.fetch('HOME', '/root') + cmd_.run_command + begin + cmd.error! + new_resource.updated_by_last_action(true) + Chef::Log.info "Successfully configured location '#{name}'." + rescue + Chef::Log.error "Failed to configure location #{name}." end - - new_resource.updated_by_last_action(true) - Chef::Log.info "Configured location '#{name}'." end end diff --git a/providers/ms.rb b/providers/ms.rb index c527463..978cfe3 100644 --- a/providers/ms.rb +++ b/providers/ms.rb @@ -33,12 +33,16 @@ action :create do end end - e = execute "configure ms #{name}" do - command cmd + cmd_ = Mixlib::ShellOut.new(cmd) + cmd_.environment['HOME'] = ENV.fetch('HOME', '/root') + cmd_.run_command + begin + cmd.error! + new_resource.updated_by_last_action(true) + Chef::Log.info "Successfully configured ms '#{name}'." + rescue + Chef::Log.error "Failed to configure ms #{name}." end - - new_resource.updated_by_last_action(true) - Chef::Log.info "configured ms '#{name}'." end end diff --git a/providers/node.rb b/providers/node.rb index 5dd623c..6033d84 100644 --- a/providers/node.rb +++ b/providers/node.rb @@ -25,12 +25,16 @@ action :add do unless resource_exists?(name) cmd = "crm configure node #{name}" - e = execute "add node #{name}" do - command cmd + cmd_ = Mixlib::ShellOut.new(cmd) + cmd_.environment['HOME'] = ENV.fetch('HOME', '/root') + cmd_.run_command + begin + cmd.error! + new_resource.updated_by_last_action(true) + Chef::Log.info "Successfully configured node '#{name}'." + rescue + Chef::Log.error "Failed to configure node #{name}." end - - new_resource.updated_by_last_action(true) - Chef::Log.info "Node '#{name}' has been added.." end end diff --git a/providers/order.rb b/providers/order.rb index fa8fed6..b564cc0 100644 --- a/providers/order.rb +++ b/providers/order.rb @@ -25,17 +25,21 @@ action :create do resources = new_resource.resources unless resource_exists?(name) - cmd = "crm configure order #{name} #{priority}:" - resources.each do |rsc| - cmd << " #{rsc}" - end - - e = execute "configure order #{name}" do - command cmd - end + cmd = "crm configure order #{name} #{priority}:" + resources.each do |rsc| + cmd << " #{rsc}" + end + cmd_ = Mixlib::ShellOut.new(cmd) + cmd_.environment['HOME'] = ENV.fetch('HOME', '/root') + cmd_.run_command + begin + cmd.error! new_resource.updated_by_last_action(true) - Chef::Log.info "Configured order '#{name}'." + Chef::Log.info "Successfully configured order '#{name}'." + rescue + Chef::Log.error "Failed to configure order #{name}." + end end end diff --git a/providers/primitive.rb b/providers/primitive.rb index 2edbb82..be2416a 100644 --- a/providers/primitive.rb +++ b/providers/primitive.rb @@ -53,18 +53,18 @@ action :create do end end - e = execute "configure primitive #{name}" do - command cmd +# 'Execute' resource doesn't throw exception even when command fails.. +# So, Mixlib::ShellOut was used instead. + cmd_ = Mixlib::ShellOut.new(cmd) + cmd_.environment['HOME'] = ENV.fetch('HOME', '/root') + cmd_.run_command + begin + cmd.error! + new_resource.updated_by_last_action(true) + Chef::Log.info "Successfully configured primitive '#{name}'." + rescue + Chef::Log.error "Failed to configure primitive #{name}." end - -# With the below commands, 'e.updated?' is always 'false' even though the execute command ran successfully. -# new_resource.updated_by_last_action(e.updated?) -# if e.updated? -# Chef::Log.info "Done creating primitive '#{name}'." -# end - - new_resource.updated_by_last_action(true) - Chef::Log.info "Configured primitive '#{name}'." end end