zhangqian
2023-10-13 13194e787d51e4ce07dfc35341d536fb5db7aaa3
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
package service
 
import (
    "errors"
)
 
type IContainer interface {
    // Image 运行环境及配置的基础目录或镜像位置
    Image() string
    //获取container的根路径
    GetContainerRoot() string
    //初始化此用户的容器运行环境
    Init(cId string) error
    //启动container
    Start(cId string) error
    //停止container
    Stop(cId string) error
    //重启container
    Restart(cId string) error
    //备份容器中的用户数据,防止删除后需要再还原
    BakData(cId string) error
    //删除container
    Destroy(cId string) error
    //监控是否有容器未创建或启动
    watch()
}
 
const (
    TypeContainerDir = iota
    TypeContainerDocker
)
 
var ContainerNotFound = errors.New("Container not found")
 
// NewContainer 目前container管理器有dir和docker两种模式
func NewContainer(typ int) IContainer {
    var c IContainer
    switch typ {
    case TypeContainerDir:
        c = &DirContainerImpl{}
    case TypeContainerDocker:
        c = &DockerImpl{}
    }
    go c.watch()
    return c
}