graph.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. package docker
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path"
  7. "path/filepath"
  8. "strings"
  9. "time"
  10. )
  11. type Graph struct {
  12. Root string
  13. }
  14. func NewGraph(root string) (*Graph, error) {
  15. abspath, err := filepath.Abs(root)
  16. if err != nil {
  17. return nil, err
  18. }
  19. // Create the root directory if it doesn't exists
  20. if err := os.Mkdir(root, 0700); err != nil && !os.IsExist(err) {
  21. return nil, err
  22. }
  23. return &Graph{
  24. Root: abspath,
  25. }, nil
  26. }
  27. // FIXME: Implement error subclass instead of looking at the error text
  28. // Note: This is the way golang implements os.IsNotExists on Plan9
  29. func (graph *Graph) IsNotExist(err error) bool {
  30. return err != nil && strings.Contains(err.Error(), "does not exist")
  31. }
  32. func (graph *Graph) Exists(id string) bool {
  33. if _, err := graph.Get(id); err != nil {
  34. return false
  35. }
  36. return true
  37. }
  38. func (graph *Graph) Get(id string) (*Image, error) {
  39. // FIXME: return nil when the image doesn't exist, instead of an error
  40. img, err := LoadImage(graph.imageRoot(id))
  41. if err != nil {
  42. return nil, err
  43. }
  44. if img.Id != id {
  45. return nil, fmt.Errorf("Image stored at '%s' has wrong id '%s'", id, img.Id)
  46. }
  47. img.graph = graph
  48. return img, nil
  49. }
  50. func (graph *Graph) Create(layerData Archive, container *Container, comment string) (*Image, error) {
  51. img := &Image{
  52. Id: GenerateId(),
  53. Comment: comment,
  54. Created: time.Now(),
  55. }
  56. if container != nil {
  57. img.Parent = container.Image
  58. img.Container = container.Id
  59. img.ContainerConfig = *container.Config
  60. }
  61. if err := graph.Register(layerData, img); err != nil {
  62. return nil, err
  63. }
  64. return img, nil
  65. }
  66. func (graph *Graph) Register(layerData Archive, img *Image) error {
  67. if err := ValidateId(img.Id); err != nil {
  68. return err
  69. }
  70. // (This is a convenience to save time. Race conditions are taken care of by os.Rename)
  71. if graph.Exists(img.Id) {
  72. return fmt.Errorf("Image %s already exists", img.Id)
  73. }
  74. tmp, err := graph.Mktemp(img.Id)
  75. defer os.RemoveAll(tmp)
  76. if err != nil {
  77. return fmt.Errorf("Mktemp failed: %s", err)
  78. }
  79. if err := StoreImage(img, layerData, tmp); err != nil {
  80. return err
  81. }
  82. // Commit
  83. if err := os.Rename(tmp, graph.imageRoot(img.Id)); err != nil {
  84. return err
  85. }
  86. img.graph = graph
  87. return nil
  88. }
  89. func (graph *Graph) Mktemp(id string) (string, error) {
  90. tmp, err := NewGraph(path.Join(graph.Root, ":tmp:"))
  91. if err != nil {
  92. return "", fmt.Errorf("Couldn't create temp: %s", err)
  93. }
  94. if tmp.Exists(id) {
  95. return "", fmt.Errorf("Image %d already exists", id)
  96. }
  97. return tmp.imageRoot(id), nil
  98. }
  99. func (graph *Graph) Garbage() (*Graph, error) {
  100. return NewGraph(path.Join(graph.Root, ":garbage:"))
  101. }
  102. // Check if given error is "not empty"
  103. // Note: this is the way golang do it internally with os.IsNotExists
  104. func isNotEmpty(err error) bool {
  105. switch pe := err.(type) {
  106. case nil:
  107. return false
  108. case *os.PathError:
  109. err = pe.Err
  110. case *os.LinkError:
  111. err = pe.Err
  112. }
  113. return strings.Contains(err.Error(), " not empty")
  114. }
  115. func (graph *Graph) Delete(id string) error {
  116. garbage, err := graph.Garbage()
  117. if err != nil {
  118. return err
  119. }
  120. err = os.Rename(graph.imageRoot(id), garbage.imageRoot(id))
  121. if err != nil {
  122. if isNotEmpty(err) {
  123. Debugf("The image %s is already present in garbage. Removing it.", id)
  124. if err = os.RemoveAll(garbage.imageRoot(id)); err != nil {
  125. Debugf("Error while removing the image %s from garbage: %s\n", id, err)
  126. return err
  127. }
  128. Debugf("Image %s removed from garbage", id)
  129. if err = os.Rename(graph.imageRoot(id), garbage.imageRoot(id)); err != nil {
  130. return err
  131. }
  132. Debugf("Image %s put in the garbage", id)
  133. } else {
  134. Debugf("Error putting the image %s to garbage: %s\n", id, err)
  135. }
  136. return err
  137. }
  138. return nil
  139. }
  140. func (graph *Graph) Undelete(id string) error {
  141. garbage, err := graph.Garbage()
  142. if err != nil {
  143. return err
  144. }
  145. return os.Rename(garbage.imageRoot(id), graph.imageRoot(id))
  146. }
  147. func (graph *Graph) GarbageCollect() error {
  148. garbage, err := graph.Garbage()
  149. if err != nil {
  150. return err
  151. }
  152. return os.RemoveAll(garbage.Root)
  153. }
  154. func (graph *Graph) Map() (map[string]*Image, error) {
  155. // FIXME: this should replace All()
  156. all, err := graph.All()
  157. if err != nil {
  158. return nil, err
  159. }
  160. images := make(map[string]*Image, len(all))
  161. for _, image := range all {
  162. images[image.Id] = image
  163. }
  164. return images, nil
  165. }
  166. func (graph *Graph) All() ([]*Image, error) {
  167. var images []*Image
  168. err := graph.WalkAll(func(image *Image) {
  169. images = append(images, image)
  170. })
  171. return images, err
  172. }
  173. func (graph *Graph) WalkAll(handler func(*Image)) error {
  174. files, err := ioutil.ReadDir(graph.Root)
  175. if err != nil {
  176. return err
  177. }
  178. for _, st := range files {
  179. if img, err := graph.Get(st.Name()); err != nil {
  180. // Skip image
  181. continue
  182. } else if handler != nil {
  183. handler(img)
  184. }
  185. }
  186. return nil
  187. }
  188. func (graph *Graph) ByParent() (map[string][]*Image, error) {
  189. byParent := make(map[string][]*Image)
  190. err := graph.WalkAll(func(image *Image) {
  191. image, err := graph.Get(image.Parent)
  192. if err != nil {
  193. return
  194. }
  195. if children, exists := byParent[image.Parent]; exists {
  196. byParent[image.Parent] = []*Image{image}
  197. } else {
  198. byParent[image.Parent] = append(children, image)
  199. }
  200. })
  201. return byParent, err
  202. }
  203. func (graph *Graph) Heads() (map[string]*Image, error) {
  204. heads := make(map[string]*Image)
  205. byParent, err := graph.ByParent()
  206. if err != nil {
  207. return nil, err
  208. }
  209. err = graph.WalkAll(func(image *Image) {
  210. // If it's not in the byParent lookup table, then
  211. // it's not a parent -> so it's a head!
  212. if _, exists := byParent[image.Id]; !exists {
  213. heads[image.Id] = image
  214. }
  215. })
  216. return heads, err
  217. }
  218. func (graph *Graph) imageRoot(id string) string {
  219. return path.Join(graph.Root, id)
  220. }