From 53a0365348c679a6fa9c05c1f4f862b6060a7e6d Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Thu, 30 Jun 2022 00:31:12 +0200 Subject: [PATCH] Log a message when a lifecycle event finished --- .../plugins/codedeploy/hook_executor.rb | 42 ++++++++++--------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/lib/instance_agent/plugins/codedeploy/hook_executor.rb b/lib/instance_agent/plugins/codedeploy/hook_executor.rb index 631747f4..c74d7dd5 100644 --- a/lib/instance_agent/plugins/codedeploy/hook_executor.rb +++ b/lib/instance_agent/plugins/codedeploy/hook_executor.rb @@ -113,28 +113,32 @@ def execute if (hooks = @app_spec.hooks[@lifecycle_event]) && !hooks.empty? create_script_log_file_if_needed do |script_log_file| - log_script("LifecycleEvent - " + @lifecycle_event + "\n", script_log_file) - hooks.each do |script| - if(!File.exist?(script_absolute_path(script))) - raise ScriptError.new(ScriptError::SCRIPT_MISSING_CODE, script.location, @script_log), 'Script does not exist at specified location: ' + File.expand_path(script_absolute_path(script)) - elsif(!InstanceAgent::Platform.util.script_executable?(script_absolute_path(script))) - log :warn, 'Script at specified location: ' + script.location + ' is not executable. Trying to make it executable.' + begin + log_script("LifecycleEvent - " + @lifecycle_event + "\n", script_log_file) + hooks.each do |script| + if(!File.exist?(script_absolute_path(script))) + raise ScriptError.new(ScriptError::SCRIPT_MISSING_CODE, script.location, @script_log), 'Script does not exist at specified location: ' + File.expand_path(script_absolute_path(script)) + elsif(!InstanceAgent::Platform.util.script_executable?(script_absolute_path(script))) + log :warn, 'Script at specified location: ' + script.location + ' is not executable. Trying to make it executable.' + begin + FileUtils.chmod("+x", script_absolute_path(script)) + rescue + raise ScriptError.new(ScriptError::SCRIPT_EXECUTABILITY_CODE, script.location, @script_log), 'Unable to set script at specified location: ' + script.location + ' as executable' + end + end begin - FileUtils.chmod("+x", script_absolute_path(script)) - rescue - raise ScriptError.new(ScriptError::SCRIPT_EXECUTABILITY_CODE, script.location, @script_log), 'Unable to set script at specified location: ' + script.location + ' as executable' + execute_script(script, script_log_file) + rescue Timeout::Error + raise ScriptError.new(ScriptError::SCRIPT_TIMED_OUT_CODE, script.location, @script_log), 'Script at specified location: ' +script.location + ' failed to complete in '+script.timeout.to_s+' seconds' + rescue ScriptError + raise + rescue StandardError => e + script_error = "#{script_error_prefix(script.location, script.runas)} failed with error #{e.class} with message #{e}" + raise ScriptError.new(ScriptError::SCRIPT_FAILED_CODE, script.location, @script_log), script_error end end - begin - execute_script(script, script_log_file) - rescue Timeout::Error - raise ScriptError.new(ScriptError::SCRIPT_TIMED_OUT_CODE, script.location, @script_log), 'Script at specified location: ' +script.location + ' failed to complete in '+script.timeout.to_s+' seconds' - rescue ScriptError - raise - rescue StandardError => e - script_error = "#{script_error_prefix(script.location, script.runas)} failed with error #{e.class} with message #{e}" - raise ScriptError.new(ScriptError::SCRIPT_FAILED_CODE, script.location, @script_log), script_error - end + ensure + log_script("LifecycleEvent finished - " + @lifecycle_event + "\n", script_log_file) end end end