From a9dbb8bc37e2b418d7d524a2d841f4881ca7b823 Mon Sep 17 00:00:00 2001 From: Daniel Gil Bayo Date: Thu, 7 Jun 2018 21:34:06 +0000 Subject: [PATCH 1/5] Add deployment-group-name and application-name parameters. --- bin/codedeploy-local | 8 ++++++++ lib/aws/codedeploy/local/deployer.rb | 8 ++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/bin/codedeploy-local b/bin/codedeploy-local index 14370914..6c8e186a 100755 --- a/bin/codedeploy-local +++ b/bin/codedeploy-local @@ -50,6 +50,8 @@ Synopsis [--type ] [--file-exists-behavior ] [--deployment-group ] + [--deployment-group-name ] + [--application-name ] [--events ] [--agent-configuration-file ] @@ -69,6 +71,12 @@ Options The path to the folder that is the target location for the content to be deployed. If you do not specify a folder, the tool creates one named default-local-deployment-group inside your deployment root directory. For each local deployment you create, the tool creates a subdirectory inside this folder with names like d-98761234-local. Your configuration shows it would be placed in #{InstanceAgent::Config.config[:root_dir]}/. You can use the :root_dir: variable in an agent configuration file to configure a custom deployment root folder. If you want to deploy to a location previously deployed to by AWS CodeDeploy, you must specify its deployment group ID, which you can look up by using the AWS CLI. [default: #{AWS::CodeDeploy::Local::Deployer::DEFAULT_DEPLOYMENT_GROUP_ID}]. See also: "get-deployment-group” in the AWS CLI Reference for AWS CodeDeploy. + -d, --deployment-group-name + Indicates the deployment group name to be used during the CodeDeploy hook execution, can be accessed on the DEPLOYMENT_GROUP_NAME environment variable. If you do not specify a name, "LocalFleet" will be used. + + -a, --application-name + Indicates the application name to be used during the CodeDeploy hook execution, can be accessed on the APPLICATION_NAME environment variable. If you do not specify a name, the bundle-location will be used. + -e, --events A set of lifecycle event hooks you want to run, in order, instead of the events listed in the AppSpec file. Multiple hook names must be separated by commas. If you don't specify DownloadBundle and Install events in the --events list, they will run before all the event hooks you do specify. If you include DownloadBundle and Install in the --events list, they can be preceded only by events that normally run before them in AWS CodeDeploy deployments. diff --git a/lib/aws/codedeploy/local/deployer.rb b/lib/aws/codedeploy/local/deployer.rb index dc352c26..2a26df27 100644 --- a/lib/aws/codedeploy/local/deployer.rb +++ b/lib/aws/codedeploy/local/deployer.rb @@ -75,7 +75,7 @@ def execute_events(args) deployment_group_id = args['--deployment-group'] events = self.class.events_from_comma_separated_list(args['--events']) - spec = build_spec(args['--bundle-location'], args['--type'], deployment_group_id, args['--file-exists-behavior'], all_possible_lifecycle_events(events)) + spec = build_spec(args['--bundle-location'], args['--type'], deployment_group_id, args['--file-exists-behavior'], all_possible_lifecycle_events(events), args['--deployment-group-name'], args['--application-name']) command_executor = InstanceAgent::Plugins::CodeDeployPlugin::CommandExecutor.new(:hook_mapping => hook_mapping(events)) all_lifecycle_events_to_execute = add_download_bundle_and_install_events(ordered_lifecycle_events(events)) @@ -125,16 +125,16 @@ def hook_mapping(events) Hash[all_events_plus_default_events_minus_required_events.map{|h|[h,[h]]}] end - def build_spec(location, bundle_type, deployment_group_id, file_exists_behavior, all_possible_lifecycle_events) + def build_spec(location, bundle_type, deployment_group_id, file_exists_behavior, all_possible_lifecycle_events, deployment_group_name, application_name) @deployment_id = self.class.random_deployment_id puts "Starting to execute deployment from within folder #{deployment_folder(deployment_group_id, @deployment_id)}" OpenStruct.new({ :format => "TEXT/JSON", :payload => { "ApplicationId" => location, - "ApplicationName" => location, + "ApplicationName" => deployment_group_name || location, "DeploymentGroupId" => deployment_group_id, - "DeploymentGroupName" => "LocalFleet", + "DeploymentGroupName" => application_name || "LocalFleet", "DeploymentId" => @deployment_id, "AgentActionOverrides" => {"AgentOverrides" => {"FileExistsBehavior" => file_exists_behavior}}, "Revision" => revision(location, bundle_type), From c9ed39db53f6c3e09977847ff0e7c48a71073f1d Mon Sep 17 00:00:00 2001 From: Daniel Gil Bayo Date: Thu, 7 Jun 2018 22:24:26 +0000 Subject: [PATCH 2/5] Add tests cr https://code.amazon.com/reviews/CR-2257661 --- lib/aws/codedeploy/local/deployer.rb | 4 +- spec/aws/codedeploy/local/deployer_spec.rb | 129 ++++++++++++++++++++- 2 files changed, 128 insertions(+), 5 deletions(-) diff --git a/lib/aws/codedeploy/local/deployer.rb b/lib/aws/codedeploy/local/deployer.rb index 2a26df27..b7534826 100644 --- a/lib/aws/codedeploy/local/deployer.rb +++ b/lib/aws/codedeploy/local/deployer.rb @@ -132,9 +132,9 @@ def build_spec(location, bundle_type, deployment_group_id, file_exists_behavior, :format => "TEXT/JSON", :payload => { "ApplicationId" => location, - "ApplicationName" => deployment_group_name || location, + "ApplicationName" => application_name || location, "DeploymentGroupId" => deployment_group_id, - "DeploymentGroupName" => application_name || "LocalFleet", + "DeploymentGroupName" => deployment_group_name || "LocalFleet", "DeploymentId" => @deployment_id, "AgentActionOverrides" => {"AgentOverrides" => {"FileExistsBehavior" => file_exists_behavior}}, "Revision" => revision(location, bundle_type), diff --git a/spec/aws/codedeploy/local/deployer_spec.rb b/spec/aws/codedeploy/local/deployer_spec.rb index e663c16b..442d829e 100644 --- a/spec/aws/codedeploy/local/deployer_spec.rb +++ b/spec/aws/codedeploy/local/deployer_spec.rb @@ -34,6 +34,9 @@ "AfterAllowTraffic"=>["AfterAllowTraffic"]} DEPLOYMENT_GROUP_ID = 'deployment-group-id' NON_DEFAULT_FILE_EXISTS_BEHAVIOR = 'OVERWRITE' + SAMPLE_APPLICATION_NAME = 'myapp' + SAMPLE_DEPLOYMENT_GROUP_NAME = 'mydeploymentgroup' + let(:test_working_directory) { Dir.mktmpdir } before do @@ -441,16 +444,136 @@ def create_config_file(working_directory, log_dir = nil) end end - def deployment_spec(location, revision_type, bundle_type, all_possible_lifecycle_events, s3revision_includes_version=false, s3revision_includes_etag=false, deployment_group_id=DEPLOYMENT_GROUP_ID, file_exists_behavior=InstanceAgent::Plugins::CodeDeployPlugin::DeploymentSpecification::DEFAULT_FILE_EXISTS_BEHAVIOR) + context 'build_spec' do + context 'when application-name is not in the args' do + let(:args) do + {"deploy"=>true, + '--file-exists-behavior'=>InstanceAgent::Plugins::CodeDeployPlugin::DeploymentSpecification::DEFAULT_FILE_EXISTS_BEHAVIOR, + "--location"=>true, + "--bundle-location"=>SAMPLE_FILE_BUNDLE, + "--type"=>'tgz', + '--deployment-group'=>DEPLOYMENT_GROUP_ID, + "--help"=>false, + "--version"=>false} + end + it 'defaults to LocalFleet' do + allow(File).to receive(:exists?).with(SAMPLE_FILE_BUNDLE).and_return(true) + executor = double(InstanceAgent::Plugins::CodeDeployPlugin::CommandExecutor) + + expect(InstanceAgent::Plugins::CodeDeployPlugin::CommandExecutor).to receive(:new). + with(:hook_mapping => EXPECTED_HOOK_MAPPING). + and_return(executor) + + AWS::CodeDeploy::Local::Deployer::DEFAULT_ORDERED_LIFECYCLE_EVENTS.each do |name| + expect(executor).to receive(:execute_command).with( + OpenStruct.new(:command_name => name), + deployment_spec(SAMPLE_FILE_BUNDLE, 'Local File', 'tgz', + AWS::CodeDeploy::Local::Deployer::DEFAULT_ORDERED_LIFECYCLE_EVENTS)).once.ordered + end + AWS::CodeDeploy::Local::Deployer.new(@config_file_location).execute_events(args) + end + end + + context 'when deployment-group-name is not in the args' do + let(:args) do + {"deploy"=>true, + '--file-exists-behavior'=>InstanceAgent::Plugins::CodeDeployPlugin::DeploymentSpecification::DEFAULT_FILE_EXISTS_BEHAVIOR, + "--location"=>true, + "--bundle-location"=>SAMPLE_FILE_BUNDLE, + "--type"=>'tgz', + '--deployment-group'=>DEPLOYMENT_GROUP_ID, + "--help"=>false, + "--version"=>false} + end + it 'defaults to the bundle-location' do + allow(File).to receive(:exists?).with(SAMPLE_FILE_BUNDLE).and_return(true) + executor = double(InstanceAgent::Plugins::CodeDeployPlugin::CommandExecutor) + + expect(InstanceAgent::Plugins::CodeDeployPlugin::CommandExecutor).to receive(:new). + with(:hook_mapping => EXPECTED_HOOK_MAPPING). + and_return(executor) + + AWS::CodeDeploy::Local::Deployer::DEFAULT_ORDERED_LIFECYCLE_EVENTS.each do |name| + expect(executor).to receive(:execute_command).with( + OpenStruct.new(:command_name => name), + deployment_spec(SAMPLE_FILE_BUNDLE, 'Local File', 'tgz', + AWS::CodeDeploy::Local::Deployer::DEFAULT_ORDERED_LIFECYCLE_EVENTS)).once.ordered + end + AWS::CodeDeploy::Local::Deployer.new(@config_file_location).execute_events(args) + end + end + + context 'when application-name is in the args' do + let(:args) do + {"deploy"=>true, + '--file-exists-behavior'=>InstanceAgent::Plugins::CodeDeployPlugin::DeploymentSpecification::DEFAULT_FILE_EXISTS_BEHAVIOR, + "--location"=>true, + "--bundle-location"=>SAMPLE_FILE_BUNDLE, + "--type"=>'tgz', + '--deployment-group'=>DEPLOYMENT_GROUP_ID, + '--application-name'=> SAMPLE_APPLICATION_NAME, + "--help"=>false, + "--version"=>false} + end + it 'generates a spec with the provided value' do + allow(File).to receive(:exists?).with(SAMPLE_FILE_BUNDLE).and_return(true) + executor = double(InstanceAgent::Plugins::CodeDeployPlugin::CommandExecutor) + + expect(InstanceAgent::Plugins::CodeDeployPlugin::CommandExecutor).to receive(:new). + with(:hook_mapping => EXPECTED_HOOK_MAPPING). + and_return(executor) + + AWS::CodeDeploy::Local::Deployer::DEFAULT_ORDERED_LIFECYCLE_EVENTS.each do |name| + expect(executor).to receive(:execute_command).with( + OpenStruct.new(:command_name => name), + deployment_spec(SAMPLE_FILE_BUNDLE, 'Local File', 'tgz', + AWS::CodeDeploy::Local::Deployer::DEFAULT_ORDERED_LIFECYCLE_EVENTS, deployment_group_name:nil, application_name:SAMPLE_APPLICATION_NAME)).once.ordered + end + AWS::CodeDeploy::Local::Deployer.new(@config_file_location).execute_events(args) + end + end + + context 'when deployment-group-name is in the args' do + let(:args) do + {"deploy"=>true, + '--file-exists-behavior'=>InstanceAgent::Plugins::CodeDeployPlugin::DeploymentSpecification::DEFAULT_FILE_EXISTS_BEHAVIOR, + "--location"=>true, + "--bundle-location"=>SAMPLE_FILE_BUNDLE, + "--type"=>'tgz', + '--deployment-group'=>DEPLOYMENT_GROUP_ID, + '--deployment-group-name'=>SAMPLE_DEPLOYMENT_GROUP_NAME, + "--help"=>false, + "--version"=>false} + end + it 'generates a spec with the provided value' do + allow(File).to receive(:exists?).with(SAMPLE_FILE_BUNDLE).and_return(true) + executor = double(InstanceAgent::Plugins::CodeDeployPlugin::CommandExecutor) + + expect(InstanceAgent::Plugins::CodeDeployPlugin::CommandExecutor).to receive(:new). + with(:hook_mapping => EXPECTED_HOOK_MAPPING). + and_return(executor) + + AWS::CodeDeploy::Local::Deployer::DEFAULT_ORDERED_LIFECYCLE_EVENTS.each do |name| + expect(executor).to receive(:execute_command).with( + OpenStruct.new(:command_name => name), + deployment_spec(SAMPLE_FILE_BUNDLE, 'Local File', 'tgz', + AWS::CodeDeploy::Local::Deployer::DEFAULT_ORDERED_LIFECYCLE_EVENTS, deployment_group_name:SAMPLE_DEPLOYMENT_GROUP_NAME, application_name: nil)).once.ordered + end + AWS::CodeDeploy::Local::Deployer.new(@config_file_location).execute_events(args) + end + end + end + + def deployment_spec(location, revision_type, bundle_type, all_possible_lifecycle_events, s3revision_includes_version=false, s3revision_includes_etag=false, deployment_group_id=DEPLOYMENT_GROUP_ID, file_exists_behavior=InstanceAgent::Plugins::CodeDeployPlugin::DeploymentSpecification::DEFAULT_FILE_EXISTS_BEHAVIOR, deployment_group_name:nil, application_name:nil) revision_data_key = revision_data(revision_type, location, bundle_type, s3revision_includes_version, s3revision_includes_etag).keys.first revision_data_value = revision_data(revision_type, location, bundle_type, s3revision_includes_version, s3revision_includes_etag).values.first OpenStruct.new({ :format => "TEXT/JSON", :payload => { "ApplicationId" => location, - "ApplicationName" => location, + "ApplicationName" => application_name || location, "DeploymentGroupId" => deployment_group_id, - "DeploymentGroupName" => "LocalFleet", + "DeploymentGroupName" => deployment_group_name || "LocalFleet", "DeploymentId" => TEST_DEPLOYMENT_ID, "AgentActionOverrides" => {"AgentOverrides" => {"FileExistsBehavior" => file_exists_behavior}}, "Revision" => {"RevisionType" => revision_type, From 3d5f974954fdac91c9f4ab38a126e1169e4dd72f Mon Sep 17 00:00:00 2001 From: Daniel Gil Bayo Date: Fri, 27 Jul 2018 21:42:06 +0000 Subject: [PATCH 3/5] Merge branch 'aws-codedeploy-agent-0.1' of ssh://git.amazon.com:2222/pkg/Aws-codedeploy-agent into aws-codedeploy-agent-0.1 From c32521a48539c28c99c89615d565a7759e6883be Mon Sep 17 00:00:00 2001 From: Daniel Gil Bayo Date: Fri, 27 Jul 2018 21:57:04 +0000 Subject: [PATCH 4/5] Minor doc change --- bin/codedeploy-local | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/codedeploy-local b/bin/codedeploy-local index 6c8e186a..82519467 100755 --- a/bin/codedeploy-local +++ b/bin/codedeploy-local @@ -75,7 +75,7 @@ Options Indicates the deployment group name to be used during the CodeDeploy hook execution, can be accessed on the DEPLOYMENT_GROUP_NAME environment variable. If you do not specify a name, "LocalFleet" will be used. -a, --application-name - Indicates the application name to be used during the CodeDeploy hook execution, can be accessed on the APPLICATION_NAME environment variable. If you do not specify a name, the bundle-location will be used. + Indicates the application name to be used during the CodeDeploy hook execution, can be accessed on the APPLICATION_NAME environment variable. If you do not specify a name, the "bundle-location" will be used. -e, --events A set of lifecycle event hooks you want to run, in order, instead of the events listed in the AppSpec file. Multiple hook names must be separated by commas. If you don't specify DownloadBundle and Install events in the --events list, they will run before all the event hooks you do specify. If you include DownloadBundle and Install in the --events list, they can be preceded only by events that normally run before them in AWS CodeDeploy deployments. From 9ecdad8739848d58841b4dc83b5a3a18274ffe11 Mon Sep 17 00:00:00 2001 From: cr-merge Date: Mon, 30 Jul 2018 16:42:34 +0000 Subject: [PATCH 5/5] Add parameters to codedeploy-local to set the application name and deployment group name Changes for 1 package (Aws-codedeploy-agent), pushed in snapshot... https://code.amazon.com/snapshots/bayod/2018-07-27T21-58-00 Changes for Aws-codedeploy-agent package: 01dbc33d Minor doc change 9c3122c9 Merge branch 'aws-codedeploy-agent-0.1' of ssh://git.amazon.com:2222/pkg/Aws-codedeploy-agent into aws-codedeploy-agent-0.1 cr https://code.amazon.com/reviews/CR-2257661