-
Notifications
You must be signed in to change notification settings - Fork 8
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
Comments
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 :) |
This would also not clean up perfectly if all resources are deleted, since the directories are not tracked in the terraform state. |
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. |
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 If you want to create a PR implementing the |
After thinking on it a bit more, and after implementing some work arounds, I think a The destroy step can also double check if the directory is empty before deleting. |
Alright, let's go with |
when you say remote_directory, do you mean just create/delete of directory or would it handle a directory (ie recursive or directory contents) |
When creating a remote file, if the full path does not exist then the resource throws and error.
For example:
Throws an error
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.The text was updated successfully, but these errors were encountered: