Handle shim delete workdir on Windows

Signed-off-by: Justin Terry (VM) <juterry@microsoft.com>
This commit is contained in:
Justin Terry (VM) 2018-09-28 15:02:10 -07:00
parent b8945d35f5
commit 2ddbb2db05
3 changed files with 37 additions and 8 deletions

View File

@ -21,6 +21,7 @@ import (
"context" "context"
"io" "io"
"os" "os"
gruntime "runtime"
"strings" "strings"
eventstypes "github.com/containerd/containerd/api/events" eventstypes "github.com/containerd/containerd/api/events"
@ -109,7 +110,23 @@ func (b *binary) Start(ctx context.Context) (_ *shim, err error) {
func (b *binary) Delete(ctx context.Context) (*runtime.Exit, error) { func (b *binary) Delete(ctx context.Context) (*runtime.Exit, error) {
log.G(ctx).Info("cleaning up dead shim") log.G(ctx).Info("cleaning up dead shim")
cmd, err := client.Command(ctx, b.runtime, b.containerdAddress, b.bundle.Path, "-id", b.bundle.ID, "delete") var bundlePath string
if gruntime.GOOS == "windows" {
// Windows cannot delete the current working directory while an
// executable is in use with it. For the cleanup case we invoke with the
// deafult work dir and forward the bundle path on the cmdline.
bundlePath = ""
} else {
bundlePath = b.bundle.Path
}
cmd, err := client.Command(ctx,
b.runtime,
b.containerdAddress,
bundlePath,
"-id", b.bundle.ID,
"-bundle", b.bundle.Path,
"delete")
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -195,13 +195,22 @@ func (s *service) Cleanup(ctx context.Context) (*taskAPI.DeleteResponse, error)
if err != nil { if err != nil {
return nil, err return nil, err
} }
path, err := os.Getwd() // Forcibly shut down any container in this bundle
if err != nil { rhcs := newRunhcs("")
return nil, err dopts := &runhcs.DeleteOpts{
Force: true,
} }
if err := os.RemoveAll(path); err != nil { if err := rhcs.Delete(ctx, s.id, dopts); err != nil {
return nil, err log.G(ctx).WithError(err).Debugf("failed to delete container")
} }
opts, ok := ctx.Value(shim.OptsKey{}).(shim.Opts)
if ok && opts.BundlePath != "" {
if err := os.RemoveAll(opts.BundlePath); err != nil {
return nil, err
}
}
return &taskAPI.DeleteResponse{ return &taskAPI.DeleteResponse{
ExitedAt: time.Now(), ExitedAt: time.Now(),
ExitStatus: 255, ExitStatus: 255,

View File

@ -58,7 +58,8 @@ type OptsKey struct{}
// Opts are context options associated with the shim invocation. // Opts are context options associated with the shim invocation.
type Opts struct { type Opts struct {
Debug bool BundlePath string
Debug bool
} }
var ( var (
@ -66,6 +67,7 @@ var (
idFlag string idFlag string
namespaceFlag string namespaceFlag string
socketFlag string socketFlag string
bundlePath string
addressFlag string addressFlag string
containerdBinaryFlag string containerdBinaryFlag string
action string action string
@ -76,6 +78,7 @@ func parseFlags() {
flag.StringVar(&namespaceFlag, "namespace", "", "namespace that owns the shim") flag.StringVar(&namespaceFlag, "namespace", "", "namespace that owns the shim")
flag.StringVar(&idFlag, "id", "", "id of the task") flag.StringVar(&idFlag, "id", "", "id of the task")
flag.StringVar(&socketFlag, "socket", "", "abstract socket path to serve") flag.StringVar(&socketFlag, "socket", "", "abstract socket path to serve")
flag.StringVar(&bundlePath, "bundle", "", "path to the bundle if not workdir")
flag.StringVar(&addressFlag, "address", "", "grpc address back to main containerd") flag.StringVar(&addressFlag, "address", "", "grpc address back to main containerd")
flag.StringVar(&containerdBinaryFlag, "publish-binary", "containerd", "path to publish binary (used for publishing events)") flag.StringVar(&containerdBinaryFlag, "publish-binary", "containerd", "path to publish binary (used for publishing events)")
@ -141,7 +144,7 @@ func run(id string, initFunc Init) error {
return fmt.Errorf("shim namespace cannot be empty") return fmt.Errorf("shim namespace cannot be empty")
} }
ctx := namespaces.WithNamespace(context.Background(), namespaceFlag) ctx := namespaces.WithNamespace(context.Background(), namespaceFlag)
ctx = context.WithValue(ctx, OptsKey{}, Opts{Debug: debugFlag}) ctx = context.WithValue(ctx, OptsKey{}, Opts{BundlePath: bundlePath, Debug: debugFlag})
ctx = log.WithLogger(ctx, log.G(ctx).WithField("runtime", id)) ctx = log.WithLogger(ctx, log.G(ctx).WithField("runtime", id))
service, err := initFunc(ctx, idFlag, publisher) service, err := initFunc(ctx, idFlag, publisher)