Skip to content

Commit

Permalink
Working Swift object storage access for worker
Browse files Browse the repository at this point in the history
  • Loading branch information
kellrott committed Sep 14, 2016
1 parent 8335bfc commit f1f4bff
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/ga4gh-engine/worker/container_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (self *DockerEngine) Run(containerName string, args []string,
list, err := self.client.ImageList(context.Background(), types.ImageListOptions{MatchName: containerName})

if err != nil || len(list) == 0 {
log.Printf("Image %s not found", containerName)
log.Printf("Image %s not found: %s", containerName, err)
pull_opt := types.ImagePullOptions{}
r, err := self.client.ImagePull(context.Background(), containerName, pull_opt)
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion src/ga4gh-engine/worker/file_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ func (self *FileMapper) MapInput(jobId string, storage string, mountPath string,
if len(base) > 0 {
dstPath := path.Join(vol.HostPath, relpath)
fmt.Printf("get %s %s\n", storage, dstPath)
self.fileSystem.Get(storage, dstPath)
err := self.fileSystem.Get(storage, dstPath)
if err != nil {
return err
}
}
}
return nil
Expand Down
27 changes: 19 additions & 8 deletions src/ga4gh-engine/worker/swift_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import (
"github.com/rackspace/gophercloud/openstack"
"io"
"os"
"fmt"
"strings"
"log"
//"github.com/rackspace/gophercloud/openstack/objectstorage/v1/containers"
"github.com/rackspace/gophercloud/openstack/objectstorage/v1/objects"
//"github.com/rackspace/gophercloud/pagination"
)

var SWIFT_PROTOCOL = "sw://"
var SWIFT_PROTOCOL = "swift://"

type SwiftAccess struct {
client *gophercloud.ServiceClient
Expand All @@ -36,40 +38,49 @@ func NewSwiftAccess() *SwiftAccess {
}

func (self *SwiftAccess) Get(storage string, hostPath string) error {
log.Printf("Starting download of %s", storage)
storage = strings.TrimPrefix(storage, SWIFT_PROTOCOL)

opts := objects.DownloadOpts{}
storage_split := strings.SplitN(storage, "/", 2)

// Download everything into a DownloadResult struct
res := objects.Download(self.client, "container_name", "object_name", opts)
opts := objects.DownloadOpts{}
res := objects.Download(self.client, storage_split[0], storage_split[1], opts)

file, err := os.Create(hostPath)
if err != nil {
return err
}
buffer := make([]byte, 10240)
total_len := 0
for {
len, err := res.Body.Read(buffer)
if err == io.EOF {
break
} else if err != nil {
return fmt.Errorf("Error reading file")
}
total_len += len
file.Write(buffer[:len])
}
file.Close()
res.Body.Close()
log.Printf("Downloaded %d bytes", total_len)
return nil
}

func (self *SwiftAccess) Put(location string, hostPath string, directory bool) error {
opts := objects.CreateOpts{}

func (self *SwiftAccess) Put(storage string, hostPath string, directory bool) error {
log.Printf("Starting upload of %s", storage)
content, err := os.Open(hostPath)
if err != nil {
return err
}

storage = strings.TrimPrefix(storage, SWIFT_PROTOCOL)
storage_split := strings.SplitN(storage, "/", 2)

// Now execute the upload
res := objects.Create(self.client, "container_name", "object_name", content, opts)
opts := objects.CreateOpts{}
res := objects.Create(self.client, storage_split[0], storage_split[1], content, opts)
_, err = res.ExtractHeader()
content.Close()
return err
Expand Down

0 comments on commit f1f4bff

Please sign in to comment.