Beispiel #1
0
func (c *NameLookupCache) CreateImageStore(op trace.Operation, storeName string) (*url.URL, error) {
	store, err := util.ImageStoreNameToURL(storeName)
	if err != nil {
		return nil, err
	}

	// Check for existence and rehydrate the cache if it exists on disk.
	_, err = c.GetImageStore(op, storeName)
	// we expect this not to exist.
	if err == nil {
		return nil, os.ErrExist
	}

	c.storeCacheLock.Lock()
	defer c.storeCacheLock.Unlock()

	store, err = c.DataStore.CreateImageStore(op, storeName)
	if err != nil {
		return nil, err
	}

	// Create the root image
	scratch, err := c.DataStore.WriteImage(op, &Image{Store: store}, Scratch.ID, nil, "", nil)
	if err != nil {
		return nil, err
	}

	indx := index.NewIndex()
	c.storeCache[*store] = indx
	if err = indx.Insert(scratch); err != nil {
		return nil, err
	}

	return store, nil
}
Beispiel #2
0
// GetImageStore checks to see if a named image store exists and returls the
// URL to it if so or error.
func (c *NameLookupCache) GetImageStore(op trace.Operation, storeName string) (*url.URL, error) {
	store, err := util.ImageStoreNameToURL(storeName)
	if err != nil {
		return nil, err
	}

	c.storeCacheLock.Lock()
	defer c.storeCacheLock.Unlock()

	// check the cache
	_, ok := c.storeCache[*store]

	if !ok {
		infof("Refreshing image cache from datastore.")
		// Store isn't in the cache.  Look it up in the datastore.
		storeName, err := util.ImageStoreName(store)
		if err != nil {
			return nil, err
		}

		// If the store doesn't exist, we'll fall out here.
		_, err = c.DataStore.GetImageStore(op, storeName)
		if err != nil {
			return nil, err
		}

		idx := index.NewIndex()

		c.storeCache[*store] = idx

		// Add Scratch
		scratch, err := c.DataStore.GetImage(op, store, Scratch.ID)
		if err != nil {
			log.Errorf("ImageCache Error: looking up scratch on %s: %s", store.String(), err)
			return nil, ErrCorruptImageStore
		}

		if err = idx.Insert(scratch); err != nil {
			return nil, err
		}

		// XXX after creating the indx and populating the map, we can put the rest in a go routine

		// Fall out here if there are no images.  We should at least have a scratch.
		images, err := c.DataStore.ListImages(op, store, nil)
		if err != nil {
			return nil, err
		}

		debugf("Found %d images", len(images))

		// Build image map to simplify tree traversal.
		imageMap := make(map[string]*Image, len(images))
		for _, img := range images {
			if img.ID == Scratch.ID {
				continue
			}
			imageMap[img.Self()] = img
		}

		for k := range imageMap {
			parentTree(k, idx, imageMap)
		}
	}

	return store, nil
}