/
workspace.go
150 lines (127 loc) · 4.19 KB
/
workspace.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package docker
import (
"context"
"fmt"
"os"
"strconv"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/errdefs"
"github.com/mazrean/separated-webshell/domain"
"github.com/mazrean/separated-webshell/domain/values"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
const (
upLabel = "up"
downLabel = "down"
)
var (
stopTimeout = 10
cpuLimit int64
memoryLimit int64
)
var containerCounter = promauto.NewGaugeVec(prometheus.GaugeOpts{
Help: "Number of containers.",
Namespace: "webshell",
Name: "container_num",
}, []string{"status"})
func containerName(userName values.UserName) string {
return fmt.Sprintf("user-%s", userName)
}
type Workspace struct{}
func NewWorkspace() (*Workspace, error) {
floatCPULimit, err := strconv.ParseFloat(os.Getenv("CPU_LIMIT"), 64)
if err != nil {
return nil, fmt.Errorf("invalid cpu limit: %w", err)
}
cpuLimit = int64(floatCPULimit * 1e9)
floatMemoryLimit, err := strconv.ParseFloat(os.Getenv("MEMORY_LIMIT"), 64)
if err != nil {
return nil, fmt.Errorf("invalid memory limit: %w", err)
}
memoryLimit = int64(floatMemoryLimit * 1e6)
return &Workspace{}, nil
}
func (w *Workspace) Create(ctx context.Context, userName values.UserName) (*domain.Workspace, error) {
ctnName := containerName(userName)
res, err := cli.ContainerCreate(ctx, &container.Config{
Image: imageRef,
User: imageUser,
Tty: true,
}, &container.HostConfig{
Resources: container.Resources{
NanoCPUs: cpuLimit,
Memory: memoryLimit,
},
}, nil, nil, ctnName)
if errdefs.IsConflict(err) {
ctnInfo, err := cli.ContainerInspect(ctx, ctnName)
if err != nil {
return nil, fmt.Errorf("failed to inspect container: %w", err)
}
workspaceID := values.NewWorkspaceID(ctnInfo.ID)
workspaceName := values.NewWorkspaceName(ctnName)
containerCounter.WithLabelValues(downLabel).Inc()
return domain.NewWorkspace(workspaceID, workspaceName, userName), nil
}
if err != nil {
return nil, fmt.Errorf("failed to create container: %w", err)
}
workspaceID := values.NewWorkspaceID(res.ID)
workspaceName := values.NewWorkspaceName(ctnName)
containerCounter.WithLabelValues(downLabel).Inc()
return domain.NewWorkspace(workspaceID, workspaceName, userName), nil
}
func (w *Workspace) Start(ctx context.Context, workspace *domain.Workspace) error {
err := cli.ContainerStart(ctx, string(workspace.ID()), types.ContainerStartOptions{})
if err != nil && !errdefs.IsConflict(err) {
return fmt.Errorf("failed to start container: %w", err)
}
workspace.Status = values.StatusUp
containerCounter.WithLabelValues(downLabel).Dec()
containerCounter.WithLabelValues(upLabel).Inc()
return nil
}
func (w *Workspace) Stop(ctx context.Context, workspace *domain.Workspace) error {
err := cli.ContainerStop(ctx, string(workspace.ID()), container.StopOptions{
Timeout: &stopTimeout,
})
if err != nil {
return fmt.Errorf("failed to stop container: %w", err)
}
workspace.Status = values.StatusDown
containerCounter.WithLabelValues(upLabel).Dec()
containerCounter.WithLabelValues(downLabel).Inc()
return nil
}
func (w *Workspace) Recreate(ctx context.Context, workspace *domain.Workspace) (*domain.Workspace, error) {
err := cli.ContainerRemove(ctx, string(workspace.ID()), types.ContainerRemoveOptions{
Force: true,
})
if err != nil {
return nil, fmt.Errorf("failed to remove container: %w", err)
}
containerCounter.WithLabelValues(upLabel).Dec()
containerCounter.WithLabelValues(downLabel).Inc()
userName := workspace.UserName()
ctnName := string(workspace.Name())
res, err := cli.ContainerCreate(ctx, &container.Config{
Image: imageRef,
User: imageUser,
Tty: true,
}, &container.HostConfig{
Resources: container.Resources{
NanoCPUs: cpuLimit,
Memory: memoryLimit,
},
}, nil, nil, ctnName)
if err != nil {
return nil, fmt.Errorf("failed to create container: %w", err)
}
containerCounter.WithLabelValues(downLabel).Dec()
containerCounter.WithLabelValues(upLabel).Inc()
workspaceID := values.NewWorkspaceID(res.ID)
workspaceName := values.NewWorkspaceName(ctnName)
return domain.NewWorkspace(workspaceID, workspaceName, userName), nil
}