github.com/pwn-term/docker@v0.0.0-20210616085119-6e977cce2565/libnetwork/cmd/dnet/cmd.go (about) 1 package main 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "io" 7 "io/ioutil" 8 "net/http" 9 "os" 10 11 "github.com/docker/libnetwork/client" 12 "github.com/moby/term" 13 "github.com/urfave/cli" 14 ) 15 16 var ( 17 containerCreateCommand = cli.Command{ 18 Name: "create", 19 Usage: "Create a container", 20 Action: runContainerCreate, 21 } 22 23 containerRmCommand = cli.Command{ 24 Name: "rm", 25 Usage: "Remove a container", 26 Action: runContainerRm, 27 } 28 29 containerCommands = []cli.Command{ 30 containerCreateCommand, 31 containerRmCommand, 32 } 33 34 dnetCommands = []cli.Command{ 35 createDockerCommand("network"), 36 createDockerCommand("service"), 37 { 38 Name: "container", 39 Usage: "Container management commands", 40 Subcommands: containerCommands, 41 }, 42 } 43 ) 44 45 func runContainerCreate(c *cli.Context) { 46 if len(c.Args()) == 0 { 47 fmt.Println("Please provide container id argument") 48 os.Exit(1) 49 } 50 51 sc := client.SandboxCreate{ContainerID: c.Args()[0]} 52 obj, _, err := readBody(epConn.httpCall("POST", "/sandboxes", sc, nil)) 53 if err != nil { 54 fmt.Printf("POST failed during create container: %v\n", err) 55 os.Exit(1) 56 } 57 58 var replyID string 59 err = json.Unmarshal(obj, &replyID) 60 if err != nil { 61 fmt.Printf("Unmarshall of response failed during create container: %v\n", err) 62 os.Exit(1) 63 } 64 65 fmt.Printf("%s\n", replyID) 66 67 } 68 69 func runContainerRm(c *cli.Context) { 70 var sbList []*client.SandboxResource 71 72 if len(c.Args()) == 0 { 73 fmt.Println("Please provide container id argument") 74 os.Exit(1) 75 } 76 77 obj, _, err := readBody(epConn.httpCall("GET", "/sandboxes?partial-container-id="+c.Args()[0], nil, nil)) 78 if err != nil { 79 fmt.Printf("GET failed during container id lookup: %v\n", err) 80 os.Exit(1) 81 } 82 83 err = json.Unmarshal(obj, &sbList) 84 if err != nil { 85 fmt.Printf("Unmarshall of container id lookup response failed: %v", err) 86 os.Exit(1) 87 } 88 89 if len(sbList) == 0 { 90 fmt.Printf("No sandbox for container %s found\n", c.Args()[0]) 91 os.Exit(1) 92 } 93 94 _, _, err = readBody(epConn.httpCall("DELETE", "/sandboxes/"+sbList[0].ID, nil, nil)) 95 if err != nil { 96 fmt.Printf("DELETE of sandbox id %s failed: %v", sbList[0].ID, err) 97 os.Exit(1) 98 } 99 } 100 101 func runDockerCommand(c *cli.Context, cmd string) { 102 _, stdout, stderr := term.StdStreams() 103 oldcli := client.NewNetworkCli(stdout, stderr, epConn.httpCall) 104 var args []string 105 args = append(args, cmd) 106 if c.Bool("h") { 107 args = append(args, "--help") 108 } else { 109 args = append(args, c.Args()...) 110 } 111 if err := oldcli.Cmd("dnet", args...); err != nil { 112 fmt.Println(err) 113 os.Exit(1) 114 } 115 } 116 117 func createDockerCommand(cmd string) cli.Command { 118 return cli.Command{ 119 Name: cmd, 120 Usage: fmt.Sprintf("%s management commands", cmd), 121 SkipFlagParsing: true, 122 Action: func(c *cli.Context) { 123 runDockerCommand(c, cmd) 124 }, 125 Subcommands: []cli.Command{ 126 { 127 Name: "h, -help", 128 Usage: fmt.Sprintf("%s help", cmd), 129 }, 130 }, 131 } 132 } 133 134 func readBody(stream io.ReadCloser, hdr http.Header, statusCode int, err error) ([]byte, int, error) { 135 if stream != nil { 136 defer stream.Close() 137 } 138 if err != nil { 139 return nil, statusCode, err 140 } 141 body, err := ioutil.ReadAll(stream) 142 if err != nil { 143 return nil, -1, err 144 } 145 return body, statusCode, nil 146 }