Skip to content

Commit fbfadb6

Browse files
authored
Merge pull request #190 from rohkat-aws/master
Codedeploy local and other small fixes
2 parents 0158c14 + 9ecdad8 commit fbfadb6

File tree

3 files changed

+138
-7
lines changed

3 files changed

+138
-7
lines changed

bin/codedeploy-local

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ Synopsis
5050
[--type <value>]
5151
[--file-exists-behavior <value>]
5252
[--deployment-group <value>]
53+
[--deployment-group-name <value>]
54+
[--application-name <value>]
5355
[--events <value>]
5456
[--agent-configuration-file <value>]
5557
@@ -69,6 +71,12 @@ Options
6971
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]}/<deployment-group>. 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}].
7072
See also: "get-deployment-group” in the AWS CLI Reference for AWS CodeDeploy.
7173
74+
-d, --deployment-group-name <value>
75+
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.
76+
77+
-a, --application-name <value>
78+
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.
79+
7280
-e, --events <comma-separated-values>
7381
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.
7482

lib/aws/codedeploy/local/deployer.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def execute_events(args)
7575
deployment_group_id = args['--deployment-group']
7676
events = self.class.events_from_comma_separated_list(args['--events'])
7777

78-
spec = build_spec(args['--bundle-location'], args['--type'], deployment_group_id, args['--file-exists-behavior'], all_possible_lifecycle_events(events))
78+
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'])
7979

8080
command_executor = InstanceAgent::Plugins::CodeDeployPlugin::CommandExecutor.new(:hook_mapping => hook_mapping(events))
8181
all_lifecycle_events_to_execute = add_download_bundle_and_install_events(ordered_lifecycle_events(events))
@@ -125,16 +125,16 @@ def hook_mapping(events)
125125
Hash[all_events_plus_default_events_minus_required_events.map{|h|[h,[h]]}]
126126
end
127127

