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

suggestion: create directories if needed #99

Open
atbentley opened this issue Jun 18, 2022 · 7 comments
Open

suggestion: create directories if needed #99

atbentley opened this issue Jun 18, 2022 · 7 comments

Comments

@atbentley
Copy link

When creating a remote file, if the full path does not exist then the resource throws and error.

For example:

resource "remote_file" "file" {
  conn        = local.conn
  path        = "/home/ubuntu/services/caddy/docker-compose.yaml"
  content     = file("${path.module}/docker-compose.yaml")
  permissions = "0644"
}

Throws an error

Error: unable to create remote file: scp: /home/ubuntu/services/caddy/docker-compose.yaml: No such file or directory

However if I create the directory /home/ubuntu/services/caddy and then re-run terraform then everything works.

I think it would be nice to have a flag on the resource create_directory which would also create the directory if need be.

@atbentley
Copy link
Author

atbentley commented Jun 18, 2022

If I would suggest an implementation for this, then looking at the code I see that there are two implementations for writing a file: the SCP implementation for non-sudo and a shell implementation for sudo. Because of this I think it would make sense to add something like this:

func (c *RemoteClient) WriteFile(content string, path string, permissions string, sudo bool) error {

       c.EnsureDirectory(path)  // use the ssh client to create the directory

	if sudo {
		return c.WriteFileShell(content, path)
	}
	return c.WriteFileSCP(content, path, permissions)
}

The owner and group would be the same as the owner and group for the file, and the permissions of the directory can maybe be hard coded to something, perhaps 775?

I'm pretty interested in this so if you also think it's reasonable I would perhaps submit a PR :)

@atbentley
Copy link
Author

This would also not clean up perfectly if all resources are deleted, since the directories are not tracked in the terraform state.

@tenstad
Copy link
Owner

tenstad commented Jun 18, 2022

Thank you for the suggestion! I agree, one should not have to manually create directories. We can, as you suggest, ensure that the directories exist when creating a file. Another possibility is to introduce a new directory resource. With such a resource, one could configure the file resource to be dependant on the directory resource, or reference its path in the file resource:

resource "remote_directory" "bar" {
    path = "/etc/foo/bar"
}

resource "remote_file" "baz" {
    path = "${remote_directory.bar.path}/baz.txt"
}

It would still be tricky to correctly delete the directory.

@tenstad
Copy link
Owner

tenstad commented Jun 18, 2022

I think we can start with your suggestion, and possibly create a directory resource in the future.

Yes, there are currently two different ways of creating a file, depending on sudo. I am tempted to use the shell implementation for everything in the future, but think we should leave it as is for now.

If you want to create a PR implementing the create_directory property and the EnsureDirectory method, that would be great! Let me know if you run into any problems.

@atbentley
Copy link
Author

After thinking on it a bit more, and after implementing some work arounds, I think a remote_directory resource is a better way to go.

The destroy step can also double check if the directory is empty before deleting.

@tenstad
Copy link
Owner

tenstad commented Jun 20, 2022

Alright, let's go with remote_directory then! You should be able to use a lot from the remote_file resource when creating it.

@rismoney
Copy link

when you say remote_directory, do you mean just create/delete of directory or would it handle a directory (ie recursive or directory contents)

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

No branches or pull requests

3 participants