Skip to content

Commit

Permalink
add code 5.3
Browse files Browse the repository at this point in the history
  • Loading branch information
xianlubird authored and Xianlu committed Jan 11, 2017
1 parent 6fac480 commit 16bc6b4
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 8 deletions.
17 changes: 16 additions & 1 deletion container/container_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"os/exec"
"syscall"
"fmt"
)

var (
Expand All @@ -13,6 +14,7 @@ var (
Exit string = "exited"
DefaultInfoLocation string = "/var/run/mydocker/%s/"
ConfigName string = "config.json"
ContainerLogFile string = "container.log"
)

type ContainerInfo struct {
Expand All @@ -24,7 +26,7 @@ type ContainerInfo struct {
Status string `json:"status"` //容器的状态
}

func NewParentProcess(tty bool) (*exec.Cmd, *os.File) {
func NewParentProcess(tty bool, containerName string) (*exec.Cmd, *os.File) {
readPipe, writePipe, err := NewPipe()
if err != nil {
log.Errorf("New pipe error %v", err)
Expand All @@ -40,6 +42,19 @@ func NewParentProcess(tty bool) (*exec.Cmd, *os.File) {
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
} else {
dirURL := fmt.Sprintf(DefaultInfoLocation, containerName)
if err := os.MkdirAll(dirURL, 0622); err != nil {
log.Errorf("NewParentProcess mkdir %s error %v", dirURL, err)
return nil, nil
}
stdLogFilePath := dirURL + ContainerLogFile
stdLogFile, err := os.Create(stdLogFilePath)
if err != nil {
log.Errorf("NewParentProcess create file %s error %v", stdLogFilePath, err)
return nil, nil
}
cmd.Stdout = stdLogFile
}

cmd.ExtraFiles = []*os.File{readPipe}
Expand Down
26 changes: 26 additions & 0 deletions log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/xianlubird/mydocker/container"
"os"
"io/ioutil"
)

func logContainer(containerName string) {
dirURL := fmt.Sprintf(container.DefaultInfoLocation, containerName)
logFileLocation := dirURL + container.ContainerLogFile
file, err := os.Open(logFileLocation)
defer file.Close()
if err != nil {
log.Errorf("Log container open file %s error %v", logFileLocation, err)
return
}
content, err := ioutil.ReadAll(file)
if err != nil {
log.Errorf("Log container read file %s error %v", logFileLocation, err)
return
}
fmt.Fprint(os.Stdout, string(content))
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func main() {
initCommand,
runCommand,
listCommand,
logCommand,
}

app.Before = func(context *cli.Context) error {
Expand Down
13 changes: 13 additions & 0 deletions main_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,16 @@ var listCommand = cli.Command{
return nil
},
}

var logCommand = cli.Command{
Name: "logs",
Usage: "print logs of a container",
Action: func(context *cli.Context) error {
if len(context.Args()) < 1 {
return fmt.Errorf("Please input your container name")
}
containerName := context.Args().Get(0)
logContainer(containerName)
return nil
},
}
15 changes: 8 additions & 7 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ import (
)

func Run(tty bool, comArray []string, res *subsystems.ResourceConfig, containerName string) {
parent, writePipe := container.NewParentProcess(tty)
containerID := randStringBytes(10)
if containerName == "" {
containerName = containerID
}

parent, writePipe := container.NewParentProcess(tty, containerName)
if parent == nil {
log.Errorf("New parent process error")
return
Expand All @@ -25,7 +30,7 @@ func Run(tty bool, comArray []string, res *subsystems.ResourceConfig, containerN
}

//record container info
containerName, err := recordContainerInfo(parent.Process.Pid, comArray, containerName)
containerName, err := recordContainerInfo(parent.Process.Pid, comArray, containerName, containerID)
if err != nil {
log.Errorf("Record container info error %v", err)
return
Expand All @@ -51,13 +56,9 @@ func sendInitCommand(comArray []string, writePipe *os.File) {
writePipe.Close()
}

func recordContainerInfo(containerPID int, commandArray []string, containerName string) (string, error) {
id := randStringBytes(10)
func recordContainerInfo(containerPID int, commandArray []string, containerName string, id string) (string, error) {
createTime := time.Now().Format("2006-01-02 15:04:05")
command := strings.Join(commandArray, "")
if containerName == "" {
containerName = id
}
containerInfo := &container.ContainerInfo{
Id: id,
Pid: strconv.Itoa(containerPID),
Expand Down

0 comments on commit 16bc6b4

Please sign in to comment.