2013-07-04 15:07:53 -04:00
|
|
|
---
|
2017-06-14 21:04:16 -04:00
|
|
|
description: |
|
|
|
|
The file Packer provisioner uploads files to machines built by Packer. The
|
|
|
|
recommended usage of the file provisioner is to use it to upload files, and
|
|
|
|
then use shell provisioner to move them to the proper place, set permissions,
|
|
|
|
etc.
|
2015-07-22 22:31:00 -04:00
|
|
|
layout: docs
|
2017-06-14 21:04:16 -04:00
|
|
|
page_title: 'File - Provisioners'
|
|
|
|
sidebar_current: 'docs-provisioners-file'
|
2017-03-25 18:13:52 -04:00
|
|
|
---
|
2013-07-04 15:07:53 -04:00
|
|
|
|
|
|
|
# File Provisioner
|
|
|
|
|
|
|
|
Type: `file`
|
|
|
|
|
2014-10-20 16:47:30 -04:00
|
|
|
The file Packer provisioner uploads files to machines built by Packer. The
|
2015-07-22 22:31:00 -04:00
|
|
|
recommended usage of the file provisioner is to use it to upload files, and then
|
|
|
|
use [shell provisioner](/docs/provisioners/shell.html) to move them to the
|
|
|
|
proper place, set permissions, etc.
|
2013-07-04 15:07:53 -04:00
|
|
|
|
2013-09-09 16:58:23 -04:00
|
|
|
The file provisioner can upload both single files and complete directories.
|
|
|
|
|
2013-07-04 15:07:53 -04:00
|
|
|
## Basic Example
|
|
|
|
|
2017-06-14 21:04:16 -04:00
|
|
|
``` json
|
2013-07-04 15:07:53 -04:00
|
|
|
{
|
|
|
|
"type": "file",
|
|
|
|
"source": "app.tar.gz",
|
|
|
|
"destination": "/tmp/app.tar.gz"
|
|
|
|
}
|
2014-10-20 13:55:16 -04:00
|
|
|
```
|
2013-07-04 15:07:53 -04:00
|
|
|
|
|
|
|
## Configuration Reference
|
|
|
|
|
|
|
|
The available configuration options are listed below. All elements are required.
|
|
|
|
|
2017-06-14 21:04:16 -04:00
|
|
|
- `source` (string) - The path to a local file or directory to upload to
|
2015-07-22 23:25:58 -04:00
|
|
|
the machine. The path can be absolute or relative. If it is relative, it is
|
|
|
|
relative to the working directory when Packer is executed. If this is a
|
|
|
|
directory, the existence of a trailing slash is important. Read below on
|
|
|
|
uploading directories.
|
|
|
|
|
2017-06-14 21:04:16 -04:00
|
|
|
- `destination` (string) - The path where the file will be uploaded to in
|
2015-07-22 23:25:58 -04:00
|
|
|
the machine. This value must be a writable location and any parent
|
|
|
|
directories must already exist.
|
|
|
|
|
2017-06-14 21:04:16 -04:00
|
|
|
- `direction` (string) - The direction of the file transfer. This defaults to
|
2017-07-12 18:03:06 -04:00
|
|
|
"upload". If it is set to "download" then the file "source" in the machine
|
2015-12-09 14:49:04 -05:00
|
|
|
will be downloaded locally to "destination"
|
2015-06-15 18:08:04 -04:00
|
|
|
|
2016-09-15 16:46:10 -04:00
|
|
|
- `generated` (boolean) - If true, check the file existence only before uploading.
|
|
|
|
This allows to upload files created on-the-fly. This defaults to false.
|
|
|
|
|
2013-09-09 16:58:23 -04:00
|
|
|
## Directory Uploads
|
|
|
|
|
2015-07-22 22:31:00 -04:00
|
|
|
The file provisioner is also able to upload a complete directory to the remote
|
|
|
|
machine. When uploading a directory, there are a few important things you should
|
|
|
|
know.
|
2013-09-09 16:58:23 -04:00
|
|
|
|
2015-07-22 22:31:00 -04:00
|
|
|
First, the destination directory must already exist. If you need to create it,
|
|
|
|
use a shell provisioner just prior to the file provisioner in order to create
|
2017-03-09 20:30:07 -05:00
|
|
|
the directory. If the destination directory does not exist, the file
|
2017-10-03 19:47:30 -04:00
|
|
|
provisioner may succeed, but it will have undefined results.
|
2013-09-09 16:58:23 -04:00
|
|
|
|
|
|
|
Next, the existence of a trailing slash on the source path will determine
|
2015-07-22 22:31:00 -04:00
|
|
|
whether the directory name will be embedded within the destination, or whether
|
|
|
|
the destination will be created. An example explains this best:
|
2013-09-09 16:58:23 -04:00
|
|
|
|
2015-07-22 22:31:00 -04:00
|
|
|
If the source is `/foo` (no trailing slash), and the destination is `/tmp`, then
|
|
|
|
the contents of `/foo` on the local machine will be uploaded to `/tmp/foo` on
|
|
|
|
the remote machine. The `foo` directory on the remote machine will be created by
|
|
|
|
Packer.
|
2013-09-09 16:58:23 -04:00
|
|
|
|
2015-07-22 22:31:00 -04:00
|
|
|
If the source, however, is `/foo/` (a trailing slash is present), and the
|
|
|
|
destination is `/tmp`, then the contents of `/foo` will be uploaded into `/tmp`
|
|
|
|
directly.
|
2013-09-09 16:58:23 -04:00
|
|
|
|
2015-07-22 22:31:00 -04:00
|
|
|
This behavior was adopted from the standard behavior of rsync. Note that under
|
|
|
|
the covers, rsync may or may not be used.
|
2017-01-27 21:31:24 -05:00
|
|
|
|
2017-11-27 20:23:31 -05:00
|
|
|
## Uploading files that don't exist before Packer starts
|
|
|
|
|
|
|
|
In general, local files used as the source **must** exist before Packer is run.
|
|
|
|
This is great for catching typos and ensuring that once a build is started,
|
|
|
|
that it will succeed. However, this also means that you can't generate a file
|
|
|
|
during your build and then upload it using the file provisioner later.
|
|
|
|
A convenient workaround is to upload a directory instead of a file. The
|
|
|
|
directory still must exist, but its contents don't. You can write your
|
|
|
|
generated file to the directory during the Packer run, and have it be uploaded
|
|
|
|
later.
|
|
|
|
|
2017-01-27 21:31:24 -05:00
|
|
|
## Symbolic link uploads
|
|
|
|
|
|
|
|
The behavior when uploading symbolic links depends on the communicator. The
|
|
|
|
Docker communicator will preserve symlinks, but all other communicators will
|
2017-03-01 18:45:29 -05:00
|
|
|
treat local symlinks as regular files. If you wish to preserve symlinks when
|
2017-01-27 21:31:24 -05:00
|
|
|
uploading, it's recommended that you use `tar`. Below is an example of what
|
|
|
|
that might look like:
|
|
|
|
|
2017-06-14 21:04:16 -04:00
|
|
|
``` text
|
2017-03-25 18:13:52 -04:00
|
|
|
$ ls -l files
|
2017-01-27 21:31:24 -05:00
|
|
|
total 16
|
|
|
|
drwxr-xr-x 3 mwhooker staff 102 Jan 27 17:10 a
|
|
|
|
lrwxr-xr-x 1 mwhooker staff 1 Jan 27 17:10 b -> a
|
|
|
|
-rw-r--r-- 1 mwhooker staff 0 Jan 27 17:10 file1
|
|
|
|
lrwxr-xr-x 1 mwhooker staff 5 Jan 27 17:10 file1link -> file1
|
2017-11-22 03:13:47 -05:00
|
|
|
$ ls -l toupload
|
|
|
|
total 0
|
|
|
|
-rw-r--r-- 1 mwhooker staff 0 Jan 27 17:10 files.tar
|
2017-01-27 21:31:24 -05:00
|
|
|
```
|
|
|
|
|
2017-06-14 21:04:16 -04:00
|
|
|
``` json
|
2017-03-25 18:13:52 -04:00
|
|
|
{
|
|
|
|
"provisioners": [
|
2017-03-09 19:30:51 -05:00
|
|
|
{
|
2017-03-25 18:13:52 -04:00
|
|
|
"type": "shell-local",
|
2017-11-22 03:13:47 -05:00
|
|
|
"command": "tar cf toupload/files.tar files"
|
2017-03-09 19:30:51 -05:00
|
|
|
},
|
|
|
|
{
|
2017-03-25 18:13:52 -04:00
|
|
|
"destination": "/tmp/",
|
|
|
|
"source": "./toupload",
|
|
|
|
"type": "file"
|
2017-03-09 19:30:51 -05:00
|
|
|
},
|
|
|
|
{
|
2017-03-25 18:13:52 -04:00
|
|
|
"inline": [
|
|
|
|
"cd /tmp && tar xf toupload/files.tar",
|
|
|
|
"rm toupload/files.tar"
|
|
|
|
],
|
|
|
|
"type": "shell"
|
2017-03-09 19:30:51 -05:00
|
|
|
}
|
2017-03-25 18:13:52 -04:00
|
|
|
]
|
|
|
|
}
|
2017-01-27 21:31:24 -05:00
|
|
|
```
|
2017-10-12 02:12:31 -04:00
|
|
|
|
|
|
|
## Slowness when transferring large files over WinRM.
|
|
|
|
|
|
|
|
Because of the way our WinRM transfers works, it can take a very long time to
|
|
|
|
upload and download even moderately sized files. If you're experiencing
|
|
|
|
slowness using the file provisioner on Windows, it's suggested that you set up
|
|
|
|
an SSH server and use the [ssh
|
|
|
|
communicator](/docs/templates/communicator.html#ssh-communicator). If you only
|
|
|
|
want to transfer files to your guest, and if your builder supports it, you may
|
|
|
|
also use the `http_directory` directive. This will cause that directory to be
|
|
|
|
available to the guest over http, and set the environment variable
|
|
|
|
`PACKER_HTTP_ADDR` to the address.
|