server.go 27 KB

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