feat: implement workspace runtime selection

This commit is contained in:
2024-12-02 09:38:01 +00:00
parent daf9bab49f
commit a92f5e8189
11 changed files with 268 additions and 119 deletions

View File

@@ -10,6 +10,7 @@ import (
type createWorkspaceRequestBody struct {
ImageID string `json:"imageId"`
Runtime string `json:"runtime"`
}
type updateWorkspaceRequestBody struct {
@@ -79,6 +80,7 @@ func createWorkspace(c echo.Context, workspaceName string) error {
w, err := mgr.createWorkspace(c.Request().Context(), createWorkspaceOptions{
name: workspaceName,
imageID: body.ImageID,
runtime: body.Runtime,
})
if err != nil {
if errors.Is(err, errImageNotFound) {
@@ -164,3 +166,12 @@ func deleteWorkspacePortMapping(c echo.Context) error {
return c.NoContent(http.StatusOK)
}
func fetchWorkspaceRuntimes(c echo.Context) error {
mgr := workspaceManagerFrom(c)
runtimes, err := mgr.findAvailableWorkspaceRuntimes(c.Request().Context())
if err != nil {
return err
}
return c.JSON(http.StatusOK, runtimes)
}

View File

@@ -11,4 +11,5 @@ func DefineRoutes(g *echo.Group, services service.Services) {
g.POST("/workspaces/:workspaceName", updateOrCreateWorkspace, currentWorkspaceMiddleware(true))
g.DELETE("/workspaces/:workspaceName", deleteWorkspace, currentWorkspaceMiddleware(false))
g.DELETE("/workspaces/:workspaceName/forwarded-ports/:portName", deleteWorkspacePortMapping, currentWorkspaceMiddleware(false))
g.GET("/workspace-runtimes", fetchWorkspaceRuntimes)
}

View File

@@ -36,6 +36,8 @@ type workspace struct {
Status status `bun:"-" json:"status"`
PortMappings []portMapping `bun:"rel:has-many,join:id=workspace_id" json:"ports,omitempty"`
Runtime string `json:"runtime"`
}
type portMapping struct {
@@ -48,6 +50,11 @@ type portMapping struct {
Workspace workspace `bun:"rel:belongs-to,join:workspace_id=id" json:"-"`
}
type workspaceRuntime struct {
Name string `json:"name"`
Path string `json:"path"`
}
// status represents the status of a workspace.
type status string

View File

@@ -31,10 +31,12 @@ type workspaceManager struct {
type createWorkspaceOptions struct {
name string
imageID string
runtime string
}
var errImageNotFound = errors.New("image not found")
var errWorkspaceNotFound = errors.New("workspace not found")
var errRuntimeNotFound = errors.New("runtime not found")
func (mgr workspaceManager) findAllWorkspaces(ctx context.Context) ([]workspace, error) {
var workspaces []workspace
@@ -126,6 +128,16 @@ func (mgr workspaceManager) hasWorkspace(ctx context.Context, name string) (bool
}
func (mgr workspaceManager) createWorkspace(ctx context.Context, opts createWorkspaceOptions) (*workspace, error) {
info, err := mgr.dockerClient.Info(ctx)
if err != nil {
return nil, err
}
_, ok := info.Runtimes[opts.runtime]
if !ok {
return nil, errRuntimeNotFound
}
tx, err := mgr.db.BeginTx(ctx, nil)
if err != nil {
return nil, err
@@ -157,6 +169,7 @@ func (mgr workspaceManager) createWorkspace(ctx context.Context, opts createWork
{"127.0.0.1", ""},
},
},
Runtime: opts.runtime,
}
res, err := mgr.dockerClient.ContainerCreate(ctx, containerConfig, hostConfig, nil, nil, opts.name)
@@ -372,3 +385,20 @@ func (mgr workspaceManager) deletePortMapping(ctx context.Context, workspace *wo
return nil
}
func (mgr workspaceManager) findAvailableWorkspaceRuntimes(ctx context.Context) ([]workspaceRuntime, error) {
info, err := mgr.dockerClient.Info(ctx)
if err != nil {
return nil, err
}
runtimes := make([]workspaceRuntime, 0, len(info.Runtimes))
for name, r := range info.Runtimes {
runtimes = append(runtimes, workspaceRuntime{
Name: name,
Path: r.Path,
})
}
return runtimes, nil
}