2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-26 14:56:29 +00:00

Upgrade ncw/swift to v2

This commit is contained in:
greatroar 2021-04-09 11:24:28 +02:00 committed by greatroar
parent ef5672a902
commit e7ec0453b1
5 changed files with 25 additions and 31 deletions

View File

@ -693,7 +693,7 @@ func open(s string, gopts GlobalOptions, opts options.Options) (restic.Backend,
case "azure": case "azure":
be, err = azure.Open(cfg.(azure.Config), rt) be, err = azure.Open(cfg.(azure.Config), rt)
case "swift": case "swift":
be, err = swift.Open(cfg.(swift.Config), rt) be, err = swift.Open(globalOptions.ctx, cfg.(swift.Config), rt)
case "b2": case "b2":
be, err = b2.Open(globalOptions.ctx, cfg.(b2.Config), rt) be, err = b2.Open(globalOptions.ctx, cfg.(b2.Config), rt)
case "rest": case "rest":
@ -769,7 +769,7 @@ func create(s string, opts options.Options) (restic.Backend, error) {
case "azure": case "azure":
return azure.Create(cfg.(azure.Config), rt) return azure.Create(cfg.(azure.Config), rt)
case "swift": case "swift":
return swift.Open(cfg.(swift.Config), rt) return swift.Open(globalOptions.ctx, cfg.(swift.Config), rt)
case "b2": case "b2":
return b2.Create(globalOptions.ctx, cfg.(b2.Config), rt) return b2.Create(globalOptions.ctx, cfg.(b2.Config), rt)
case "rest": case "rest":

2
go.mod
View File

@ -19,7 +19,7 @@ require (
github.com/kurin/blazer v0.5.3 github.com/kurin/blazer v0.5.3
github.com/minio/minio-go/v7 v7.0.12 github.com/minio/minio-go/v7 v7.0.12
github.com/minio/sha256-simd v1.0.0 github.com/minio/sha256-simd v1.0.0
github.com/ncw/swift v1.0.53 github.com/ncw/swift/v2 v2.0.0
github.com/pkg/errors v0.9.1 github.com/pkg/errors v0.9.1
github.com/pkg/profile v1.6.0 github.com/pkg/profile v1.6.0
github.com/pkg/sftp v1.13.2 github.com/pkg/sftp v1.13.2

4
go.sum
View File

@ -275,8 +275,8 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lN
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI= github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modocache/gover v0.0.0-20171022184752-b58185e213c5/go.mod h1:caMODM3PzxT8aQXRPkAt8xlV/e7d7w8GM5g0fa5F0D8= github.com/modocache/gover v0.0.0-20171022184752-b58185e213c5/go.mod h1:caMODM3PzxT8aQXRPkAt8xlV/e7d7w8GM5g0fa5F0D8=
github.com/ncw/swift v1.0.53 h1:luHjjTNtekIEvHg5KdAFIBaH7bWfNkefwFnpDffSIks= github.com/ncw/swift/v2 v2.0.0 h1:Q1jkMe/yhCkx7yAKq4bBZ/Th3NR+ejRcwbVK8Pi1i/0=
github.com/ncw/swift v1.0.53/go.mod h1:23YIA4yWVnGwv2dQlN4bB7egfYX6YLn0Yo/S6zZO/ZM= github.com/ncw/swift/v2 v2.0.0/go.mod h1:z0A9RVdYPjNjXVo2pDOPxZ4eu3oarO1P91fTItcb+Kg=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pelletier/go-toml v1.9.3/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= github.com/pelletier/go-toml v1.9.3/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=

View File

@ -19,7 +19,7 @@ import (
"github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/restic"
"github.com/cenkalti/backoff/v4" "github.com/cenkalti/backoff/v4"
"github.com/ncw/swift" "github.com/ncw/swift/v2"
) )
// beSwift is a backend which stores the data on a swift endpoint. // beSwift is a backend which stores the data on a swift endpoint.
@ -36,7 +36,7 @@ var _ restic.Backend = &beSwift{}
// Open opens the swift backend at a container in region. The container is // Open opens the swift backend at a container in region. The container is
// created if it does not exist yet. // created if it does not exist yet.
func Open(cfg Config, rt http.RoundTripper) (restic.Backend, error) { func Open(ctx context.Context, cfg Config, rt http.RoundTripper) (restic.Backend, error) {
debug.Log("config %#v", cfg) debug.Log("config %#v", cfg)
sem, err := backend.NewSemaphore(cfg.Connections) sem, err := backend.NewSemaphore(cfg.Connections)
@ -79,18 +79,18 @@ func Open(cfg Config, rt http.RoundTripper) (restic.Backend, error) {
// Authenticate if needed // Authenticate if needed
if !be.conn.Authenticated() { if !be.conn.Authenticated() {
if err := be.conn.Authenticate(); err != nil { if err := be.conn.Authenticate(ctx); err != nil {
return nil, errors.Wrap(err, "conn.Authenticate") return nil, errors.Wrap(err, "conn.Authenticate")
} }
} }
// Ensure container exists // Ensure container exists
switch _, _, err := be.conn.Container(be.container); err { switch _, _, err := be.conn.Container(ctx, be.container); err {
case nil: case nil:
// Container exists // Container exists
case swift.ContainerNotFound: case swift.ContainerNotFound:
err = be.createContainer(cfg.DefaultContainerPolicy) err = be.createContainer(ctx, cfg.DefaultContainerPolicy)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "beSwift.createContainer") return nil, errors.Wrap(err, "beSwift.createContainer")
} }
@ -102,7 +102,7 @@ func Open(cfg Config, rt http.RoundTripper) (restic.Backend, error) {
return be, nil return be, nil
} }
func (be *beSwift) createContainer(policy string) error { func (be *beSwift) createContainer(ctx context.Context, policy string) error {
var h swift.Headers var h swift.Headers
if policy != "" { if policy != "" {
h = swift.Headers{ h = swift.Headers{
@ -110,7 +110,7 @@ func (be *beSwift) createContainer(policy string) error {
} }
} }
return be.conn.ContainerCreate(be.container, h) return be.conn.ContainerCreate(ctx, be.container, h)
} }
// Location returns this backend's location (the container name). // Location returns this backend's location (the container name).
@ -159,7 +159,7 @@ func (be *beSwift) openReader(ctx context.Context, h restic.Handle, length int,
} }
be.sem.GetToken() be.sem.GetToken()
obj, _, err := be.conn.ObjectOpen(be.container, objName, false, headers) obj, _, err := be.conn.ObjectOpen(ctx, be.container, objName, false, headers)
if err != nil { if err != nil {
debug.Log(" err %v", err) debug.Log(" err %v", err)
be.sem.ReleaseToken() be.sem.ReleaseToken()
@ -186,7 +186,9 @@ func (be *beSwift) Save(ctx context.Context, h restic.Handle, rd restic.RewindRe
debug.Log("PutObject(%v, %v, %v)", be.container, objName, encoding) debug.Log("PutObject(%v, %v, %v)", be.container, objName, encoding)
hdr := swift.Headers{"Content-Length": strconv.FormatInt(rd.Length(), 10)} hdr := swift.Headers{"Content-Length": strconv.FormatInt(rd.Length(), 10)}
_, err := be.conn.ObjectPut(be.container, objName, rd, true, hex.EncodeToString(rd.Hash()), encoding, hdr) _, err := be.conn.ObjectPut(ctx,
be.container, objName, rd, true, hex.EncodeToString(rd.Hash()),
encoding, hdr)
// swift does not return the upload length // swift does not return the upload length
debug.Log("%v, err %#v", objName, err) debug.Log("%v, err %#v", objName, err)
@ -202,7 +204,7 @@ func (be *beSwift) Stat(ctx context.Context, h restic.Handle) (bi restic.FileInf
be.sem.GetToken() be.sem.GetToken()
defer be.sem.ReleaseToken() defer be.sem.ReleaseToken()
obj, _, err := be.conn.Object(be.container, objName) obj, _, err := be.conn.Object(ctx, be.container, objName)
if err != nil { if err != nil {
debug.Log("Object() err %v", err) debug.Log("Object() err %v", err)
return restic.FileInfo{}, errors.Wrap(err, "conn.Object") return restic.FileInfo{}, errors.Wrap(err, "conn.Object")
@ -218,7 +220,7 @@ func (be *beSwift) Test(ctx context.Context, h restic.Handle) (bool, error) {
be.sem.GetToken() be.sem.GetToken()
defer be.sem.ReleaseToken() defer be.sem.ReleaseToken()
switch _, _, err := be.conn.Object(be.container, objName); err { switch _, _, err := be.conn.Object(ctx, be.container, objName); err {
case nil: case nil:
return true, nil return true, nil
@ -237,7 +239,7 @@ func (be *beSwift) Remove(ctx context.Context, h restic.Handle) error {
be.sem.GetToken() be.sem.GetToken()
defer be.sem.ReleaseToken() defer be.sem.ReleaseToken()
err := be.conn.ObjectDelete(be.container, objName) err := be.conn.ObjectDelete(ctx, be.container, objName)
debug.Log("Remove(%v) -> err %v", h, err) debug.Log("Remove(%v) -> err %v", h, err)
return errors.Wrap(err, "conn.ObjectDelete") return errors.Wrap(err, "conn.ObjectDelete")
} }
@ -250,10 +252,10 @@ func (be *beSwift) List(ctx context.Context, t restic.FileType, fn func(restic.F
prefix, _ := be.Basedir(t) prefix, _ := be.Basedir(t)
prefix += "/" prefix += "/"
err := be.conn.ObjectsWalk(be.container, &swift.ObjectsOpts{Prefix: prefix}, err := be.conn.ObjectsWalk(ctx, be.container, &swift.ObjectsOpts{Prefix: prefix},
func(opts *swift.ObjectsOpts) (interface{}, error) { func(ctx context.Context, opts *swift.ObjectsOpts) (interface{}, error) {
be.sem.GetToken() be.sem.GetToken()
newObjects, err := be.conn.Objects(be.container, opts) newObjects, err := be.conn.Objects(ctx, be.container, opts)
be.sem.ReleaseToken() be.sem.ReleaseToken()
if err != nil { if err != nil {
@ -270,18 +272,10 @@ func (be *beSwift) List(ctx context.Context, t restic.FileType, fn func(restic.F
Size: obj.Bytes, Size: obj.Bytes,
} }
if ctx.Err() != nil {
return nil, ctx.Err()
}
err := fn(fi) err := fn(fi)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if ctx.Err() != nil {
return nil, ctx.Err()
}
} }
return newObjects, nil return newObjects, nil
}) })

View File

@ -61,7 +61,7 @@ func newSwiftTestSuite(t testing.TB) *test.Suite {
Create: func(config interface{}) (restic.Backend, error) { Create: func(config interface{}) (restic.Backend, error) {
cfg := config.(swift.Config) cfg := config.(swift.Config)
be, err := swift.Open(cfg, tr) be, err := swift.Open(context.TODO(), cfg, tr)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -81,14 +81,14 @@ func newSwiftTestSuite(t testing.TB) *test.Suite {
// OpenFn is a function that opens a previously created temporary repository. // OpenFn is a function that opens a previously created temporary repository.
Open: func(config interface{}) (restic.Backend, error) { Open: func(config interface{}) (restic.Backend, error) {
cfg := config.(swift.Config) cfg := config.(swift.Config)
return swift.Open(cfg, tr) return swift.Open(context.TODO(), cfg, tr)
}, },
// CleanupFn removes data created during the tests. // CleanupFn removes data created during the tests.
Cleanup: func(config interface{}) error { Cleanup: func(config interface{}) error {
cfg := config.(swift.Config) cfg := config.(swift.Config)
be, err := swift.Open(cfg, tr) be, err := swift.Open(context.TODO(), cfg, tr)
if err != nil { if err != nil {
return err return err
} }