Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: project post start commands #430

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions internal/util/apiclient/server/conversion/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import (

func ToProjectDTO(project *workspace.Project) *serverapiclient.Project {
projectDto := &serverapiclient.Project{
Name: &project.Name,
Target: &project.Target,
WorkspaceId: &project.WorkspaceId,
Image: &project.Image,
User: &project.User,
Name: &project.Name,
Target: &project.Target,
WorkspaceId: &project.WorkspaceId,
Image: &project.Image,
User: &project.User,
PostStartCommands: project.PostStartCommands,
Repository: &serverapiclient.GitRepository{
Id: &project.Repository.Id,
Name: &project.Repository.Name,
Expand Down
22 changes: 22 additions & 0 deletions pkg/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"net/url"
"os/exec"
"time"

"github.com/daytonaio/daytona/cmd/daytona/config"
Expand Down Expand Up @@ -106,6 +107,8 @@ func (a *Agent) startProjectMode() error {
}
}()

a.runPostStartCommands(project)

return nil
}

Expand Down Expand Up @@ -198,6 +201,25 @@ func (a *Agent) setDefaultConfig() error {
return config.Save()
}

func (a *Agent) runPostStartCommands(project *serverapiclient.Project) {
log.Info("Running post start commands...")

for _, command := range project.PostStartCommands {
go func() {
log.Info("Running command: " + command)
cmd := exec.Command("sh", "-c", command)
cmd.Dir = a.Config.ProjectDir
cmd.Stdout = a.LogWriter
cmd.Stderr = a.LogWriter

err := cmd.Run()
if err != nil {
log.Error(fmt.Sprintf("command '%s' failed: %v", command, err))
}
}()
}
}

// Agent uptime in seconds
func (a *Agent) uptime() int32 {
return int32(time.Since(a.startTime).Seconds())
Expand Down
8 changes: 6 additions & 2 deletions pkg/agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ var project1 = &workspace.Project{
Url: "https://github.com/daytonaio/daytona",
Name: "daytona",
},
WorkspaceId: "123",
Target: "local",
WorkspaceId: "123",
Target: "local",
PostStartCommands: []string{"echo 'test' > test.txt"},
}

var workspace1 = &workspace.Workspace{
Expand Down Expand Up @@ -73,6 +74,9 @@ func TestAgent(t *testing.T) {
err := a.Start()

require.Nil(t, err)

// Check post start command result
require.FileExists(t, mockConfig.ProjectDir+"/test.txt")
})

t.Cleanup(func() {
Expand Down
18 changes: 18 additions & 0 deletions pkg/api/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,12 @@ const docTemplate = `{
"name": {
"type": "string"
},
"postStartCommands": {
"type": "array",
"items": {
"type": "string"
}
},
"source": {
"$ref": "#/definitions/CreateWorkspaceRequestProjectSource"
},
Expand Down Expand Up @@ -1283,6 +1289,12 @@ const docTemplate = `{
"name": {
"type": "string"
},
"postStartCommands": {
"type": "array",
"items": {
"type": "string"
}
},
"repository": {
"$ref": "#/definitions/GitRepository"
},
Expand Down Expand Up @@ -1375,6 +1387,12 @@ const docTemplate = `{
"defaultProjectImage": {
"type": "string"
},
"defaultProjectPostStartCommands": {
"type": "array",
"items": {
"type": "string"
}
},
"defaultProjectUser": {
"type": "string"
},
Expand Down
18 changes: 18 additions & 0 deletions pkg/api/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,12 @@
"name": {
"type": "string"
},
"postStartCommands": {
"type": "array",
"items": {
"type": "string"
}
},
"source": {
"$ref": "#/definitions/CreateWorkspaceRequestProjectSource"
},
Expand Down Expand Up @@ -1280,6 +1286,12 @@
"name": {
"type": "string"
},
"postStartCommands": {
"type": "array",
"items": {
"type": "string"
}
},
"repository": {
"$ref": "#/definitions/GitRepository"
},
Expand Down Expand Up @@ -1372,6 +1384,12 @@
"defaultProjectImage": {
"type": "string"
},
"defaultProjectPostStartCommands": {
"type": "array",
"items": {
"type": "string"
}
},
"defaultProjectUser": {
"type": "string"
},
Expand Down
12 changes: 12 additions & 0 deletions pkg/api/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ definitions:
type: string
name:
type: string
postStartCommands:
items:
type: string
type: array
source:
$ref: '#/definitions/CreateWorkspaceRequestProjectSource'
user:
Expand Down Expand Up @@ -149,6 +153,10 @@ definitions:
type: string
name:
type: string
postStartCommands:
items:
type: string
type: array
repository:
$ref: '#/definitions/GitRepository'
state:
Expand Down Expand Up @@ -209,6 +217,10 @@ definitions:
type: string
defaultProjectImage:
type: string
defaultProjectPostStartCommands:
items:
type: string
type: array
defaultProjectUser:
type: string
frps:
Expand Down
19 changes: 10 additions & 9 deletions pkg/cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,16 @@ var ServerCmd = &cobra.Command{
})

workspaceService := workspaces.NewWorkspaceService(workspaces.WorkspaceServiceConfig{
WorkspaceStore: workspaceStore,
TargetStore: providerTargetStore,
ApiKeyService: apiKeyService,
ContainerRegistryStore: containerRegistryStore,
ServerApiUrl: util.GetFrpcApiUrl(c.Frps.Protocol, c.Id, c.Frps.Domain),
ServerUrl: util.GetFrpcServerUrl(c.Frps.Protocol, c.Id, c.Frps.Domain),
DefaultProjectImage: c.DefaultProjectImage,
DefaultProjectUser: c.DefaultProjectUser,
Provisioner: provisioner,
WorkspaceStore: workspaceStore,
TargetStore: providerTargetStore,
ApiKeyService: apiKeyService,
ContainerRegistryStore: containerRegistryStore,
ServerApiUrl: util.GetFrpcApiUrl(c.Frps.Protocol, c.Id, c.Frps.Domain),
ServerUrl: util.GetFrpcServerUrl(c.Frps.Protocol, c.Id, c.Frps.Domain),
DefaultProjectImage: c.DefaultProjectImage,
DefaultProjectUser: c.DefaultProjectUser,
DefaultProjectPostStartCommands: c.DefaultProjectPostStartCommands,
Provisioner: provisioner,
NewWorkspaceLogger: func(workspaceId string) logger.Logger {
return logger.NewWorkspaceLogger(logsDir, workspaceId)
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/workspace/util/creation_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func GetCreationDataFromPrompt(workspaceNames []string, userGitProviders []serve
return "", nil, err
}

if workspaceCreationPromptResponse.PrimaryProject == (serverapiclient.CreateWorkspaceRequestProject{}) {
if workspaceCreationPromptResponse.PrimaryProject.Source == nil {
return "", nil, errors.New("primary project is required")
}

Expand Down
33 changes: 18 additions & 15 deletions pkg/db/dto/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,22 @@ type ProjectStateDTO struct {
}

type ProjectDTO struct {
Name string `json:"name"`
Repository RepositoryDTO `json:"repository"`
WorkspaceId string `json:"workspaceId"`
Target string `json:"target"`
State *ProjectStateDTO `json:"state,omitempty" gorm:"serializer:json"`
Name string `json:"name"`
Repository RepositoryDTO `json:"repository"`
WorkspaceId string `json:"workspaceId"`
Target string `json:"target"`
State *ProjectStateDTO `json:"state,omitempty" gorm:"serializer:json"`
PostStartCommands []string `json:"postStartCommands,omitempty"`
}

func ToProjectDTO(project *workspace.Project, workspace *workspace.Workspace) ProjectDTO {
return ProjectDTO{
Name: project.Name,
Repository: ToRepositoryDTO(project.Repository),
WorkspaceId: project.WorkspaceId,
Target: project.Target,
State: ToProjectStateDTO(project.State),
Name: project.Name,
Repository: ToRepositoryDTO(project.Repository),
WorkspaceId: project.WorkspaceId,
Target: project.Target,
State: ToProjectStateDTO(project.State),
PostStartCommands: project.PostStartCommands,
}
}

Expand Down Expand Up @@ -75,11 +77,12 @@ func ToProjectStateDTO(state *workspace.ProjectState) *ProjectStateDTO {

func ToProject(projectDTO ProjectDTO) *workspace.Project {
return &workspace.Project{
Name: projectDTO.Name,
Repository: ToRepository(projectDTO.Repository),
WorkspaceId: projectDTO.WorkspaceId,
Target: projectDTO.Target,
State: ToProjectState(projectDTO.State),
Name: projectDTO.Name,
Repository: ToRepository(projectDTO.Repository),
WorkspaceId: projectDTO.WorkspaceId,
Target: projectDTO.Target,
State: ToProjectState(projectDTO.State),
PostStartCommands: projectDTO.PostStartCommands,
}
}

Expand Down
25 changes: 15 additions & 10 deletions pkg/server/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const defaultServerDownloadUrl = "https://download.daytona.io/daytona/install.sh
const defaultHeadscalePort = 3001
const defaultApiPort = 3000
const defaultProjectImage = "daytonaio/workspace-project:latest"
const defaultProjectUser = "daytona"

var defaultProjectPostStartCommands = []string{"sudo dockerd"}

var us_defaultFrpsConfig = FRPSConfig{
Domain: "try-us.daytona.app",
Expand Down Expand Up @@ -94,16 +97,18 @@ func getDefaultConfig() (*Config, error) {
}

c := Config{
Id: generateUuid(),
RegistryUrl: defaultRegistryUrl,
ProvidersDir: providersDir,
ServerDownloadUrl: defaultServerDownloadUrl,
ApiPort: defaultApiPort,
HeadscalePort: defaultHeadscalePort,
BinariesPath: binariesPath,
Frps: getDefaultFRPSConfig(),
LogFilePath: logFilePath,
DefaultProjectImage: defaultProjectImage,
Id: generateUuid(),
RegistryUrl: defaultRegistryUrl,
ProvidersDir: providersDir,
ServerDownloadUrl: defaultServerDownloadUrl,
ApiPort: defaultApiPort,
HeadscalePort: defaultHeadscalePort,
BinariesPath: binariesPath,
Frps: getDefaultFRPSConfig(),
LogFilePath: logFilePath,
DefaultProjectImage: defaultProjectImage,
DefaultProjectUser: defaultProjectUser,
DefaultProjectPostStartCommands: defaultProjectPostStartCommands,
}

if os.Getenv("DEFAULT_REGISTRY_URL") != "" {
Expand Down
23 changes: 12 additions & 11 deletions pkg/server/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@ type NetworkKey struct {
} // @name NetworkKey

type Config struct {
ProvidersDir string `json:"providersDir"`
RegistryUrl string `json:"registryUrl"`
Id string `json:"id"`
ServerDownloadUrl string `json:"serverDownloadUrl"`
Frps *FRPSConfig `json:"frps,omitempty"`
ApiPort uint32 `json:"apiPort"`
HeadscalePort uint32 `json:"headscalePort"`
BinariesPath string `json:"binariesPath"`
LogFilePath string `json:"logFilePath"`
DefaultProjectImage string `json:"defaultProjectImage"`
DefaultProjectUser string `json:"defaultProjectUser"`
ProvidersDir string `json:"providersDir"`
RegistryUrl string `json:"registryUrl"`
Id string `json:"id"`
ServerDownloadUrl string `json:"serverDownloadUrl"`
Frps *FRPSConfig `json:"frps,omitempty"`
ApiPort uint32 `json:"apiPort"`
HeadscalePort uint32 `json:"headscalePort"`
BinariesPath string `json:"binariesPath"`
LogFilePath string `json:"logFilePath"`
DefaultProjectImage string `json:"defaultProjectImage"`
DefaultProjectUser string `json:"defaultProjectUser"`
DefaultProjectPostStartCommands []string `json:"defaultProjectPostStartCommands"`
} // @name ServerConfig
22 changes: 14 additions & 8 deletions pkg/server/workspaces/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,21 @@ func (s *WorkspaceService) CreateWorkspace(req dto.CreateWorkspaceRequest) (*wor
projectUser = *project.User
}

postStartCommands := s.defaultProjectPostStartCommands
if project.PostStartCommands != nil {
postStartCommands = *project.PostStartCommands
}

project := &workspace.Project{
Name: project.Name,
Image: projectImage,
User: projectUser,
Repository: project.Source.Repository,
WorkspaceId: w.Id,
ApiKey: apiKey,
Target: w.Target,
EnvVars: project.EnvVars,
Name: project.Name,
Image: projectImage,
User: projectUser,
PostStartCommands: postStartCommands,
Repository: project.Source.Repository,
WorkspaceId: w.Id,
ApiKey: apiKey,
Target: w.Target,
EnvVars: project.EnvVars,
}
w.Projects = append(w.Projects, project)
}
Expand Down
11 changes: 6 additions & 5 deletions pkg/server/workspaces/dto/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ type CreateWorkspaceRequestProjectSource struct {
} // @name CreateWorkspaceRequestProjectSource

type CreateWorkspaceRequestProject struct {
Name string `json:"name" validate:"required,gt=0"`
Image *string `json:"image,omitempty"`
User *string `json:"user,omitempty"`
Source CreateWorkspaceRequestProjectSource `json:"source"`
EnvVars map[string]string `json:"envVars"`
Name string `json:"name" validate:"required,gt=0"`
Image *string `json:"image,omitempty"`
User *string `json:"user,omitempty"`
Source CreateWorkspaceRequestProjectSource `json:"source"`
EnvVars map[string]string `json:"envVars"`
PostStartCommands *[]string `json:"postStartCommands,omitempty"`
} // @name CreateWorkspaceRequestProject

type CreateWorkspaceRequest struct {
Expand Down
Loading
Loading