From ecb22a3209fb32cbadd579504b33f952de480edc Mon Sep 17 00:00:00 2001 From: xwjahahahaha <1438575858@qq.com> Date: Mon, 15 Nov 2021 11:04:36 +0800 Subject: [PATCH] add remove command --- cmd/commands.go | 11 +++++++++++ cmd/init.go | 2 +- container/exec.go | 22 ++++++++++++++++++++++ container/process.go | 9 ++++++--- container/record.go | 6 ++++-- container/run.go | 7 +++---- 6 files changed, 47 insertions(+), 10 deletions(-) diff --git a/cmd/commands.go b/cmd/commands.go index bbd6e8f..b06ee93 100644 --- a/cmd/commands.go +++ b/cmd/commands.go @@ -119,4 +119,15 @@ var stopCommand = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { container.StopContainer(args[0]) }, +} + + +var removeCommand = &cobra.Command{ + Use: "rm [container_id]", + Short: "remove a container", + Long: "remove a container", + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + container.RemoveContainer(args[0]) + }, } \ No newline at end of file diff --git a/cmd/init.go b/cmd/init.go index ea422e9..02b46ef 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -1,7 +1,7 @@ package cmd func init() { - rootCmd.AddCommand(initDocker, runDocker, commitCommand, listContainers, logCommand, execCommand, stopCommand) + rootCmd.AddCommand(initDocker, runDocker, commitCommand, listContainers, logCommand, execCommand, stopCommand, removeCommand) 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") diff --git a/container/exec.go b/container/exec.go index 1d4b7fe..7b0b330 100644 --- a/container/exec.go +++ b/container/exec.go @@ -121,4 +121,26 @@ func StopContainer(containerID string) { if err := ioutil.WriteFile(configPath, newContentBytes, 0622); err != nil { log.LogErrorFrom("StopContainer", "WriteFile", err) } +} + +// RemoveContainer +// @Description: 删除一个容器 +// @param containerID +func RemoveContainer(containerID string) { + containerInfo, err := getContainerByID(containerID) + if err != nil { + log.LogErrorFrom("StopContainer", "getContainerByID", err) + return + } + if containerInfo.Status == STOP { + containerInfoPath := filepath.Join(DefaultInfoLocation, containerID) + if err := os.RemoveAll(containerInfoPath); err != nil { + log.LogErrorFrom("RemoveContainer", "RemoveAll", err) + return + } + mntUrl := filepath.Join(ROOTURL, "mnt", containerID) + DeleteWorkSpace(ROOTURL, mntUrl, containerInfo.Volume, containerID) + }else { + log.Log.Warnf("Please stop container first.") + } } \ No newline at end of file diff --git a/container/process.go b/container/process.go index 4e5617b..fd1cc44 100644 --- a/container/process.go +++ b/container/process.go @@ -8,6 +8,10 @@ import ( "xwj/mydocker/log" ) +const ( + ROOTURL = "/var/lib/mydocker/aufs/" +) + // NewParentProcess // @Description: 创建新的命令进程(并未执行) // @param tty @@ -38,9 +42,8 @@ func NewParentProcess(tty bool, volume, ImageTarPath, cId string) (*exec.Cmd, *o recordContainerLog(cId, &cmd.Stdout) } // 创建新的工作空间 - rootUrl := "/var/lib/mydocker/aufs/" // 根目录 - mntUrl := filepath.Join(rootUrl, "mnt", cId) // 容器运行空间 - NewWorkSpace(rootUrl, ImageTarPath, mntUrl, volume, cId) + mntUrl := filepath.Join(ROOTURL, "mnt", cId) // 容器运行空间 + NewWorkSpace(ROOTURL, ImageTarPath, mntUrl, volume, cId) cmd.Dir = mntUrl // 设置进程启动的路径 // 在这里传入管道文件读取端的句柄 // ExtraFiles指定要由新进程继承的其他打开文件。它不包括标准输入、标准输出或标准错误。 diff --git a/container/record.go b/container/record.go index 6340fbd..1f58e90 100644 --- a/container/record.go +++ b/container/record.go @@ -24,6 +24,7 @@ type ContainerInfo struct { Id string `json:"id"` Name string `json:"name"` Command string `json:"command"` + Volume string `json:"volume"` CreatedTime string `json:"created_time"` Status string `json:"status"` } @@ -57,7 +58,7 @@ func RandStringContainerID(n int) string { // @param cName // @return string // @return error -func recordContainerInfo(id string, cPID int, commandArray []string, cName string) (string, error) { +func recordContainerInfo(id string, cPID int, commandArray []string, cName, volume string) (string, error) { // 以当前时间为容器的创建时间 createTime := time.Now().Format("2006-01-02 15:04:05") // 如果用户没有指定容器名就用容器ID做为容器名 @@ -69,6 +70,7 @@ func recordContainerInfo(id string, cPID int, commandArray []string, cName strin Id: id, Name: cName, Command: strings.Join(commandArray, ""), + Volume: volume, CreatedTime: createTime, Status: RUNNING, } @@ -199,7 +201,7 @@ func getContainerInfo(file os.FileInfo) (*ContainerInfo, error) { // LogContainer // @Description: 输出一个容器的日志 // @param containerId -func LogContainer(containerId string) { +func LogContainer(containerId string) { logFilePath := filepath.Join(DefaultInfoLocation, containerId, LogFileName) file, err := os.OpenFile(logFilePath, os.O_RDONLY, 0644) if err != nil { diff --git a/container/run.go b/container/run.go index 3db592a..891f7ef 100644 --- a/container/run.go +++ b/container/run.go @@ -41,7 +41,7 @@ func Run(tty bool, cmdArray []string, res *subsystems.ResourceConfig, cgroupName log.Log.Error(err) } // 记录容器信息 - containerInfo, err := recordContainerInfo(cId, parent.Process.Pid, cmdArray, cName) + containerInfo, err := recordContainerInfo(cId, parent.Process.Pid, cmdArray, cName, volume) if err != nil { log.LogErrorFrom("Run", "recordContainerInfo", err) return @@ -62,9 +62,8 @@ func Run(tty bool, cmdArray []string, res *subsystems.ResourceConfig, cgroupName } containerCM.Destroy() // 删除设置的AUFS工作目录 - rootUrl := "/var/lib/mydocker/aufs/" - mntUrl := filepath.Join(rootUrl, "mnt", cId) - DeleteWorkSpace(rootUrl, mntUrl, volume, cId) + mntUrl := filepath.Join(ROOTURL, "mnt", cId) + DeleteWorkSpace(ROOTURL, mntUrl, volume, cId) DeleteContainerInfo(containerInfo) os.Exit(1) }else {