build_routes.go 5.8 KB

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