tags.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package graph
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/dotcloud/docker/image"
  6. "github.com/dotcloud/docker/utils"
  7. "io/ioutil"
  8. "os"
  9. "path/filepath"
  10. "sort"
  11. "strings"
  12. )
  13. const DEFAULTTAG = "latest"
  14. type TagStore struct {
  15. path string
  16. graph *Graph
  17. Repositories map[string]Repository
  18. }
  19. type Repository map[string]string
  20. func NewTagStore(path string, graph *Graph) (*TagStore, error) {
  21. abspath, err := filepath.Abs(path)
  22. if err != nil {
  23. return nil, err
  24. }
  25. store := &TagStore{
  26. path: abspath,
  27. graph: graph,
  28. Repositories: make(map[string]Repository),
  29. }
  30. // Load the json file if it exists, otherwise create it.
  31. if err := store.Reload(); os.IsNotExist(err) {
  32. if err := store.Save(); err != nil {
  33. return nil, err
  34. }
  35. } else if err != nil {
  36. return nil, err
  37. }
  38. return store, nil
  39. }
  40. func (store *TagStore) Save() error {
  41. // Store the json ball
  42. jsonData, err := json.Marshal(store)
  43. if err != nil {
  44. return err
  45. }
  46. if err := ioutil.WriteFile(store.path, jsonData, 0600); err != nil {
  47. return err
  48. }
  49. return nil
  50. }
  51. func (store *TagStore) Reload() error {
  52. jsonData, err := ioutil.ReadFile(store.path)
  53. if err != nil {
  54. return err
  55. }
  56. if err := json.Unmarshal(jsonData, store); err != nil {
  57. return err
  58. }
  59. return nil
  60. }
  61. func (store *TagStore) LookupImage(name string) (*image.Image, error) {
  62. // FIXME: standardize on returning nil when the image doesn't exist, and err for everything else
  63. // (so we can pass all errors here)
  64. repos, tag := utils.ParseRepositoryTag(name)
  65. if tag == "" {
  66. tag = DEFAULTTAG
  67. }
  68. img, err := store.GetImage(repos, tag)
  69. if err != nil {
  70. return nil, err
  71. } else if img == nil {
  72. if img, err = store.graph.Get(name); err != nil {
  73. return nil, err
  74. }
  75. }
  76. return img, nil
  77. }
  78. // Return a reverse-lookup table of all the names which refer to each image
  79. // Eg. {"43b5f19b10584": {"base:latest", "base:v1"}}
  80. func (store *TagStore) ByID() map[string][]string {
  81. byID := make(map[string][]string)
  82. for repoName, repository := range store.Repositories {
  83. for tag, id := range repository {
  84. name := repoName + ":" + tag
  85. if _, exists := byID[id]; !exists {
  86. byID[id] = []string{name}
  87. } else {
  88. byID[id] = append(byID[id], name)
  89. sort.Strings(byID[id])
  90. }
  91. }
  92. }
  93. return byID
  94. }
  95. func (store *TagStore) ImageName(id string) string {
  96. if names, exists := store.ByID()[id]; exists && len(names) > 0 {
  97. return names[0]
  98. }
  99. return utils.TruncateID(id)
  100. }
  101. func (store *TagStore) DeleteAll(id string) error {
  102. names, exists := store.ByID()[id]
  103. if !exists || len(names) == 0 {
  104. return nil
  105. }
  106. for _, name := range names {
  107. if strings.Contains(name, ":") {
  108. nameParts := strings.Split(name, ":")
  109. if _, err := store.Delete(nameParts[0], nameParts[1]); err != nil {
  110. return err
  111. }
  112. } else {
  113. if _, err := store.Delete(name, ""); err != nil {
  114. return err
  115. }
  116. }
  117. }
  118. return nil
  119. }
  120. func (store *TagStore) Delete(repoName, tag string) (bool, error) {
  121. deleted := false
  122. if err := store.Reload(); err != nil {
  123. return false, err
  124. }
  125. if r, exists := store.Repositories[repoName]; exists {
  126. if tag != "" {
  127. if _, exists2 := r[tag]; exists2 {
  128. delete(r, tag)
  129. if len(r) == 0 {
  130. delete(store.Repositories, repoName)
  131. }
  132. deleted = true
  133. } else {
  134. return false, fmt.Errorf("No such tag: %s:%s", repoName, tag)
  135. }
  136. } else {
  137. delete(store.Repositories, repoName)
  138. deleted = true
  139. }
  140. } else {
  141. fmt.Errorf("No such repository: %s", repoName)
  142. }
  143. return deleted, store.Save()
  144. }
  145. func (store *TagStore) Set(repoName, tag, imageName string, force bool) error {
  146. img, err := store.LookupImage(imageName)
  147. if err != nil {
  148. return err
  149. }
  150. if tag == "" {
  151. tag = DEFAULTTAG
  152. }
  153. if err := validateRepoName(repoName); err != nil {
  154. return err
  155. }
  156. if err := validateTagName(tag); err != nil {
  157. return err
  158. }
  159. if err := store.Reload(); err != nil {
  160. return err
  161. }
  162. var repo Repository
  163. if r, exists := store.Repositories[repoName]; exists {
  164. repo = r
  165. } else {
  166. repo = make(map[string]string)
  167. if old, exists := store.Repositories[repoName]; exists && !force {
  168. return fmt.Errorf("Conflict: Tag %s:%s is already set to %s", repoName, tag, old)
  169. }
  170. store.Repositories[repoName] = repo
  171. }
  172. repo[tag] = img.ID
  173. return store.Save()
  174. }
  175. func (store *TagStore) Get(repoName string) (Repository, error) {
  176. if err := store.Reload(); err != nil {
  177. return nil, err
  178. }
  179. if r, exists := store.Repositories[repoName]; exists {
  180. return r, nil
  181. }
  182. return nil, nil
  183. }
  184. func (store *TagStore) GetImage(repoName, tagOrID string) (*image.Image, error) {
  185. repo, err := store.Get(repoName)
  186. if err != nil {
  187. return nil, err
  188. } else if repo == nil {
  189. return nil, nil
  190. }
  191. if revision, exists := repo[tagOrID]; exists {
  192. return store.graph.Get(revision)
  193. }
  194. // If no matching tag is found, search through images for a matching image id
  195. for _, revision := range repo {
  196. if strings.HasPrefix(revision, tagOrID) {
  197. return store.graph.Get(revision)
  198. }
  199. }
  200. return nil, nil
  201. }
  202. // Validate the name of a repository
  203. func validateRepoName(name string) error {
  204. if name == "" {
  205. return fmt.Errorf("Repository name can't be empty")
  206. }
  207. return nil
  208. }
  209. // Validate the name of a tag
  210. func validateTagName(name string) error {
  211. if name == "" {
  212. return fmt.Errorf("Tag name can't be empty")
  213. }
  214. if strings.Contains(name, "/") || strings.Contains(name, ":") {
  215. return fmt.Errorf("Illegal tag name: %s", name)
  216. }
  217. return nil
  218. }