server.go 27 KB

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