image_build.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package client
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "net/http"
  6. "net/url"
  7. "regexp"
  8. "strconv"
  9. "strings"
  10. "golang.org/x/net/context"
  11. "github.com/docker/engine-api/types"
  12. "github.com/docker/engine-api/types/container"
  13. )
  14. var headerRegexp = regexp.MustCompile(`\ADocker/.+\s\((.+)\)\z`)
  15. // ImageBuild sends request to the daemon to build images.
  16. // The Body in the response implement an io.ReadCloser and it's up to the caller to
  17. // close it.
  18. func (cli *Client) ImageBuild(ctx context.Context, options types.ImageBuildOptions) (types.ImageBuildResponse, error) {
  19. query, err := imageBuildOptionsToQuery(options)
  20. if err != nil {
  21. return types.ImageBuildResponse{}, err
  22. }
  23. headers := http.Header(make(map[string][]string))
  24. buf, err := json.Marshal(options.AuthConfigs)
  25. if err != nil {
  26. return types.ImageBuildResponse{}, err
  27. }
  28. headers.Add("X-Registry-Config", base64.URLEncoding.EncodeToString(buf))
  29. headers.Set("Content-Type", "application/tar")
  30. serverResp, err := cli.postRaw(ctx, "/build", query, options.Context, headers)
  31. if err != nil {
  32. return types.ImageBuildResponse{}, err
  33. }
  34. osType := getDockerOS(serverResp.header.Get("Server"))
  35. return types.ImageBuildResponse{
  36. Body: serverResp.body,
  37. OSType: osType,
  38. }, nil
  39. }
  40. func imageBuildOptionsToQuery(options types.ImageBuildOptions) (url.Values, error) {
  41. query := url.Values{
  42. "t": options.Tags,
  43. }
  44. if options.SuppressOutput {
  45. query.Set("q", "1")
  46. }
  47. if options.RemoteContext != "" {
  48. query.Set("remote", options.RemoteContext)
  49. }
  50. if options.NoCache {
  51. query.Set("nocache", "1")
  52. }
  53. if options.Remove {
  54. query.Set("rm", "1")
  55. } else {
  56. query.Set("rm", "0")
  57. }
  58. if options.ForceRemove {
  59. query.Set("forcerm", "1")
  60. }
  61. if options.PullParent {
  62. query.Set("pull", "1")
  63. }
  64. if !container.Isolation.IsDefault(options.Isolation) {
  65. query.Set("isolation", string(options.Isolation))
  66. }
  67. query.Set("cpusetcpus", options.CPUSetCPUs)
  68. query.Set("cpusetmems", options.CPUSetMems)
  69. query.Set("cpushares", strconv.FormatInt(options.CPUShares, 10))
  70. query.Set("cpuquota", strconv.FormatInt(options.CPUQuota, 10))
  71. query.Set("cpuperiod", strconv.FormatInt(options.CPUPeriod, 10))
  72. query.Set("memory", strconv.FormatInt(options.Memory, 10))
  73. query.Set("memswap", strconv.FormatInt(options.MemorySwap, 10))
  74. query.Set("cgroupparent", options.CgroupParent)
  75. query.Set("shmsize", strconv.FormatInt(options.ShmSize, 10))
  76. query.Set("dockerfile", options.Dockerfile)
  77. ulimitsJSON, err := json.Marshal(options.Ulimits)
  78. if err != nil {
  79. return query, err
  80. }
  81. query.Set("ulimits", string(ulimitsJSON))
  82. buildArgsJSON, err := json.Marshal(options.BuildArgs)
  83. if err != nil {
  84. return query, err
  85. }
  86. query.Set("buildargs", string(buildArgsJSON))
  87. labelsJSON, err := json.Marshal(options.Labels)
  88. if err != nil {
  89. return query, err
  90. }
  91. query.Set("labels", string(labelsJSON))
  92. return query, nil
  93. }
  94. func getDockerOS(serverHeader string) string {
  95. var osType string
  96. matches := headerRegexp.FindStringSubmatch(serverHeader)
  97. if len(matches) > 0 {
  98. osType = matches[1]
  99. }
  100. return osType
  101. }
  102. // convertKVStringsToMap converts ["key=value"] to {"key":"value"}
  103. func convertKVStringsToMap(values []string) map[string]string {
  104. result := make(map[string]string, len(values))
  105. for _, value := range values {
  106. kv := strings.SplitN(value, "=", 2)
  107. if len(kv) == 1 {
  108. result[kv[0]] = ""
  109. } else {
  110. result[kv[0]] = kv[1]
  111. }
  112. }
  113. return result
  114. }