cmd.go 3.1 KB

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