-
Notifications
You must be signed in to change notification settings - Fork 38
/
stop.go
101 lines (93 loc) · 3.08 KB
/
stop.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 main
import (
"encoding/json"
"fmt"
"mydocker/network"
"os"
"path"
"strconv"
"syscall"
"mydocker/constant"
"mydocker/container"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
func stopContainer(containerId string) {
// 1. 根据容器Id查询容器信息
containerInfo, err := getInfoByContainerId(containerId)
if err != nil {
log.Errorf("Get container %s info error %v", containerId, err)
return
}
pidInt, err := strconv.Atoi(containerInfo.Pid)
if err != nil {
log.Errorf("Conver pid from string to int error %v", err)
return
}
// 2.发送SIGTERM信号
if err = syscall.Kill(pidInt, syscall.SIGTERM); err != nil {
log.Errorf("Stop container %s error %v", containerId, err)
return
}
// 3.修改容器信息,将容器置为STOP状态,并清空PID
containerInfo.Status = container.STOP
containerInfo.Pid = " "
newContentBytes, err := json.Marshal(containerInfo)
if err != nil {
log.Errorf("Json marshal %s error %v", containerId, err)
return
}
// 4.重新写回存储容器信息的文件
dirPath := fmt.Sprintf(container.InfoLocFormat, containerId)
configFilePath := path.Join(dirPath, container.ConfigName)
if err = os.WriteFile(configFilePath, newContentBytes, constant.Perm0622); err != nil {
log.Errorf("Write file %s error:%v", configFilePath, err)
}
}
func getInfoByContainerId(containerId string) (*container.Info, error) {
dirPath := fmt.Sprintf(container.InfoLocFormat, containerId)
configFilePath := path.Join(dirPath, container.ConfigName)
contentBytes, err := os.ReadFile(configFilePath)
if err != nil {
return nil, errors.Wrapf(err, "read file %s", configFilePath)
}
var containerInfo container.Info
if err = json.Unmarshal(contentBytes, &containerInfo); err != nil {
return nil, err
}
return &containerInfo, nil
}
func removeContainer(containerId string, force bool) {
containerInfo, err := getInfoByContainerId(containerId)
if err != nil {
log.Errorf("Get container %s info error %v", containerId, err)
return
}
switch containerInfo.Status {
case container.STOP: // STOP 状态容器直接删除即可
// 先删除配置目录,再删除rootfs 目录
if err = container.DeleteContainerInfo(containerId); err != nil {
log.Errorf("Remove container [%s]'s config failed, detail: %v", containerId, err)
return
}
container.DeleteWorkSpace(containerId, containerInfo.Volume)
if containerInfo.NetworkName != "" { // 清理网络资源
if err = network.Disconnect(containerInfo.NetworkName, containerInfo); err != nil {
log.Errorf("Remove container [%s]'s config failed, detail: %v", containerId, err)
return
}
}
case container.RUNNING: // RUNNING 状态容器如果指定了 force 则先 stop 然后再删除
if !force {
log.Errorf("Couldn't remove running container [%s], Stop the container before attempting removal or"+
" force remove", containerId)
return
}
log.Infof("force delete running container [%s]", containerId)
stopContainer(containerId)
removeContainer(containerId, force)
default:
log.Errorf("Couldn't remove container,invalid status %s", containerInfo.Status)
return
}
}