diff --git a/bin/codedeploy-local b/bin/codedeploy-local index 14370914..82519467 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..b7534826 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" => application_name || location, "DeploymentGroupId" => deployment_group_id, - "DeploymentGroupName" => "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,