image_build.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package lib
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "net/http"
  6. "net/url"
  7. "regexp"
  8. "strconv"
  9. "strings"
  10. "github.com/docker/docker/api/types"
  11. "github.com/docker/docker/runconfig"
  12. "github.com/docker/go-units"
  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(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("/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 !runconfig.IsolationLevel.IsDefault(runconfig.IsolationLevel(options.Isolation)) {
  65. query.Set("isolation", 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. if options.ShmSize != "" {
  76. parsedShmSize, err := units.RAMInBytes(options.ShmSize)
  77. if err != nil {
  78. return query, err
  79. }
  80. query.Set("shmsize", strconv.FormatInt(parsedShmSize, 10))
  81. }
  82. query.Set("dockerfile", options.Dockerfile)
  83. ulimitsJSON, err := json.Marshal(options.Ulimits)
  84. if err != nil {
  85. return query, err
  86. }
  87. query.Set("ulimits", string(ulimitsJSON))
  88. buildArgs := convertKVStringsToMap(options.BuildArgs)
  89. buildArgsJSON, err := json.Marshal(buildArgs)
  90. if err != nil {
  91. return query, err
  92. }
  93. query.Set("buildargs", string(buildArgsJSON))
  94. return query, nil
  95. }
  96. func getDockerOS(serverHeader string) string {
  97. var osType string
  98. matches := headerRegexp.FindStringSubmatch(serverHeader)
  99. if len(matches) > 0 {
  100. osType = matches[1]
  101. }
  102. return osType
  103. }
  104. // convertKVStringsToMap converts ["key=value"] to {"key":"value"}
  105. func convertKVStringsToMap(values []string) map[string]string {
  106. result := make(map[string]string, len(values))
  107. for _, value := range values {
  108. kv := strings.SplitN(value, "=", 2)
  109. if len(kv) == 1 {
  110. result[kv[0]] = ""
  111. } else {
  112. result[kv[0]] = kv[1]
  113. }
  114. }
  115. return result
  116. }