Merge pull request #778 from nalind/kpod-shutdown

kpod: shut down the storage library at exit
This commit is contained in:
Daniel J Walsh 2017-08-22 07:02:36 -04:00 committed by GitHub
commit c67859731f
10 changed files with 48 additions and 13 deletions

View file

@ -10,6 +10,10 @@ import (
"github.com/urfave/cli" "github.com/urfave/cli"
) )
var (
stores = make(map[storage.Store]struct{})
)
func getStore(c *libkpod.Config) (storage.Store, error) { func getStore(c *libkpod.Config) (storage.Store, error) {
options := storage.DefaultStoreOptions options := storage.DefaultStoreOptions
options.GraphRoot = c.Root options.GraphRoot = c.Root
@ -22,9 +26,18 @@ func getStore(c *libkpod.Config) (storage.Store, error) {
return nil, err return nil, err
} }
is.Transport.SetStore(store) is.Transport.SetStore(store)
stores[store] = struct{}{}
return store, nil return store, nil
} }
func shutdownStores() {
for store := range stores {
if _, err := store.Shutdown(false); err != nil {
break
}
}
}
func getConfig(c *cli.Context) (*libkpod.Config, error) { func getConfig(c *cli.Context) (*libkpod.Config, error) {
config := libkpod.DefaultConfig() config := libkpod.DefaultConfig()
if c.GlobalIsSet("config") { if c.GlobalIsSet("config") {

View file

@ -86,6 +86,7 @@ func diffCmd(c *cli.Context) error {
if err != nil { if err != nil {
return errors.Wrapf(err, "could not get container server") return errors.Wrapf(err, "could not get container server")
} }
defer server.Shutdown()
to := c.Args().Get(0) to := c.Args().Get(0)
changes, err := server.GetDiff("", to) changes, err := server.GetDiff("", to)

View file

@ -137,9 +137,9 @@ func outputImages(store storage.Store, images []storage.Image, truncate, digests
} }
createdTime := img.Created createdTime := img.Created
name := "" names := []string{""}
if len(img.Names) > 0 { if len(img.Names) > 0 {
name = img.Names[0] names = img.Names
} }
info, imageDigest, size, _ := libkpodimage.InfoAndDigestAndSize(store, img) info, imageDigest, size, _ := libkpodimage.InfoAndDigestAndSize(store, img)
@ -149,7 +149,7 @@ func outputImages(store storage.Store, images []storage.Image, truncate, digests
params := imageOutputParams{ params := imageOutputParams{
ID: img.ID, ID: img.ID,
Name: name, Name: names,
Digest: imageDigest, Digest: imageDigest,
CreatedAt: createdTime.Format("Jan 2, 2006 15:04"), CreatedAt: createdTime.Format("Jan 2, 2006 15:04"),
Size: libkpodimage.FormattedSize(float64(size)), Size: libkpodimage.FormattedSize(float64(size)),
@ -173,7 +173,7 @@ func outputImages(store storage.Store, images []storage.Image, truncate, digests
type imageOutputParams struct { type imageOutputParams struct {
ID string `json:"id"` ID string `json:"id"`
Name string `json:"names"` Name []string `json:"names"`
Digest digest.Digest `json:"digest"` Digest digest.Digest `json:"digest"`
CreatedAt string `json:"created"` CreatedAt string `json:"created"`
Size string `json:"size"` Size string `json:"size"`

View file

@ -71,6 +71,7 @@ func inspectCmd(c *cli.Context) error {
if err != nil { if err != nil {
return errors.Wrapf(err, "could not get container server") return errors.Wrapf(err, "could not get container server")
} }
defer server.Shutdown()
if err = server.Update(); err != nil { if err = server.Update(); err != nil {
return errors.Wrapf(err, "could not update list of containers") return errors.Wrapf(err, "could not update list of containers")
} }

View file

@ -69,6 +69,7 @@ func logsCmd(c *cli.Context) error {
if err != nil { if err != nil {
return errors.Wrapf(err, "could not create container server") return errors.Wrapf(err, "could not create container server")
} }
defer server.Shutdown()
err = server.Update() err = server.Update()
if err != nil { if err != nil {
return errors.Wrapf(err, "could not update list of containers") return errors.Wrapf(err, "could not update list of containers")

View file

@ -50,6 +50,16 @@ func main() {
} }
return nil return nil
} }
app.After = func(*cli.Context) error {
// called by Run() when the command handler succeeds
shutdownStores()
return nil
}
cli.OsExiter = func(code int) {
// called by Run() when the command fails, bypassing After()
shutdownStores()
os.Exit(code)
}
app.Flags = []cli.Flag{ app.Flags = []cli.Flag{
cli.StringFlag{ cli.StringFlag{
Name: "config, c", Name: "config, c",
@ -82,6 +92,6 @@ func main() {
} }
if err := app.Run(os.Args); err != nil { if err := app.Run(os.Args); err != nil {
logrus.Errorf(err.Error()) logrus.Errorf(err.Error())
os.Exit(1) cli.OsExiter(1)
} }
} }

View file

@ -32,6 +32,7 @@ func renameCmd(c *cli.Context) error {
if err != nil { if err != nil {
return errors.Wrapf(err, "could not get container server") return errors.Wrapf(err, "could not get container server")
} }
defer server.Shutdown()
err = server.Update() err = server.Update()
if err != nil { if err != nil {
return errors.Wrapf(err, "could not update list of containers") return errors.Wrapf(err, "could not update list of containers")

View file

@ -70,6 +70,7 @@ func statsCmd(c *cli.Context) error {
if err != nil { if err != nil {
return errors.Wrapf(err, "could not create container server") return errors.Wrapf(err, "could not create container server")
} }
defer containerServer.Shutdown()
err = containerServer.Update() err = containerServer.Update()
if err != nil { if err != nil {
return errors.Wrapf(err, "could not update list of containers") return errors.Wrapf(err, "could not update list of containers")

View file

@ -570,7 +570,10 @@ func (c *ContainerServer) ReleasePodName(name string) {
// Shutdown attempts to shut down the server's storage cleanly // Shutdown attempts to shut down the server's storage cleanly
func (c *ContainerServer) Shutdown() error { func (c *ContainerServer) Shutdown() error {
_, err := c.store.Shutdown(false) _, err := c.store.Shutdown(false)
return err if err != nil && errors.Cause(err) != cstorage.ErrLayerUsedByContainer {
return err
}
return nil
} }
type containerServerState struct { type containerServerState struct {

View file

@ -198,8 +198,9 @@ function teardown() {
run bash -c "${KPOD_BINARY} $KPOD_OPTIONS inspect redis:alpine | python -m json.tool" run bash -c "${KPOD_BINARY} $KPOD_OPTIONS inspect redis:alpine | python -m json.tool"
echo "$output" echo "$output"
[ "$status" -eq 0 ] [ "$status" -eq 0 ]
}
run ${KPOD_BINARY} $KPOD_OPTIONS rmi redis:alpine run ${KPOD_BINARY} $KPOD_OPTIONS rmi redis:alpine
[ "$status" -eq 0 ]
}
@test "kpod inspect non-existent container" { @test "kpod inspect non-existent container" {
@ -211,13 +212,14 @@ function teardown() {
@test "kpod inspect with format" { @test "kpod inspect with format" {
run ${KPOD_BINARY} $KPOD_OPTIONS pull redis:alpine run ${KPOD_BINARY} $KPOD_OPTIONS pull redis:alpine
[ "$status" -eq 0 ] [ "$status" -eq 0 ]
run ${KPOD_BINARY} $KPOD_OPTIONS --format {{.ID}} inspect redis:alpine run ${KPOD_BINARY} $KPOD_OPTIONS inspect --format {{.ID}} redis:alpine
[ "$status" -eq 0] [ "$status" -eq 0 ]
inspectOutput="$output" inspectOutput="$output"
run ${KPOD_BINARY} $KPOD_OPTIONS images --quiet redis:alpine run ${KPOD_BINARY} $KPOD_OPTIONS images --quiet redis:alpine
[ "$status" -eq 0] [ "$status" -eq 0 ]
[ "$output" -eq "$inspectOutput" ] [ "$output" = "$inspectOutput" ]
run ${KPOD_BINARY} $KPOD_OPTIONS rmi redis:alpine run ${KPOD_BINARY} $KPOD_OPTIONS rmi redis:alpine
[ "$status" -eq 0 ]
} }
@test "kpod inspect specified type" { @test "kpod inspect specified type" {
@ -225,8 +227,9 @@ function teardown() {
[ "$status" -eq 0 ] [ "$status" -eq 0 ]
run bash -c "${KPOD_BINARY} $KPOD_OPTIONS inspect --type image redis:alpine | python -m json.tool" run bash -c "${KPOD_BINARY} $KPOD_OPTIONS inspect --type image redis:alpine | python -m json.tool"
echo "$output" echo "$output"
[ "$status" -eq 0] [ "$status" -eq 0 ]
run ${KPOD_BINARY} $KPOD_OPTIONS rmi redis:alpine run ${KPOD_BINARY} $KPOD_OPTIONS rmi redis:alpine
[ "$status" -eq 0 ]
} }
@test "kpod images" { @test "kpod images" {
@ -245,6 +248,7 @@ function teardown() {
@test "kpod images check name json output" { @test "kpod images check name json output" {
run ${KPOD_BINARY} $KPOD_OPTIONS pull debian:6.0.10 run ${KPOD_BINARY} $KPOD_OPTIONS pull debian:6.0.10
run ${KPOD_BINARY} $KPOD_OPTIONS images --format json run ${KPOD_BINARY} $KPOD_OPTIONS images --format json
echo "$output"
name=$(echo $output | python -c 'import sys; import json; print(json.loads(sys.stdin.read())[0])["names"][0]') name=$(echo $output | python -c 'import sys; import json; print(json.loads(sys.stdin.read())[0])["names"][0]')
[ "$name" == "docker.io/library/debian:6.0.10" ] [ "$name" = "docker.io/library/debian:6.0.10" ]
} }