Skip to content

Commit 27b4e78

Browse files
authored
Merge pull request #1725 from afbjorklund/reverse-remove
Don't delete local socket when reverse
2 parents ca9f32f + 05cff79 commit 27b4e78

File tree

1 file changed

+39
-11
lines changed

1 file changed

+39
-11
lines changed

pkg/hostagent/hostagent.go

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,21 @@ const (
567567
verbCancel = "cancel"
568568
)
569569

570+
func executeSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, command ...string) error {
571+
args := sshConfig.Args()
572+
args = append(args,
573+
"-p", strconv.Itoa(port),
574+
"127.0.0.1",
575+
"--",
576+
)
577+
args = append(args, command...)
578+
cmd := exec.CommandContext(ctx, sshConfig.Binary(), args...)
579+
if out, err := cmd.Output(); err != nil {
580+
return fmt.Errorf("failed to run %v: %q: %w", cmd.Args, string(out), err)
581+
}
582+
return nil
583+
}
584+
570585
func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local, remote string, verb string, reverse bool) error {
571586
args := sshConfig.Args()
572587
args = append(args,
@@ -594,36 +609,49 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
594609
case verbForward:
595610
if reverse {
596611
logrus.Infof("Forwarding %q (host) to %q (guest)", local, remote)
612+
if err := executeSSH(ctx, sshConfig, port, "rm", "-f", remote); err != nil {
613+
logrus.WithError(err).Warnf("Failed to clean up %q (guest) before setting up forwarding", remote)
614+
}
597615
} else {
598616
logrus.Infof("Forwarding %q (guest) to %q (host)", remote, local)
599-
}
600-
if err := os.RemoveAll(local); err != nil {
601-
logrus.WithError(err).Warnf("Failed to clean up %q (host) before setting up forwarding", local)
617+
if err := os.RemoveAll(local); err != nil {
618+
logrus.WithError(err).Warnf("Failed to clean up %q (host) before setting up forwarding", local)
619+
}
602620
}
603621
if err := os.MkdirAll(filepath.Dir(local), 0750); err != nil {
604622
return fmt.Errorf("can't create directory for local socket %q: %w", local, err)
605623
}
606624
case verbCancel:
607625
if reverse {
608626
logrus.Infof("Stopping forwarding %q (host) to %q (guest)", local, remote)
627+
if err := executeSSH(ctx, sshConfig, port, "rm", "-f", remote); err != nil {
628+
logrus.WithError(err).Warnf("Failed to clean up %q (guest) after stopping forwarding", remote)
629+
}
609630
} else {
610631
logrus.Infof("Stopping forwarding %q (guest) to %q (host)", remote, local)
632+
defer func() {
633+
if err := os.RemoveAll(local); err != nil {
634+
logrus.WithError(err).Warnf("Failed to clean up %q (host) after stopping forwarding", local)
635+
}
636+
}()
611637
}
612-
defer func() {
613-
if err := os.RemoveAll(local); err != nil {
614-
logrus.WithError(err).Warnf("Failed to clean up %q (host) after stopping forwarding", local)
615-
}
616-
}()
617638
default:
618639
panic(fmt.Errorf("invalid verb %q", verb))
619640
}
620641
}
621642
cmd := exec.CommandContext(ctx, sshConfig.Binary(), args...)
622643
if out, err := cmd.Output(); err != nil {
623644
if verb == verbForward && strings.HasPrefix(local, "/") {
624-
logrus.WithError(err).Warnf("Failed to set up forward from %q (guest) to %q (host)", remote, local)
625-
if removeErr := os.RemoveAll(local); err != nil {
626-
logrus.WithError(removeErr).Warnf("Failed to clean up %q (host) after forwarding failed", local)
645+
if reverse {
646+
logrus.WithError(err).Warnf("Failed to set up forward from %q (host) to %q (guest)", local, remote)
647+
if err := executeSSH(ctx, sshConfig, port, "rm", "-f", remote); err != nil {
648+
logrus.WithError(err).Warnf("Failed to clean up %q (guest) after forwarding failed", remote)
649+
}
650+
} else {
651+
logrus.WithError(err).Warnf("Failed to set up forward from %q (guest) to %q (host)", remote, local)
652+
if removeErr := os.RemoveAll(local); err != nil {
653+
logrus.WithError(removeErr).Warnf("Failed to clean up %q (host) after forwarding failed", local)
654+
}
627655
}
628656
}
629657
return fmt.Errorf("failed to run %v: %q: %w", cmd.Args, string(out), err)

0 commit comments

Comments
 (0)