Class: InstanceAgent::CodeDeployPlugin::HookExecutor

Inherits:
Object
  • Object
show all
Defined in:
lib/instance_agent/codedeploy_plugin/hook_executor.rb

Constant Summary collapse

LAST_SUCCESSFUL_DEPLOYMENT =
"OldOrIgnore"
CURRENT =
"New"

Instance Method Summary collapse

Constructor Details

#initialize(arguments = {}) ⇒ HookExecutor

Returns a new instance of HookExecutor.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/instance_agent/codedeploy_plugin/hook_executor.rb', line 65

def initialize(arguments = {})
  #check arguments
  raise "Lifecycle Event Required " if arguments[:lifecycle_event].nil?
  raise "Deployment Root Directory Required " if arguments[:deployment_root_dir].nil?
  raise "App Spec Path Required " if arguments[:app_spec_path].nil?
  @lifecycle_event = arguments[:lifecycle_event]
  select_correct_deployment_root_dir(arguments[:deployment_root_dir], arguments[:last_successful_deployment_dir])
  return if @deployment_root_dir.nil?
  @deployment_archive_dir = File.join(@deployment_root_dir, 'deployment-archive')
  @app_spec_path = arguments[:app_spec_path]
  parse_app_spec
  @hook_logging_mutex = Mutex.new
  @script_log = ScriptLog.new
end

Instance Method Details

#executeObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/instance_agent/codedeploy_plugin/hook_executor.rb', line 80

def execute
  return if @app_spec.nil?
  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: ' + script.location
        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
          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'
        end
      end
    end
  end
  @script_log.log
end