forked from lixd/mydocker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stop.go
85 lines (78 loc) · 2.41 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strconv"
"syscall"
"mydocker/constant"
"mydocker/container"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
func stopContainer(containerName string) {
// 1. 根据容器名称获取对应 PID
containerInfo, err := getContainerInfoByName(containerName)
if err != nil {
log.Errorf("Get container %s info error %v", containerName, 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", containerName, 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", containerName, err)
return
}
// 4.重新写回存储容器信息的文件
dirURL := fmt.Sprintf(container.InfoLocFormat, containerName)
configFilePath := dirURL + container.ConfigName
if err := ioutil.WriteFile(configFilePath, newContentBytes, constant.Perm0622); err != nil {
log.Errorf("Write file %s error:%v", configFilePath, err)
}
}
func getContainerInfoByName(containerName string) (*container.Info, error) {
dirURL := fmt.Sprintf(container.InfoLocFormat, containerName)
configFilePath := dirURL + container.ConfigName
contentBytes, err := ioutil.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(containerName string) {
containerInfo, err := getContainerInfoByName(containerName)
if err != nil {
log.Errorf("Get container %s info error %v", containerName, err)
return
}
// 限制只能删除STOP状态的容器
if containerInfo.Status != container.STOP {
log.Errorf("Couldn't remove running container")
return
}
dirURL := fmt.Sprintf(container.InfoLocFormat, containerName)
if err := os.RemoveAll(dirURL); err != nil {
log.Errorf("Remove file %s error %v", dirURL, err)
}
err = container.DeleteWorkSpace(containerInfo.Volume, containerName)
if err != nil {
log.Errorf("DeleteWorkSpace error %v", err)
}
}