Skip to content

Commit

Permalink
add code 6.5
Browse files Browse the repository at this point in the history
  • Loading branch information
xianlubird authored and Xianlu committed Jan 11, 2017
1 parent 8e72856 commit 5763560
Show file tree
Hide file tree
Showing 61 changed files with 11,624 additions and 17 deletions.
14 changes: 13 additions & 1 deletion Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions container/container_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ type ContainerInfo struct {
Command string `json:"command"` //容器内init运行命令
CreatedTime string `json:"createTime"` //创建时间
Status string `json:"status"` //容器的状态
Volume string `json:"volume"` //容器的数据卷
Volume string `json:"volume"` //容器的数据卷
PortMapping []string `json:"portmapping"` //端口映射
}

func NewParentProcess(tty bool, containerName, volume, imageName string, envSlice []string) (*exec.Cmd, *os.File) {
Expand All @@ -36,7 +37,13 @@ func NewParentProcess(tty bool, containerName, volume, imageName string, envSlic
log.Errorf("New pipe error %v", err)
return nil, nil
}
cmd := exec.Command("/proc/self/exe", "init")
initCmd, err := os.Readlink("/proc/self/exe")
if err != nil {
log.Errorf("get init process error %v", err)
return nil, nil
}

cmd := exec.Command(initCmd, "init")
cmd.SysProcAttr = &syscall.SysProcAttr{
Cloneflags: syscall.CLONE_NEWUTS | syscall.CLONE_NEWPID | syscall.CLONE_NEWNS |
syscall.CLONE_NEWNET | syscall.CLONE_NEWIPC,
Expand Down
8 changes: 4 additions & 4 deletions container/volume.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package container

import (
"fmt"
log "github.com/Sirupsen/logrus"
"os"
"os/exec"
"strings"
"fmt"
)

//Create a AUFS filesystem as container root workspace
Expand Down Expand Up @@ -62,7 +62,7 @@ func MountVolume(volumeURLs []string, containerName string) error {
}
containerUrl := volumeURLs[1]
mntURL := fmt.Sprintf(MntUrl, containerName)
containerVolumeURL := mntURL + "/" + containerUrl
containerVolumeURL := mntURL + "/" + containerUrl
if err := os.Mkdir(containerVolumeURL, 0777); err != nil {
log.Infof("Mkdir container dir %s error. %v", containerVolumeURL, err)
}
Expand All @@ -75,7 +75,7 @@ func MountVolume(volumeURLs []string, containerName string) error {
return nil
}

func CreateMountPoint(containerName, imageName string) error {
func CreateMountPoint(containerName , imageName string) error {
mntUrl := fmt.Sprintf(MntUrl, containerName)
if err := os.MkdirAll(mntUrl, 0777); err != nil {
log.Errorf("Mkdir mountpoint dir %s error. %v", mntUrl, err)
Expand Down Expand Up @@ -125,7 +125,7 @@ func DeleteMountPoint(containerName string) error {

func DeleteMountPointWithVolume(volumeURLs []string, containerName string) error {
mntURL := fmt.Sprintf(MntUrl, containerName)
containerUrl := mntURL + "/" + volumeURLs[1]
containerUrl := mntURL + "/" + volumeURLs[1]
if _, err := exec.Command("umount", containerUrl).CombinedOutput(); err != nil {
log.Errorf("Umount volume %s failed. %v", containerUrl, err)
return err
Expand Down
3 changes: 3 additions & 0 deletions list.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ func ListContainers() {

var containers []*container.ContainerInfo
for _, file := range files {
if file.Name() == "network" {
continue
}
tmpContainer, err := getContainerInfo(file)
if err != nil {
log.Errorf("Get container info error %v", err)
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func main() {
stopCommand,
removeCommand,
commitCommand,
networkCommand,
}

app.Before = func(context *cli.Context) error {
Expand Down
82 changes: 75 additions & 7 deletions main_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
"github.com/urfave/cli"
"github.com/xianlubird/mydocker/cgroups/subsystems"
"github.com/xianlubird/mydocker/container"
"github.com/xianlubird/mydocker/network"
"os"
)

var runCommand = cli.Command{
Name: "run",
Name: "run",
Usage: `Create a container with namespace and cgroups limit ie: mydocker run -ti [image] [command]`,
Flags: []cli.Flag{
cli.BoolFlag{
Expand Down Expand Up @@ -42,9 +43,17 @@ var runCommand = cli.Command{
Usage: "volume",
},
cli.StringSliceFlag{
Name: "e",
Name: "e",
Usage: "set environment",
},
cli.StringFlag{
Name: "net",
Usage: "container network",
},
cli.StringSliceFlag{
Name: "p",
Usage: "port mapping",
},
},
Action: func(context *cli.Context) error {
if len(context.Args()) < 1 {
Expand Down Expand Up @@ -73,9 +82,12 @@ var runCommand = cli.Command{
log.Infof("createTty %v", createTty)
containerName := context.String("name")
volume := context.String("v")
network := context.String("net")

envSlice := context.StringSlice("e")
Run(createTty, cmdArray, resConf, containerName, volume, imageName, envSlice)
portmapping := context.StringSlice("p")

Run(createTty, cmdArray, resConf, containerName, volume, imageName, envSlice, network, portmapping)
return nil
},
}
Expand All @@ -100,7 +112,7 @@ var listCommand = cli.Command{
}

var logCommand = cli.Command{
Name: "logs",
Name: "logs",
Usage: "print logs of a container",
Action: func(context *cli.Context) error {
if len(context.Args()) < 1 {
Expand All @@ -113,7 +125,7 @@ var logCommand = cli.Command{
}

var execCommand = cli.Command{
Name: "exec",
Name: "exec",
Usage: "exec a command into container",
Action: func(context *cli.Context) error {
//This is for callback
Expand All @@ -136,7 +148,7 @@ var execCommand = cli.Command{
}

var stopCommand = cli.Command{
Name: "stop",
Name: "stop",
Usage: "stop a container",
Action: func(context *cli.Context) error {
if len(context.Args()) < 1 {
Expand All @@ -149,7 +161,7 @@ var stopCommand = cli.Command{
}

var removeCommand = cli.Command{
Name: "rm",
Name: "rm",
Usage: "remove unused containers",
Action: func(context *cli.Context) error {
if len(context.Args()) < 1 {
Expand All @@ -174,3 +186,59 @@ var commitCommand = cli.Command{
return nil
},
}

var networkCommand = cli.Command{
Name: "network",
Usage: "container network commands",
Subcommands: []cli.Command {
{
Name: "create",
Usage: "create a container network",
Flags: []cli.Flag{
cli.StringFlag{
Name: "driver",
Usage: "network driver",
},
cli.StringFlag{
Name: "subnet",
Usage: "subnet cidr",
},
},
Action:func(context *cli.Context) error {
if len(context.Args()) < 1 {
return fmt.Errorf("Missing network name")
}
network.Init()
err := network.CreateNetwork(context.String("driver"), context.String("subnet"), context.Args()[0])
if err != nil {
return fmt.Errorf("create network error: %+v", err)
}
return nil
},
},
{
Name: "list",
Usage: "list container network",
Action:func(context *cli.Context) error {
network.Init()
network.ListNetwork()
return nil
},
},
{
Name: "remove",
Usage: "remove container network",
Action:func(context *cli.Context) error {
if len(context.Args()) < 1 {
return fmt.Errorf("Missing network name")
}
network.Init()
err := network.DeleteNetwork(context.Args()[0])
if err != nil {
return fmt.Errorf("remove network error: %+v", err)
}
return nil
},
},
},
}
Loading

0 comments on commit 5763560

Please sign in to comment.