From 3456577268b62d38c17f2edc668b9a55f15c14a2 Mon Sep 17 00:00:00 2001 From: jianqli Date: Thu, 1 Nov 2018 23:02:33 +0800 Subject: [PATCH] add command timeout support for skopeo * add global command-timeout option for skopeo --- cmd/skopeo/copy.go | 6 ++++-- cmd/skopeo/delete.go | 6 ++++-- cmd/skopeo/inspect.go | 4 ++-- cmd/skopeo/layers.go | 4 ++-- cmd/skopeo/main.go | 4 ++++ cmd/skopeo/utils.go | 9 +++++++++ completions/bash/skopeo | 1 + docs/skopeo.1.md | 2 ++ 8 files changed, 28 insertions(+), 8 deletions(-) diff --git a/cmd/skopeo/copy.go b/cmd/skopeo/copy.go index 798eb854..be8e9068 100644 --- a/cmd/skopeo/copy.go +++ b/cmd/skopeo/copy.go @@ -1,7 +1,6 @@ package main import ( - "context" "errors" "fmt" "os" @@ -88,7 +87,10 @@ func copyHandler(c *cli.Context) error { } } - _, err = copy.Image(context.Background(), policyContext, destRef, srcRef, ©.Options{ + ctx, cancel := commandTimeoutContextFromGlobalOptions(c) + defer cancel() + + _, err = copy.Image(ctx, policyContext, destRef, srcRef, ©.Options{ RemoveSignatures: removeSignatures, SignBy: signBy, ReportWriter: os.Stdout, diff --git a/cmd/skopeo/delete.go b/cmd/skopeo/delete.go index 4cc63b00..5cc7e203 100644 --- a/cmd/skopeo/delete.go +++ b/cmd/skopeo/delete.go @@ -1,7 +1,6 @@ package main import ( - "context" "errors" "fmt" "strings" @@ -25,7 +24,10 @@ func deleteHandler(c *cli.Context) error { if err != nil { return err } - return ref.DeleteImage(context.Background(), sys) + + ctx, cancel := commandTimeoutContextFromGlobalOptions(c) + defer cancel() + return ref.DeleteImage(ctx, sys) } var deleteCmd = cli.Command{ diff --git a/cmd/skopeo/inspect.go b/cmd/skopeo/inspect.go index aef03851..2238e436 100644 --- a/cmd/skopeo/inspect.go +++ b/cmd/skopeo/inspect.go @@ -1,7 +1,6 @@ package main import ( - "context" "encoding/json" "fmt" "strings" @@ -67,7 +66,8 @@ var inspectCmd = cli.Command{ }, }, Action: func(c *cli.Context) (retErr error) { - ctx := context.Background() + ctx, cancel := commandTimeoutContextFromGlobalOptions(c) + defer cancel() img, err := parseImage(ctx, c) if err != nil { diff --git a/cmd/skopeo/layers.go b/cmd/skopeo/layers.go index 84619580..166bd2a7 100644 --- a/cmd/skopeo/layers.go +++ b/cmd/skopeo/layers.go @@ -1,7 +1,6 @@ package main import ( - "context" "fmt" "io/ioutil" "os" @@ -26,7 +25,8 @@ var layersCmd = cli.Command{ return errors.New("Usage: layers imageReference [layer...]") } - ctx := context.Background() + ctx, cancel := commandTimeoutContextFromGlobalOptions(c) + defer cancel() sys, err := contextFromGlobalOptions(c, "") if err != nil { diff --git a/cmd/skopeo/main.go b/cmd/skopeo/main.go index 2dc524da..78fa2af3 100644 --- a/cmd/skopeo/main.go +++ b/cmd/skopeo/main.go @@ -60,6 +60,10 @@ func createApp() *cli.App { Value: "", Usage: "use `OS` instead of the running OS for choosing images", }, + cli.DurationFlag{ + Name: "command-timeout", + Usage: "timeout for the command execution", + }, } app.Before = func(c *cli.Context) error { if c.GlobalBool("debug") { diff --git a/cmd/skopeo/utils.go b/cmd/skopeo/utils.go index f07b24b6..4dd5308e 100644 --- a/cmd/skopeo/utils.go +++ b/cmd/skopeo/utils.go @@ -40,6 +40,15 @@ func contextFromGlobalOptions(c *cli.Context, flagPrefix string) (*types.SystemC return ctx, nil } +func commandTimeoutContextFromGlobalOptions(c *cli.Context) (context.Context, context.CancelFunc) { + ctx := context.Background() + var cancel context.CancelFunc = func() {} + if c.GlobalDuration("command-timeout") > 0 { + ctx, cancel = context.WithTimeout(ctx, c.GlobalDuration("command-timeout")) + } + return ctx, cancel +} + func parseCreds(creds string) (string, string, error) { if creds == "" { return "", "", errors.New("credentials can't be empty") diff --git a/completions/bash/skopeo b/completions/bash/skopeo index 8763ab14..9e81cd7b 100644 --- a/completions/bash/skopeo +++ b/completions/bash/skopeo @@ -110,6 +110,7 @@ _skopeo_skopeo() { --registries.d --override-arch --override-os + --command-timeout " local boolean_options=" --insecure-policy diff --git a/docs/skopeo.1.md b/docs/skopeo.1.md index eec1fdbe..91afa63b 100644 --- a/docs/skopeo.1.md +++ b/docs/skopeo.1.md @@ -59,6 +59,8 @@ Most commands refer to container images, using a _transport_`:`_details_ format. **--override-os** _OS_ Use _OS_ instead of the running OS for choosing images. + **--command-timeout** _duration_ Timeout for the command execution. + **--help**|**-h** Show help **--version**|**-v** print the version number