Merge pull request #692 from nalind/api-usage

Fix a couple of bugs in our use of containers/storage and containers/image
This commit is contained in:
Mrunal Patel 2017-07-26 09:35:11 -07:00 committed by GitHub
commit 120af8ef01
6 changed files with 33 additions and 95 deletions

View file

@ -134,42 +134,20 @@ func storeInfo(c *cli.Context) (string, map[string]interface{}, error) {
info := map[string]interface{}{} info := map[string]interface{}{}
info["GraphRoot"] = store.GraphRoot() info["GraphRoot"] = store.GraphRoot()
info["GraphDriverName"] = store.GraphDriverName() info["GraphDriverName"] = store.GraphDriverName()
if is, err := store.ImageStore(); err != nil { images, err := store.Images()
if err != nil {
info["ImageStore"] = infoErr(err) info["ImageStore"] = infoErr(err)
} else { } else {
images, err := is.Images() info["ImageStore"] = map[string]interface{}{
if err != nil { "number": len(images),
info["ImageStore"] = infoErr(err)
} else {
info["ImageStore"] = map[string]interface{}{
"number": len(images),
}
} }
} }
/* Oh this is in master on containers/storage, rebase later containers, err := store.Containers()
if is, err := store.ROImageStores(); err != nil { if err != nil {
info["ROImageStore"] = infoErr(err)
} else {
images, err := is.Images()
if err != nil {
info["ROImageStore"] = infoErr(err)
} else {
info["ROImageStore"] = map[string]interface{}{
"number": len(images),
}
}
}
*/
if cs, err := store.ContainerStore(); err != nil {
info["ContainerStore"] = infoErr(err) info["ContainerStore"] = infoErr(err)
} else { } else {
containers, err := cs.Containers() info["ContainerStore"] = map[string]interface{}{
if err != nil { "number": len(containers),
info["ContainerStore"] = infoErr(err)
} else {
info["ContainerStore"] = map[string]interface{}{
"number": len(containers),
}
} }
} }
return "store", info, nil return "store", info, nil

View file

