Skip to content

Commit

Permalink
refactor: attach env variables to projects (#290)
Browse files Browse the repository at this point in the history
Signed-off-by: Toma Puljak <[email protected]>
  • Loading branch information
Tpuljak authored Mar 26, 2024
1 parent 5dd0529 commit 1def298
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
47 changes: 44 additions & 3 deletions pkg/server/provisioner/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
"fmt"
"io"

"github.com/daytonaio/daytona/internal"
"github.com/daytonaio/daytona/internal/util"
"github.com/daytonaio/daytona/pkg/logger"
provider_types "github.com/daytonaio/daytona/pkg/provider"
"github.com/daytonaio/daytona/pkg/provider/manager"
"github.com/daytonaio/daytona/pkg/server/config"
"github.com/daytonaio/daytona/pkg/server/event_bus"
"github.com/daytonaio/daytona/pkg/server/frpc"
"github.com/daytonaio/daytona/pkg/server/targets"
"github.com/daytonaio/daytona/pkg/types"
log "github.com/sirupsen/logrus"
Expand All @@ -29,6 +31,11 @@ func CreateWorkspace(workspace *types.Workspace) error {
return err
}

c, err := config.GetConfig()
if err != nil {
return err
}

workspaceLogger := logger.GetWorkspaceLogger(logsDir, workspace.Id)
defer workspaceLogger.Close()

Expand Down Expand Up @@ -69,9 +76,13 @@ func CreateWorkspace(workspace *types.Workspace) error {
if err != nil {
log.Error(err)
}

projectToCreate := *project
projectToCreate.EnvVars = getProjectEnvVars(project, c)

_, err = (*provider).CreateProject(&provider_types.ProjectRequest{
TargetOptions: target.Options,
Project: project,
Project: &projectToCreate,
})
if err != nil {
return err
Expand Down Expand Up @@ -111,6 +122,11 @@ func StartWorkspace(workspace *types.Workspace) error {
return err
}

c, err := config.GetConfig()
if err != nil {
return err
}

target, err := targets.GetTarget(workspace.Target)
if err != nil {
return err
Expand Down Expand Up @@ -153,10 +169,13 @@ func StartWorkspace(workspace *types.Workspace) error {
projectLogWriter := io.MultiWriter(wsLogWriter, projectLogger)
projectLogWriter.Write([]byte(fmt.Sprintf("Starting project %s\n", project.Name)))

projectToStart := *project
projectToStart.EnvVars = getProjectEnvVars(project, c)

// todo: go routines
_, err = (*provider).StartProject(&provider_types.ProjectRequest{
TargetOptions: target.Options,
Project: project,
Project: &projectToStart,
})
if err != nil {
return err
Expand All @@ -181,14 +200,22 @@ func StartProject(project *types.Project) error {
return err
}

c, err := config.GetConfig()
if err != nil {
return err
}

provider, err := manager.GetProvider(target.ProviderInfo.Name)
if err != nil {
return err
}

projectToStart := *project
projectToStart.EnvVars = getProjectEnvVars(project, c)

_, err = (*provider).StartProject(&provider_types.ProjectRequest{
TargetOptions: target.Options,
Project: project,
Project: &projectToStart,
})
if err != nil {
return err
Expand Down Expand Up @@ -352,3 +379,17 @@ func GetWorkspaceInfo(workspace *types.Workspace) (*types.WorkspaceInfo, error)
Workspace: workspace,
})
}

func getProjectEnvVars(project *types.Project, config *types.ServerConfig) map[string]string {
envVars := map[string]string{
"DAYTONA_WS_ID": project.WorkspaceId,
"DAYTONA_WS_PROJECT_NAME": project.Name,
"DAYTONA_WS_PROJECT_REPOSITORY_URL": project.Repository.Url,
"DAYTONA_SERVER_API_KEY": project.ApiKey,
"DAYTONA_SERVER_VERSION": internal.Version,
"DAYTONA_SERVER_URL": frpc.GetServerUrl(config),
"DAYTONA_SERVER_API_URL": frpc.GetApiUrl(config),
}

return envVars
}
11 changes: 6 additions & 5 deletions pkg/types/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ type Repository struct {
} // @name Repository

type Project struct {
Name string `json:"name"`
Repository *Repository `json:"repository"`
WorkspaceId string `json:"workspaceId"`
ApiKey string `json:"-"`
Target string `json:"target"`
Name string `json:"name"`
Repository *Repository `json:"repository"`
WorkspaceId string `json:"workspaceId"`
ApiKey string `json:"-"`
Target string `json:"target"`
EnvVars map[string]string `json:"-"`
} // @name Project
type Workspace struct {
Id string `json:"id"`
Expand Down

0 comments on commit 1def298

Please sign in to comment.