Skip to content

Commit

Permalink
add stop command
Browse files Browse the repository at this point in the history
  • Loading branch information
xwjahahahaha committed Nov 15, 2021
1 parent 68376d0 commit 1fc5d35
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
10 changes: 10 additions & 0 deletions cmd/commands.go
Expand Up @@ -110,3 +110,13 @@ var execCommand = &cobra.Command{
container.ExecContainer(cid, commandAry)
},
}

var stopCommand = &cobra.Command{
Use: "stop [container_id]",
Short: "stop a container",
Long: "stop a container",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
container.StopContainer(args[0])
},
}
2 changes: 1 addition & 1 deletion cmd/init.go
@@ -1,7 +1,7 @@
package cmd

func init() {
rootCmd.AddCommand(initDocker, runDocker, commitCommand, listContainers, logCommand, execCommand)
rootCmd.AddCommand(initDocker, runDocker, commitCommand, listContainers, logCommand, execCommand, stopCommand)
runDocker.Flags().BoolVarP(&tty, "tty", "t", false, "enable tty")
runDocker.Flags().StringVarP(&ResourceLimitCfg.MemoryLimit, "memory-limit", "m", "200m", "memory limit")
runDocker.Flags().StringVarP(&ResourceLimitCfg.CpuShare, "cpu-shares", "", "1024", "cpu shares")
Expand Down
53 changes: 53 additions & 0 deletions container/exec.go
Expand Up @@ -6,7 +6,9 @@ import (
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"syscall"
"xwj/mydocker/log"
)

Expand Down Expand Up @@ -68,4 +70,55 @@ func getContainerPidByID(containerID string) (string, error) {
return "", err
}
return containerInfo.Pid, nil
}

// getContainerByID
// @Description: 根据容器ID获取容器信息结构体
// @param containerID
// @return *ContainerInfo
// @return error
func getContainerByID(containerID string) (*ContainerInfo, error) {
// 读取容器信息文件
containerInfoPath := filepath.Join(DefaultInfoLocation, containerID, ConfigName)
content, err := ioutil.ReadFile(containerInfoPath)
if err != nil {
log.LogErrorFrom("getContainerByID", "ReadFile", err)
return nil, err
}
var containerInfo ContainerInfo
if err := json.Unmarshal(content, &containerInfo); err != nil {
log.LogErrorFrom("getContainerByID", "Unmarshal", err)
return nil, err
}
return &containerInfo, nil
}

// StopContainer
// @Description: 关闭容器
// @param containerID
func StopContainer(containerID string) {
containerInfo, err := getContainerByID(containerID)
if err != nil {
log.LogErrorFrom("StopContainer", "getContainerByID", err)
return
}
// 系统调用kill可以发送信号给进程,通过传递syscall.SIGTERM信号,去杀掉容器主进程
pid, _ := strconv.Atoi(containerInfo.Pid)
if err := syscall.Kill(pid, syscall.SIGTERM); err != nil {
log.LogErrorFrom("StopContainer", "Kill", err)
return
}
// 修改容器的状态
containerInfo.Status = STOP
containerInfo.Pid = " " // 注意这里要设置一个空格,为了exec判断pid不为空""
newContentBytes, err := json.Marshal(containerInfo)
if err != nil {
log.LogErrorFrom("StopContainer", "Marshal", err)
return
}
configPath := filepath.Join(DefaultInfoLocation, containerID, ConfigName)
// 写入
if err := ioutil.WriteFile(configPath, newContentBytes, 0622); err != nil {
log.LogErrorFrom("StopContainer", "WriteFile", err)
}
}

0 comments on commit 1fc5d35

Please sign in to comment.