image.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. package local
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "strings"
  10. "github.com/docker/distribution/digest"
  11. "github.com/docker/docker/api/server/httputils"
  12. "github.com/docker/docker/builder/dockerfile"
  13. derr "github.com/docker/docker/errors"
  14. "github.com/docker/docker/pkg/ioutils"
  15. "github.com/docker/docker/pkg/streamformatter"
  16. "github.com/docker/docker/reference"
  17. "github.com/docker/docker/runconfig"
  18. "github.com/docker/engine-api/types"
  19. "github.com/docker/engine-api/types/container"
  20. "golang.org/x/net/context"
  21. )
  22. func (s *router) postCommit(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  23. if err := httputils.ParseForm(r); err != nil {
  24. return err
  25. }
  26. if err := httputils.CheckForJSON(r); err != nil {
  27. return err
  28. }
  29. cname := r.Form.Get("container")
  30. pause := httputils.BoolValue(r, "pause")
  31. version := httputils.VersionFromContext(ctx)
  32. if r.FormValue("pause") == "" && version.GreaterThanOrEqualTo("1.13") {
  33. pause = true
  34. }
  35. c, _, err := runconfig.DecodeContainerConfig(r.Body)
  36. if err != nil && err != io.EOF { //Do not fail if body is empty.
  37. return err
  38. }
  39. if c == nil {
  40. c = &container.Config{}
  41. }
  42. if !s.daemon.Exists(cname) {
  43. return derr.ErrorCodeNoSuchContainer.WithArgs(cname)
  44. }
  45. newConfig, err := dockerfile.BuildFromConfig(c, r.Form["changes"])
  46. if err != nil {
  47. return err
  48. }
  49. commitCfg := &types.ContainerCommitConfig{
  50. Pause: pause,
  51. Repo: r.Form.Get("repo"),
  52. Tag: r.Form.Get("tag"),
  53. Author: r.Form.Get("author"),
  54. Comment: r.Form.Get("comment"),
  55. Config: newConfig,
  56. MergeConfigs: true,
  57. }
  58. imgID, err := s.daemon.Commit(cname, commitCfg)
  59. if err != nil {
  60. return err
  61. }
  62. return httputils.WriteJSON(w, http.StatusCreated, &types.ContainerCommitResponse{
  63. ID: string(imgID),
  64. })
  65. }
  66. // Creates an image from Pull or from Import
  67. func (s *router) postImagesCreate(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  68. if err := httputils.ParseForm(r); err != nil {
  69. return err
  70. }
  71. var (
  72. image = r.Form.Get("fromImage")
  73. repo = r.Form.Get("repo")
  74. tag = r.Form.Get("tag")
  75. message = r.Form.Get("message")
  76. )
  77. authEncoded := r.Header.Get("X-Registry-Auth")
  78. authConfig := &types.AuthConfig{}
  79. if authEncoded != "" {
  80. authJSON := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authEncoded))
  81. if err := json.NewDecoder(authJSON).Decode(authConfig); err != nil {
  82. // for a pull it is not an error if no auth was given
  83. // to increase compatibility with the existing api it is defaulting to be empty
  84. authConfig = &types.AuthConfig{}
  85. }
  86. }
  87. var (
  88. err error
  89. output = ioutils.NewWriteFlusher(w)
  90. )
  91. defer output.Close()
  92. w.Header().Set("Content-Type", "application/json")
  93. if image != "" { //pull
  94. // Special case: "pull -a" may send an image name with a
  95. // trailing :. This is ugly, but let's not break API
  96. // compatibility.
  97. image = strings.TrimSuffix(image, ":")
  98. var ref reference.Named
  99. ref, err = reference.ParseNamed(image)
  100. if err == nil {
  101. if tag != "" {
  102. // The "tag" could actually be a digest.
  103. var dgst digest.Digest
  104. dgst, err = digest.ParseDigest(tag)
  105. if err == nil {
  106. ref, err = reference.WithDigest(ref, dgst)
  107. } else {
  108. ref, err = reference.WithTag(ref, tag)
  109. }
  110. }
  111. if err == nil {
  112. metaHeaders := map[string][]string{}
  113. for k, v := range r.Header {
  114. if strings.HasPrefix(k, "X-Meta-") {
  115. metaHeaders[k] = v
  116. }
  117. }
  118. err = s.daemon.PullImage(ref, metaHeaders, authConfig, output)
  119. }
  120. }
  121. } else { //import
  122. var newRef reference.Named
  123. if repo != "" {
  124. var err error
  125. newRef, err = reference.ParseNamed(repo)
  126. if err != nil {
  127. return err
  128. }
  129. if _, isCanonical := newRef.(reference.Canonical); isCanonical {
  130. return errors.New("cannot import digest reference")
  131. }
  132. if tag != "" {
  133. newRef, err = reference.WithTag(newRef, tag)
  134. if err != nil {
  135. return err
  136. }
  137. }
  138. }
  139. src := r.Form.Get("fromSrc")
  140. // 'err' MUST NOT be defined within this block, we need any error
  141. // generated from the download to be available to the output
  142. // stream processing below
  143. var newConfig *container.Config
  144. newConfig, err = dockerfile.BuildFromConfig(&container.Config{}, r.Form["changes"])
  145. if err != nil {
  146. return err
  147. }
  148. err = s.daemon.ImportImage(src, newRef, message, r.Body, output, newConfig)
  149. }
  150. if err != nil {
  151. if !output.Flushed() {
  152. return err
  153. }
  154. sf := streamformatter.NewJSONStreamFormatter()
  155. output.Write(sf.FormatError(err))
  156. }
  157. return nil
  158. }
  159. func (s *router) postImagesPush(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  160. metaHeaders := map[string][]string{}
  161. for k, v := range r.Header {
  162. if strings.HasPrefix(k, "X-Meta-") {
  163. metaHeaders[k] = v
  164. }
  165. }
  166. if err := httputils.ParseForm(r); err != nil {
  167. return err
  168. }
  169. authConfig := &types.AuthConfig{}
  170. authEncoded := r.Header.Get("X-Registry-Auth")
  171. if authEncoded != "" {
  172. // the new format is to handle the authConfig as a header
  173. authJSON := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authEncoded))
  174. if err := json.NewDecoder(authJSON).Decode(authConfig); err != nil {
  175. // to increase compatibility to existing api it is defaulting to be empty
  176. authConfig = &types.AuthConfig{}
  177. }
  178. } else {
  179. // the old format is supported for compatibility if there was no authConfig header
  180. if err := json.NewDecoder(r.Body).Decode(authConfig); err != nil {
  181. return fmt.Errorf("Bad parameters and missing X-Registry-Auth: %v", err)
  182. }
  183. }
  184. ref, err := reference.ParseNamed(vars["name"])
  185. if err != nil {
  186. return err
  187. }
  188. tag := r.Form.Get("tag")
  189. if tag != "" {
  190. // Push by digest is not supported, so only tags are supported.
  191. ref, err = reference.WithTag(ref, tag)
  192. if err != nil {
  193. return err
  194. }
  195. }
  196. output := ioutils.NewWriteFlusher(w)
  197. defer output.Close()
  198. w.Header().Set("Content-Type", "application/json")
  199. if err := s.daemon.PushImage(ref, metaHeaders, authConfig, output); err != nil {
  200. if !output.Flushed() {
  201. return err
  202. }
  203. sf := streamformatter.NewJSONStreamFormatter()
  204. output.Write(sf.FormatError(err))
  205. }
  206. return nil
  207. }
  208. func (s *router) getImagesGet(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  209. if err := httputils.ParseForm(r); err != nil {
  210. return err
  211. }
  212. w.Header().Set("Content-Type", "application/x-tar")
  213. output := ioutils.NewWriteFlusher(w)
  214. defer output.Close()
  215. var names []string
  216. if name, ok := vars["name"]; ok {
  217. names = []string{name}
  218. } else {
  219. names = r.Form["names"]
  220. }
  221. if err := s.daemon.ExportImage(names, output); err != nil {
  222. if !output.Flushed() {
  223. return err
  224. }
  225. sf := streamformatter.NewJSONStreamFormatter()
  226. output.Write(sf.FormatError(err))
  227. }
  228. return nil
  229. }
  230. func (s *router) postImagesLoad(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  231. return s.daemon.LoadImage(r.Body, w)
  232. }
  233. func (s *router) deleteImages(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  234. if err := httputils.ParseForm(r); err != nil {
  235. return err
  236. }
  237. name := vars["name"]
  238. if strings.TrimSpace(name) == "" {
  239. return fmt.Errorf("image name cannot be blank")
  240. }
  241. force := httputils.BoolValue(r, "force")
  242. prune := !httputils.BoolValue(r, "noprune")
  243. list, err := s.daemon.ImageDelete(name, force, prune)
  244. if err != nil {
  245. return err
  246. }
  247. return httputils.WriteJSON(w, http.StatusOK, list)
  248. }
  249. func (s *router) getImagesByName(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  250. imageInspect, err := s.daemon.LookupImage(vars["name"])
  251. if err != nil {
  252. return err
  253. }
  254. return httputils.WriteJSON(w, http.StatusOK, imageInspect)
  255. }
  256. func (s *router) getImagesJSON(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  257. if err := httputils.ParseForm(r); err != nil {
  258. return err
  259. }
  260. // FIXME: The filter parameter could just be a match filter
  261. images, err := s.daemon.Images(r.Form.Get("filters"), r.Form.Get("filter"), httputils.BoolValue(r, "all"))
  262. if err != nil {
  263. return err
  264. }
  265. return httputils.WriteJSON(w, http.StatusOK, images)
  266. }
  267. func (s *router) getImagesHistory(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  268. name := vars["name"]
  269. history, err := s.daemon.ImageHistory(name)
  270. if err != nil {
  271. return err
  272. }
  273. return httputils.WriteJSON(w, http.StatusOK, history)
  274. }
  275. func (s *router) postImagesTag(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  276. if err := httputils.ParseForm(r); err != nil {
  277. return err
  278. }
  279. repo := r.Form.Get("repo")
  280. tag := r.Form.Get("tag")
  281. newTag, err := reference.WithName(repo)
  282. if err != nil {
  283. return err
  284. }
  285. if tag != "" {
  286. if newTag, err = reference.WithTag(newTag, tag); err != nil {
  287. return err
  288. }
  289. }
  290. if err := s.daemon.TagImage(newTag, vars["name"]); err != nil {
  291. return err
  292. }
  293. w.WriteHeader(http.StatusCreated)
  294. return nil
  295. }
  296. func (s *router) getImagesSearch(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  297. if err := httputils.ParseForm(r); err != nil {
  298. return err
  299. }
  300. var (
  301. config *types.AuthConfig
  302. authEncoded = r.Header.Get("X-Registry-Auth")
  303. headers = map[string][]string{}
  304. )
  305. if authEncoded != "" {
  306. authJSON := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authEncoded))
  307. if err := json.NewDecoder(authJSON).Decode(&config); err != nil {
  308. // for a search it is not an error if no auth was given
  309. // to increase compatibility with the existing api it is defaulting to be empty
  310. config = &types.AuthConfig{}
  311. }
  312. }
  313. for k, v := range r.Header {
  314. if strings.HasPrefix(k, "X-Meta-") {
  315. headers[k] = v
  316. }
  317. }
  318. query, err := s.daemon.SearchRegistryForImages(r.Form.Get("term"), config, headers)
  319. if err != nil {
  320. return err
  321. }
  322. return httputils.WriteJSON(w, http.StatusOK, query.Results)
  323. }