Class: InstanceAgent::Plugins::CodeDeployPlugin::HookExecutor

Inherits:
Object
  • Object
show all
Defined in:
lib/instance_agent/plugins/codedeploy/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.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/instance_agent/plugins/codedeploy/hook_executor.rb', line 64

def initialize(arguments = {})
  #check arguments
  raise "Lifecycle Event Required " if arguments[:lifecycle_event].nil?
  raise "Deployment ID required " if arguments[:deployment_id].nil?
  raise "Deployment Root Directory Required " if arguments[:deployment_root_dir].nil?
  raise "App Spec Path Required " if arguments[:app_spec_path].nil?
  raise "Application name required" if arguments[:application_name].nil?
  raise "Deployment Group name required" if arguments[:deployment_group_name].nil?
  @lifecycle_event = arguments[:lifecycle_event]
  @deployment_id = arguments[:deployment_id]
  @application_name = arguments[:application_name]
  @deployment_group_name = arguments[:deployment_group_name]
  @deployment_group_id = arguments[:deployment_group_id]
  @current_deployment_root_dir = arguments[:deployment_root_dir]
  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
  @child_envs={'LIFECYCLE_EVENT' => @lifecycle_event.to_s,
              'DEPLOYMENT_ID'   => @deployment_id.to_s,
              'APPLICATION_NAME' => @application_name,
              'DEPLOYMENT_GROUP_NAME' => @deployment_group_name,
              'DEPLOYMENT_GROUP_ID' => @deployment_group_id}
end

Instance Method Details

#executeObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/instance_agent/plugins/codedeploy/hook_executor.rb', line 92

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: ' + 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
          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