Merge pull request #565 from armstrongli/master

add timeout support for image copy
This commit is contained in:
Daniel J Walsh 2018-11-02 11:32:36 -04:00 committed by GitHub
commit ba649c56bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 28 additions and 8 deletions

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"context"
"errors" "errors"
"fmt" "fmt"
"os" "os"
@ -88,7 +87,10 @@ func copyHandler(c *cli.Context) error {
} }
} }
_, err = copy.Image(context.Background(), policyContext, destRef, srcRef, &copy.Options{ ctx, cancel := commandTimeoutContextFromGlobalOptions(c)
defer cancel()
_, err = copy.Image(ctx, policyContext, destRef, srcRef, &copy.Options{
RemoveSignatures: removeSignatures, RemoveSignatures: removeSignatures,
SignBy: signBy, SignBy: signBy,
ReportWriter: os.Stdout, ReportWriter: os.Stdout,

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"context"
"errors" "errors"
"fmt" "fmt"
"strings" "strings"
@ -25,7 +24,10 @@ func deleteHandler(c *cli.Context) error {
if err != nil { if err != nil {
return err return err
} }
return ref.DeleteImage(context.Background(), sys)
ctx, cancel := commandTimeoutContextFromGlobalOptions(c)
defer cancel()
return ref.DeleteImage(ctx, sys)
} }
var deleteCmd = cli.Command{ var deleteCmd = cli.Command{

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"strings" "strings"
@ -67,7 +66,8 @@ var inspectCmd = cli.Command{
}, },
}, },
Action: func(c *cli.Context) (retErr error) { Action: func(c *cli.Context) (retErr error) {
ctx := context.Background() ctx, cancel := commandTimeoutContextFromGlobalOptions(c)
defer cancel()
img, err := parseImage(ctx, c) img, err := parseImage(ctx, c)
if err != nil { if err != nil {

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"context"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
@ -26,7 +25,8 @@ var layersCmd = cli.Command{
return errors.New("Usage: layers imageReference [layer...]") return errors.New("Usage: layers imageReference [layer...]")
} }
ctx := context.Background() ctx, cancel := commandTimeoutContextFromGlobalOptions(c)
defer cancel()
sys, err := contextFromGlobalOptions(c, "") sys, err := contextFromGlobalOptions(c, "")
if err != nil { if err != nil {

View File

@ -60,6 +60,10 @@ func createApp() *cli.App {
Value: "", Value: "",
Usage: "use `OS` instead of the running OS for choosing images", 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 { app.Before = func(c *cli.Context) error {
if c.GlobalBool("debug") { if c.GlobalBool("debug") {

View File

@ -40,6 +40,15 @@ func contextFromGlobalOptions(c *cli.Context, flagPrefix string) (*types.SystemC
return ctx, nil 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) { func parseCreds(creds string) (string, string, error) {
if creds == "" { if creds == "" {
return "", "", errors.New("credentials can't be empty") return "", "", errors.New("credentials can't be empty")

View File

@ -110,6 +110,7 @@ _skopeo_skopeo() {
--registries.d --registries.d
--override-arch --override-arch
--override-os --override-os
--command-timeout
" "
local boolean_options=" local boolean_options="
--insecure-policy --insecure-policy

View File

@ -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. **--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 **--help**|**-h** Show help
**--version**|**-v** print the version number **--version**|**-v** print the version number