build_routes.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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/backend"
  15. "github.com/docker/docker/pkg/ioutils"
  16. "github.com/docker/docker/pkg/progress"
  17. "github.com/docker/docker/pkg/streamformatter"
  18. "github.com/docker/engine-api/types"
  19. "github.com/docker/engine-api/types/container"
  20. "github.com/docker/engine-api/types/versions"
  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.NewDecoder(strings.NewReader(ulimitsJSON)).Decode(&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.NewDecoder(strings.NewReader(buildArgsJSON)).Decode(&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.NewDecoder(strings.NewReader(labelsJSON)).Decode(&labels); err != nil {
  83. return nil, err
  84. }
  85. options.Labels = labels
  86. }
  87. return options, nil
  88. }
  89. type syncWriter struct {
  90. w io.Writer
  91. mu sync.Mutex
  92. }
  93. func (s *syncWriter) Write(b []byte) (count int, err error) {
  94. s.mu.Lock()
  95. count, err = s.w.Write(b)
  96. s.mu.Unlock()
  97. return
  98. }
  99. func (br *buildRouter) postBuild(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  100. var (
  101. authConfigs = map[string]types.AuthConfig{}
  102. authConfigsEncoded = r.Header.Get("X-Registry-Config")
  103. notVerboseBuffer = bytes.NewBuffer(nil)
  104. )
  105. if authConfigsEncoded != "" {
  106. authConfigsJSON := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authConfigsEncoded))
  107. if err := json.NewDecoder(authConfigsJSON).Decode(&authConfigs); err != nil {
  108. // for a pull it is not an error if no auth was given
  109. // to increase compatibility with the existing api it is defaulting
  110. // to be empty.
  111. }
  112. }
  113. w.Header().Set("Content-Type", "application/json")
  114. output := ioutils.NewWriteFlusher(w)
  115. defer output.Close()
  116. sf := streamformatter.NewJSONStreamFormatter()
  117. errf := func(err error) error {
  118. if httputils.BoolValue(r, "q") && notVerboseBuffer.Len() > 0 {
  119. output.Write(notVerboseBuffer.Bytes())
  120. }
  121. // Do not write the error in the http output if it's still empty.
  122. // This prevents from writing a 200(OK) when there is an internal error.
  123. if !output.Flushed() {
  124. return err
  125. }
  126. _, err = w.Write(sf.FormatError(err))
  127. if err != nil {
  128. logrus.Warnf("could not write error response: %v", err)
  129. }
  130. return nil
  131. }
  132. buildOptions, err := newImageBuildOptions(ctx, r)
  133. if err != nil {
  134. return errf(err)
  135. }
  136. buildOptions.AuthConfigs = authConfigs
  137. remoteURL := r.FormValue("remote")
  138. // Currently, only used if context is from a remote url.
  139. // Look at code in DetectContextFromRemoteURL for more information.
  140. createProgressReader := func(in io.ReadCloser) io.ReadCloser {
  141. progressOutput := sf.NewProgressOutput(output, true)
  142. if buildOptions.SuppressOutput {
  143. progressOutput = sf.NewProgressOutput(notVerboseBuffer, true)
  144. }
  145. return progress.NewProgressReader(in, progressOutput, r.ContentLength, "Downloading context", remoteURL)
  146. }
  147. var out io.Writer = output
  148. if buildOptions.SuppressOutput {
  149. out = notVerboseBuffer
  150. }
  151. out = &syncWriter{w: out}
  152. stdout := &streamformatter.StdoutFormatter{Writer: out, StreamFormatter: sf}
  153. stderr := &streamformatter.StderrFormatter{Writer: out, StreamFormatter: sf}
  154. pg := backend.ProgressWriter{
  155. Output: out,
  156. StdoutFormatter: stdout,
  157. StderrFormatter: stderr,
  158. ProgressReaderFunc: createProgressReader,
  159. }
  160. imgID, err := br.backend.BuildFromContext(ctx, r.Body, remoteURL, buildOptions, pg)
  161. if err != nil {
  162. return errf(err)
  163. }
  164. // Everything worked so if -q was provided the output from the daemon
  165. // should be just the image ID and we'll print that to stdout.
  166. if buildOptions.SuppressOutput {
  167. stdout := &streamformatter.StdoutFormatter{Writer: output, StreamFormatter: sf}
  168. fmt.Fprintf(stdout, "%s\n", string(imgID))
  169. }
  170. return nil
  171. }