server.go 30 KB

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