hashicorp / hashicorp/packer

A meaningful error message is not displayed when trying to overwrite a directory with a file

Open
#12,339 1 comment 0 reactions 0 assignees View on GitHub
bug provisioner/file sync to jira
Dominant language
Go
Stars
15.8k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

#### Community Note

* Please vote on this issue by adding a 👍 [reaction](https://blog.github.com/2016-03-10-add-reactions-to-pull-requests-issues-and-comments/) to the original issue to help the community and maintainers prioritize this request
* Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
* If you are interested in working on this issue or have submitted a pull request, please leave a comment

#### Overview of the Issue

While uploading a file to AWS using the file provisioner I made a mistake in my code which resulted in Packer trying to replace the directory instead of uploading the file _inside_ of the directory. That's nobody's fault but my own, but I do believe that this error should be made clear in the logs. Currently the upload just fails instead of printing out why the upload is failing. This led to a frustrating debug session until I turned on `export PACKER_LOG=1`...

Expected output:
```
==> build_file_upload_test.amazon-ebs.ubuntu_focal_nvirginia: Uploading temp_file => /home/ubuntu
==> build_file_upload_test.amazon-ebs.ubuntu_focal_nvirginia: Upload failed: Process exited with status 1
==> build_file_upload_test.amazon-ebs.ubuntu_focal_nvirginia: ERROR: scp output: scp: /home/ubuntu: Is a directory
build_file_upload_test.amazon-ebs.ubuntu_focal_nvirginia: temp_file 672.00 KiB / 10.00 MiB [=====>----------------------------------------------------------------------------------] 6.56% 0s
==> build_file_upload_test.amazon-ebs.ubuntu_focal_nvirginia: Provisioning step had errors: Running the cleanup provisioner, if present...
```

Actual output:
```
==> build_file_upload_test.amazon-ebs.ubuntu_focal_nvirginia: Uploading temp_file => /home/ubuntu
==> build_file_upload_test.amazon-ebs.ubuntu_focal_nvirginia: Upload failed: Process exited with status 1
build_file_upload_test.amazon-ebs.ubuntu_focal_nvirginia: temp_file 672.00 KiB / 10.00 MiB [=====>----------------------------------------------------------------------------------] 6.56% 0s
==> build_file_upload_test.amazon-ebs.ubuntu_focal_nvirginia: Provisioning step had errors: Running the cleanup provisioner, if present...
```

#### Reproduction Steps

Set the destination of a file provisioner to be a directory without a trailing slash, ex: `/home/ubuntu` instead of `/home/ubuntu/`

### Packer version
```
$ packer --version
1.8.6
```

### Simplified Packer Template

Click to expand code

```
# simplified_packer.pkr.hcl

packer {
required_plugins {
amazon = {
version = ">= 0.0.2"
source = "github.com/hashicorp/amazon"
}
}
}

variable "file_to_upload" {
type = string
default = "temp_file"
}

source "amazon-ebs" "ubuntu_focal_oregon" {
ami_name = "file_upload_test"
instance_type = "t2.micro"
region = "us-west-2"
source_ami_filter {
filters = {
name = "ubuntu/images/*ubuntu-xenial-16.04-amd64-server-*"
root-device-type = "ebs"
virtualization-type = "hvm"
}
most_recent = true
owners = ["099720109477"]
}
ssh_username = "ubuntu"
}

build {
name = "build_file_upload_test"
sources = [
"source.amazon-ebs.ubuntu_focal_oregon"
]

provisioner "file" {
destination = "/home/ubuntu" <---------------------------------- should be `/home/ubuntu/`
source = "${var.file_to_upload}"
}
}
```

To execute this file:
1. Create a random file `mkfile -n 10m temp_file`
2. Build the image with Packer `packer build simplified_packer.pkr.hcl`

### Operating system and Environment details
```
$ sw_vers
ProductName: macOS
ProductVersion: 13.2.1
BuildVersion: 22D68
```

### Log Fragments and crash.log files

Click to expand log with debug turned off

```
$ packer build simplified_packer.pkr.hcl
build_file_upload_test.amazon-ebs.ubuntu_focal_oregon: output will be in this color.

==> build_file_upload_test.amazon-ebs.ubuntu_focal_oregon: Prevalidating any provided VPC information
==> build_file_upload_test.amazon-ebs.ubuntu_focal_oregon: Prevalidating AMI Name: file_upload_test
build_file_upload_test.amazon-ebs.ubuntu_focal_oregon: Found Image ID: ami-0688ba7eeeeefe3cd
==> build_file_upload_test.amazon-ebs.ubuntu_focal_oregon: Creating temporary keypair: packer_642b2604-944a-4e69-3ca8-2412f3b0991a
==> build_file_upload_test.amazon-ebs.ubuntu_focal_oregon: Creating temporary security group for this instance: packer_642b2605-e286-baa0-1c0f-b9703ea5e418
==> build_file_upload_test.amazon-ebs.ubuntu_focal_oregon: Authorizing access to port 22 from [0.0.0.0/0] in the temporary security groups...
==> build_file_upload_test.amazon-ebs.ubuntu_focal_oregon: Launching a source AWS instance...
build_file_upload_test.amazon-ebs.ubuntu_focal_oregon: Instance ID: i-0d61fd5d02ff420dd
==> build_file_upload_test.amazon-ebs.ubuntu_focal_oregon: Waiting for instance (i-0d61fd5d02ff420dd) to become ready...
==> build_file_upload_test.amazon-ebs.ubuntu_focal_oregon: Using SSH communicator to connect: 35.89.249.10
==> build_file_upload_test.amazon-ebs.ubuntu_focal_oregon: Waiting for SSH to become available...
==> build_file_upload_test.amazon-ebs.ubuntu_focal_oregon: Connected to SSH!
==> build_file_upload_test.amazon-ebs.ubuntu_focal_oregon: Uploading temp_file => /home/ubuntu
==> build_file_upload_test.amazon-ebs.ubuntu_focal_oregon: Upload failed: Process exited with status 1
build_file_upload_test.amazon-ebs.ubuntu_focal_oregon: temp_file 672.00 KiB / 10.00 MiB [=====>----------------------------------------------------------------------------------] 6.56% 0s
==> build_file_upload_test.amazon-ebs.ubuntu_focal_oregon: Provisioning step had errors: Running the cleanup provisioner, if present...
==> build_file_upload_test.amazon-ebs.ubuntu_focal_oregon: Terminating the source AWS instance...
==> build_file_upload_test.amazon-ebs.ubuntu_focal_oregon: Cleaning up any extra volumes...
==> build_file_upload_test.amazon-ebs.ubuntu_focal_oregon: No volumes to clean up, skipping
==> build_file_upload_test.amazon-ebs.ubuntu_focal_oregon: Deleting temporary security group...
==> build_file_upload_test.amazon-ebs.ubuntu_focal_oregon: Deleting temporary keypair...
Build 'build_file_upload_test.amazon-ebs.ubuntu_focal_oregon' errored after 2 minutes 6 seconds: Process exited with status 1

==> Wait completed after 2 minutes 6 seconds

==> Some builds didn't complete successfully and had errors:
--> build_file_upload_test.amazon-ebs.ubuntu_focal_oregon: Process exited with status 1

==> Builds finished but no artifacts were created.
```

If you look at the log with `export PACKER_LOG=1` then you can see the actual cause of the error:
```
==> build_file_upload_test.amazon-ebs.ubuntu_focal_oregon: Uploading temp_file => /home/ubuntu
2023/04/03 12:24:52 packer-plugin-amazon_v1.2.1_x5.0_darwin_amd64 plugin: 2023/04/03 12:24:52 [DEBUG] Opening new ssh session
2023/04/03 12:24:52 packer-plugin-amazon_v1.2.1_x5.0_darwin_amd64 plugin: 2023/04/03 12:24:52 [DEBUG] Starting remote scp process: scp -vt /home
2023/04/03 12:24:52 packer-plugin-amazon_v1.2.1_x5.0_darwin_amd64 plugin: 2023/04/03 12:24:52 [DEBUG] Started SCP session, beginning transfers...
2023/04/03 12:24:52 packer-plugin-amazon_v1.2.1_x5.0_darwin_amd64 plugin: 2023/04/03 12:24:52 [DEBUG] scp: Uploading ubuntu: perms=C0600 size=10485760
2023/04/03 12:24:52 packer-plugin-amazon_v1.2.1_x5.0_darwin_amd64 plugin: 2023/04/03 12:24:52 [DEBUG] SCP session complete, closing stdin pipe.
2023/04/03 12:24:52 packer-plugin-amazon_v1.2.1_x5.0_darwin_amd64 plugin: 2023/04/03 12:24:52 [DEBUG] Waiting for SSH session to complete.
2023/04/03 12:24:52 packer-plugin-amazon_v1.2.1_x5.0_darwin_amd64 plugin: 2023/04/03 12:24:52 [DEBUG] scp stderr (length 34): Sink: C0600 10485760 ubuntu
2023/04/03 12:24:52 packer-plugin-amazon_v1.2.1_x5.0_darwin_amd64 plugin: Sink:
2023/04/03 12:24:52 packer-plugin-amazon_v1.2.1_x5.0_darwin_amd64 plugin: 2023/04/03 12:24:52 [DEBUG] non-zero exit status: 1, Process exited with status 1
2023/04/03 12:24:52 packer-plugin-amazon_v1.2.1_x5.0_darwin_amd64 plugin: 2023/04/03 12:24:52 [DEBUG] scp output: scp: /home/ubuntu: Is a directory
2023/04/03 12:24:52 packer-plugin-amazon_v1.2.1_x5.0_darwin_amd64 plugin: scp: protocol error: expected control record
==> build_file_upload_test.amazon-ebs.ubuntu_focal_oregon: Upload failed: Process exited with status 1
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.