Class: InstanceAgent::CodeDeployPlugin::CommandExecutor

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

Constant Summary collapse

InvalidCommandNameFailure =
Class.new(Exception)

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CommandExecutor

Returns a new instance of CommandExecutor.



24
25
26
27
28
29
30
31
# File 'lib/instance_agent/codedeploy_plugin/command_executor.rb', line 24

def initialize(options = {})
  @deployment_system = "CodeDeploy"
  @deploy_control_client = options[:deploy_control_client]
  @hook_mapping = options[:hook_mapping]
  if(!@hook_mapping.nil?)
    map
  end
end

Class Attribute Details

.command_methodsObject (readonly)

Returns the value of attribute command_methods.



17
18
19
# File 'lib/instance_agent/codedeploy_plugin/command_executor.rb', line 17

def command_methods
  @command_methods
end

Instance Attribute Details

#deployment_systemObject (readonly)

Returns the value of attribute deployment_system.



20
21
22
# File 'lib/instance_agent/codedeploy_plugin/command_executor.rb', line 20

def deployment_system
  @deployment_system
end

Class Method Details

.command(name, &blk) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/instance_agent/codedeploy_plugin/command_executor.rb', line 33

def self.command(name, &blk)
  @command_methods ||= Hash.new

  method = Seahorse::Util.underscore(name).to_sym
  @command_methods[name] = method

  define_method(method, &blk)
end

Instance Method Details

#command_method(command_name) ⇒ Object



56
57
58
59
# File 'lib/instance_agent/codedeploy_plugin/command_executor.rb', line 56

def command_method(command_name)
  raise InvalidCommandNameFailure.new("Unsupported command type: #{command_name}.") unless self.class.command_methods.has_key?(command_name)
  self.class.command_methods[command_name]
end

#execute_command(command, deployment_specification) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/instance_agent/codedeploy_plugin/command_executor.rb', line 42

def execute_command(command, deployment_specification)
  method_name = command_method(command.command_name)
  log(:debug, "Command #{command.command_name} maps to method #{method_name}")

  deployment_specification = DeploymentSpecification.parse(deployment_specification)
  log(:debug, "Successfully parsed the deployment spec")

  log(:debug, "Creating deployment root directory #{deployment_root_dir(deployment_specification)}")
  FileUtils.mkdir_p(deployment_root_dir(deployment_specification))
  raise "Error creating deployment root directory #{deployment_root_dir(deployment_specification)}" if !File.directory?(deployment_root_dir(deployment_specification))

  send(method_name, command, deployment_specification)
end

#mapObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/instance_agent/codedeploy_plugin/command_executor.rb', line 110

def map
  @hook_mapping.each_pair do |command, lifecycle_events|
    InstanceAgent::CodeDeployPlugin::CommandExecutor.command command do |cmd, deployment_spec|
      #run the scripts
      script_log = ScriptLog.new
      lifecycle_events.each do |lifecycle_event|
        hook_command = HookExecutor.new(:lifecycle_event => lifecycle_event,
                                        :deployment_root_dir => deployment_root_dir(deployment_spec),
                                        :last_successful_deployment_dir => last_successful_deployment_dir(deployment_spec.deployment_group_id),
                                        :app_spec_path => app_spec_path)
        script_log.concat_log(hook_command.execute)
      end
      script_log.log
    end
  end
end