feat: implement workspace runtime selection
This commit is contained in:
@@ -5,6 +5,7 @@ CREATE TABLE IF NOT EXISTS workspaces
|
||||
container_id TEXT NOT NULL,
|
||||
image_tag TEXT NOT NULL,
|
||||
created_at TEXT NOT NULL,
|
||||
runtime TEXT NOT NULL,
|
||||
|
||||
CONSTRAINT pk_workspaces PRIMARY KEY (id)
|
||||
);
|
||||
|
@@ -6,7 +6,7 @@ type baseTemplate struct {
|
||||
Content string `json:"-"`
|
||||
}
|
||||
|
||||
var baseTemplates = []baseTemplate{fedora40WithSSH}
|
||||
var baseTemplates = []baseTemplate{fedora40WithSSH, fedora40SSHDocker}
|
||||
|
||||
var baseTemplateMap = map[string]baseTemplate{
|
||||
"empty": {
|
||||
@@ -14,7 +14,8 @@ var baseTemplateMap = map[string]baseTemplate{
|
||||
ID: "empty",
|
||||
Content: "",
|
||||
},
|
||||
"fedora-40-openssh": fedora40WithSSH,
|
||||
"fedora-40-openssh": fedora40WithSSH,
|
||||
"fedora-40-openssh-docker": fedora40SSHDocker,
|
||||
}
|
||||
|
||||
var fedora40WithSSH = baseTemplate{
|
||||
@@ -22,12 +23,36 @@ var fedora40WithSSH = baseTemplate{
|
||||
ID: "fedora-40-openssh",
|
||||
Content: `FROM fedora:40
|
||||
|
||||
ARG user
|
||||
ARG password
|
||||
|
||||
RUN dnf install -y openssh-server \
|
||||
&& mkdir -p /etc/ssh \
|
||||
&& ssh-keygen -q -N "" -t rsa -b 4096 -f /etc/ssh/ssh_host_rsa_key \
|
||||
&& useradd testuser \
|
||||
&& echo "testuser:12345678" | chpasswd
|
||||
&& usermod -aG wheel testuser
|
||||
&& useradd "$user" \
|
||||
&& echo "$user:$password" | chpasswd \
|
||||
&& usermod -aG wheel "$user"
|
||||
|
||||
CMD ["/usr/sbin/sshd", "-D"]
|
||||
`,
|
||||
}
|
||||
|
||||
var fedora40SSHDocker = baseTemplate{
|
||||
Name: "Fedora 40 + OpenSSH Server + Docker",
|
||||
ID: "fedora-40-openssh-docker",
|
||||
Content: `FROM fedora:40
|
||||
|
||||
ARG user
|
||||
ARG password
|
||||
|
||||
RUN dnf install -y openssh-server dnf-plugins-core \
|
||||
&& dnf-3 config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo \
|
||||
&& dnf install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin \
|
||||
&& mkdir -p /etc/ssh \
|
||||
&& ssh-keygen -q -N "" -t rsa -b 4096 -f /etc/ssh/ssh_host_rsa_key \
|
||||
&& useradd "$user" \
|
||||
&& echo "$user:$password" | chpasswd \
|
||||
&& usermod -aG wheel,docker "$user"
|
||||
|
||||
CMD ["/usr/sbin/sshd", "-D"]
|
||||
`,
|
||||
|
@@ -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)
|
||||
}
|
||||
|
@@ -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)
|
||||
}
|
||||
|
@@ -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
|
||||
|
||||
|
@@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user