build_routes.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package build
  2. import (
  3. "bytes"
  4. "encoding/base64"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. "io"
  9. "net/http"
  10. "strconv"
  11. "strings"
  12. "github.com/Sirupsen/logrus"
  13. "github.com/docker/docker/api/server/httputils"
  14. "github.com/docker/docker/builder"
  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/docker/utils"
  19. "github.com/docker/engine-api/types"
  20. "github.com/docker/engine-api/types/container"
  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") && version.GreaterThanOrEqualTo("1.12") {
  28. options.Remove = true
  29. } else if r.FormValue("rm") == "" && version.GreaterThanOrEqualTo("1.12") {
  30. options.Remove = true
  31. } else {
  32. options.Remove = httputils.BoolValue(r, "rm")
  33. }
  34. if httputils.BoolValue(r, "pull") && version.GreaterThanOrEqualTo("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.IsolationLevel(r.FormValue("isolation")); i != "" {
  58. if !container.IsolationLevel.IsValid(i) {
  59. return nil, fmt.Errorf("Unsupported isolation: %q", i)
  60. }
  61. options.IsolationLevel = 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. return options, nil
  80. }
  81. func (br *buildRouter) postBuild(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  82. var (
  83. authConfigs = map[string]types.AuthConfig{}
  84. authConfigsEncoded = r.Header.Get("X-Registry-Config")
  85. notVerboseBuffer = bytes.NewBuffer(nil)
  86. )
  87. if authConfigsEncoded != "" {
  88. authConfigsJSON := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authConfigsEncoded))
  89. if err := json.NewDecoder(authConfigsJSON).Decode(&authConfigs); err != nil {
  90. // for a pull it is not an error if no auth was given
  91. // to increase compatibility with the existing api it is defaulting
  92. // to be empty.
  93. }
  94. }
  95. w.Header().Set("Content-Type", "application/json")
  96. output := ioutils.NewWriteFlusher(w)
  97. defer output.Close()
  98. sf := streamformatter.NewJSONStreamFormatter()
  99. errf := func(err error) error {
  100. if httputils.BoolValue(r, "q") && notVerboseBuffer.Len() > 0 {
  101. output.Write(notVerboseBuffer.Bytes())
  102. }
  103. // Do not write the error in the http output if it's still empty.
  104. // This prevents from writing a 200(OK) when there is an internal error.
  105. if !output.Flushed() {
  106. return err
  107. }
  108. _, err = w.Write(sf.FormatError(errors.New(utils.GetErrorMessage(err))))
  109. if err != nil {
  110. logrus.Warnf("could not write error response: %v", err)
  111. }
  112. return nil
  113. }
  114. buildOptions, err := newImageBuildOptions(ctx, r)
  115. if err != nil {
  116. return errf(err)
  117. }
  118. remoteURL := r.FormValue("remote")
  119. // Currently, only used if context is from a remote url.
  120. // Look at code in DetectContextFromRemoteURL for more information.
  121. createProgressReader := func(in io.ReadCloser) io.ReadCloser {
  122. progressOutput := sf.NewProgressOutput(output, true)
  123. if buildOptions.SuppressOutput {
  124. progressOutput = sf.NewProgressOutput(notVerboseBuffer, true)
  125. }
  126. return progress.NewProgressReader(in, progressOutput, r.ContentLength, "Downloading context", remoteURL)
  127. }
  128. var (
  129. context builder.ModifiableContext
  130. dockerfileName string
  131. out io.Writer
  132. )
  133. context, dockerfileName, err = builder.DetectContextFromRemoteURL(r.Body, remoteURL, createProgressReader)
  134. if err != nil {
  135. return errf(err)
  136. }
  137. defer func() {
  138. if err := context.Close(); err != nil {
  139. logrus.Debugf("[BUILDER] failed to remove temporary context: %v", err)
  140. }
  141. }()
  142. if len(dockerfileName) > 0 {
  143. buildOptions.Dockerfile = dockerfileName
  144. }
  145. out = output
  146. if buildOptions.SuppressOutput {
  147. out = notVerboseBuffer
  148. }
  149. stdout := &streamformatter.StdoutFormatter{Writer: out, StreamFormatter: sf}
  150. stderr := &streamformatter.StderrFormatter{Writer: out, StreamFormatter: sf}
  151. closeNotifier := make(<-chan bool)
  152. if notifier, ok := w.(http.CloseNotifier); ok {
  153. closeNotifier = notifier.CloseNotify()
  154. }
  155. imgID, err := br.backend.Build(buildOptions,
  156. builder.DockerIgnoreContext{ModifiableContext: context},
  157. stdout, stderr, out,
  158. closeNotifier)
  159. if err != nil {
  160. return errf(err)
  161. }
  162. // Everything worked so if -q was provided the output from the daemon
  163. // should be just the image ID and we'll print that to stdout.
  164. if buildOptions.SuppressOutput {
  165. stdout := &streamformatter.StdoutFormatter{Writer: output, StreamFormatter: sf}
  166. fmt.Fprintf(stdout, "%s\n", string(imgID))
  167. }
  168. return nil
  169. }