@ -96,12 +96,7 @@ func rmiCmd(c *cli.Context) error {
// TODO: replace this with something in libkpod // TODO: replace this with something in libkpod
func runningContainers(image *storage.Image, store storage.Store) ([]string, error) { func runningContainers(image *storage.Image, store storage.Store) ([]string, error) {
ctrIDs := []string{} ctrIDs := []string{}
ctrStore, err := store.ContainerStore() containers, err := store.Containers()
if err != nil {
return nil, err
}
containers, err := ctrStore.Containers()
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -115,12 +110,8 @@ func runningContainers(image *storage.Image, store storage.Store) ([]string, err
// TODO: replace this with something in libkpod // TODO: replace this with something in libkpod
func removeContainers(ctrIDs []string, store storage.Store) error { func removeContainers(ctrIDs []string, store storage.Store) error {
ctrStore, err := store.ContainerStore()
if err != nil {
return err
}
for _, ctrID := range ctrIDs { for _, ctrID := range ctrIDs {
if err = ctrStore.Delete(ctrID); err != nil { if err := store.DeleteContainer(ctrID); err != nil {
return errors.Wrapf(err, "could not remove container %q", ctrID) return errors.Wrapf(err, "could not remove container %q", ctrID)
} }
} }

View file

@ -7,11 +7,7 @@ import (
// FindContainer searches for a container with the given name or ID in the given store // FindContainer searches for a container with the given name or ID in the given store
func FindContainer(store cstorage.Store, container string) (*cstorage.Container, error) { func FindContainer(store cstorage.Store, container string) (*cstorage.Container, error) {
ctrStore, err := store.ContainerStore() return store.Container(container)
if err != nil {
return nil, err
}
return ctrStore.Get(container)
} }
// GetContainerTopLayerID gets the ID of the top layer of the given container // GetContainerTopLayerID gets the ID of the top layer of the given container
@ -25,15 +21,7 @@ func GetContainerTopLayerID(store cstorage.Store, containerID string) (string, e
// GetContainerRwSize Gets the size of the mutable top layer of the container // GetContainerRwSize Gets the size of the mutable top layer of the container
func GetContainerRwSize(store cstorage.Store, containerID string) (int64, error) { func GetContainerRwSize(store cstorage.Store, containerID string) (int64, error) {
ctrStore, err := store.ContainerStore() container, err := store.Container(containerID)
if err != nil {
return 0, err
}
container, err := ctrStore.Get(containerID)
if err != nil {
return 0, err
}
lstore, err := store.LayerStore()
if err != nil { if err != nil {
return 0, err return 0, err
} }
@ -41,11 +29,11 @@ func GetContainerRwSize(store cstorage.Store, containerID string) (int64, error)
// Get the size of the top layer by calculating the size of the diff // Get the size of the top layer by calculating the size of the diff
// between the layer and its parent. The top layer of a container is // between the layer and its parent. The top layer of a container is
// the only RW layer, all others are immutable // the only RW layer, all others are immutable
layer, err := lstore.Get(container.LayerID) layer, err := store.Layer(container.LayerID)
if err != nil { if err != nil {
return 0, err return 0, err
} }
return lstore.DiffSize(layer.Parent, layer.ID) return store.DiffSize(layer.Parent, layer.ID)
} }
// GetContainerRootFsSize gets the size of the container's root filesystem // GetContainerRootFsSize gets the size of the container's root filesystem
@ -53,38 +41,30 @@ func GetContainerRwSize(store cstorage.Store, containerID string) (int64, error)
// mutable layer, and the rest is the RootFS: the set of immutable layers // mutable layer, and the rest is the RootFS: the set of immutable layers
// that make up the image on which the container is based // that make up the image on which the container is based
func GetContainerRootFsSize(store cstorage.Store, containerID string) (int64, error) { func GetContainerRootFsSize(store cstorage.Store, containerID string) (int64, error) {
ctrStore, err := store.ContainerStore() container, err := store.Container(containerID)
if err != nil {
return 0, err
}
container, err := ctrStore.Get(containerID)
if err != nil {
return 0, err
}
lstore, err := store.LayerStore()
if err != nil { if err != nil {
return 0, err return 0, err
} }
// Ignore the size of the top layer. The top layer is a mutable RW layer // Ignore the size of the top layer. The top layer is a mutable RW layer
// and is not considered a part of the rootfs // and is not considered a part of the rootfs
rwLayer, err := lstore.Get(container.LayerID) rwLayer, err := store.Layer(container.LayerID)
if err != nil { if err != nil {
return 0, err return 0, err
} }
layer, err := lstore.Get(rwLayer.Parent) layer, err := store.Layer(rwLayer.Parent)
if err != nil { if err != nil {
return 0, err return 0, err
} }
size := int64(0) size := int64(0)
for layer.Parent != "" { for layer.Parent != "" {
layerSize, err := lstore.DiffSize(layer.Parent, layer.ID) layerSize, err := store.DiffSize(layer.Parent, layer.ID)
if err != nil { if err != nil {
return size, errors.Wrapf(err, "getting diffsize of layer %q and its parent %q", layer.ID, layer.Parent) return size, errors.Wrapf(err, "getting diffsize of layer %q and its parent %q", layer.ID, layer.Parent)
} }
size += layerSize size += layerSize
layer, err = lstore.Get(layer.Parent) layer, err = store.Layer(layer.Parent)
if err != nil { if err != nil {
return 0, err return 0, err
} }
@ -92,6 +72,6 @@ func GetContainerRootFsSize(store cstorage.Store, containerID string) (int64, er
// Get the size of the last layer. Has to be outside of the loop // Get the size of the last layer. Has to be outside of the loop
// because the parent of the last layer is "", andlstore.Get("") // because the parent of the last layer is "", andlstore.Get("")
// will return an error // will return an error
layerSize, err := lstore.DiffSize(layer.Parent, layer.ID) layerSize, err := store.DiffSize(layer.Parent, layer.ID)
return size + layerSize, err return size + layerSize, err
} }

View file

@ -96,6 +96,7 @@ func matchesLabel(image storage.Image, store storage.Store, label string) bool {
if err != nil { if err != nil {
return false return false
} }
defer img.Close()
info, err := img.Inspect() info, err := img.Inspect()
if err != nil { if err != nil {
return false return false
@ -229,6 +230,7 @@ func Size(store storage.Store, img storage.Image) (int64, error) {
if err != nil { if err != nil {
return -1, err return -1, err
} }
defer imgRef.Close()
imgSize, err := imgRef.Size() imgSize, err := imgRef.Size()
if err != nil { if err != nil {
return -1, err return -1, err

View file

@ -112,15 +112,11 @@ func GetImageData(store storage.Store, name string) (*ImageData, error) {
return nil, err return nil, err
} }
lstore, err := store.LayerStore() layer, err := store.Layer(topLayerID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
layer, err := lstore.Get(topLayerID) size, err := store.DiffSize(layer.Parent, layer.ID)
if err != nil {
return nil, err
}
size, err := lstore.DiffSize(layer.Parent, layer.ID)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -7,11 +7,7 @@ import (
// UntagImage removes the tag from the given image // UntagImage removes the tag from the given image
func UntagImage(store storage.Store, image *storage.Image, imgArg string) (string, error) { func UntagImage(store storage.Store, image *storage.Image, imgArg string) (string, error) {
// Remove name from image.Names and set the new name in the ImageStore // Remove name from image.Names and set the new names
imgStore, err := store.ImageStore()
if err != nil {
return "", errors.Wrap(err, "could not untag image")
}
newNames := []string{} newNames := []string{}
removedName := "" removedName := ""
for _, name := range image.Names { for _, name := range image.Names {
@ -21,24 +17,19 @@ func UntagImage(store storage.Store, image *storage.Image, imgArg string) (strin
} }
newNames = append(newNames, name) newNames = append(newNames, name)
} }
imgStore.SetNames(image.ID, newNames) if removedName != "" {
err = imgStore.Save() if err := store.SetNames(image.ID, newNames); err != nil {
return removedName, err return "", errors.Wrapf(err, "error removing name %q from image %q", removedName, image.ID)
}
}
return removedName, nil
} }
// RemoveImage removes the given image from storage // RemoveImage removes the given image from storage
func RemoveImage(image *storage.Image, store storage.Store) (string, error) { func RemoveImage(image *storage.Image, store storage.Store) (string, error) {
imgStore, err := store.ImageStore() _, err := store.DeleteImage(image.ID, true)
if err != nil { if err != nil {
return "", errors.Wrapf(err, "could not open image store") return "", errors.Wrapf(err, "could not remove image %q", image.ID)
}
err = imgStore.Delete(image.ID)
if err != nil {
return "", errors.Wrapf(err, "could not remove image")
}
err = imgStore.Save()
if err != nil {
return "", errors.Wrapf(err, "could not save image store")
} }
return image.ID, nil return image.ID, nil
} }