From 5fac6c570b8ff94c342a532105447c35354337ca Mon Sep 17 00:00:00 2001 From: Jvst Me Date: Thu, 5 Dec 2024 16:48:38 +0100 Subject: [PATCH] [chore]: drop unused gateway-related runner code --- runner/internal/executor/executor.go | 16 ----- runner/internal/gateway/ssh.go | 92 ---------------------------- runner/internal/schemas/schemas.go | 11 ---- 3 files changed, 119 deletions(-) delete mode 100644 runner/internal/gateway/ssh.go diff --git a/runner/internal/executor/executor.go b/runner/internal/executor/executor.go index c5fdd0eeb..1519a9f64 100644 --- a/runner/internal/executor/executor.go +++ b/runner/internal/executor/executor.go @@ -125,22 +125,6 @@ func (ex *RunExecutor) Run(ctx context.Context) (err error) { } defer cleanupCredentials() - // var gatewayControl *gateway.SSHControl - //if ex.run.Configuration.Type == "service" { - // log.Info(ctx, "Forwarding service port to the gateway", "hostname", ex.jobSpec.Gateway.Hostname) - // gatewayControl, err = gateway.NewSSHControl(ex.jobSpec.Gateway.Hostname, ex.jobSpec.Gateway.SSHKey) - // if err != nil { - // ex.SetJobState(ctx, states.Failed) - // return gerrors.Wrap(err) - // } - // defer gatewayControl.Cleanup() - // if err = gatewayControl.Publish(strconv.Itoa(ex.jobSpec.Gateway.ServicePort), ex.jobSpec.Gateway.SockPath); err != nil { - // ex.SetJobState(ctx, states.Failed) - // return gerrors.Wrap(err) - // } - // log.Info(ctx, "SSH tunnel established", "sock_path", ex.jobSpec.Gateway.SockPath, "service_port", ex.jobSpec.Gateway.ServicePort) - //} - ex.SetJobState(ctx, states.Running) timeoutCtx := ctx var cancelTimeout context.CancelFunc diff --git a/runner/internal/gateway/ssh.go b/runner/internal/gateway/ssh.go deleted file mode 100644 index f62677fc6..000000000 --- a/runner/internal/gateway/ssh.go +++ /dev/null @@ -1,92 +0,0 @@ -package gateway - -import ( - "fmt" - "os" - "os/exec" - "path/filepath" - - "github.com/dstackai/dstack/runner/internal/gerrors" -) - -type SSHControl struct { - keyPath string - controlPath string - hostname string - user string - localTempDir string -} - -func NewSSHControl(hostname, sshKey string) (*SSHControl, error) { - localTempDir, err := os.MkdirTemp("", "") - if err != nil { - return nil, gerrors.Wrap(err) - } - keyPath := filepath.Join(localTempDir, "id_rsa") - if err := os.WriteFile(keyPath, []byte(sshKey), 0o600); err != nil { - return nil, gerrors.Wrap(err) - } - c := &SSHControl{ - keyPath: keyPath, - controlPath: filepath.Join(localTempDir, "ssh.control"), - hostname: hostname, - user: "www-data", - localTempDir: localTempDir, - } - return c, gerrors.Wrap(err) -} - -func (c *SSHControl) exec(args []string, command string) ([]byte, error) { - allArgs := []string{ - "-i", c.keyPath, - "-o", "StrictHostKeyChecking=accept-new", - "-o", fmt.Sprintf("ControlPath=%s", c.controlPath), - "-o", "ControlMaster=auto", - "-o", "ControlPersist=yes", - "-o", "ServerAliveInterval=60", - } - if args != nil { - allArgs = append(allArgs, args...) - } - allArgs = append(allArgs, fmt.Sprintf("%s@%s", c.user, c.hostname)) - if command != "" { - allArgs = append(allArgs, command) - } - cmd := exec.Command("ssh", allArgs...) - - stdoutFile, err := os.CreateTemp("", "") - if err != nil { - panic(err) - } - defer func() { _ = os.Remove(stdoutFile.Name()) }() - stderrFile, err := os.CreateTemp("", "") - if err != nil { - panic(err) - } - defer func() { _ = os.Remove(stderrFile.Name()) }() - // OpenSSH 8.2 (on Ubuntu 20.04) doesn't close stdout/stderr when running in the background (-f option). - // Run command waits indefinitely for closing pipes, but exits immediately if we are using files. - cmd.Stdout = stdoutFile - cmd.Stderr = stderrFile - - if err := cmd.Run(); err != nil { - stderr, _ := os.ReadFile(stderrFile.Name()) - return nil, gerrors.Newf("ssh exec: %s", string(stderr)) - } - stdout, _ := os.ReadFile(stdoutFile.Name()) - return stdout, nil -} - -func (c *SSHControl) Publish(localPort, sockPath string) error { - _, err := c.exec([]string{ - "-f", "-N", - "-R", fmt.Sprintf("%s:localhost:%s", sockPath, localPort), - }, "") - return gerrors.Wrap(err) -} - -func (c *SSHControl) Cleanup() { - // todo cleanup remote - _ = exec.Command("ssh", "-F", "none", "-o", "ControlPath="+c.controlPath, "-O", "exit", c.hostname).Run() - _ = os.RemoveAll(c.localTempDir) -} diff --git a/runner/internal/schemas/schemas.go b/runner/internal/schemas/schemas.go index eb676635d..d152738cd 100644 --- a/runner/internal/schemas/schemas.go +++ b/runner/internal/schemas/schemas.go @@ -45,7 +45,6 @@ type JobSpec struct { Commands []string `json:"commands"` Entrypoint []string `json:"entrypoint"` Env map[string]string `json:"env"` - Gateway *Gateway `json:"gateway"` MaxDuration int `json:"max_duration"` WorkingDir *string `json:"working_dir"` } @@ -99,16 +98,6 @@ func (u *User) GetGroupname() string { return *u.Groupname } -type Gateway struct { - GatewayName string `json:"gateway_name"` - ServicePort int `json:"service_port"` - SSHKey string `json:"ssh_key"` - SockPath string `json:"sock_path"` - Hostname string `json:"hostname"` - PublicPort int `json:"public_port"` - Secure bool `json:"secure"` -} - type HealthcheckResponse struct { Service string `json:"service"` Version string `json:"version"`