Skip to content

Commit

Permalink
Add support for using deployment spec values within appspec.yml file.…
Browse files Browse the repository at this point in the history
  • Loading branch information
ejhayes committed Jun 22, 2018
1 parent 81ffec2 commit 6aa99fd
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
require 'instance_agent/plugins/codedeploy/application_specification/file_info'
require 'instance_agent/plugins/codedeploy/application_specification/linux_permission_info'
require 'instance_agent/plugins/codedeploy/application_specification/mode_info'
require 'ostruct'
require 'erb'

module InstanceAgent
module Plugins
Expand All @@ -23,7 +25,10 @@ def initialize(yaml_hash, opts = {})
@permissions = parse_permissions(yaml_hash['permissions'] || [])
end

def self.parse(app_spec_string)
def self.parse(app_spec_template_string, deployment_spec)
# make deployment_spec keys available to the yaml template
app_spec_string = ERB.new(app_spec_template_string || '').result(OpenStruct.new(deployment_spec).instance_eval { binding })

new(YAML.load(app_spec_string))
end

Expand Down
8 changes: 8 additions & 0 deletions lib/instance_agent/plugins/codedeploy/command_executor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,14 @@ def most_recent_deployment_dir(deployment_group)
def default_app_spec(deployment_spec)
default_app_spec_location = File.join(archive_root_dir(deployment_spec), app_spec_path)
log(:debug, "Checking for app spec in #{default_app_spec_location}")
app_spec = ApplicationSpecification::ApplicationSpecification.parse(File.read(default_app_spec_location), {
:application_name => deployment_spec.application_name,
:deployment_id => deployment_spec.deployment_id,
:deployment_group_name => deployment_spec.deployment_group_name,
:deployment_group_id => deployment_spec.deployment_group_id,
:deployment_root_dir => deployment_root_dir(deployment_spec),
:last_successful_deployment_dir => last_successful_deployment_dir(deployment_spec.deployment_group_id)
})
validate_app_spec_hooks(ApplicationSpecification::ApplicationSpecification.parse(File.read(default_app_spec_location)), deployment_spec.all_possible_lifecycle_events)
end

Expand Down
14 changes: 13 additions & 1 deletion lib/instance_agent/plugins/codedeploy/hook_executor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,19 @@ def parse_app_spec
http://docs.aws.amazon.com/codedeploy/latest/userguide/reference-appspec-file.html
MESSAGE
end
@app_spec = InstanceAgent::Plugins::CodeDeployPlugin::ApplicationSpecification::ApplicationSpecification.parse(File.read(app_spec_location))
@app_spec = InstanceAgent::Plugins::CodeDeployPlugin::ApplicationSpecification::ApplicationSpecification.parse(File.read(app_spec_location), deployment_spec)
end

private
def deployment_spec
{
:application_name => @application_name,
:deployment_id => @deployment_id,
:deployment_group_name => @deployment_group_name,
:deployment_group_id => @deployment_group_id,
:deployment_root_dir => @current_deployment_root_dir,
:last_successful_deployment_dir => @last_successful_deployment_dir
}
end

private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def generate_signed_message_for(map)
stubs(:read).
with("#@archive_root_dir/appspec.yml").
returns("APP SPEC")
ApplicationSpecification::ApplicationSpecification.stubs(:parse).with("APP SPEC").returns(@app_spec)
ApplicationSpecification::ApplicationSpecification.stubs(:parse).with("APP SPEC", {}).returns(@app_spec)
end

should "create an appropriate Installer" do
Expand Down Expand Up @@ -201,7 +201,7 @@ def generate_signed_message_for(map)
app_spec_hooks = {'UnknownHook' => nil}
app_spec.expects(:hooks).returns(app_spec_hooks)
File.stubs(:read).with("#@archive_root_dir/appspec.yml").returns("APP SPEC")
ApplicationSpecification::ApplicationSpecification.stubs(:parse).with("APP SPEC").returns(app_spec)
ApplicationSpecification::ApplicationSpecification.stubs(:parse).with("APP SPEC", {}).returns(app_spec)
unknown_hooks = app_spec_hooks.merge(@test_hook_mapping)
assert_raised_with_message("appspec.yml file contains unknown lifecycle events: #{unknown_hooks.keys}", ArgumentError) do
@command_executor.execute_command(@command, deployment_spec)
Expand Down Expand Up @@ -229,7 +229,7 @@ def generate_signed_message_for(map)
app_spec_hooks = InstanceAgent::Plugins::CodeDeployPlugin::CommandPoller::DEFAULT_HOOK_MAPPING.merge({'ExampleLifecycleEvent' => nil, 'SecondLifecycleEvent' => nil})
app_spec.expects(:hooks).twice.returns(app_spec_hooks)
File.stubs(:read).with("#@archive_root_dir/appspec.yml").returns("APP SPEC")
ApplicationSpecification::ApplicationSpecification.stubs(:parse).with("APP SPEC").returns(app_spec)
ApplicationSpecification::ApplicationSpecification.stubs(:parse).with("APP SPEC", {}).returns(app_spec)
assert_raised_with_message("You specified a lifecycle event which is not a default one and doesn't exist in your appspec.yml file: CustomHookNotInAppspec", ArgumentError) do
@command_executor.execute_command(@command, deployment_spec)
end
Expand Down

0 comments on commit 6aa99fd

Please sign in to comment.