-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
53 lines (46 loc) · 1.24 KB
/
run.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
package python2
import (
"context"
"fmt"
"log"
"os"
"github.com/MSC-XDU/playground/dockerserver"
"github.com/MSC-XDU/playground/lang/internal"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
)
const (
sourcePath = "/source/source.py"
)
// Python 代码运行容器 Creator
type RunCreator struct {
tempfile *os.File
}
// 创建 Python 代码运行容器
func (config RunCreator) Create(ctx context.Context, client *client.Client) (string, error) {
limits := lang.DefaultResourceLimits()
hostConfig := container.HostConfig{
AutoRemove: true,
Binds: []string{fmt.Sprintf("%s:%s:rw", config.tempfile.Name(), sourcePath)},
Resources: limits,
}
containerConfig := container.Config{
Image: imageRef,
AttachStderr: true,
AttachStdout: true,
Cmd: []string{"python", sourcePath},
NetworkDisabled: true,
Tty: true,
}
result, err := client.ContainerCreate(ctx, &containerConfig, &hostConfig, nil, "")
if err != nil {
return "", err
}
log.Printf("创建 Python2 容器, ID: %s, 警告信息: %v", result.ID, result.Warnings)
return result.ID, nil
}
func NewRunCreator(f *os.File) dockerserver.ContainerCreator {
return RunCreator{
tempfile: f,
}
}