0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-07-01 19:12:59 +00:00
terraform-provider-proxmox/docs/resources/virtual_environment_file.md
Sven Greb cc9d0e7131
feat(file): add support to set the file mode (#1478)
* feat(file): Add support to set the file mode

GH-733 [1] implemented basic support for hook scripts, but the authors
"did not manage to find time to work on" [2] also including support to
set the file mode. This small but important feature makes the use of the
`proxmox_virtual_environment_container.hook_script_file_id` [3] and
`virtual_environment_vm.hook_script_file_id` [34] attributes basically
useless when not combined with the manual step of making the uploaded
file executable (manually running `chmod +x /path/to/script` or using
other methods, based on the storage backend). Using the
`hook_script_file_id` on its own also causes all planned and applies
changes in the same execution to not be saved in the state because the
Proxmox VE API responses with a HTTP `500` because the uploaded and
assigned file is not executable.

This pull request implements the missing feature to set the file mode
by adding a new `file_mode` attribute of type `string` where an
octal-formatted value can be passed, e.g. `0700` or only `600`.
Note that the support for the octal prefixes `0o` and `0x` are not
supported to reduced the complexity, even though Go of course support
it, including the used `os.FileMode` type [5].
Changing the file mode also causes the file to be replaced, which is
true for almost any attribute in the `proxmox_virtual_environment_file`
resource, to ensure that the file mode can also be changed after the
initial creation.

[1]: https://github.com/bpg/terraform-provider-proxmox/pull/733
[2]: https://github.com/bpg/terraform-provider-proxmox/pull/733#issuecomment-2096716738
[3]: https://registry.terraform.io/providers/bpg/proxmox/latest/docs/resources/virtual_environment_container#hook_script_file_id
[4]: https://registry.terraform.io/providers/bpg/proxmox/latest/docs/resources/virtual_environment_vm#hook_script_file_id
[5]: https://pkg.go.dev/os#FileMode

Related to GH-570
Related to GH-733

Signed-off-by: Sven Greb <development@svengreb.de>


---------

Signed-off-by: Sven Greb <development@svengreb.de>
2024-08-13 21:28:48 -04:00

188 lines
6.6 KiB
Markdown

---
layout: page
title: proxmox_virtual_environment_file
parent: Resources
subcategory: Virtual Environment
---
# Resource: proxmox_virtual_environment_file
Manages a file.
## Example Usage
### Backups (`dump`)
-> The resource with this content type uses SSH access to the node. You might need to configure the [`ssh` option in the `provider` section](../index.md#node-ip-address-used-for-ssh-connection).
```hcl
resource "proxmox_virtual_environment_file" "backup" {
content_type = "dump"
datastore_id = "local"
node_name = "pve"
source_file {
path = "vzdump-lxc-100-2023_11_08-23_10_05.tar"
}
}
```
### Images
-> Consider using `proxmox_virtual_environment_download_file` resource instead. Using this resource for images is less efficient (requires to transfer uploaded image to node) though still supported.
```hcl
resource "proxmox_virtual_environment_file" "ubuntu_container_template" {
content_type = "iso"
datastore_id = "local"
node_name = "pve"
source_file {
path = "https://cloud-images.ubuntu.com/jammy/20230929/jammy-server-cloudimg-amd64-disk-kvm.img"
}
}
```
### Snippets
-> Snippets are not enabled by default in new Proxmox installations. You need to enable them in the 'Datacenter>Storage' section of the proxmox interface before first using this resource.
-> The resource with this content type uses SSH access to the node. You might need to configure the [`ssh` option in the `provider` section](../index.md#node-ip-address-used-for-ssh-connection).
```hcl
resource "proxmox_virtual_environment_file" "cloud_config" {
content_type = "snippets"
datastore_id = "local"
node_name = "pve"
source_raw {
data = <<-EOF
#cloud-config
chpasswd:
list: |
ubuntu:example
expire: false
hostname: example-hostname
packages:
- qemu-guest-agent
users:
- default
- name: ubuntu
groups: sudo
shell: /bin/bash
ssh-authorized-keys:
- ${trimspace(tls_private_key.example.public_key_openssh)}
sudo: ALL=(ALL) NOPASSWD:ALL
EOF
file_name = "example.cloud-config.yaml"
}
}
```
The `file_mode` attribute can be used to make a script file executable, e.g. when referencing the file in the `hook_script_file_id` attribute of [a container](https://registry.terraform.io/providers/bpg/proxmox/latest/docs/resources/virtual_environment_container#hook_script_file_id) or [a VM](https://registry.terraform.io/providers/bpg/proxmox/latest/docs/resources/virtual_environment_vm#hook_script_file_id) resource which is a requirement enforced by the Proxmox VE API.
```hcl
resource "proxmox_virtual_environment_file" "hook_script" {
content_type = "snippets"
datastore_id = "local"
node_name = "pve"
# Hook scripts must be executable, otherwise the Proxmox VE API will reject the configuration for the VM/CT.
file_mode = "0700"
source_raw {
data = <<-EOF
#!/usr/bin/env bash
echo "Running hook script"
EOF
file_name = "prepare-hook.sh"
}
}
```
### Container Template (`vztmpl`)
-> Consider using `proxmox_virtual_environment_download_file` resource instead. Using this resource for container images is less efficient (requires to transfer uploaded image to node) though still supported.
```hcl
resource "proxmox_virtual_environment_file" "ubuntu_container_template" {
content_type = "vztmpl"
datastore_id = "local"
node_name = "first-node"
source_file {
path = "https://download.proxmox.com/images/system/ubuntu-20.04-standard_20.04-1_amd64.tar.gz"
}
}
```
## Argument Reference
- `content_type` - (Optional) The content type. If not specified, the content
type will be inferred from the file extension. Valid values are:
- `dump` (allowed extensions: `.vzdump`)
- `iso` (allowed extensions: `.iso`, `.img`)
- `snippets` (allowed extensions: any)
- `vztmpl` (allowed extensions: `.tar.gz`, `.tar.xz`, `tar.zst`)
- `datastore_id` - (Required) The datastore id.
- `file_mode` - The file mode in octal format, e.g. `0700` or `600`. Note that the prefixes `0o` and `0x` is not supported! Setting this attribute is also only allowed for `root@pam` authenticated user.
- `node_name` - (Required) The node name.
- `overwrite` - (Optional) Whether to overwrite an existing file (defaults to
`true`).
- `source_file` - (Optional) The source file (conflicts with `source_raw`),
could be a local file or a URL. If the source file is a URL, the file will
be downloaded and stored locally before uploading it to Proxmox VE.
- `checksum` - (Optional) The SHA256 checksum of the source file.
- `file_name` - (Optional) The file name to use instead of the source file
name. Useful when the source file does not have a valid file extension,
for example when the source file is a URL referencing a `.qcow2` image.
- `insecure` - (Optional) Whether to skip the TLS verification step for
HTTPS sources (defaults to `false`).
- `min_tls` - (Optional) The minimum required TLS version for HTTPS
sources. "Supported values: `1.0|1.1|1.2|1.3` (defaults to `1.3`).
- `path` - (Required) A path to a local file or a URL.
- `source_raw` - (Optional) The raw source (conflicts with `source_file`).
- `data` - (Required) The raw data.
- `file_name` - (Required) The file name.
- `resize` - (Optional) The number of bytes to resize the file to.
- `timeout_upload` - (Optional) Timeout for uploading ISO/VSTMPL files in
seconds (defaults to 1800).
## Attribute Reference
- `file_modification_date` - The file modification date (RFC 3339).
- `file_name` - The file name.
- `file_size` - The file size in bytes.
- `file_tag` - The file tag.
## Important Notes
The Proxmox VE API endpoint for file uploads does not support chunked transfer
encoding, which means that we must first store the source file as a temporary
file locally before uploading it.
You must ensure that you have at least `Size-in-MB * 2 + 1` MB of storage space
available (twice the size plus overhead because a multipart payload needs to be
created as another temporary file).
By default, if the specified file already exists, the resource will
unconditionally replace it and take ownership of the resource. On destruction,
the file will be deleted as if it did not exist before. If you want to prevent
the resource from replacing the file, set `overwrite` to `false`.
## Import
Instances can be imported using the `node_name`, `datastore_id`, `content_type`
and the `file_name` in the following format:
```text
node_name:datastore_id/content_type/file_name
```
Example:
```bash
terraform import proxmox_virtual_environment_file.cloud_config pve/local:snippets/example.cloud-config.yaml
```