server.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. package docker
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/dotcloud/docker/auth"
  6. "github.com/dotcloud/docker/registry"
  7. "github.com/dotcloud/docker/utils"
  8. "io"
  9. "io/ioutil"
  10. "log"
  11. "net/http"
  12. "net/url"
  13. "os"
  14. "path"
  15. "runtime"
  16. "strings"
  17. "sync"
  18. )
  19. func (srv *Server) DockerVersion() APIVersion {
  20. return APIVersion{
  21. Version: VERSION,
  22. GitCommit: GITCOMMIT,
  23. GoVersion: runtime.Version(),
  24. }
  25. }
  26. func (srv *Server) ContainerKill(name string) error {
  27. if container := srv.runtime.Get(name); container != nil {
  28. if err := container.Kill(); err != nil {
  29. return fmt.Errorf("Error restarting container %s: %s", name, err.Error())
  30. }
  31. } else {
  32. return fmt.Errorf("No such container: %s", name)
  33. }
  34. return nil
  35. }
  36. func (srv *Server) ContainerExport(name string, out io.Writer) error {
  37. if container := srv.runtime.Get(name); container != nil {
  38. data, err := container.Export()
  39. if err != nil {
  40. return err
  41. }
  42. // Stream the entire contents of the container (basically a volatile snapshot)
  43. if _, err := io.Copy(out, data); err != nil {
  44. return err
  45. }
  46. return nil
  47. }
  48. return fmt.Errorf("No such container: %s", name)
  49. }
  50. func (srv *Server) ImagesSearch(term string) ([]APISearch, error) {
  51. results, err := registry.NewRegistry(srv.runtime.root, nil).SearchRepositories(term)
  52. if err != nil {
  53. return nil, err
  54. }
  55. var outs []APISearch
  56. for _, repo := range results.Results {
  57. var out APISearch
  58. out.Description = repo["description"]
  59. out.Name = repo["name"]
  60. outs = append(outs, out)
  61. }
  62. return outs, nil
  63. }
  64. func (srv *Server) ImageInsert(name, url, path string, out io.Writer, sf *utils.StreamFormatter) (string, error) {
  65. out = utils.NewWriteFlusher(out)
  66. img, err := srv.runtime.repositories.LookupImage(name)
  67. if err != nil {
  68. return "", err
  69. }
  70. file, err := utils.Download(url, out)
  71. if err != nil {
  72. return "", err
  73. }
  74. defer file.Body.Close()
  75. config, _, err := ParseRun([]string{img.ID, "echo", "insert", url, path}, srv.runtime.capabilities)
  76. if err != nil {
  77. return "", err
  78. }
  79. b := NewBuilder(srv.runtime)
  80. c, err := b.Create(config)
  81. if err != nil {
  82. return "", err
  83. }
  84. if err := c.Inject(utils.ProgressReader(file.Body, int(file.ContentLength), out, sf.FormatProgress("Downloading", "%v/%v (%v)"), sf), path); err != nil {
  85. return "", err
  86. }
  87. // FIXME: Handle custom repo, tag comment, author
  88. img, err = b.Commit(c, "", "", img.Comment, img.Author, nil)
  89. if err != nil {
  90. return "", err
  91. }
  92. out.Write(sf.FormatStatus(img.ID))
  93. return img.ShortID(), nil
  94. }
  95. func (srv *Server) ImagesViz(out io.Writer) error {
  96. images, _ := srv.runtime.graph.All()
  97. if images == nil {
  98. return nil
  99. }
  100. out.Write([]byte("digraph docker {\n"))
  101. var (
  102. parentImage *Image
  103. err error
  104. )
  105. for _, image := range images {
  106. parentImage, err = image.GetParent()
  107. if err != nil {
  108. return fmt.Errorf("Error while getting parent image: %v", err)
  109. }
  110. if parentImage != nil {
  111. out.Write([]byte(" \"" + parentImage.ShortID() + "\" -> \"" + image.ShortID() + "\"\n"))
  112. } else {
  113. out.Write([]byte(" base -> \"" + image.ShortID() + "\" [style=invis]\n"))
  114. }
  115. }
  116. reporefs := make(map[string][]string)
  117. for name, repository := range srv.runtime.repositories.Repositories {
  118. for tag, id := range repository {
  119. reporefs[utils.TruncateID(id)] = append(reporefs[utils.TruncateID(id)], fmt.Sprintf("%s:%s", name, tag))
  120. }
  121. }
  122. for id, repos := range reporefs {
  123. out.Write([]byte(" \"" + id + "\" [label=\"" + id + "\\n" + strings.Join(repos, "\\n") + "\",shape=box,fillcolor=\"paleturquoise\",style=\"filled,rounded\"];\n"))
  124. }
  125. out.Write([]byte(" base [style=invisible]\n}\n"))
  126. return nil
  127. }
  128. func (srv *Server) Images(all bool, filter string) ([]APIImages, error) {
  129. var (
  130. allImages map[string]*Image
  131. err error
  132. )
  133. if all {
  134. allImages, err = srv.runtime.graph.Map()
  135. } else {
  136. allImages, err = srv.runtime.graph.Heads()
  137. }
  138. if err != nil {
  139. return nil, err
  140. }
  141. outs := []APIImages{} //produce [] when empty instead of 'null'
  142. for name, repository := range srv.runtime.repositories.Repositories {
  143. if filter != "" && name != filter {
  144. continue
  145. }
  146. for tag, id := range repository {
  147. var out APIImages
  148. image, err := srv.runtime.graph.Get(id)
  149. if err != nil {
  150. log.Printf("Warning: couldn't load %s from %s/%s: %s", id, name, tag, err)
  151. continue
  152. }
  153. delete(allImages, id)
  154. out.Repository = name
  155. out.Tag = tag
  156. out.ID = image.ID
  157. out.Created = image.Created.Unix()
  158. out.Size = image.Size
  159. out.VirtualSize = image.getParentsSize(0) + image.Size
  160. outs = append(outs, out)
  161. }
  162. }
  163. // Display images which aren't part of a
  164. if filter == "" {
  165. for _, image := range allImages {
  166. var out APIImages
  167. out.ID = image.ID
  168. out.Created = image.Created.Unix()
  169. out.Size = image.Size
  170. out.VirtualSize = image.getParentsSize(0) + image.Size
  171. outs = append(outs, out)
  172. }
  173. }
  174. return outs, nil
  175. }
  176. func (srv *Server) DockerInfo() *APIInfo {
  177. images, _ := srv.runtime.graph.All()
  178. var imgcount int
  179. if images == nil {
  180. imgcount = 0
  181. } else {
  182. imgcount = len(images)
  183. }
  184. return &APIInfo{
  185. Containers: len(srv.runtime.List()),
  186. Images: imgcount,
  187. MemoryLimit: srv.runtime.capabilities.MemoryLimit,
  188. SwapLimit: srv.runtime.capabilities.SwapLimit,
  189. Debug: os.Getenv("DEBUG") != "",
  190. NFd: utils.GetTotalUsedFds(),
  191. NGoroutines: runtime.NumGoroutine(),
  192. }
  193. }
  194. func (srv *Server) ImageHistory(name string) ([]APIHistory, error) {
  195. image, err := srv.runtime.repositories.LookupImage(name)
  196. if err != nil {
  197. return nil, err
  198. }
  199. lookupMap := make(map[string][]string)
  200. for name, repository := range srv.runtime.repositories.Repositories {
  201. for tag, id := range repository {
  202. // If the ID already has a reverse lookup, do not update it unless for "latest"
  203. if _, exists := lookupMap[id]; !exists {
  204. lookupMap[id] = []string{}
  205. }
  206. lookupMap[id] = append(lookupMap[id], name+":"+tag)
  207. }
  208. }
  209. outs := []APIHistory{} //produce [] when empty instead of 'null'
  210. err = image.WalkHistory(func(img *Image) error {
  211. var out APIHistory
  212. out.ID = srv.runtime.repositories.ImageName(img.ShortID())
  213. out.Created = img.Created.Unix()
  214. out.CreatedBy = strings.Join(img.ContainerConfig.Cmd, " ")
  215. out.Tags = lookupMap[img.ID]
  216. outs = append(outs, out)
  217. return nil
  218. })
  219. return outs, nil
  220. }
  221. func (srv *Server) ContainerChanges(name string) ([]Change, error) {
  222. if container := srv.runtime.Get(name); container != nil {
  223. return container.Changes()
  224. }
  225. return nil, fmt.Errorf("No such container: %s", name)
  226. }
  227. func (srv *Server) Containers(all bool, n int, since, before string) []APIContainers {
  228. var foundBefore bool
  229. var displayed int
  230. retContainers := []APIContainers{}
  231. for _, container := range srv.runtime.List() {
  232. if !container.State.Running && !all && n == -1 && since == "" && before == "" {
  233. continue
  234. }
  235. if before != "" {
  236. if container.ShortID() == before {
  237. foundBefore = true
  238. continue
  239. }
  240. if !foundBefore {
  241. continue
  242. }
  243. }
  244. if displayed == n {
  245. break
  246. }
  247. if container.ShortID() == since {
  248. break
  249. }
  250. displayed++
  251. c := APIContainers{
  252. ID: container.ID,
  253. }
  254. c.Image = srv.runtime.repositories.ImageName(container.Image)
  255. c.Command = fmt.Sprintf("%s %s", container.Path, strings.Join(container.Args, " "))
  256. c.Created = container.Created.Unix()
  257. c.Status = container.State.String()
  258. c.Ports = container.NetworkSettings.PortMappingHuman()
  259. c.SizeRw, c.SizeRootFs = container.GetSize()
  260. retContainers = append(retContainers, c)
  261. }
  262. return retContainers
  263. }
  264. func (srv *Server) ContainerCommit(name, repo, tag, author, comment string, config *Config) (string, error) {
  265. container := srv.runtime.Get(name)
  266. if container == nil {
  267. return "", fmt.Errorf("No such container: %s", name)
  268. }
  269. img, err := NewBuilder(srv.runtime).Commit(container, repo, tag, comment, author, config)
  270. if err != nil {
  271. return "", err
  272. }
  273. return img.ShortID(), err
  274. }
  275. func (srv *Server) ContainerTag(name, repo, tag string, force bool) error {
  276. if err := srv.runtime.repositories.Set(repo, tag, name, force); err != nil {
  277. return err
  278. }
  279. return nil
  280. }
  281. func (srv *Server) pullImage(r *registry.Registry, out io.Writer, imgId, endpoint string, token []string, sf *utils.StreamFormatter) error {
  282. history, err := r.GetRemoteHistory(imgId, endpoint, token)
  283. if err != nil {
  284. return err
  285. }
  286. // FIXME: Try to stream the images?
  287. // FIXME: Launch the getRemoteImage() in goroutines
  288. for _, id := range history {
  289. if !srv.runtime.graph.Exists(id) {
  290. out.Write(sf.FormatStatus("Pulling %s metadata", id))
  291. imgJSON, imgSize, err := r.GetRemoteImageJSON(id, endpoint, token)
  292. if err != nil {
  293. // FIXME: Keep goging in case of error?
  294. return err
  295. }
  296. img, err := NewImgJSON(imgJSON)
  297. if err != nil {
  298. return fmt.Errorf("Failed to parse json: %s", err)
  299. }
  300. // Get the layer
  301. out.Write(sf.FormatStatus("Pulling %s fs layer", id))
  302. layer, err := r.GetRemoteImageLayer(img.ID, endpoint, token)
  303. if err != nil {
  304. return err
  305. }
  306. defer layer.Close()
  307. if err := srv.runtime.graph.Register(utils.ProgressReader(layer, imgSize, out, sf.FormatProgress("Downloading", "%v/%v (%v)"), sf), false, img); err != nil {
  308. return err
  309. }
  310. }
  311. }
  312. return nil
  313. }
  314. func (srv *Server) pullRepository(r *registry.Registry, out io.Writer, local, remote, askedTag string, sf *utils.StreamFormatter) error {
  315. out.Write(sf.FormatStatus("Pulling repository %s from %s", local, auth.IndexServerAddress()))
  316. repoData, err := r.GetRepositoryData(remote)
  317. if err != nil {
  318. return err
  319. }
  320. utils.Debugf("Updating checksums")
  321. // Reload the json file to make sure not to overwrite faster sums
  322. if err := srv.runtime.graph.UpdateChecksums(repoData.ImgList); err != nil {
  323. return err
  324. }
  325. utils.Debugf("Retrieving the tag list")
  326. tagsList, err := r.GetRemoteTags(repoData.Endpoints, remote, repoData.Tokens)
  327. if err != nil {
  328. return err
  329. }
  330. utils.Debugf("Registering tags")
  331. // If not specific tag have been asked, take all
  332. if askedTag == "" {
  333. for tag, id := range tagsList {
  334. repoData.ImgList[id].Tag = tag
  335. }
  336. } else {
  337. // Otherwise, check that the tag exists and use only that one
  338. id, exists := tagsList[askedTag]
  339. if !exists {
  340. return fmt.Errorf("Tag %s not found in repositoy %s", askedTag, local)
  341. }
  342. repoData.ImgList[id].Tag = askedTag
  343. }
  344. for _, img := range repoData.ImgList {
  345. if askedTag != "" && img.Tag != askedTag {
  346. utils.Debugf("(%s) does not match %s (id: %s), skipping", img.Tag, askedTag, img.ID)
  347. continue
  348. }
  349. out.Write(sf.FormatStatus("Pulling image %s (%s) from %s", img.ID, img.Tag, remote))
  350. success := false
  351. for _, ep := range repoData.Endpoints {
  352. if err := srv.pullImage(r, out, img.ID, "https://"+ep+"/v1", repoData.Tokens, sf); err != nil {
  353. out.Write(sf.FormatStatus("Error while retrieving image for tag: %s (%s); checking next endpoint", askedTag, err))
  354. continue
  355. }
  356. success = true
  357. break
  358. }
  359. if !success {
  360. return fmt.Errorf("Could not find repository on any of the indexed registries.")
  361. }
  362. }
  363. for tag, id := range tagsList {
  364. if askedTag != "" && tag != askedTag {
  365. continue
  366. }
  367. if err := srv.runtime.repositories.Set(local, tag, id, true); err != nil {
  368. return err
  369. }
  370. }
  371. if err := srv.runtime.repositories.Save(); err != nil {
  372. return err
  373. }
  374. return nil
  375. }
  376. func (srv *Server) poolAdd(kind, key string) error {
  377. srv.lock.Lock()
  378. defer srv.lock.Unlock()
  379. if _, exists := srv.pullingPool[key]; exists {
  380. return fmt.Errorf("%s %s is already in progress", key, kind)
  381. }
  382. switch kind {
  383. case "pull":
  384. srv.pullingPool[key] = struct{}{}
  385. break
  386. case "push":
  387. srv.pushingPool[key] = struct{}{}
  388. break
  389. default:
  390. return fmt.Errorf("Unkown pool type")
  391. }
  392. return nil
  393. }
  394. func (srv *Server) poolRemove(kind, key string) error {
  395. switch kind {
  396. case "pull":
  397. delete(srv.pullingPool, key)
  398. break
  399. case "push":
  400. delete(srv.pushingPool, key)
  401. break
  402. default:
  403. return fmt.Errorf("Unkown pool type")
  404. }
  405. return nil
  406. }
  407. func (srv *Server) ImagePull(name, tag, endpoint string, out io.Writer, sf *utils.StreamFormatter, authConfig *auth.AuthConfig) error {
  408. if err := srv.poolAdd("pull", name+":"+tag); err != nil {
  409. return err
  410. }
  411. defer srv.poolRemove("pull", name+":"+tag)
  412. r := registry.NewRegistry(srv.runtime.root, authConfig)
  413. out = utils.NewWriteFlusher(out)
  414. if endpoint != "" {
  415. if err := srv.pullImage(r, out, name, endpoint, nil, sf); err != nil {
  416. return err
  417. }
  418. return nil
  419. }
  420. remote := name
  421. parts := strings.Split(name, "/")
  422. if len(parts) > 2 {
  423. remote = fmt.Sprintf("src/%s", url.QueryEscape(strings.Join(parts, "/")))
  424. }
  425. if err := srv.pullRepository(r, out, name, remote, tag, sf); err != nil {
  426. return err
  427. }
  428. return nil
  429. }
  430. // Retrieve the checksum of an image
  431. // Priority:
  432. // - Check on the stored checksums
  433. // - Check if the archive exists, if it does not, ask the registry
  434. // - If the archive does exists, process the checksum from it
  435. // - If the archive does not exists and not found on registry, process checksum from layer
  436. func (srv *Server) getChecksum(imageId string) (string, error) {
  437. // FIXME: Use in-memory map instead of reading the file each time
  438. if sums, err := srv.runtime.graph.getStoredChecksums(); err != nil {
  439. return "", err
  440. } else if checksum, exists := sums[imageId]; exists {
  441. return checksum, nil
  442. }
  443. img, err := srv.runtime.graph.Get(imageId)
  444. if err != nil {
  445. return "", err
  446. }
  447. if _, err := os.Stat(layerArchivePath(srv.runtime.graph.imageRoot(imageId))); err != nil {
  448. if os.IsNotExist(err) {
  449. // TODO: Ask the registry for the checksum
  450. // As the archive is not there, it is supposed to come from a pull.
  451. } else {
  452. return "", err
  453. }
  454. }
  455. checksum, err := img.Checksum()
  456. if err != nil {
  457. return "", err
  458. }
  459. return checksum, nil
  460. }
  461. // Retrieve the all the images to be uploaded in the correct order
  462. // Note: we can't use a map as it is not ordered
  463. func (srv *Server) getImageList(localRepo map[string]string) ([]*registry.ImgData, error) {
  464. var imgList []*registry.ImgData
  465. imageSet := make(map[string]struct{})
  466. for tag, id := range localRepo {
  467. img, err := srv.runtime.graph.Get(id)
  468. if err != nil {
  469. return nil, err
  470. }
  471. img.WalkHistory(func(img *Image) error {
  472. if _, exists := imageSet[img.ID]; exists {
  473. return nil
  474. }
  475. imageSet[img.ID] = struct{}{}
  476. checksum, err := srv.getChecksum(img.ID)
  477. if err != nil {
  478. return err
  479. }
  480. imgList = append([]*registry.ImgData{{
  481. ID: img.ID,
  482. Checksum: checksum,
  483. Tag: tag,
  484. }}, imgList...)
  485. return nil
  486. })
  487. }
  488. return imgList, nil
  489. }
  490. func (srv *Server) pushRepository(r *registry.Registry, out io.Writer, name string, localRepo map[string]string, sf *utils.StreamFormatter) error {
  491. out = utils.NewWriteFlusher(out)
  492. out.Write(sf.FormatStatus("Processing checksums"))
  493. imgList, err := srv.getImageList(localRepo)
  494. if err != nil {
  495. return err
  496. }
  497. out.Write(sf.FormatStatus("Sending image list"))
  498. srvName := name
  499. parts := strings.Split(name, "/")
  500. if len(parts) > 2 {
  501. srvName = fmt.Sprintf("src/%s", url.QueryEscape(strings.Join(parts, "/")))
  502. }
  503. repoData, err := r.PushImageJSONIndex(srvName, imgList, false, nil)
  504. if err != nil {
  505. return err
  506. }
  507. for _, ep := range repoData.Endpoints {
  508. out.Write(sf.FormatStatus("Pushing repository %s to %s (%d tags)", name, ep, len(localRepo)))
  509. // For each image within the repo, push them
  510. for _, elem := range imgList {
  511. if _, exists := repoData.ImgList[elem.ID]; exists {
  512. out.Write(sf.FormatStatus("Image %s already on registry, skipping", name))
  513. continue
  514. }
  515. if err := srv.pushImage(r, out, name, elem.ID, ep, repoData.Tokens, sf); err != nil {
  516. // FIXME: Continue on error?
  517. return err
  518. }
  519. out.Write(sf.FormatStatus("Pushing tags for rev [%s] on {%s}", elem.ID, ep+"/repositories/"+srvName+"/tags/"+elem.Tag))
  520. if err := r.PushRegistryTag(srvName, elem.ID, elem.Tag, ep, repoData.Tokens); err != nil {
  521. return err
  522. }
  523. }
  524. }
  525. if _, err := r.PushImageJSONIndex(srvName, imgList, true, repoData.Endpoints); err != nil {
  526. return err
  527. }
  528. return nil
  529. }
  530. func (srv *Server) pushImage(r *registry.Registry, out io.Writer, remote, imgId, ep string, token []string, sf *utils.StreamFormatter) error {
  531. out = utils.NewWriteFlusher(out)
  532. jsonRaw, err := ioutil.ReadFile(path.Join(srv.runtime.graph.Root, imgId, "json"))
  533. if err != nil {
  534. return fmt.Errorf("Error while retreiving the path for {%s}: %s", imgId, err)
  535. }
  536. out.Write(sf.FormatStatus("Pushing %s", imgId))
  537. // Make sure we have the image's checksum
  538. checksum, err := srv.getChecksum(imgId)
  539. if err != nil {
  540. return err
  541. }
  542. imgData := &registry.ImgData{
  543. ID: imgId,
  544. Checksum: checksum,
  545. }
  546. // Send the json
  547. if err := r.PushImageJSONRegistry(imgData, jsonRaw, ep, token); err != nil {
  548. if err == registry.ErrAlreadyExists {
  549. out.Write(sf.FormatStatus("Image %s already uploaded ; skipping", imgData.ID))
  550. return nil
  551. }
  552. return err
  553. }
  554. // Retrieve the tarball to be sent
  555. var layerData *TempArchive
  556. // If the archive exists, use it
  557. file, err := os.Open(layerArchivePath(srv.runtime.graph.imageRoot(imgId)))
  558. if err != nil {
  559. if os.IsNotExist(err) {
  560. // If the archive does not exist, create one from the layer
  561. layerData, err = srv.runtime.graph.TempLayerArchive(imgId, Xz, out)
  562. if err != nil {
  563. return fmt.Errorf("Failed to generate layer archive: %s", err)
  564. }
  565. } else {
  566. return err
  567. }
  568. } else {
  569. defer file.Close()
  570. st, err := file.Stat()
  571. if err != nil {
  572. return err
  573. }
  574. layerData = &TempArchive{
  575. File: file,
  576. Size: st.Size(),
  577. }
  578. }
  579. // Send the layer
  580. if err := r.PushImageLayerRegistry(imgData.ID, utils.ProgressReader(layerData, int(layerData.Size), out, sf.FormatProgress("Pushing", "%v/%v (%v)"), sf), ep, token); err != nil {
  581. return err
  582. }
  583. return nil
  584. }
  585. // FIXME: Allow to interupt current push when new push of same image is done.
  586. func (srv *Server) ImagePush(name, endpoint string, out io.Writer, sf *utils.StreamFormatter, authConfig *auth.AuthConfig) error {
  587. if err := srv.poolAdd("push", name); err != nil {
  588. return err
  589. }
  590. defer srv.poolRemove("push", name)
  591. out = utils.NewWriteFlusher(out)
  592. img, err := srv.runtime.graph.Get(name)
  593. r := registry.NewRegistry(srv.runtime.root, authConfig)
  594. if err != nil {
  595. out.Write(sf.FormatStatus("The push refers to a repository [%s] (len: %d)", name, len(srv.runtime.repositories.Repositories[name])))
  596. // If it fails, try to get the repository
  597. if localRepo, exists := srv.runtime.repositories.Repositories[name]; exists {
  598. if err := srv.pushRepository(r, out, name, localRepo, sf); err != nil {
  599. return err
  600. }
  601. return nil
  602. }
  603. return err
  604. }
  605. out.Write(sf.FormatStatus("The push refers to an image: [%s]", name))
  606. if err := srv.pushImage(r, out, name, img.ID, endpoint, nil, sf); err != nil {
  607. return err
  608. }
  609. return nil
  610. }
  611. func (srv *Server) ImageImport(src, repo, tag string, in io.Reader, out io.Writer, sf *utils.StreamFormatter) error {
  612. var archive io.Reader
  613. var resp *http.Response
  614. if src == "-" {
  615. archive = in
  616. } else {
  617. u, err := url.Parse(src)
  618. if err != nil {
  619. return err
  620. }
  621. if u.Scheme == "" {
  622. u.Scheme = "http"
  623. u.Host = src
  624. u.Path = ""
  625. }
  626. out.Write(sf.FormatStatus("Downloading from %s", u))
  627. // Download with curl (pretty progress bar)
  628. // If curl is not available, fallback to http.Get()
  629. resp, err = utils.Download(u.String(), out)
  630. if err != nil {
  631. return err
  632. }
  633. archive = utils.ProgressReader(resp.Body, int(resp.ContentLength), out, sf.FormatProgress("Importing", "%v/%v (%v)"), sf)
  634. }
  635. img, err := srv.runtime.graph.Create(archive, nil, "Imported from "+src, "", nil)
  636. if err != nil {
  637. return err
  638. }
  639. // Optionally register the image at REPO/TAG
  640. if repo != "" {
  641. if err := srv.runtime.repositories.Set(repo, tag, img.ID, true); err != nil {
  642. return err
  643. }
  644. }
  645. out.Write(sf.FormatStatus(img.ShortID()))
  646. return nil
  647. }
  648. func (srv *Server) ContainerCreate(config *Config) (string, error) {
  649. if config.Memory != 0 && config.Memory < 524288 {
  650. return "", fmt.Errorf("Memory limit must be given in bytes (minimum 524288 bytes)")
  651. }
  652. if config.Memory > 0 && !srv.runtime.capabilities.MemoryLimit {
  653. config.Memory = 0
  654. }
  655. if config.Memory > 0 && !srv.runtime.capabilities.SwapLimit {
  656. config.MemorySwap = -1
  657. }
  658. b := NewBuilder(srv.runtime)
  659. container, err := b.Create(config)
  660. if err != nil {
  661. if srv.runtime.graph.IsNotExist(err) {
  662. return "", fmt.Errorf("No such image: %s", config.Image)
  663. }
  664. return "", err
  665. }
  666. return container.ShortID(), nil
  667. }
  668. func (srv *Server) ContainerRestart(name string, t int) error {
  669. if container := srv.runtime.Get(name); container != nil {
  670. if err := container.Restart(t); err != nil {
  671. return fmt.Errorf("Error restarting container %s: %s", name, err.Error())
  672. }
  673. } else {
  674. return fmt.Errorf("No such container: %s", name)
  675. }
  676. return nil
  677. }
  678. func (srv *Server) ContainerDestroy(name string, removeVolume bool) error {
  679. if container := srv.runtime.Get(name); container != nil {
  680. volumes := make(map[string]struct{})
  681. // Store all the deleted containers volumes
  682. for _, volumeId := range container.Volumes {
  683. volumes[volumeId] = struct{}{}
  684. }
  685. if err := srv.runtime.Destroy(container); err != nil {
  686. return fmt.Errorf("Error destroying container %s: %s", name, err.Error())
  687. }
  688. if removeVolume {
  689. // Retrieve all volumes from all remaining containers
  690. usedVolumes := make(map[string]*Container)
  691. for _, container := range srv.runtime.List() {
  692. for _, containerVolumeId := range container.Volumes {
  693. usedVolumes[containerVolumeId] = container
  694. }
  695. }
  696. for volumeId := range volumes {
  697. // If the requested volu
  698. if c, exists := usedVolumes[volumeId]; exists {
  699. log.Printf("The volume %s is used by the container %s. Impossible to remove it. Skipping.\n", volumeId, c.ID)
  700. continue
  701. }
  702. if err := srv.runtime.volumes.Delete(volumeId); err != nil {
  703. return err
  704. }
  705. }
  706. }
  707. } else {
  708. return fmt.Errorf("No such container: %s", name)
  709. }
  710. return nil
  711. }
  712. var ErrImageReferenced = errors.New("Image referenced by a repository")
  713. func (srv *Server) deleteImageAndChildren(id string, imgs *[]APIRmi) error {
  714. // If the image is referenced by a repo, do not delete
  715. if len(srv.runtime.repositories.ByID()[id]) != 0 {
  716. return ErrImageReferenced
  717. }
  718. // If the image is not referenced but has children, go recursive
  719. referenced := false
  720. byParents, err := srv.runtime.graph.ByParent()
  721. if err != nil {
  722. return err
  723. }
  724. for _, img := range byParents[id] {
  725. if err := srv.deleteImageAndChildren(img.ID, imgs); err != nil {
  726. if err != ErrImageReferenced {
  727. return err
  728. }
  729. referenced = true
  730. }
  731. }
  732. if referenced {
  733. return ErrImageReferenced
  734. }
  735. // If the image is not referenced and has no children, remove it
  736. byParents, err = srv.runtime.graph.ByParent()
  737. if err != nil {
  738. return err
  739. }
  740. if len(byParents[id]) == 0 {
  741. if err := srv.runtime.repositories.DeleteAll(id); err != nil {
  742. return err
  743. }
  744. err := srv.runtime.graph.Delete(id)
  745. if err != nil {
  746. return err
  747. }
  748. *imgs = append(*imgs, APIRmi{Deleted: utils.TruncateID(id)})
  749. return nil
  750. }
  751. return nil
  752. }
  753. func (srv *Server) deleteImageParents(img *Image, imgs *[]APIRmi) error {
  754. if img.Parent != "" {
  755. parent, err := srv.runtime.graph.Get(img.Parent)
  756. if err != nil {
  757. return err
  758. }
  759. // Remove all children images
  760. if err := srv.deleteImageAndChildren(img.Parent, imgs); err != nil {
  761. return err
  762. }
  763. return srv.deleteImageParents(parent, imgs)
  764. }
  765. return nil
  766. }
  767. func (srv *Server) deleteImage(img *Image, repoName, tag string) (*[]APIRmi, error) {
  768. //Untag the current image
  769. var imgs []APIRmi
  770. tagDeleted, err := srv.runtime.repositories.Delete(repoName, tag)
  771. if err != nil {
  772. return nil, err
  773. }
  774. if tagDeleted {
  775. imgs = append(imgs, APIRmi{Untagged: img.ShortID()})
  776. }
  777. if len(srv.runtime.repositories.ByID()[img.ID]) == 0 {
  778. if err := srv.deleteImageAndChildren(img.ID, &imgs); err != nil {
  779. if err != ErrImageReferenced {
  780. return &imgs, err
  781. }
  782. } else if err := srv.deleteImageParents(img, &imgs); err != nil {
  783. if err != ErrImageReferenced {
  784. return &imgs, err
  785. }
  786. }
  787. }
  788. return &imgs, nil
  789. }
  790. func (srv *Server) ImageDelete(name string, autoPrune bool) (*[]APIRmi, error) {
  791. img, err := srv.runtime.repositories.LookupImage(name)
  792. if err != nil {
  793. return nil, fmt.Errorf("No such image: %s", name)
  794. }
  795. if !autoPrune {
  796. if err := srv.runtime.graph.Delete(img.ID); err != nil {
  797. return nil, fmt.Errorf("Error deleting image %s: %s", name, err.Error())
  798. }
  799. return nil, nil
  800. }
  801. var tag string
  802. if strings.Contains(name, ":") {
  803. nameParts := strings.Split(name, ":")
  804. name = nameParts[0]
  805. tag = nameParts[1]
  806. }
  807. return srv.deleteImage(img, name, tag)
  808. }
  809. func (srv *Server) ImageGetCached(imgId string, config *Config) (*Image, error) {
  810. // Retrieve all images
  811. images, err := srv.runtime.graph.All()
  812. if err != nil {
  813. return nil, err
  814. }
  815. // Store the tree in a map of map (map[parentId][childId])
  816. imageMap := make(map[string]map[string]struct{})
  817. for _, img := range images {
  818. if _, exists := imageMap[img.Parent]; !exists {
  819. imageMap[img.Parent] = make(map[string]struct{})
  820. }
  821. imageMap[img.Parent][img.ID] = struct{}{}
  822. }
  823. // Loop on the children of the given image and check the config
  824. for elem := range imageMap[imgId] {
  825. img, err := srv.runtime.graph.Get(elem)
  826. if err != nil {
  827. return nil, err
  828. }
  829. if CompareConfig(&img.ContainerConfig, config) {
  830. return img, nil
  831. }
  832. }
  833. return nil, nil
  834. }
  835. func (srv *Server) ContainerStart(name string) error {
  836. if container := srv.runtime.Get(name); container != nil {
  837. if err := container.Start(); err != nil {
  838. return fmt.Errorf("Error starting container %s: %s", name, err.Error())
  839. }
  840. } else {
  841. return fmt.Errorf("No such container: %s", name)
  842. }
  843. return nil
  844. }
  845. func (srv *Server) ContainerStop(name string, t int) error {
  846. if container := srv.runtime.Get(name); container != nil {
  847. if err := container.Stop(t); err != nil {
  848. return fmt.Errorf("Error stopping container %s: %s", name, err.Error())
  849. }
  850. } else {
  851. return fmt.Errorf("No such container: %s", name)
  852. }
  853. return nil
  854. }
  855. func (srv *Server) ContainerWait(name string) (int, error) {
  856. if container := srv.runtime.Get(name); container != nil {
  857. return container.Wait(), nil
  858. }
  859. return 0, fmt.Errorf("No such container: %s", name)
  860. }
  861. func (srv *Server) ContainerResize(name string, h, w int) error {
  862. if container := srv.runtime.Get(name); container != nil {
  863. return container.Resize(h, w)
  864. }
  865. return fmt.Errorf("No such container: %s", name)
  866. }
  867. func (srv *Server) ContainerAttach(name string, logs, stream, stdin, stdout, stderr bool, in io.ReadCloser, out io.Writer) error {
  868. container := srv.runtime.Get(name)
  869. if container == nil {
  870. return fmt.Errorf("No such container: %s", name)
  871. }
  872. //logs
  873. if logs {
  874. if stdout {
  875. cLog, err := container.ReadLog("stdout")
  876. if err != nil {
  877. utils.Debugf(err.Error())
  878. } else if _, err := io.Copy(out, cLog); err != nil {
  879. utils.Debugf(err.Error())
  880. }
  881. }
  882. if stderr {
  883. cLog, err := container.ReadLog("stderr")
  884. if err != nil {
  885. utils.Debugf(err.Error())
  886. } else if _, err := io.Copy(out, cLog); err != nil {
  887. utils.Debugf(err.Error())
  888. }
  889. }
  890. }
  891. //stream
  892. if stream {
  893. if container.State.Ghost {
  894. return fmt.Errorf("Impossible to attach to a ghost container")
  895. }
  896. var (
  897. cStdin io.ReadCloser
  898. cStdout, cStderr io.Writer
  899. cStdinCloser io.Closer
  900. )
  901. if stdin {
  902. r, w := io.Pipe()
  903. go func() {
  904. defer w.Close()
  905. defer utils.Debugf("Closing buffered stdin pipe")
  906. io.Copy(w, in)
  907. }()
  908. cStdin = r
  909. cStdinCloser = in
  910. }
  911. if stdout {
  912. cStdout = out
  913. }
  914. if stderr {
  915. cStderr = out
  916. }
  917. <-container.Attach(cStdin, cStdinCloser, cStdout, cStderr)
  918. // If we are in stdinonce mode, wait for the process to end
  919. // otherwise, simply return
  920. if container.Config.StdinOnce && !container.Config.Tty {
  921. container.Wait()
  922. }
  923. }
  924. return nil
  925. }
  926. func (srv *Server) ContainerInspect(name string) (*Container, error) {
  927. if container := srv.runtime.Get(name); container != nil {
  928. return container, nil
  929. }
  930. return nil, fmt.Errorf("No such container: %s", name)
  931. }
  932. func (srv *Server) ImageInspect(name string) (*Image, error) {
  933. if image, err := srv.runtime.repositories.LookupImage(name); err == nil && image != nil {
  934. return image, nil
  935. }
  936. return nil, fmt.Errorf("No such image: %s", name)
  937. }
  938. func NewServer(autoRestart, enableCors bool, dns ListOpts) (*Server, error) {
  939. if runtime.GOARCH != "amd64" {
  940. log.Fatalf("The docker runtime currently only supports amd64 (not %s). This will change in the future. Aborting.", runtime.GOARCH)
  941. }
  942. runtime, err := NewRuntime(autoRestart, dns)
  943. if err != nil {
  944. return nil, err
  945. }
  946. srv := &Server{
  947. runtime: runtime,
  948. enableCors: enableCors,
  949. lock: &sync.Mutex{},
  950. pullingPool: make(map[string]struct{}),
  951. pushingPool: make(map[string]struct{}),
  952. }
  953. runtime.srv = srv
  954. return srv, nil
  955. }
  956. type Server struct {
  957. runtime *Runtime
  958. enableCors bool
  959. lock *sync.Mutex
  960. pullingPool map[string]struct{}
  961. pushingPool map[string]struct{}
  962. }