Skip to content

Commit

Permalink
feat: server IP configuration
Browse files Browse the repository at this point in the history
If the server is running in a network with a static IP it can be directly accessed from the CLI and workspaces

Signed-off-by: Toma Puljak <[email protected]>
  • Loading branch information
Tpuljak committed Jun 11, 2024
1 parent c6d118e commit 0eeb568
Show file tree
Hide file tree
Showing 16 changed files with 197 additions and 42 deletions.
44 changes: 44 additions & 0 deletions internal/util/apiclient/conversion/server_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2024 Daytona Platforms Inc.
// SPDX-License-Identifier: Apache-2.0

package conversion

import (
"github.com/daytonaio/daytona/pkg/apiclient"
"github.com/daytonaio/daytona/pkg/server"
)

func ToServerConfig(serverConfigDto *apiclient.ServerConfig) *server.Config {
if serverConfigDto == nil {
return nil
}

config := &server.Config{
Id: *serverConfigDto.Id,
ProvidersDir: *serverConfigDto.ProvidersDir,
RegistryUrl: *serverConfigDto.RegistryUrl,
ServerDownloadUrl: *serverConfigDto.ServerDownloadUrl,
IpWithProtocol: serverConfigDto.IpWithProtocol,
ApiPort: uint32(*serverConfigDto.ApiPort),
LocalBuilderRegistryPort: uint32(*serverConfigDto.LocalBuilderRegistryPort),
BuilderRegistryServer: *serverConfigDto.BuilderRegistryServer,
BuildImageNamespace: *serverConfigDto.BuildImageNamespace,
HeadscalePort: uint32(*serverConfigDto.HeadscalePort),
BinariesPath: *serverConfigDto.BinariesPath,
LogFilePath: *serverConfigDto.LogFilePath,
DefaultProjectImage: *serverConfigDto.DefaultProjectImage,
DefaultProjectUser: *serverConfigDto.DefaultProjectUser,
DefaultProjectPostStartCommands: serverConfigDto.DefaultProjectPostStartCommands,
BuilderImage: *serverConfigDto.BuilderImage,
}

if serverConfigDto.Frps != nil {
config.Frps = &server.FRPSConfig{
Domain: *serverConfigDto.Frps.Domain,
Port: uint32(*serverConfigDto.Frps.Port),
Protocol: *serverConfigDto.Frps.Protocol,
}
}

return config
}
3 changes: 3 additions & 0 deletions pkg/api/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1563,6 +1563,9 @@ const docTemplate = `{
"id": {
"type": "string"
},
"ipWithProtocol": {
"type": "string"
},
"localBuilderRegistryPort": {
"type": "integer"
},
Expand Down
3 changes: 3 additions & 0 deletions pkg/api/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1560,6 +1560,9 @@
"id": {
"type": "string"
},
"ipWithProtocol": {
"type": "string"
},
"localBuilderRegistryPort": {
"type": "integer"
},
Expand Down
2 changes: 2 additions & 0 deletions pkg/api/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ definitions:
type: integer
id:
type: string
ipWithProtocol:
type: string
localBuilderRegistryPort:
type: integer
logFilePath:
Expand Down
3 changes: 3 additions & 0 deletions pkg/apiclient/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1301,6 +1301,7 @@ components:
apiPort: 0
headscalePort: 1
buildImageNamespace: buildImageNamespace
ipWithProtocol: ipWithProtocol
serverDownloadUrl: serverDownloadUrl
binariesPath: binariesPath
logFilePath: logFilePath
Expand Down Expand Up @@ -1336,6 +1337,8 @@ components:
type: integer
id:
type: string
ipWithProtocol:
type: string
localBuilderRegistryPort:
type: integer
logFilePath:
Expand Down
26 changes: 26 additions & 0 deletions pkg/apiclient/docs/ServerConfig.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Name | Type | Description | Notes
**Frps** | Pointer to [**FRPSConfig**](FRPSConfig.md) | | [optional]
**HeadscalePort** | Pointer to **int32** | | [optional]
**Id** | Pointer to **string** | | [optional]
**IpWithProtocol** | Pointer to **string** | | [optional]
**LocalBuilderRegistryPort** | Pointer to **int32** | | [optional]
**LogFilePath** | Pointer to **string** | | [optional]
**ProvidersDir** | Pointer to **string** | | [optional]
Expand Down Expand Up @@ -315,6 +316,31 @@ SetId sets Id field to given value.

HasId returns a boolean if a field has been set.

### GetIpWithProtocol

`func (o *ServerConfig) GetIpWithProtocol() string`

GetIpWithProtocol returns the IpWithProtocol field if non-nil, zero value otherwise.

### GetIpWithProtocolOk

`func (o *ServerConfig) GetIpWithProtocolOk() (*string, bool)`

GetIpWithProtocolOk returns a tuple with the IpWithProtocol field if it's non-nil, zero value otherwise
and a boolean to check if the value has been set.

### SetIpWithProtocol

`func (o *ServerConfig) SetIpWithProtocol(v string)`

SetIpWithProtocol sets IpWithProtocol field to given value.

### HasIpWithProtocol

`func (o *ServerConfig) HasIpWithProtocol() bool`

HasIpWithProtocol returns a boolean if a field has been set.

### GetLocalBuilderRegistryPort

`func (o *ServerConfig) GetLocalBuilderRegistryPort() int32`
Expand Down
36 changes: 36 additions & 0 deletions pkg/apiclient/model_server_config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pkg/cmd/apikey/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"github.com/spf13/cobra"

"github.com/daytonaio/daytona/cmd/daytona/config"
"github.com/daytonaio/daytona/internal/util"
apiclient_util "github.com/daytonaio/daytona/internal/util/apiclient"
"github.com/daytonaio/daytona/internal/util/apiclient/conversion"
"github.com/daytonaio/daytona/pkg/apiclient"
"github.com/daytonaio/daytona/pkg/views"
"github.com/daytonaio/daytona/pkg/views/server/apikey"
Expand Down Expand Up @@ -71,7 +71,7 @@ var GenerateCmd = &cobra.Command{
log.Fatal(err)
}

apiUrl := util.GetFrpcApiUrl(*serverConfig.Frps.Protocol, *serverConfig.Id, *serverConfig.Frps.Domain)
apiUrl := conversion.ToServerConfig(serverConfig).GetApiUrl()

view.Render(key, apiUrl)
},
Expand Down
4 changes: 1 addition & 3 deletions pkg/cmd/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/daytonaio/daytona/internal/util"
"github.com/daytonaio/daytona/pkg/cmd/output"
"github.com/daytonaio/daytona/pkg/server"
)
Expand All @@ -22,8 +21,7 @@ var configCmd = &cobra.Command{
log.Fatal(err)
}

apiUrl := util.GetFrpcApiUrl(config.Frps.Protocol, config.Id, config.Frps.Domain)
output.Output = apiUrl
output.Output = config.GetApiUrl()

view.RenderConfig(config)
},
Expand Down
27 changes: 17 additions & 10 deletions pkg/cmd/server/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,18 @@ var ServeCmd = &cobra.Command{
log.Fatal(err)
}

var headscaleServerUrl string
if c.IpWithProtocol != nil {
headscaleServerUrl = fmt.Sprintf("%s:%d", *c.IpWithProtocol, c.HeadscalePort)
headscaleServerUrl = strings.Replace(headscaleServerUrl, "http://", "https://", 1)
} else if c.Frps != nil {
headscaleServerUrl = fmt.Sprintf("https://%s.%s", c.Id, c.Frps.Domain)
}

headscaleServer := headscale.NewHeadscaleServer(&headscale.HeadscaleServerConfig{
ServerId: c.Id,
FrpsDomain: c.Frps.Domain,
FrpsProtocol: c.Frps.Protocol,
HeadscalePort: c.HeadscalePort,
ServerId: c.Id,
ServerUrl: headscaleServerUrl,
Port: c.HeadscalePort,
})
err = headscaleServer.Init()
if err != nil {
Expand Down Expand Up @@ -139,9 +146,9 @@ var ServeCmd = &cobra.Command{
providerManager := manager.NewProviderManager(manager.ProviderManagerConfig{
LogsDir: logsDir,
ProviderTargetService: providerTargetService,
ServerApiUrl: util.GetFrpcApiUrl(c.Frps.Protocol, c.Id, c.Frps.Domain),
ServerApiUrl: c.GetApiUrl(),
ServerDownloadUrl: getDaytonaScriptUrl(c),
ServerUrl: util.GetFrpcServerUrl(c.Frps.Protocol, c.Id, c.Frps.Domain),
ServerUrl: headscaleServerUrl,
RegistryUrl: c.RegistryUrl,
BaseDir: c.ProvidersDir,
CreateProviderNetworkKey: func(providerName string) (string, error) {
Expand Down Expand Up @@ -180,8 +187,8 @@ var ServeCmd = &cobra.Command{
ApiKeyService: apiKeyService,
GitProviderService: gitProviderService,
ContainerRegistryService: containerRegistryService,
ServerApiUrl: util.GetFrpcApiUrl(c.Frps.Protocol, c.Id, c.Frps.Domain),
ServerUrl: util.GetFrpcServerUrl(c.Frps.Protocol, c.Id, c.Frps.Domain),
ServerApiUrl: c.GetApiUrl(),
ServerUrl: headscaleServerUrl,
DefaultProjectImage: c.DefaultProjectImage,
DefaultProjectUser: c.DefaultProjectUser,
DefaultProjectPostStartCommands: c.DefaultProjectPostStartCommands,
Expand Down Expand Up @@ -263,12 +270,12 @@ func waitForServerToStart(apiServer *api.ApiServer) error {
}

func getDaytonaScriptUrl(config *server.Config) string {
url, _ := url.JoinPath(util.GetFrpcApiUrl(config.Frps.Protocol, config.Id, config.Frps.Domain), "binary", "script")
url, _ := url.JoinPath(config.GetApiUrl(), "binary", "script")
return url
}

func printServerStartedMessage(c *server.Config, runAsDaemon bool) {
started_view.Render(c.ApiPort, util.GetFrpcApiUrl(c.Frps.Protocol, c.Id, c.Frps.Domain), runAsDaemon)
started_view.Render(c.ApiPort, c.GetApiUrl(), runAsDaemon)
}

func getDbPath() (string, error) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/server/headscale/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ func (s *HeadscaleServer) getHeadscaleConfig() (*hstypes.Config, error) {

cfg := &hstypes.Config{
DBtype: "sqlite3",
ServerURL: fmt.Sprintf("https://%s.%s", s.serverId, s.frpsDomain),
Addr: fmt.Sprintf("127.0.0.1:%d", s.headscalePort),
ServerURL: s.serverUrl,
Addr: fmt.Sprintf("127.0.0.1:%d", s.port),
EphemeralNodeInactivityTimeout: 5 * time.Minute,
NodeUpdateCheckInterval: 10 * time.Second,
BaseDomain: "daytona.local",
Expand Down
3 changes: 1 addition & 2 deletions pkg/server/headscale/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"tailscale.com/tsnet"

"github.com/daytonaio/daytona/internal/util"
log "github.com/sirupsen/logrus"
)

Expand All @@ -28,7 +27,7 @@ func (s *HeadscaleServer) Connect() error {
log.Fatal(err)
}

tsNetServer.ControlURL = util.GetFrpcServerUrl(s.frpsProtocol, s.serverId, s.frpsDomain)
tsNetServer.ControlURL = s.serverUrl
tsNetServer.AuthKey = authKey

defer tsNetServer.Close()
Expand Down
21 changes: 9 additions & 12 deletions pkg/server/headscale/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,23 @@ import (
)

type HeadscaleServerConfig struct {
ServerId string
FrpsDomain string
FrpsProtocol string
HeadscalePort uint32
ServerId string
Port uint32
ServerUrl string
}

func NewHeadscaleServer(config *HeadscaleServerConfig) *HeadscaleServer {
return &HeadscaleServer{
serverId: config.ServerId,
frpsDomain: config.FrpsDomain,
frpsProtocol: config.FrpsProtocol,
headscalePort: config.HeadscalePort,
serverId: config.ServerId,
port: config.Port,
serverUrl: config.ServerUrl,
}
}

type HeadscaleServer struct {
serverId string
frpsDomain string
frpsProtocol string
headscalePort uint32
serverId string
port uint32
serverUrl string
}

func (s *HeadscaleServer) Init() error {
Expand Down
14 changes: 14 additions & 0 deletions pkg/server/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
package server

import (
"fmt"
"net/http"

"github.com/daytonaio/daytona/internal/util"
)

type TailscaleServer interface {
Expand Down Expand Up @@ -35,6 +38,7 @@ type Config struct {
Id string `json:"id"`
ServerDownloadUrl string `json:"serverDownloadUrl"`
Frps *FRPSConfig `json:"frps,omitempty"`
IpWithProtocol *string `json:"ipWithProtocol,omitempty"`
ApiPort uint32 `json:"apiPort"`
HeadscalePort uint32 `json:"headscalePort"`
BinariesPath string `json:"binariesPath"`
Expand All @@ -47,3 +51,13 @@ type Config struct {
BuilderRegistryServer string `json:"builderRegistryServer"`
BuildImageNamespace string `json:"buildImageNamespace"`
} // @name ServerConfig

func (config *Config) GetApiUrl() string {
apiUrl := util.GetFrpcApiUrl(config.Frps.Protocol, config.Id, config.Frps.Domain)

if config.IpWithProtocol != nil {
apiUrl = fmt.Sprintf("%s:%d", *config.IpWithProtocol, config.ApiPort)
}

return apiUrl
}
Loading

0 comments on commit 0eeb568

Please sign in to comment.