Skip to content

Commit

Permalink
fix: sock files into tmp and ssh error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Tpuljak committed Mar 6, 2024
1 parent f27baba commit 428be90
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
23 changes: 16 additions & 7 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ import (
"path"
"provider/pkg/ssh_tunnel/util"
"provider/pkg/types"
"strings"

"github.com/docker/docker/client"

log "github.com/sirupsen/logrus"
)

func GetClient(targetOptions types.TargetOptions, pluginPath string) (*client.Client, error) {
func GetClient(targetOptions types.TargetOptions, sockDir string) (*client.Client, error) {
if targetOptions.RemoteHostname == nil {
return getLocalClient()
}

return getRemoteClient(targetOptions, pluginPath)
return getRemoteClient(targetOptions, sockDir)
}

func getLocalClient() (*client.Client, error) {
Expand All @@ -30,8 +31,8 @@ func getLocalClient() (*client.Client, error) {
return cli, nil
}

func getRemoteClient(targetOptions types.TargetOptions, pluginPath string) (*client.Client, error) {
localSockPath, err := forwardDockerSock(targetOptions, pluginPath)
func getRemoteClient(targetOptions types.TargetOptions, sockDir string) (*client.Client, error) {
localSockPath, err := forwardDockerSock(targetOptions, sockDir)
if err != nil {
return nil, err
}
Expand All @@ -44,8 +45,15 @@ func getRemoteClient(targetOptions types.TargetOptions, pluginPath string) (*cli
return cli, nil
}

func forwardDockerSock(targetOptions types.TargetOptions, pluginPath string) (string, error) {
localSockPath := path.Join(pluginPath, fmt.Sprintf("daytona-%s-docker.sock", *targetOptions.RemoteHostname))
func forwardDockerSock(targetOptions types.TargetOptions, sockDir string) (string, error) {
localSockPath := path.Join(sockDir, fmt.Sprintf("daytona-%s-docker.sock", strings.ReplaceAll(*targetOptions.RemoteHostname, ".", "-")))

if _, err := os.Stat(path.Dir(localSockPath)); err != nil {
err := os.MkdirAll(path.Dir(localSockPath), 0755)
if err != nil {
return "", err
}
}

if _, err := os.Stat(localSockPath); err == nil {
return localSockPath, nil
Expand All @@ -62,8 +70,9 @@ func forwardDockerSock(targetOptions types.TargetOptions, pluginPath string) (st
err := <-errChan
if err != nil {
log.Error(err)
startedChan <- false
os.Remove(localSockPath)
}
os.Remove(localSockPath)
}()

<-startedChan
Expand Down
24 changes: 23 additions & 1 deletion pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"provider/pkg/client"
"provider/pkg/provider/util"
provider_types "provider/pkg/types"
"runtime"

"github.com/daytonaio/daytona/pkg/provider"
"github.com/daytonaio/daytona/pkg/types"
Expand All @@ -22,13 +23,34 @@ type DockerProvider struct {
ServerVersion *string
ServerUrl *string
ServerApiUrl *string
RemoteSockDir string
}

type workspaceMetadata struct {
NetworkId string
}

func (p *DockerProvider) Initialize(req provider.InitializeProviderRequest) (*types.Empty, error) {
tmpDir := "/tmp"
if runtime.GOOS == "windows" {
tmpDir = os.TempDir()
if tmpDir == "" {
return new(types.Empty), errors.New("could not determine temp dir")
}
}

p.RemoteSockDir = path.Join(tmpDir, "target-socks")

// Clear old sockets
err := os.RemoveAll(p.RemoteSockDir)
if err != nil {
return new(types.Empty), err
}
err = os.MkdirAll(p.RemoteSockDir, 0755)
if err != nil {
return new(types.Empty), err
}

p.BasePath = &req.BasePath
p.ServerDownloadUrl = &req.ServerDownloadUrl
p.ServerVersion = &req.ServerVersion
Expand Down Expand Up @@ -321,5 +343,5 @@ func (p DockerProvider) getClient(targetOptionsJson string) (*docker_client.Clie
return nil, err
}

return client.GetClient(*targetOptions, *p.BasePath)
return client.GetClient(*targetOptions, p.RemoteSockDir)
}
3 changes: 2 additions & 1 deletion pkg/ssh_tunnel/util/forward_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func ForwardRemoteUnixSock(ctx context.Context, targetOptions types.TargetOption
}
}

errChan := make(chan error)

sshTun.SetTunneledConnState(func(tun *ssh_tunnel.SshTunnel, state *ssh_tunnel.TunneledConnectionState) {
log.Debugf("%+v", state)
})
Expand All @@ -58,7 +60,6 @@ func ForwardRemoteUnixSock(ctx context.Context, targetOptions types.TargetOption
}
})

errChan := make(chan error)
go func() {
errChan <- sshTun.Start(ctx)
}()
Expand Down

0 comments on commit 428be90

Please sign in to comment.