Skip to content

Commit 0b7bf98

Browse files
comchangsclaude
andcommitted
refactor: #47 replace rsync with tar+SCP in UploadDir
Removes rsync dependency from target servers. UploadDir now: 1. tar czf local dir → temp file 2. SCP temp file to remote /tmp/ 3. SSH: mkdir + tar xzf + rm temp Also removed unused sshCommandOpts() and its tests. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 27ef56f commit 0b7bf98

2 files changed

Lines changed: 43 additions & 46 deletions

File tree

‎internal/ssh/ssh.go‎

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -395,35 +395,58 @@ func (m *Manager) Download(env *config.Environment, host, remotePath, localDir s
395395
return cmd.Run()
396396
}
397397

398-
// sshCommandOpts returns SSH options for external commands (rsync, ssh)
399-
func (m *Manager) sshCommandOpts(keyPath string, port int) string {
400-
opts := fmt.Sprintf("ssh -i %s -p %d", keyPath, port)
401-
if m.InsecureHostKey {
402-
opts += " -o StrictHostKeyChecking=no"
403-
}
404-
return opts
405-
}
406-
407-
// UploadDir transfers a local directory to a remote server
398+
// UploadDir transfers a local directory to a remote server via tar + SCP
408399
func (m *Manager) UploadDir(env *config.Environment, host, localDir, remoteDir string) error {
409400
if m.DryRun {
410401
logger.Info("[DRY-RUN] Would upload directory %s to %s:%s", localDir, host, remoteDir)
411402
return nil
412403
}
413404

414405
expandedKey := expandPath(env.SSHKeyPath)
415-
sshOpts := m.sshCommandOpts(expandedKey, env.SSHPort)
416406

417-
cmd := exec.Command("rsync", "-avz", "--progress",
418-
"-e", sshOpts,
419-
localDir+"/",
420-
fmt.Sprintf("%s@%s:%s/", env.SSHUser, host, remoteDir),
421-
)
422-
cmd.Stdout = os.Stdout
423-
cmd.Stderr = os.Stderr
407+
// Create temp tar.gz
408+
tmpFile, err := os.CreateTemp("", "tow-upload-*.tar.gz")
409+
if err != nil {
410+
return fmt.Errorf("creating temp file: %w", err)
411+
}
412+
tmpPath := tmpFile.Name()
413+
tmpFile.Close()
414+
defer os.Remove(tmpPath)
424415

425-
logger.Debug("[%s] rsync %s → %s", host, localDir, remoteDir)
426-
return cmd.Run()
416+
// tar the directory
417+
tarCmd := exec.Command("tar", "czf", tmpPath, "-C", localDir, ".")
418+
if err := tarCmd.Run(); err != nil {
419+
return fmt.Errorf("tar %s: %w", localDir, err)
420+
}
421+
422+
// SCP to remote
423+
remoteTmp := fmt.Sprintf("/tmp/tow-upload-%d.tar.gz", time.Now().UnixNano())
424+
scpArgs := []string{"-i", expandedKey, "-P", fmt.Sprintf("%d", env.SSHPort)}
425+
if m.InsecureHostKey {
426+
scpArgs = append(scpArgs, "-o", "StrictHostKeyChecking=no")
427+
}
428+
scpArgs = append(scpArgs, tmpPath, fmt.Sprintf("%s@%s:%s", env.SSHUser, host, remoteTmp))
429+
430+
scpCmd := exec.Command("scp", scpArgs...)
431+
if err := scpCmd.Run(); err != nil {
432+
return fmt.Errorf("scp to %s: %w", host, err)
433+
}
434+
435+
// Extract on remote
436+
extractCmd := fmt.Sprintf("mkdir -p %s && tar xzf %s -C %s && rm -f %s", remoteDir, remoteTmp, remoteDir, remoteTmp)
437+
sshArgs := []string{"-i", expandedKey, "-p", fmt.Sprintf("%d", env.SSHPort)}
438+
if m.InsecureHostKey {
439+
sshArgs = append(sshArgs, "-o", "StrictHostKeyChecking=no")
440+
}
441+
sshArgs = append(sshArgs, fmt.Sprintf("%s@%s", env.SSHUser, host), extractCmd)
442+
443+
sshCmd := exec.Command("ssh", sshArgs...)
444+
if err := sshCmd.Run(); err != nil {
445+
return fmt.Errorf("extract on %s: %w", host, err)
446+
}
447+
448+
logger.Debug("[%s] uploaded dir %s → %s", host, localDir, remoteDir)
449+
return nil
427450
}
428451

429452
// InteractiveLogin opens an interactive SSH session

‎internal/ssh/ssh_test.go‎

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -219,32 +219,6 @@ func TestExpandPathTableDriven(t *testing.T) {
219219
}
220220
}
221221

222-
// --- sshCommandOpts tests ---
223-
224-
func TestSSHCommandOpts(t *testing.T) {
225-
m := NewManager(false)
226-
opts := m.sshCommandOpts("/path/to/key", 22)
227-
if opts != "ssh -i /path/to/key -p 22" {
228-
t.Errorf("unexpected opts: %q", opts)
229-
}
230-
}
231-
232-
func TestSSHCommandOptsInsecure(t *testing.T) {
233-
m := NewManager(true)
234-
opts := m.sshCommandOpts("/path/to/key", 2222)
235-
if opts != "ssh -i /path/to/key -p 2222 -o StrictHostKeyChecking=no" {
236-
t.Errorf("unexpected opts: %q", opts)
237-
}
238-
}
239-
240-
func TestSSHCommandOptsCustomPort(t *testing.T) {
241-
m := NewManager(false)
242-
opts := m.sshCommandOpts("~/.ssh/id_rsa", 9922)
243-
if opts != "ssh -i ~/.ssh/id_rsa -p 9922" {
244-
t.Errorf("unexpected opts: %q", opts)
245-
}
246-
}
247-
248222
// --- resolveAuth tests ---
249223

250224
func TestResolveAuthDefaults(t *testing.T) {

0 commit comments

Comments
 (0)