Skip to content

Commit

Permalink
chore: refactor docker client
Browse files Browse the repository at this point in the history
Signed-off-by: bcmmbaga <[email protected]>
  • Loading branch information
bcmmbaga committed Aug 1, 2024
1 parent bbc2d62 commit 4d923e3
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 50 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/daytonaio/daytona-provider-fly
go 1.22.2

require (
github.com/daytonaio/daytona v0.23.0
github.com/daytonaio/daytona v0.23.1
github.com/docker/docker v26.1.0+incompatible
github.com/google/uuid v1.6.0
github.com/hashicorp/go-hclog v1.6.3
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,8 @@ github.com/daytonaio/daytona v0.22.0 h1:JW9VY3iNZv0lAc06hux5hnJBaCQnmti35D4ZGKT7
github.com/daytonaio/daytona v0.22.0/go.mod h1:Wt2JaDzVbwyOKSj4/g//+TQrdZk0WP6IvUgUDanHVHI=
github.com/daytonaio/daytona v0.23.0 h1:gwOqjKN/RSe1xhew+CrgefD8I+v53jOy37EBf9CQcmE=
github.com/daytonaio/daytona v0.23.0/go.mod h1:8yXvTGwapmO6ygfj2jMR9wWwjbpW7HBGNm8ALAeqFMQ=
github.com/daytonaio/daytona v0.23.1 h1:07ZvgiZWT1kM4X/Iu1cq86p6m8SjIXoVdqw52agOGp8=
github.com/daytonaio/daytona v0.23.1/go.mod h1:8yXvTGwapmO6ygfj2jMR9wWwjbpW7HBGNm8ALAeqFMQ=
github.com/dblohm7/wingoes v0.0.0-20231025182615-65d8b4b5428f h1:c5mkOIXbHZVKGQaSEZZyLW9ORD+h4PT2TPF8IQPwyOs=
github.com/dblohm7/wingoes v0.0.0-20231025182615-65d8b4b5428f/go.mod h1:6NCrWM5jRefaG7iN0iMShPalLsljHWBh9v1zxM2f8Xs=
github.com/digitalocean/go-smbios v0.0.0-20180907143718-390a4f403a8e h1:vUmf0yezR0y7jJ5pceLHthLaYf4bA5T14B6q39S4q2Q=
Expand Down
38 changes: 2 additions & 36 deletions pkg/provider/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package provider

import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"time"

Expand All @@ -13,7 +11,6 @@ import (
"github.com/daytonaio/daytona/pkg/tailscale"
"github.com/docker/docker/client"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
"tailscale.com/tsnet"
)

Expand Down Expand Up @@ -63,39 +60,8 @@ func (p *FlyProvider) getDockerClient(workspaceId string) (docker.IDockerClient,
return nil, err
}

localSockPath := filepath.Join(p.LocalSockDir, workspaceId, "docker-forward.sock")

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

startedChan, errChan := tailscale.ForwardRemoteUnixSock(tailscale.ForwardConfig{
Ctx: context.Background(),
TsnetConn: tsnetConn,
Hostname: workspaceId,
SshPort: config.SSH_PORT,
LocalSock: localSockPath,
RemoteSock: "/var/run/docker.sock",
})

go func() {
err := <-errChan
if err != nil {
log.Error(err)
startedChan <- false
_ = os.Remove(localSockPath)
}
}()

started := <-startedChan
if !started {
return nil, errors.New("failed to start SSH tunnel")
}
}

cli, err := client.NewClientWithOpts(client.WithHost(fmt.Sprintf("unix://%s", localSockPath)), client.WithAPIVersionNegotiation())
remoteHost := fmt.Sprintf("tcp://%s:2375", workspaceId)
cli, err := client.NewClientWithOpts(client.WithDialContext(tsnetConn.Dial), client.WithHost(remoteHost), client.WithAPIVersionNegotiation())
if err != nil {
return nil, err
}
Expand Down
12 changes: 0 additions & 12 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import (
"errors"
"fmt"
"io"
"os"
"path"
"runtime"
"time"

"github.com/daytonaio/daytona-provider-fly/internal"
Expand Down Expand Up @@ -36,20 +34,11 @@ type FlyProvider struct {
ApiPort *uint32
ServerPort *uint32
LogsDir *string
LocalSockDir string
tsnetConn *tsnet.Server
}

// Initialize initializes the provider with the given configuration.
func (p *FlyProvider) Initialize(req provider.InitializeProviderRequest) (*util.Empty, error) {
tmpDir := "/tmp"
if runtime.GOOS == "windows" {
tmpDir = os.TempDir()
if tmpDir == "" {
return nil, errors.New("could not determine temp dir")
}
}

p.BasePath = &req.BasePath
p.DaytonaDownloadUrl = &req.DaytonaDownloadUrl
p.DaytonaVersion = &req.DaytonaVersion
Expand All @@ -59,7 +48,6 @@ func (p *FlyProvider) Initialize(req provider.InitializeProviderRequest) (*util.
p.ApiPort = &req.ApiPort
p.ServerPort = &req.ServerPort
p.LogsDir = &req.LogsDir
p.LocalSockDir = path.Join(tmpDir, "fly-socks")

return new(util.Empty), nil
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/provider/util/fly.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ adduser -D -G docker daytona
su daytona -c "daytona agent --host"
`, initScript)

envVars := workspace.EnvVars
// Disable running docker with TLS
envVars["DOCKER_TLS_VERIFY"] = ""
envVars["DOCKER_TLS_CERTDIR"] = ""

return flapsClient.Launch(context.Background(), fly.LaunchMachineInput{
Name: getResourceName(workspace.Id),
Config: &fly.MachineConfig{
Expand All @@ -176,7 +181,7 @@ su daytona -c "daytona agent --host"
Init: fly.MachineInit{
Entrypoint: []string{"/bin/sh", "-c", script},
},
Env: workspace.EnvVars,
Env: envVars,
},
Region: opts.Region,
})
Expand Down

0 comments on commit 4d923e3

Please sign in to comment.