2014-03-08 02:04:38 +00:00
|
|
|
package graph
|
2013-03-21 19:18:47 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2013-03-22 03:06:20 +00:00
|
|
|
"fmt"
|
2014-03-08 01:36:47 +00:00
|
|
|
"github.com/dotcloud/docker/image"
|
2013-05-14 22:37:35 +00:00
|
|
|
"github.com/dotcloud/docker/utils"
|
2013-03-21 19:18:47 +00:00
|
|
|
"io/ioutil"
|
2013-03-22 02:00:43 +00:00
|
|
|
"os"
|
2013-03-21 19:18:47 +00:00
|
|
|
"path/filepath"
|
2013-03-26 22:30:16 +00:00
|
|
|
"sort"
|
2013-03-22 03:06:20 +00:00
|
|
|
"strings"
|
2013-03-21 19:18:47 +00:00
|
|
|
)
|
|
|
|
|
2013-06-04 18:00:22 +00:00
|
|
|
const DEFAULTTAG = "latest"
|
2013-03-22 23:07:13 +00:00
|
|
|
|
2013-03-22 00:35:49 +00:00
|
|
|
type TagStore struct {
|
2013-03-21 19:18:47 +00:00
|
|
|
path string
|
|
|
|
graph *Graph
|
2013-03-22 00:35:49 +00:00
|
|
|
Repositories map[string]Repository
|
2013-03-21 19:18:47 +00:00
|
|
|
}
|
|
|
|
|
2013-03-22 00:35:49 +00:00
|
|
|
type Repository map[string]string
|
2013-03-21 19:18:47 +00:00
|
|
|
|
2013-03-22 00:35:49 +00:00
|
|
|
func NewTagStore(path string, graph *Graph) (*TagStore, error) {
|
2013-03-21 19:18:47 +00:00
|
|
|
abspath, err := filepath.Abs(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-03-22 00:35:49 +00:00
|
|
|
store := &TagStore{
|
2013-03-21 19:18:47 +00:00
|
|
|
path: abspath,
|
|
|
|
graph: graph,
|
2013-03-22 00:35:49 +00:00
|
|
|
Repositories: make(map[string]Repository),
|
2013-03-21 19:18:47 +00:00
|
|
|
}
|
2013-03-22 02:00:43 +00:00
|
|
|
// Load the json file if it exists, otherwise create it.
|
|
|
|
if err := store.Reload(); os.IsNotExist(err) {
|
|
|
|
if err := store.Save(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
2013-03-21 19:18:47 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return store, nil
|
|
|
|
}
|
|
|
|
|
2013-03-22 00:35:49 +00:00
|
|
|
func (store *TagStore) Save() error {
|
2013-03-21 19:18:47 +00:00
|
|
|
// Store the json ball
|
|
|
|
jsonData, err := json.Marshal(store)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := ioutil.WriteFile(store.path, jsonData, 0600); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-03-22 00:35:49 +00:00
|
|
|
func (store *TagStore) Reload() error {
|
2013-03-21 19:18:47 +00:00
|
|
|
jsonData, err := ioutil.ReadFile(store.path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(jsonData, store); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-03-08 01:36:47 +00:00
|
|
|
func (store *TagStore) LookupImage(name string) (*image.Image, error) {
|
2013-11-29 19:06:35 +00:00
|
|
|
// FIXME: standardize on returning nil when the image doesn't exist, and err for everything else
|
|
|
|
// (so we can pass all errors here)
|
|
|
|
repos, tag := utils.ParseRepositoryTag(name)
|
|
|
|
if tag == "" {
|
|
|
|
tag = DEFAULTTAG
|
|
|
|
}
|
|
|
|
img, err := store.GetImage(repos, tag)
|
2013-03-22 23:07:13 +00:00
|
|
|
if err != nil {
|
2013-11-29 19:06:35 +00:00
|
|
|
return nil, err
|
|
|
|
} else if img == nil {
|
|
|
|
if img, err = store.graph.Get(name); err != nil {
|
2013-03-22 23:07:13 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return img, nil
|
|
|
|
}
|
|
|
|
|
2013-03-23 02:22:06 +00:00
|
|
|
// Return a reverse-lookup table of all the names which refer to each image
|
|
|
|
// Eg. {"43b5f19b10584": {"base:latest", "base:v1"}}
|
2013-06-04 18:00:22 +00:00
|
|
|
func (store *TagStore) ByID() map[string][]string {
|
|
|
|
byID := make(map[string][]string)
|
2013-03-23 02:22:06 +00:00
|
|
|
for repoName, repository := range store.Repositories {
|
|
|
|
for tag, id := range repository {
|
|
|
|
name := repoName + ":" + tag
|
2013-06-04 18:00:22 +00:00
|
|
|
if _, exists := byID[id]; !exists {
|
|
|
|
byID[id] = []string{name}
|
2013-03-23 02:22:06 +00:00
|
|
|
} else {
|
2013-06-04 18:00:22 +00:00
|
|
|
byID[id] = append(byID[id], name)
|
|
|
|
sort.Strings(byID[id])
|
2013-03-23 02:22:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-04 18:00:22 +00:00
|
|
|
return byID
|
2013-03-23 02:22:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (store *TagStore) ImageName(id string) string {
|
2013-06-04 18:00:22 +00:00
|
|
|
if names, exists := store.ByID()[id]; exists && len(names) > 0 {
|
2013-03-23 02:22:06 +00:00
|
|
|
return names[0]
|
|
|
|
}
|
2013-06-04 18:00:22 +00:00
|
|
|
return utils.TruncateID(id)
|
2013-03-23 02:22:06 +00:00
|
|
|
}
|
|
|
|
|
2013-05-30 22:53:45 +00:00
|
|
|
func (store *TagStore) DeleteAll(id string) error {
|
2013-06-10 21:05:54 +00:00
|
|
|
names, exists := store.ByID()[id]
|
2013-05-30 22:53:45 +00:00
|
|
|
if !exists || len(names) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, name := range names {
|
|
|
|
if strings.Contains(name, ":") {
|
|
|
|
nameParts := strings.Split(name, ":")
|
2013-05-31 14:37:02 +00:00
|
|
|
if _, err := store.Delete(nameParts[0], nameParts[1]); err != nil {
|
2013-05-30 22:53:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2013-05-31 14:37:02 +00:00
|
|
|
if _, err := store.Delete(name, ""); err != nil {
|
2013-05-30 22:53:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-05-31 14:37:02 +00:00
|
|
|
func (store *TagStore) Delete(repoName, tag string) (bool, error) {
|
|
|
|
deleted := false
|
2013-05-15 13:11:39 +00:00
|
|
|
if err := store.Reload(); err != nil {
|
2013-05-31 14:37:02 +00:00
|
|
|
return false, err
|
2013-05-15 13:11:39 +00:00
|
|
|
}
|
|
|
|
if r, exists := store.Repositories[repoName]; exists {
|
|
|
|
if tag != "" {
|
|
|
|
if _, exists2 := r[tag]; exists2 {
|
|
|
|
delete(r, tag)
|
|
|
|
if len(r) == 0 {
|
|
|
|
delete(store.Repositories, repoName)
|
|
|
|
}
|
2013-05-31 14:37:02 +00:00
|
|
|
deleted = true
|
2013-05-15 13:11:39 +00:00
|
|
|
} else {
|
2013-05-31 14:37:02 +00:00
|
|
|
return false, fmt.Errorf("No such tag: %s:%s", repoName, tag)
|
2013-05-15 13:11:39 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
delete(store.Repositories, repoName)
|
2013-05-31 14:37:02 +00:00
|
|
|
deleted = true
|
2013-05-15 13:11:39 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fmt.Errorf("No such repository: %s", repoName)
|
|
|
|
}
|
2013-05-31 14:37:02 +00:00
|
|
|
return deleted, store.Save()
|
2013-03-23 02:22:06 +00:00
|
|
|
}
|
|
|
|
|
2013-03-23 01:27:18 +00:00
|
|
|
func (store *TagStore) Set(repoName, tag, imageName string, force bool) error {
|
|
|
|
img, err := store.LookupImage(imageName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-03-22 23:07:13 +00:00
|
|
|
if tag == "" {
|
2013-06-04 18:00:22 +00:00
|
|
|
tag = DEFAULTTAG
|
2013-03-22 03:06:20 +00:00
|
|
|
}
|
2013-03-22 23:07:13 +00:00
|
|
|
if err := validateRepoName(repoName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := validateTagName(tag); err != nil {
|
|
|
|
return err
|
2013-03-22 03:06:20 +00:00
|
|
|
}
|
2013-03-21 19:18:47 +00:00
|
|
|
if err := store.Reload(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-03-22 00:35:49 +00:00
|
|
|
var repo Repository
|
2013-03-21 19:18:47 +00:00
|
|
|
if r, exists := store.Repositories[repoName]; exists {
|
|
|
|
repo = r
|
|
|
|
} else {
|
2013-03-22 00:35:49 +00:00
|
|
|
repo = make(map[string]string)
|
2013-03-23 01:27:18 +00:00
|
|
|
if old, exists := store.Repositories[repoName]; exists && !force {
|
2013-05-20 18:31:45 +00:00
|
|
|
return fmt.Errorf("Conflict: Tag %s:%s is already set to %s", repoName, tag, old)
|
2013-03-23 01:27:18 +00:00
|
|
|
}
|
2013-03-21 19:18:47 +00:00
|
|
|
store.Repositories[repoName] = repo
|
|
|
|
}
|
2013-06-04 18:00:22 +00:00
|
|
|
repo[tag] = img.ID
|
2013-03-21 19:18:47 +00:00
|
|
|
return store.Save()
|
|
|
|
}
|
|
|
|
|
2013-03-22 00:35:49 +00:00
|
|
|
func (store *TagStore) Get(repoName string) (Repository, error) {
|
2013-03-21 19:18:47 +00:00
|
|
|
if err := store.Reload(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if r, exists := store.Repositories[repoName]; exists {
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2014-03-08 01:36:47 +00:00
|
|
|
func (store *TagStore) GetImage(repoName, tagOrID string) (*image.Image, error) {
|
2013-03-21 19:18:47 +00:00
|
|
|
repo, err := store.Get(repoName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if repo == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2013-06-28 16:06:00 +00:00
|
|
|
if revision, exists := repo[tagOrID]; exists {
|
|
|
|
return store.graph.Get(revision)
|
|
|
|
}
|
|
|
|
// If no matching tag is found, search through images for a matching image id
|
2013-06-03 20:00:15 +00:00
|
|
|
for _, revision := range repo {
|
2013-07-02 22:27:22 +00:00
|
|
|
if strings.HasPrefix(revision, tagOrID) {
|
2013-06-03 20:00:15 +00:00
|
|
|
return store.graph.Get(revision)
|
|
|
|
}
|
|
|
|
}
|
2013-03-21 19:18:47 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
2013-03-22 23:07:13 +00:00
|
|
|
|
|
|
|
// Validate the name of a repository
|
|
|
|
func validateRepoName(name string) error {
|
|
|
|
if name == "" {
|
|
|
|
return fmt.Errorf("Repository name can't be empty")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the name of a tag
|
|
|
|
func validateTagName(name string) error {
|
|
|
|
if name == "" {
|
|
|
|
return fmt.Errorf("Tag name can't be empty")
|
|
|
|
}
|
|
|
|
if strings.Contains(name, "/") || strings.Contains(name, ":") {
|
|
|
|
return fmt.Errorf("Illegal tag name: %s", name)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|