hashicorp / terraform-provider-docker

As part of our introduction to self-service publishing in the Terraform Registry, this copy of the provider has been archived, and ownership has been transferred to active maintainers in the community. Please see the new location on the Terraform Registry: https://registry.terraform.io/providers/kreuzwerker/docker/latest
https://registry.terraform.io/providers/kreuzwerker/docker/latest
Mozilla Public License 2.0
132 stars 92 forks source link

Upload file with new timestamp #246

Open mleonhard opened 4 years ago

mleonhard commented 4 years ago

Terraform Version

Terraform v0.12.20
+ provider.digitalocean v1.14.0
+ provider.docker v2.7.0   <--- Latest version as of 2012-03-12
+ provider.local v1.4.0
+ provider.null v2.1.2
+ provider.random v2.2.1
+ provider.tls v2.1.1
+ provider.twilio (unversioned)

Affected Resource(s)

Terraform Configuration Files

resource "docker_container" "grafana" {
  name = "grafana"
  user = "1000"
  upload {
    file = "/etc/grafana/provisioning/dashboards/containers.json"
    content = file("${path.module}/containers.json")
  }
  ...
}

Expected Behavior

I need Terraform to update the timestamp of the uploaded file when it changes the file's contents.

Actual Behavior

Terraform currently uploads the file and sets its timestamp to 0. It also sets its owner to root. A non-root container cannot update the file timestamp.

root@staging19:~# docker exec -t -i grafana sh
/usr/share/grafana $ ls -alF /etc/grafana/provisioning/dashboards/containers.json
-rw-r--r--    1 root     root         32101 Jan  1  1970 /etc/grafana/provisioning/dashboards/containers.json
/usr/share/grafana $ touch /etc/grafana/provisioning/dashboards/containers.json
touch: /etc/grafana/provisioning/dashboards/containers.json: Permission denied
/usr/share/grafana $ 
mleonhard commented 4 years ago

I need this because Grafana reloads dashboard files only when their timestamp has increased. It stores the timestamp in its database. Grafana's dashboards.fileReader.saveDashboard()

mleonhard commented 4 years ago

I found two workarounds:

  1. Include a hash of the file contents in the filename:
    locals {
     dashboard_json = file("${path.module}/containers.json")
    }
    resource "docker_container" "grafana" {
     upload {
       file = "/etc/grafana/provisioning/dashboards/containers.json"
       content = file("${path.module}/containers.json")
       file = "/etc/grafana/provisioning/dashboards/dashboard.${md5(local.dashboard_json)}.json"
       content = local.dashboard_json
     }
     ...
    }
  2. Put the file in the image. This works as long as the dashboard doesn't require any config data from Terraform.