-
Notifications
You must be signed in to change notification settings - Fork 16
/
create.go
101 lines (91 loc) · 2.89 KB
/
create.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package command
import (
"context"
"fmt"
"path"
"github.com/Bio-OS/bioos/internal/context/notebookserver/domain"
"github.com/Bio-OS/bioos/internal/context/workspace/interface/grpc/proto"
"github.com/Bio-OS/bioos/pkg/errors"
"github.com/Bio-OS/bioos/pkg/log"
"github.com/Bio-OS/bioos/pkg/notebook"
"github.com/Bio-OS/bioos/pkg/storage"
)
type CreateCommand struct {
WorkspaceID string
Image string
ResourceSize notebook.ResourceSize
}
type CreateHandler interface {
Handle(context.Context, *CreateCommand) (string, error)
}
func NewCreateHandler(svc domain.Service, factory *domain.Factory, workspaceService proto.WorkspaceServiceServer, storageOpts *storage.Options) CreateHandler {
return &createHandler{
factory: factory,
service: svc,
workspaceClient: workspaceService,
storageOpts: storageOpts,
}
}
type createHandler struct {
factory *domain.Factory
service domain.Service
workspaceClient proto.WorkspaceServiceServer
storageOpts *storage.Options
}
func (h *createHandler) Handle(ctx context.Context, cmd *CreateCommand) (string, error) {
// check workspace exist
gotWorkspace, err := h.workspaceClient.GetWorkspace(ctx, &proto.GetWorkspaceRequest{
Id: cmd.WorkspaceID,
})
if err != nil {
// TODO how to check not found error? grpc status error define in internal
/*if gerr, ok := err.(*status.Error); ok {
if gerr.GRPCStatus().Code() == codes.NotFound {
return "", errors.NewNotFoundError("workspace", cmd.WorkspaceID)
}
}*/
return "", err
}
if gotWorkspace.Workspace == nil {
return "", errors.NewInternalError(fmt.Errorf("get workspace response nil object"))
}
// add workspace data
var volumes []domain.Volume
if gotWorkspace.Workspace.Storage != nil {
if gotWorkspace.Workspace.Storage.Nfs != nil {
volumes = append(volumes, domain.Volume{
Type: domain.VolumeTypeNFS,
Name: "bioos-workspace",
Source: gotWorkspace.Workspace.Storage.Nfs.MountPath,
MountRelativePath: notebook.MountRelativePathWorkspaceData,
})
} else {
log.Warnf("workspace %s has none storage", cmd.WorkspaceID)
}
} else {
log.Warnf("workspace %s has none storage", cmd.WorkspaceID)
}
// add notebook ipynb
if h.storageOpts.FileSystem != nil {
volumes = append(volumes, domain.Volume{
Type: domain.VolumeTypeNFS,
Name: "bioos-notebook",
Source: path.Join(h.storageOpts.FileSystem.NotebookRootPath(), cmd.WorkspaceID), // TODO how to find out notebook store location?
MountRelativePath: notebook.MountRelativePathNotebook,
})
}
param := domain.CreateParam{
WorkspaceID: cmd.WorkspaceID,
Image: cmd.Image,
ResourceSize: cmd.ResourceSize,
Volumes: volumes,
}
do, err := h.factory.New(¶m)
if err != nil {
return "", err
}
if err = h.service.Create(ctx, do); err != nil {
return "", err
}
return do.ID, nil
}