build_routes.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package build
  2. import (
  3. "bytes"
  4. "encoding/base64"
  5. "encoding/json"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "strconv"
  10. "strings"
  11. "sync"
  12. "github.com/Sirupsen/logrus"
  13. "github.com/docker/docker/api/server/httputils"
  14. "github.com/docker/docker/api/types"
  15. "github.com/docker/docker/api/types/backend"
  16. "github.com/docker/docker/api/types/container"
  17. "github.com/docker/docker/api/types/versions"
  18. "github.com/docker/docker/pkg/ioutils"
  19. "github.com/docker/docker/pkg/progress"
  20. "github.com/docker/docker/pkg/streamformatter"
  21. "github.com/docker/go-units"
  22. "golang.org/x/net/context"
  23. )
  24. func newImageBuildOptions(ctx context.Context, r *http.Request) (*types.ImageBuildOptions, error) {
  25. version := httputils.VersionFromContext(ctx)
  26. options := &types.ImageBuildOptions{}
  27. if httputils.BoolValue(r, "forcerm") && versions.GreaterThanOrEqualTo(version, "1.12") {
  28. options.Remove = true
  29. } else if r.FormValue("rm") == "" && versions.GreaterThanOrEqualTo(version, "1.12") {
  30. options.Remove = true
  31. } else {
  32. options.Remove = httputils.BoolValue(r, "rm")
  33. }
  34. if httputils.BoolValue(r, "pull") && versions.GreaterThanOrEqualTo(version, "1.16") {
  35. options.PullParent = true
  36. }
  37. options.Dockerfile = r.FormValue("dockerfile")
  38. options.SuppressOutput = httputils.BoolValue(r, "q")
  39. options.NoCache = httputils.BoolValue(r, "nocache")
  40. options.ForceRemove = httputils.BoolValue(r, "forcerm")
  41. options.MemorySwap = httputils.Int64ValueOrZero(r, "memswap")
  42. options.Memory = httputils.Int64ValueOrZero(r, "memory")
  43. options.CPUShares = httputils.Int64ValueOrZero(r, "cpushares")
  44. options.CPUPeriod = httputils.Int64ValueOrZero(r, "cpuperiod")
  45. options.CPUQuota = httputils.Int64ValueOrZero(r, "cpuquota")
  46. options.CPUSetCPUs = r.FormValue("cpusetcpus")
  47. options.CPUSetMems = r.FormValue("cpusetmems")
  48. options.CgroupParent = r.FormValue("cgroupparent")
  49. options.Tags = r.Form["t"]
  50. if r.Form.Get("shmsize") != "" {
  51. shmSize, err := strconv.ParseInt(r.Form.Get("shmsize"), 10, 64)
  52. if err != nil {
  53. return nil, err
  54. }
  55. options.ShmSize = shmSize
  56. }
  57. if i := container.Isolation(r.FormValue("isolation")); i != "" {
  58. if !container.Isolation.IsValid(i) {
  59. return nil, fmt.Errorf("Unsupported isolation: %q", i)
  60. }
  61. options.Isolation = i
  62. }
  63. var buildUlimits = []*units.Ulimit{}
  64. ulimitsJSON := r.FormValue("ulimits")
  65. if ulimitsJSON != "" {
  66. if err := json.Unmarshal([]byte(ulimitsJSON), &buildUlimits); err != nil {
  67. return nil, err
  68. }
  69. options.Ulimits = buildUlimits
  70. }
  71. var buildArgs = map[string]string{}
  72. buildArgsJSON := r.FormValue("buildargs")
  73. if buildArgsJSON != "" {
  74. if err := json.Unmarshal([]byte(buildArgsJSON), &buildArgs); err != nil {
  75. return nil, err
  76. }
  77. options.BuildArgs = buildArgs
  78. }
  79. var labels = map[string]string{}
  80. labelsJSON := r.FormValue("labels")
  81. if labelsJSON != "" {
  82. if err := json.Unmarshal([]byte(labelsJSON), &labels); err != nil {
  83. return nil, err
  84. }
  85. options.Labels = labels
  86. }
  87. var cacheFrom = []string{}
  88. cacheFromJSON := r.FormValue("cachefrom")
  89. if cacheFromJSON != "" {
  90. if err := json.Unmarshal([]byte(cacheFromJSON), &cacheFrom); err != nil {
  91. return nil, err
  92. }
  93. options.CacheFrom = cacheFrom
  94. }
  95. return options, nil
  96. }
  97. type syncWriter struct {
  98. w io.Writer
  99. mu sync.Mutex
  100. }
  101. func (s *syncWriter) Write(b []byte) (count int, err error) {
  102. s.mu.Lock()
  103. count, err = s.w.Write(b)
  104. s.mu.Unlock()
  105. return
  106. }
  107. func (br *buildRouter) postBuild(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  108. var (
  109. authConfigs = map[string]types.AuthConfig{}
  110. authConfigsEncoded = r.Header.Get("X-Registry-Config")
  111. notVerboseBuffer = bytes.NewBuffer(nil)
  112. )
  113. if authConfigsEncoded != "" {
  114. authConfigsJSON := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authConfigsEncoded))
  115. if err := json.NewDecoder(authConfigsJSON).Decode(&authConfigs); err != nil {
  116. // for a pull it is not an error if no auth was given
  117. // to increase compatibility with the existing api it is defaulting
  118. // to be empty.
  119. }
  120. }
  121. w.Header().Set("Content-Type", "application/json")
  122. output := ioutils.NewWriteFlusher(w)
  123. defer output.Close()
  124. sf := streamformatter.NewJSONStreamFormatter()
  125. errf := func(err error) error {
  126. if httputils.BoolValue(r, "q") && notVerboseBuffer.Len() > 0 {
  127. output.Write(notVerboseBuffer.Bytes())
  128. }
  129. // Do not write the error in the http output if it's still empty.
  130. // This prevents from writing a 200(OK) when there is an internal error.
  131. if !output.Flushed() {
  132. return err
  133. }
  134. _, err = w.Write(sf.FormatError(err))
  135. if err != nil {
  136. logrus.Warnf("could not write error response: %v", err)
  137. }
  138. return nil
  139. }
  140. buildOptions, err := newImageBuildOptions(ctx, r)
  141. if err != nil {
  142. return errf(err)
  143. }
  144. buildOptions.AuthConfigs = authConfigs
  145. remoteURL := r.FormValue("remote")
  146. // Currently, only used if context is from a remote url.
  147. // Look at code in DetectContextFromRemoteURL for more information.
  148. createProgressReader := func(in io.ReadCloser) io.ReadCloser {
  149. progressOutput := sf.NewProgressOutput(output, true)
  150. if buildOptions.SuppressOutput {
  151. progressOutput = sf.NewProgressOutput(notVerboseBuffer, true)
  152. }
  153. return progress.NewProgressReader(in, progressOutput, r.ContentLength, "Downloading context", remoteURL)
  154. }
  155. out := io.Writer(output)
  156. if buildOptions.SuppressOutput {
  157. out = notVerboseBuffer
  158. }
  159. out = &syncWriter{w: out}
  160. stdout := &streamformatter.StdoutFormatter{Writer: out, StreamFormatter: sf}
  161. stderr := &streamformatter.StderrFormatter{Writer: out, StreamFormatter: sf}
  162. pg := backend.ProgressWriter{
  163. Output: out,
  164. StdoutFormatter: stdout,
  165. StderrFormatter: stderr,
  166. ProgressReaderFunc: createProgressReader,
  167. }
  168. imgID, err := br.backend.BuildFromContext(ctx, r.Body, remoteURL, buildOptions, pg)
  169. if err != nil {
  170. return errf(err)
  171. }
  172. // Everything worked so if -q was provided the output from the daemon
  173. // should be just the image ID and we'll print that to stdout.
  174. if buildOptions.SuppressOutput {
  175. stdout := &streamformatter.StdoutFormatter{Writer: output, StreamFormatter: sf}
  176. fmt.Fprintf(stdout, "%s\n", string(imgID))
  177. }
  178. return nil
  179. }