Skip to content

Commit

Permalink
add remove command
Browse files Browse the repository at this point in the history
  • Loading branch information
xwjahahahaha committed Nov 15, 2021
1 parent 1fc5d35 commit ecb22a3
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 10 deletions.
11 changes: 11 additions & 0 deletions cmd/commands.go
Expand Up @@ -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])
},
}
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, 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")
Expand Down
22 changes: 22 additions & 0 deletions container/exec.go
Expand Up @@ -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.")
}
}
9 changes: 6 additions & 3 deletions container/process.go
Expand Up @@ -8,6 +8,10 @@ import (
"xwj/mydocker/log"
)

const (
ROOTURL = "/var/lib/mydocker/aufs/"
)

// NewParentProcess
// @Description: 创建新的命令进程(并未执行)
// @param tty
Expand Down Expand Up @@ -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指定要由新进程继承的其他打开文件。它不包括标准输入、标准输出或标准错误。
Expand Down
6 changes: 4 additions & 2 deletions container/record.go
Expand Up @@ -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"`
}
Expand Down Expand Up @@ -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做为容器名
Expand All @@ -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,
}
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 3 additions & 4 deletions container/run.go
Expand Up @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit ecb22a3

Please sign in to comment.