Skip to content

Commit

Permalink
refactoring copy task to use build vfs func
Browse files Browse the repository at this point in the history
Updated comments for copytask, still need to translate URL.
  • Loading branch information
chrislovecnm committed Dec 15, 2017
1 parent 1134e78 commit 8eb2ce3
Showing 1 changed file with 24 additions and 23 deletions.
47 changes: 24 additions & 23 deletions upup/pkg/fi/assettasks/copyfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,26 @@ func transferFile(c *fi.Context, source string, target string, sha string) error
return fmt.Errorf("error downloading file %q: %v", source, err)
}

uploadVFS, err := buildVFSPath(target)
objectStore, err := buildVFSPath(target)
if err != nil {
return err
}

shaVFS, err := buildVFSPath(target + ".sha1")
uploadVFS, err := vfs.Context.BuildVfsPath(objectStore)
if err != nil {
return err
return fmt.Errorf("error building path %q: %v", objectStore, err)
}

shaTarget := objectStore + ".sha1"
shaVFS, err := vfs.Context.BuildVfsPath(shaTarget)
if err != nil {
return fmt.Errorf("error building path %q: %v", shaTarget, err)
}

in := bytes.NewReader(data)
dataHash, err := hashing.HashAlgorithmSHA1.Hash(in)
if err != nil {
return fmt.Errorf("unable to parse sha from file %q downloaded: %v", target+".sha1", err)
return fmt.Errorf("unable to parse sha from file %q downloaded: %v", sha, err)
}

shaHash, err := hashing.FromString(strings.TrimSpace(sha))
Expand All @@ -154,43 +160,38 @@ func transferFile(c *fi.Context, source string, target string, sha string) error
}

if !shaHash.Equal(dataHash) {
return fmt.Errorf("the sha value in %q does not match %q calculated value %q", target+".sha1", source, dataHash.String())
return fmt.Errorf("the sha value in %q does not match %q calculated value %q", shaTarget, source, dataHash.String())
}

glog.Infof("uploading %q to %q", source, target)
glog.Infof("uploading %q to %q", source, objectStore)
if err := writeFile(c, uploadVFS, data); err != nil {
return fmt.Errorf("error uploading file %q: %v", uploadVFS, err)
return err
}

b := []byte(shaHash.Hex())
if err := writeFile(c, shaVFS, b); err != nil {
return fmt.Errorf("error uploading sha %q: %v", shaVFS, err)
return err
}

return nil
}

func writeFile(c *fi.Context, vfsPath string, data []byte) error {
glog.V(2).Infof("uploading to %q", vfsPath)
p, err := vfs.Context.BuildVfsPath(vfsPath)
if err != nil {
return fmt.Errorf("error building path %q: %v", vfsPath, err)
}
func writeFile(c *fi.Context, p vfs.Path, data []byte) error {

acl, err := acls.GetACL(p, c.Cluster)
if err != nil {
return err
}

if err = p.WriteFile(data, acl); err != nil {
return fmt.Errorf("error writing path %q: %v", vfsPath, err)
return fmt.Errorf("error writing path %v: %v", p, err)
}

glog.V(2).Infof("upload complete: %q", vfsPath)

return nil
}

// buildVFSPath task a recognizable https url and transforms that URL into the equivalent url with the the object
// store prefix.
func buildVFSPath(target string) (string, error) {
if !strings.Contains(target, "://") || strings.HasPrefix(target, "memfs://") {
return target, nil
Expand All @@ -203,20 +204,20 @@ func buildVFSPath(target string) (string, error) {

var vfsPath string

// I am not a huge fan of this, but it does work. We could have two urls, one for vfs and one for https?
// These matches only cover a subset of the URLs that you can use, but I am uncertain how to cover more of the possible
// options.
// This code parses the HOST and determines to use s3 or gs.
// URLs. For instance you can have the bucket name in the s3 url hostname.
// We are translating known https urls such as https://s3.amazonaws.com/example-kops to vfs path like
// s3://example-kops
if u.Host == "s3.amazonaws.com" {
vfsPath = "s3:/" + u.Path
} else if u.Host == "storage.googleapis.com" {
vfsPath = "gs:/" + u.Path
}

if vfsPath == "" {
} else {
glog.Errorf("unable to determine vfs path s3, google storage, and file paths are supported")
glog.Errorf("for s3 use s3.amazonaws.com and for google storage use storage.googleapis.com hostnames.")
return "", fmt.Errorf("unable to determine vfs for %q", target)
glog.Errorf("URLs starting with https://s3.amazonaws.com and http://storage.googleapis.com are transformed into s3 and gs URLs")
return "", fmt.Errorf("unable to determine vfs type for %q", target)
}

return vfsPath, nil
Expand Down

0 comments on commit 8eb2ce3

Please sign in to comment.