1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/tags.go

240 lines
5.4 KiB
Go
Raw Normal View History

2013-03-21 20:47:23 -04:00
package docker
import (
"encoding/json"
"fmt"
2013-05-14 18:37:35 -04:00
"github.com/dotcloud/docker/utils"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
)
2013-06-04 14:00:22 -04:00
const DEFAULTTAG = "latest"
type TagStore struct {
path string
graph *Graph
Repositories map[string]Repository
}
type Repository map[string]string
func NewTagStore(path string, graph *Graph) (*TagStore, error) {
abspath, err := filepath.Abs(path)
if err != nil {
return nil, err
}
store := &TagStore{
path: abspath,
graph: graph,
Repositories: make(map[string]Repository),
}
// 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 {
return nil, err
}
return store, nil
}
func (store *TagStore) Save() error {
// 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
}
func (store *TagStore) Reload() error {
jsonData, err := ioutil.ReadFile(store.path)
if err != nil {
return err
}
if err := json.Unmarshal(jsonData, store); err != nil {
return err
}
return nil
}
func (store *TagStore) LookupImage(name string) (*Image, error) {
img, err := store.graph.Get(name)
if err != nil {
// FIXME: standardize on returning nil when the image doesn't exist, and err for everything else
// (so we can pass all errors here)
repoAndTag := strings.SplitN(name, ":", 2)
if len(repoAndTag) == 1 {
2013-06-04 14:00:22 -04:00
repoAndTag = append(repoAndTag, DEFAULTTAG)
}
if i, err := store.GetImage(repoAndTag[0], repoAndTag[1]); err != nil {
return nil, err
} else if i == nil {
return nil, fmt.Errorf("Image does not exist: %s", name)
} else {
img = i
}
}
return img, nil
}
// Return a reverse-lookup table of all the names which refer to each image
// Eg. {"43b5f19b10584": {"base:latest", "base:v1"}}
2013-06-04 14:00:22 -04:00
func (store *TagStore) ByID() map[string][]string {
byID := make(map[string][]string)
for repoName, repository := range store.Repositories {
for tag, id := range repository {
name := repoName + ":" + tag
2013-06-04 14:00:22 -04:00
if _, exists := byID[id]; !exists {
byID[id] = []string{name}
} else {
2013-06-04 14:00:22 -04:00
byID[id] = append(byID[id], name)
sort.Strings(byID[id])
}
}
}
2013-06-04 14:00:22 -04:00
return byID
}
func (store *TagStore) ImageName(id string) string {
2013-06-04 14:00:22 -04:00
if names, exists := store.ByID()[id]; exists && len(names) > 0 {
return names[0]
}
2013-06-04 14:00:22 -04:00
return utils.TruncateID(id)
}
2013-05-30 18:53:45 -04:00
func (store *TagStore) DeleteAll(id string) error {
2013-06-10 17:05:54 -04:00
names, exists := store.ByID()[id]
2013-05-30 18:53:45 -04:00
if !exists || len(names) == 0 {
return nil
}
for _, name := range names {
if strings.Contains(name, ":") {
nameParts := strings.Split(name, ":")
if _, err := store.Delete(nameParts[0], nameParts[1]); err != nil {
2013-05-30 18:53:45 -04:00
return err
}
} else {
if _, err := store.Delete(name, ""); err != nil {
2013-05-30 18:53:45 -04:00
return err
}
}
}
return nil
}
func (store *TagStore) Delete(repoName, tag string) (bool, error) {
deleted := false
2013-05-15 09:11:39 -04:00
if err := store.Reload(); err != nil {
return false, err
2013-05-15 09:11:39 -04: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)
}
deleted = true
2013-05-15 09:11:39 -04:00
} else {
return false, fmt.Errorf("No such tag: %s:%s", repoName, tag)
2013-05-15 09:11:39 -04:00
}
} else {
delete(store.Repositories, repoName)
deleted = true
2013-05-15 09:11:39 -04:00
}
} else {
fmt.Errorf("No such repository: %s", repoName)
}
return deleted, store.Save()
}
func (store *TagStore) Set(repoName, tag, imageName string, force bool) error {
img, err := store.LookupImage(imageName)
if err != nil {
return err
}
if tag == "" {
2013-06-04 14:00:22 -04:00
tag = DEFAULTTAG
}
if err := validateRepoName(repoName); err != nil {
return err
}
if err := validateTagName(tag); err != nil {
return err
}
if err := store.Reload(); err != nil {
return err
}
var repo Repository
if r, exists := store.Repositories[repoName]; exists {
repo = r
} else {
repo = make(map[string]string)
if old, exists := store.Repositories[repoName]; exists && !force {
return fmt.Errorf("Conflict: Tag %s:%s is already set to %s", repoName, tag, old)
}
store.Repositories[repoName] = repo
}
2013-06-04 14:00:22 -04:00
repo[tag] = img.ID
return store.Save()
}
func (store *TagStore) Get(repoName string) (Repository, error) {
if err := store.Reload(); err != nil {
return nil, err
}
if r, exists := store.Repositories[repoName]; exists {
return r, nil
}
return nil, nil
}
2013-06-03 16:00:15 -04:00
func (store *TagStore) GetImage(repoName, tagOrId string) (*Image, error) {
repo, err := store.Get(repoName)
if err != nil {
return nil, err
} else if repo == nil {
return nil, nil
}
2013-06-03 16:00:15 -04:00
//go through all the tags, to see if tag is in fact an ID
for _, revision := range repo {
2013-06-13 09:17:56 -04:00
if strings.HasPrefix(revision, tagOrId) {
2013-06-03 16:00:15 -04:00
return store.graph.Get(revision)
}
}
if revision, exists := repo[tagOrId]; exists {
return store.graph.Get(revision)
}
return nil, nil
}
// Validate the name of a repository
func validateRepoName(name string) error {
if name == "" {
return fmt.Errorf("Repository name can't be empty")
}
if strings.Contains(name, ":") {
return fmt.Errorf("Illegal repository name: %s", name)
}
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
}