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

[feature request] Mimic "scp -r" #58

Open
denismakogon opened this issue Dec 20, 2022 · 2 comments
Open

[feature request] Mimic "scp -r" #58

denismakogon opened this issue Dec 20, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@denismakogon
Copy link

It's good to see that this module can copy files (multiple if using dynamic "..." { }) over the wire, but it's not that simple to implement the "walk-directory" functionality using the built-in fileset TF function. So, since you already have the functionality to copy put a file onto a disk, maybe there should also be a way to "deep copy" the whole directory by providing a path to it?

@denismakogon
Copy link
Author

denismakogon commented Dec 20, 2022

It seems like easyssh can do an scp-like copying: https://github.com/hypersleep/easyssh/blob/70879c819ea11f5f4967d0fa7080dfb051884e2f/easyssh.go#L183
So, that will eventually will simplify the existing code from:

if f.Source != "" {
  src, srcErr := os.Open(f.Source)
  if srcErr != nil {
    _, _ = config.Debug("Failed to open source file %s: %v\n", f.Source, srcErr)
    return srcErr
  }
  srcStat, statErr := src.Stat()
  if statErr != nil {
    _, _ = config.Debug("Failed to stat source file %s: %v\n", f.Source, statErr)
    _ = src.Close()
    return statErr
  }
  _ = ssh.WriteFile(src, srcStat.Size(), f.Destination)
  _, _ = config.Debug("Copied %s to remote file %s:%s: %d bytes\n", f.Source, ssh.Server, f.Destination, srcStat.Size())
  _ = src.Close()
}

it could be like this:

if f.Source != "" {
    err := ssh.Scp(f.Source, f.Destination)
    if err != nil {
        config.Debug("Failed to copy file(s) %s: %v\n", f.Source, err)
        return err
    }
}

@loafoe
Copy link
Owner

loafoe commented Dec 24, 2022

Hi @denismakogon , we have a similar use cases but have been using archive_file up till now. It does require unzip on the server though. Contrived example:

data "archive_file" "data" {
  type        = "zip"
  source_dir  = "/tmp/data"
  output_path = "/tmp/data.zip"
}

resource "ssh_resource" "test" {
  host         = "vorlon.terrakube.com"
  user         = "andy"
  agent        = true
  port = "22"

  pre_commands = [
    "mkdir -p /tmp/test"
  ]

  file {
    source     = "/tmp/data.zip" // Local
    destination = "/tmp/data.zip" // Remote
    permissions = "0700"
  }

  commands = [
    "unzip -o /tmp/data.zip -d /tmp/data"
  ]
}

Will definitely have a look at the scp option though, thanks! There might have been a reason why this was not used, but I've forgotten by now, haha. I'll have a look after the holidays, in the meantime the archive_file option might be a temporary workaround for you.

@loafoe loafoe added the enhancement New feature or request label Dec 24, 2022
@loafoe loafoe moved this to Todo in Terraform providers Dec 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Development

No branches or pull requests

2 participants