Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement lxd_instance_file data resource #319

Conversation

tarruda
Copy link

@tarruda tarruda commented Jul 24, 2023

This data source provides a simple method for reading data from files in the instance (not created by the current terraform config).

It can also be used as a method to synchronize instance creation and transfer data between them, as shown in the following example:

# Explanation of what happens in order:
#
# - c1 is created
# - c1 installs nginx and writes its own LAN ip to /my-lan-ip file
# - c2, which was waiting for "c1_ip" to be read, is started
# - c2 invokes curl to read nginx index.html (running in c1) and converts to a text file (/index.txt)
# - output value is set.

data "lxd_instance_file" "c1_ip" {
  instance_name = lxd_instance.c1.name
  target_file = "/my-lan-ip"
  timeout = 30
}

data "lxd_instance_file" "index_txt" {
  instance_name = lxd_instance.c2.name
  target_file = "/index.txt"
  timeout = 30
}

resource "lxd_instance" "c1" {
  name = "c1"
  image = "ubuntu:20.04"
  config = {
    "user.user-data": <<-EOF
    #cloud-config
    package_update: true
    runcmd:
      - apt-get install --no-install-recommends -y nginx
      # get LAN ip
      - ip route get 8.8.8.8 | sed -n '/src/{s/.*src *\([^ ]*\).*/\1/p;q}' > /my-lan-ip
    EOF
  }
}

resource "lxd_instance" "c2" {
  name = "c2"
  image = "ubuntu:20.04"
  config = {
    "user.user-data": <<-EOF
    #cloud-config
    package_update: true
    runcmd:
      - apt-get install --no-install-recommends -y lynx
      - curl http://${trimspace(data.lxd_instance_file.c1_ip.content)} > index.html
      - lynx --dump index.html > /index.txt
    EOF
  }
}

output "out" {
  value = trimspace(data.lxd_instance_file.index_txt.content)
}

This resource represents a file that is created by the instance
(possibly by a setup script) and provides a simple method for
synchronization and communication between resources.
@tarruda tarruda marked this pull request as ready for review July 24, 2023 00:07
@tarruda tarruda changed the title [POC] Implement lxd_instance_created_file resource [POC] Implement lxd_instance_file data resource Jul 25, 2023
A data resource is the ideal terraform object that represents an
externally created file.
@tarruda tarruda force-pushed the implement-instance-created-file-resource branch from a1072b3 to a1241b2 Compare July 25, 2023 17:55
@tarruda tarruda force-pushed the implement-instance-created-file-resource branch from 87fb268 to c3bd114 Compare July 25, 2023 19:13
@tarruda tarruda changed the title [POC] Implement lxd_instance_file data resource Implement lxd_instance_file data resource Jul 26, 2023
@tarruda
Copy link
Author

tarruda commented Jul 26, 2023

@jtopjian @adamcstephens this is ready for review

@tarruda tarruda force-pushed the implement-instance-created-file-resource branch from 3f91873 to b0332fc Compare July 26, 2023 11:17
@adamcstephens
Copy link
Collaborator

adamcstephens commented Aug 4, 2023

Do you have another use case besides the example given? It seems to me that using wait_for_network along with the computed ipv4 address would be a simpler way of getting this data.

@tarruda
Copy link
Author

tarruda commented Aug 4, 2023

Do you have another use case besides the example given? It seems to me that using wait_for_network along with the computed ipv4 address would be a simpler way of getting this data.

Yes. I'm using this for passing a ca certificate generated in one container (A squid package cache) to another container that uses it as a proxy.

I'm sure there are more use cases for reading files from a container/VM though.

@adamcstephens
Copy link
Collaborator

Are you aware that resource output data gets put into the terraform state? The fact that the content is returned concerns me.

@tarruda
Copy link
Author

tarruda commented Aug 4, 2023

Are you aware that resource output data gets put into the terraform state? The fact that the content is returned concerns me.

Yes, but this is the case for other similar data resources: https://registry.terraform.io/providers/hashicorp/local/latest/docs/data-sources/file

@adamcstephens
Copy link
Collaborator

Thanks for that link. I'm ok with this resource given that example.

We'll need some acceptance tests for this resource to ensure it works as desired. I'm thinking we need at least the cases where the file is retrieved and when the timeout occurs. I assume there is no case of missing file, since the retries will happen until the timeout.

You can see some examples in the other resources, and here are a couple links:

https://developer.hashicorp.com/terraform/plugin/sdkv2/testing/acceptance-tests
https://pkg.go.dev/github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource#TestCase

@tarruda tarruda force-pushed the implement-instance-created-file-resource branch from 4dc21cd to 115600c Compare August 5, 2023 10:49
@tarruda
Copy link
Author

tarruda commented Aug 5, 2023

@adamcstephens I've tried writing an acceptance test based on the existing one for instance file resource and this one

It doesn't look the tests are running correctly in my machine though (test always succeed).

@adamcstephens
Copy link
Collaborator

Are you sure you're running the acceptance tests? You can run them with make testacc. I plan on disabling the debug in that task, so a heads up it's quite verbose.

@tarruda
Copy link
Author

tarruda commented Aug 6, 2023

I'm closing this since I've found shell script provider which has everything I need to automate LXD with terraform. If anyone else is interested, feel free to pick up where I stopped.

@tarruda tarruda closed this Aug 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants