Skip to content

Commit d20ae1d

Browse files
authored
Merge pull request #253 from aws/folder_branch
If a bundle is an archive of a directory, strip the leading directory
2 parents 88f45b1 + f3f033c commit d20ae1d

File tree

3 files changed

+45
-31
lines changed

3 files changed

+45
-31
lines changed

features/codedeploy-local/codedeploy_local.feature

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ Feature: Local Deploy using AWS CodeDeploy Local CLI
3131
And the expected files should have have been locally deployed to my host
3232
And the scripts should have been executed during local deployment
3333

34+
Scenario: Doing a sample local deployment using a zipped_directory bundle
35+
Given I have a sample local zipped_directory bundle
36+
When I create a local deployment with my bundle
37+
Then the local deployment command should succeed
38+
And the expected files should have have been locally deployed to my host
39+
And the scripts should have been executed during local deployment
40+
3441
Scenario: Doing a sample local deployment using a tgz bundle
3542
Given I have a sample local tgz bundle
3643
When I create a local deployment with my bundle

features/step_definitions/codedeploy_local_steps.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
FileUtils.rm_rf(@test_directory) unless @test_directory.nil?
2222
end
2323

24-
Given(/^I have a sample local (tgz|tar|zip|directory|relative_directory|custom_event_directory|directory_with_destination_files) bundle$/) do |bundle_type|
24+
Given(/^I have a sample local (tgz|tar|zip|zipped_directory|directory|relative_directory|custom_event_directory|directory_with_destination_files) bundle$/) do |bundle_type|
2525
case bundle_type
2626
when 'custom_event_directory'
2727
@bundle_original_directory_location = StepConstants::SAMPLE_CUSTOM_EVENT_APP_BUNDLE_FULL_PATH
@@ -32,13 +32,15 @@
3232
end
3333

3434
expect(File.directory?(@bundle_original_directory_location)).to be true
35-
@bundle_type = bundle_type.include?('directory') ? 'directory' : bundle_type
35+
@bundle_type = bundle_type.include?('zip') ? 'zip' : (bundle_type.include?('directory') ? 'directory' : bundle_type)
3636

3737
case bundle_type
3838
when 'relative_directory'
3939
@bundle_location = Pathname.new(@bundle_original_directory_location).relative_path_from Pathname.getwd
4040
when 'zip'
4141
@bundle_location = zip_app_bundle(@test_directory)
42+
when 'zipped_directory'
43+
@bundle_location = zip_app_dir_bundle(@test_directory)
4244
when 'tar'
4345
@bundle_location = tar_app_bundle(@test_directory)
4446
when 'tgz'
@@ -50,6 +52,19 @@
5052
expect(File.file?(@bundle_location)).to be true unless bundle_type.include? 'directory'
5153
end
5254

55+
# Create a zip of the bundle with a nested directory containing the actual bundle files
56+
def zip_app_dir_bundle(temp_directory_to_create_bundle)
57+
zip_file_name = "#{temp_directory_to_create_bundle}/app_dir_bundle.zip"
58+
Dir.mktmpdir { |zip_build_dir|
59+
# Copy the bundle files into a temporary directory so we can just zip the sample bundle
60+
FileUtils.cp_r(StepConstants::SAMPLE_APP_BUNDLE_FULL_PATH, zip_build_dir)
61+
zip_directory(zip_build_dir, zip_file_name)
62+
}
63+
# Confirm appspec.yml is not at top level
64+
Zip::File.open(zip_file_name) { |zip| expect(zip.entries.map(&:name).include? "appspec.yml").to be false }
65+
zip_file_name
66+
end
67+
5368
def tar_app_bundle(temp_directory_to_create_bundle)
5469
tar_file_name = "#{temp_directory_to_create_bundle}/app_bundle.tar"
5570
old_direcory = Dir.pwd

lib/instance_agent/plugins/codedeploy/command_executor.rb

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -374,16 +374,7 @@ def handle_local_directory(deployment_spec, local_location)
374374

375375
private
376376
def unpack_bundle(cmd, bundle_file, deployment_spec)
377-
strip_leading_directory = deployment_spec.revision_source == 'GitHub'
378-
379-
if strip_leading_directory
380-
# Extract to a temporary directory first so we can move the files around
381-
dst = File.join(deployment_root_dir(deployment_spec), 'deployment-archive-temp')
382-
actual_dst = File.join(deployment_root_dir(deployment_spec), 'deployment-archive')
383-
FileUtils.rm_rf(dst)
384-
else
385-
dst = File.join(deployment_root_dir(deployment_spec), 'deployment-archive')
386-
end
377+
dst = File.join(deployment_root_dir(deployment_spec), 'deployment-archive')
387378

388379
if "tar".eql? deployment_spec.bundle_type
389380
InstanceAgent::Platform.util.extract_tar(bundle_file, dst)
@@ -407,26 +398,27 @@ def unpack_bundle(cmd, bundle_file, deployment_spec)
407398
InstanceAgent::Platform.util.extract_tar(bundle_file, dst)
408399
end
409400

410-
if strip_leading_directory
411-
log(:info, "Stripping leading directory from archive bundle contents.")
412-
413-
# Find leading directory to remove
414-
archive_root_files = Dir.entries(dst)
415-
archive_root_files.delete_if { |name| name == '.' || name == '..' }
401+
archive_root_files = Dir.entries(dst)
402+
archive_root_files.delete_if { |name| name == '.' || name == '..' }
416403

417-
if (archive_root_files.size != 1)
418-
log(:warn, "Expected archive to have a single root directory containing the actual bundle root, but it had #{archive_root_files.size} entries instead. Skipping leading directory removal and using archive as is.")
419-
FileUtils.mv(dst, actual_dst)
420-
return
421-
end
422-
423-
nested_archive_root = File.join(dst, archive_root_files[0])
424-
log(:debug, "Actual archive root at #{nested_archive_root}. Moving to #{actual_dst}")
425-
426-
FileUtils.mv(nested_archive_root, actual_dst)
427-
FileUtils.rmdir(dst)
428-
429-
log(:debug, Dir.entries(actual_dst).join("; "))
404+
# If the top level of the archive is a directory that contains an appspec,
405+
# strip that before giving up
406+
if ((archive_root_files.size == 1) &&
407+
File.directory?(File.join(dst, archive_root_files[0])) &&
408+
Dir.entries(File.join(dst, archive_root_files[0])).grep(/appspec/i).any?)
409+
log(:info, "Stripping leading directory from archive bundle contents.")
410+
# Move the unpacked files to a temporary location
411+
tmp_dst = File.join(deployment_root_dir(deployment_spec), 'deployment-archive-temp')
412+
FileUtils.rm_rf(tmp_dst)
413+
FileUtils.mv(dst, tmp_dst)
414+
415+
# Move the top level directory to the intended location
416+
nested_archive_root = File.join(tmp_dst, archive_root_files[0])
417+
log(:debug, "Actual archive root at #{nested_archive_root}. Moving to #{dst}")
418+
FileUtils.mv(nested_archive_root, dst)
419+
FileUtils.rmdir(tmp_dst)
420+
421+
log(:debug, Dir.entries(dst).join("; "))
430422
end
431423
end
432424

0 commit comments

Comments
 (0)