128-
def build_spec(location, bundle_type, deployment_group_id, file_exists_behavior, all_possible_lifecycle_events)
128+
def build_spec(location, bundle_type, deployment_group_id, file_exists_behavior, all_possible_lifecycle_events, deployment_group_name, application_name)
129129
@deployment_id = self.class.random_deployment_id
130130
puts "Starting to execute deployment from within folder #{deployment_folder(deployment_group_id, @deployment_id)}"
131131
OpenStruct.new({
132132
:format => "TEXT/JSON",
133133
:payload => {
134134
"ApplicationId" => location,
135-
"ApplicationName" => location,
135+
"ApplicationName" => application_name || location,
136136
"DeploymentGroupId" => deployment_group_id,
137-
"DeploymentGroupName" => "LocalFleet",
137+
"DeploymentGroupName" => deployment_group_name || "LocalFleet",
138138
"DeploymentId" => @deployment_id,
139139
"AgentActionOverrides" => {"AgentOverrides" => {"FileExistsBehavior" => file_exists_behavior}},
140140
"Revision" => revision(location, bundle_type),

spec/aws/codedeploy/local/deployer_spec.rb

Lines changed: 126 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
"AfterAllowTraffic"=>["AfterAllowTraffic"]}
3535
DEPLOYMENT_GROUP_ID = 'deployment-group-id'
3636
NON_DEFAULT_FILE_EXISTS_BEHAVIOR = 'OVERWRITE'
37+
SAMPLE_APPLICATION_NAME = 'myapp'
38+
SAMPLE_DEPLOYMENT_GROUP_NAME = 'mydeploymentgroup'
39+
3740
let(:test_working_directory) { Dir.mktmpdir }
3841

3942
before do
@@ -441,16 +444,136 @@ def create_config_file(working_directory, log_dir = nil)
441444
end
442445
end
443446

444-
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)
447+
context 'build_spec' do
448+
context 'when application-name is not in the args' do
449+
let(:args) do
450+
{"deploy"=>true,
451+
'--file-exists-behavior'=>InstanceAgent::Plugins::CodeDeployPlugin::DeploymentSpecification::DEFAULT_FILE_EXISTS_BEHAVIOR,
452+
"--location"=>true,
453+
"--bundle-location"=>SAMPLE_FILE_BUNDLE,
454+
"--type"=>'tgz',
455+
'--deployment-group'=>DEPLOYMENT_GROUP_ID,
456+
"--help"=>false,
457+
"--version"=>false}
458+
end
459+
it 'defaults to LocalFleet' do
460+
allow(File).to receive(:exists?).with(SAMPLE_FILE_BUNDLE).and_return(true)
461+
executor = double(InstanceAgent::Plugins::CodeDeployPlugin::CommandExecutor)
462+
463+
expect(InstanceAgent::Plugins::CodeDeployPlugin::CommandExecutor).to receive(:new).
464+
with(:hook_mapping => EXPECTED_HOOK_MAPPING).
465+
and_return(executor)
466+
467+
AWS::CodeDeploy::Local::Deployer::DEFAULT_ORDERED_LIFECYCLE_EVENTS.each do |name|
468+
expect(executor).to receive(:execute_command).with(
469+
OpenStruct.new(:command_name => name),
470+
deployment_spec(SAMPLE_FILE_BUNDLE, 'Local File', 'tgz',
471+
AWS::CodeDeploy::Local::Deployer::DEFAULT_ORDERED_LIFECYCLE_EVENTS)).once.ordered
472+
end
473+
AWS::CodeDeploy::Local::Deployer.new(@config_file_location).execute_events(args)
474+
end
475+
end
476+
477+
context 'when deployment-group-name is not in the args' do
478+
let(:args) do
479+
{"deploy"=>true,
480+
'--file-exists-behavior'=>InstanceAgent::Plugins::CodeDeployPlugin::DeploymentSpecification::DEFAULT_FILE_EXISTS_BEHAVIOR,
481+
"--location"=>true,
482+
"--bundle-location"=>SAMPLE_FILE_BUNDLE,
483+
"--type"=>'tgz',
484+
'--deployment-group'=>DEPLOYMENT_GROUP_ID,
485+
"--help"=>false,
486+
"--version"=>false}
487+
end
488+
it 'defaults to the bundle-location' do
489+
allow(File).to receive(:exists?).with(SAMPLE_FILE_BUNDLE).and_return(true)
490+
executor = double(InstanceAgent::Plugins::CodeDeployPlugin::CommandExecutor)
491+
492+
expect(InstanceAgent::Plugins::CodeDeployPlugin::CommandExecutor).to receive(:new).
493+
with(:hook_mapping => EXPECTED_HOOK_MAPPING).
494+
and_return(executor)
495+
496+
AWS::CodeDeploy::Local::Deployer::DEFAULT_ORDERED_LIFECYCLE_EVENTS.each do |name|
497+
expect(executor).to receive(:execute_command).with(
498+
OpenStruct.new(:command_name => name),
499+
deployment_spec(SAMPLE_FILE_BUNDLE, 'Local File', 'tgz',
500+
AWS::CodeDeploy::Local::Deployer::DEFAULT_ORDERED_LIFECYCLE_EVENTS)).once.ordered
501+
end
502+
AWS::CodeDeploy::Local::Deployer.new(@config_file_location).execute_events(args)
503+
end
504+
end
505+
506+
context 'when application-name is in the args' do
507+
let(:args) do
508+
{"deploy"=>true,
509+
'--file-exists-behavior'=>InstanceAgent::Plugins::CodeDeployPlugin::DeploymentSpecification::DEFAULT_FILE_EXISTS_BEHAVIOR,
510+
"--location"=>true,
511+
"--bundle-location"=>SAMPLE_FILE_BUNDLE,
512+
"--type"=>'tgz',
513+
'--deployment-group'=>DEPLOYMENT_GROUP_ID,
514+
'--application-name'=> SAMPLE_APPLICATION_NAME,
515+
"--help"=>false,
516+
"--version"=>false}
517+
end
518+
it 'generates a spec with the provided value' do
519+
allow(File).to receive(:exists?).with(SAMPLE_FILE_BUNDLE).and_return(true)
520+
executor = double(InstanceAgent::Plugins::CodeDeployPlugin::CommandExecutor)
521+
522+
expect(InstanceAgent::Plugins::CodeDeployPlugin::CommandExecutor).to receive(:new).
523+
with(:hook_mapping => EXPECTED_HOOK_MAPPING).
524+
and_return(executor)
525+
526+
AWS::CodeDeploy::Local::Deployer::DEFAULT_ORDERED_LIFECYCLE_EVENTS.each do |name|
527+
expect(executor).to receive(:execute_command).with(
528+
OpenStruct.new(:command_name => name),
529+
deployment_spec(SAMPLE_FILE_BUNDLE, 'Local File', 'tgz',
530+
AWS::CodeDeploy::Local::Deployer::DEFAULT_ORDERED_LIFECYCLE_EVENTS, deployment_group_name:nil, application_name:SAMPLE_APPLICATION_NAME)).once.ordered
531+
end
532+
AWS::CodeDeploy::Local::Deployer.new(@config_file_location).execute_events(args)
533+
end
534+
end
535+
536+
context 'when deployment-group-name is in the args' do
537+
let(:args) do
538+
{"deploy"=>true,
539+
'--file-exists-behavior'=>InstanceAgent::Plugins::CodeDeployPlugin::DeploymentSpecification::DEFAULT_FILE_EXISTS_BEHAVIOR,
540+
"--location"=>true,
541+
"--bundle-location"=>SAMPLE_FILE_BUNDLE,
542+
"--type"=>'tgz',
543+
'--deployment-group'=>DEPLOYMENT_GROUP_ID,
544+
'--deployment-group-name'=>SAMPLE_DEPLOYMENT_GROUP_NAME,
545+
"--help"=>false,
546+
"--version"=>false}
547+
end
548+
it 'generates a spec with the provided value' do
549+
allow(File).to receive(:exists?).with(SAMPLE_FILE_BUNDLE).and_return(true)
550+
executor = double(InstanceAgent::Plugins::CodeDeployPlugin::CommandExecutor)
551+
552+
expect(InstanceAgent::Plugins::CodeDeployPlugin::CommandExecutor).to receive(:new).
553+
with(:hook_mapping => EXPECTED_HOOK_MAPPING).
554+
and_return(executor)
555+
556+
AWS::CodeDeploy::Local::Deployer::DEFAULT_ORDERED_LIFECYCLE_EVENTS.each do |name|
557+
expect(executor).to receive(:execute_command).with(
558+
OpenStruct.new(:command_name => name),
559+
deployment_spec(SAMPLE_FILE_BUNDLE, 'Local File', 'tgz',
560+
AWS::CodeDeploy::Local::Deployer::DEFAULT_ORDERED_LIFECYCLE_EVENTS, deployment_group_name:SAMPLE_DEPLOYMENT_GROUP_NAME, application_name: nil)).once.ordered
561+
end
562+
AWS::CodeDeploy::Local::Deployer.new(@config_file_location).execute_events(args)
563+
end
564+
end
565+
end
566+
567+
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)
445568
revision_data_key = revision_data(revision_type, location, bundle_type, s3revision_includes_version, s3revision_includes_etag).keys.first
446569
revision_data_value = revision_data(revision_type, location, bundle_type, s3revision_includes_version, s3revision_includes_etag).values.first
447570
OpenStruct.new({
448571
:format => "TEXT/JSON",
449572
:payload => {
450573
"ApplicationId" => location,
451-
"ApplicationName" => location,
574+
"ApplicationName" => application_name || location,
452575
"DeploymentGroupId" => deployment_group_id,
453-
"DeploymentGroupName" => "LocalFleet",
576+
"DeploymentGroupName" => deployment_group_name || "LocalFleet",
454577
"DeploymentId" => TEST_DEPLOYMENT_ID,
455578
"AgentActionOverrides" => {"AgentOverrides" => {"FileExistsBehavior" => file_exists_behavior}},
456579
"Revision" => {"RevisionType" => revision_type,

0 commit comments

Comments
 (0)