tags.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. package graph
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "path/filepath"
  8. "regexp"
  9. "sort"
  10. "strings"
  11. "sync"
  12. "github.com/docker/docker/image"
  13. "github.com/docker/docker/pkg/parsers"
  14. "github.com/docker/docker/registry"
  15. "github.com/docker/docker/utils"
  16. )
  17. const DEFAULTTAG = "latest"
  18. var (
  19. validTagName = regexp.MustCompile(`^[\w][\w.-]{0,127}$`)
  20. )
  21. type TagStore struct {
  22. path string
  23. graph *Graph
  24. Repositories map[string]Repository
  25. sync.Mutex
  26. // FIXME: move push/pull-related fields
  27. // to a helper type
  28. pullingPool map[string]chan struct{}
  29. pushingPool map[string]chan struct{}
  30. }
  31. type Repository map[string]string
  32. // update Repository mapping with content of u
  33. func (r Repository) Update(u Repository) {
  34. for k, v := range u {
  35. r[k] = v
  36. }
  37. }
  38. // return true if the contents of u Repository, are wholly contained in r Repository
  39. func (r Repository) Contains(u Repository) bool {
  40. for k, v := range u {
  41. // if u's key is not present in r OR u's key is present, but not the same value
  42. if rv, ok := r[k]; !ok || (ok && rv != v) {
  43. return false
  44. }
  45. }
  46. return true
  47. }
  48. func NewTagStore(path string, graph *Graph) (*TagStore, error) {
  49. abspath, err := filepath.Abs(path)
  50. if err != nil {
  51. return nil, err
  52. }
  53. store := &TagStore{
  54. path: abspath,
  55. graph: graph,
  56. Repositories: make(map[string]Repository),
  57. pullingPool: make(map[string]chan struct{}),
  58. pushingPool: make(map[string]chan struct{}),
  59. }
  60. // Load the json file if it exists, otherwise create it.
  61. if err := store.reload(); os.IsNotExist(err) {
  62. if err := store.save(); err != nil {
  63. return nil, err
  64. }
  65. } else if err != nil {
  66. return nil, err
  67. }
  68. return store, nil
  69. }
  70. func (store *TagStore) save() error {
  71. // Store the json ball
  72. jsonData, err := json.Marshal(store)
  73. if err != nil {
  74. return err
  75. }
  76. if err := ioutil.WriteFile(store.path, jsonData, 0600); err != nil {
  77. return err
  78. }
  79. return nil
  80. }
  81. func (store *TagStore) reload() error {
  82. jsonData, err := ioutil.ReadFile(store.path)
  83. if err != nil {
  84. return err
  85. }
  86. if err := json.Unmarshal(jsonData, store); err != nil {
  87. return err
  88. }
  89. return nil
  90. }
  91. func (store *TagStore) LookupImage(name string) (*image.Image, error) {
  92. // FIXME: standardize on returning nil when the image doesn't exist, and err for everything else
  93. // (so we can pass all errors here)
  94. repos, tag := parsers.ParseRepositoryTag(name)
  95. if tag == "" {
  96. tag = DEFAULTTAG
  97. }
  98. img, err := store.GetImage(repos, tag)
  99. store.Lock()
  100. defer store.Unlock()
  101. if err != nil {
  102. return nil, err
  103. } else if img == nil {
  104. if img, err = store.graph.Get(name); err != nil {
  105. return nil, err
  106. }
  107. }
  108. return img, nil
  109. }
  110. // Return a reverse-lookup table of all the names which refer to each image
  111. // Eg. {"43b5f19b10584": {"base:latest", "base:v1"}}
  112. func (store *TagStore) ByID() map[string][]string {
  113. store.Lock()
  114. defer store.Unlock()
  115. byID := make(map[string][]string)
  116. for repoName, repository := range store.Repositories {
  117. for tag, id := range repository {
  118. name := repoName + ":" + tag
  119. if _, exists := byID[id]; !exists {
  120. byID[id] = []string{name}
  121. } else {
  122. byID[id] = append(byID[id], name)
  123. sort.Strings(byID[id])
  124. }
  125. }
  126. }
  127. return byID
  128. }
  129. func (store *TagStore) ImageName(id string) string {
  130. if names, exists := store.ByID()[id]; exists && len(names) > 0 {
  131. return names[0]
  132. }
  133. return utils.TruncateID(id)
  134. }
  135. func (store *TagStore) DeleteAll(id string) error {
  136. names, exists := store.ByID()[id]
  137. if !exists || len(names) == 0 {
  138. return nil
  139. }
  140. for _, name := range names {
  141. if strings.Contains(name, ":") {
  142. nameParts := strings.Split(name, ":")
  143. if _, err := store.Delete(nameParts[0], nameParts[1]); err != nil {
  144. return err
  145. }
  146. } else {
  147. if _, err := store.Delete(name, ""); err != nil {
  148. return err
  149. }
  150. }
  151. }
  152. return nil
  153. }
  154. func (store *TagStore) Delete(repoName, tag string) (bool, error) {
  155. store.Lock()
  156. defer store.Unlock()
  157. deleted := false
  158. if err := store.reload(); err != nil {
  159. return false, err
  160. }
  161. repoName = registry.NormalizeLocalName(repoName)
  162. if r, exists := store.Repositories[repoName]; exists {
  163. if tag != "" {
  164. if _, exists2 := r[tag]; exists2 {
  165. delete(r, tag)
  166. if len(r) == 0 {
  167. delete(store.Repositories, repoName)
  168. }
  169. deleted = true
  170. } else {
  171. return false, fmt.Errorf("No such tag: %s:%s", repoName, tag)
  172. }
  173. } else {
  174. delete(store.Repositories, repoName)
  175. deleted = true
  176. }
  177. } else {
  178. return false, fmt.Errorf("No such repository: %s", repoName)
  179. }
  180. return deleted, store.save()
  181. }
  182. func (store *TagStore) Set(repoName, tag, imageName string, force bool) error {
  183. img, err := store.LookupImage(imageName)
  184. store.Lock()
  185. defer store.Unlock()
  186. if err != nil {
  187. return err
  188. }
  189. if tag == "" {
  190. tag = DEFAULTTAG
  191. }
  192. if err := validateRepoName(repoName); err != nil {
  193. return err
  194. }
  195. if err := ValidateTagName(tag); err != nil {
  196. return err
  197. }
  198. if err := store.reload(); err != nil {
  199. return err
  200. }
  201. var repo Repository
  202. repoName = registry.NormalizeLocalName(repoName)
  203. if r, exists := store.Repositories[repoName]; exists {
  204. repo = r
  205. if old, exists := store.Repositories[repoName][tag]; exists && !force {
  206. return fmt.Errorf("Conflict: Tag %s is already set to image %s, if you want to replace it, please use -f option", tag, old)
  207. }
  208. } else {
  209. repo = make(map[string]string)
  210. store.Repositories[repoName] = repo
  211. }
  212. repo[tag] = img.ID
  213. return store.save()
  214. }
  215. func (store *TagStore) Get(repoName string) (Repository, error) {
  216. store.Lock()
  217. defer store.Unlock()
  218. if err := store.reload(); err != nil {
  219. return nil, err
  220. }
  221. repoName = registry.NormalizeLocalName(repoName)
  222. if r, exists := store.Repositories[repoName]; exists {
  223. return r, nil
  224. }
  225. return nil, nil
  226. }
  227. func (store *TagStore) GetImage(repoName, tagOrID string) (*image.Image, error) {
  228. repo, err := store.Get(repoName)
  229. store.Lock()
  230. defer store.Unlock()
  231. if err != nil {
  232. return nil, err
  233. } else if repo == nil {
  234. return nil, nil
  235. }
  236. if revision, exists := repo[tagOrID]; exists {
  237. return store.graph.Get(revision)
  238. }
  239. // If no matching tag is found, search through images for a matching image id
  240. for _, revision := range repo {
  241. if strings.HasPrefix(revision, tagOrID) {
  242. return store.graph.Get(revision)
  243. }
  244. }
  245. return nil, nil
  246. }
  247. func (store *TagStore) GetRepoRefs() map[string][]string {
  248. store.Lock()
  249. reporefs := make(map[string][]string)
  250. for name, repository := range store.Repositories {
  251. for tag, id := range repository {
  252. shortID := utils.TruncateID(id)
  253. reporefs[shortID] = append(reporefs[shortID], fmt.Sprintf("%s:%s", name, tag))
  254. }
  255. }
  256. store.Unlock()
  257. return reporefs
  258. }
  259. // Validate the name of a repository
  260. func validateRepoName(name string) error {
  261. if name == "" {
  262. return fmt.Errorf("Repository name can't be empty")
  263. }
  264. if name == "scratch" {
  265. return fmt.Errorf("'scratch' is a reserved name")
  266. }
  267. return nil
  268. }
  269. // Validate the name of a tag
  270. func ValidateTagName(name string) error {
  271. if name == "" {
  272. return fmt.Errorf("Tag name can't be empty")
  273. }
  274. if !validTagName.MatchString(name) {
  275. return fmt.Errorf("Illegal tag name (%s): only [A-Za-z0-9_.-] are allowed, minimum 1, maximum 128 in length", name)
  276. }
  277. return nil
  278. }
  279. func (store *TagStore) poolAdd(kind, key string) (chan struct{}, error) {
  280. store.Lock()
  281. defer store.Unlock()
  282. if c, exists := store.pullingPool[key]; exists {
  283. return c, fmt.Errorf("pull %s is already in progress", key)
  284. }
  285. if c, exists := store.pushingPool[key]; exists {
  286. return c, fmt.Errorf("push %s is already in progress", key)
  287. }
  288. c := make(chan struct{})
  289. switch kind {
  290. case "pull":
  291. store.pullingPool[key] = c
  292. case "push":
  293. store.pushingPool[key] = c
  294. default:
  295. return nil, fmt.Errorf("Unknown pool type")
  296. }
  297. return c, nil
  298. }
  299. func (store *TagStore) poolRemove(kind, key string) error {
  300. store.Lock()
  301. defer store.Unlock()
  302. switch kind {
  303. case "pull":
  304. if c, exists := store.pullingPool[key]; exists {
  305. close(c)
  306. delete(store.pullingPool, key)
  307. }
  308. case "push":
  309. if c, exists := store.pushingPool[key]; exists {
  310. close(c)
  311. delete(store.pushingPool, key)
  312. }
  313. default:
  314. return fmt.Errorf("Unknown pool type")
  315. }
  316. return nil
  317. }