Skip to content

Commit

Permalink
Stream gcsfuse Output to Logger (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
maennchen authored Apr 23, 2020
1 parent def7874 commit 297e5c7
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions pkg/driver/mounter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package driver

import (
"bufio"
"context"
"errors"
"fmt"
Expand Down Expand Up @@ -41,24 +42,49 @@ func NewGcsFuseMounter(bucket string, keyFile string, flags []string) (*GcsFuseM
}

func (gcsfuse *GcsFuseMounter) Mount(target string) error {
args := []string{fmt.Sprintf("--key-file=%s", gcsfuse.keyFile), "-o=allow_other"}
args := []string{fmt.Sprintf("--key-file=%s", gcsfuse.keyFile), "-o=allow_other", "--foreground"}
args = append(args, gcsfuse.flags...)
args = append(args, gcsfuse.bucket, target)

if err := util.CreateDir(target); err != nil {
return status.Errorf(codes.Internal, "Unable to create target path %s: %v", target, err)
}

return FuseMount(target, gcsFuseCommand, args)
return FuseMount(target, gcsFuseCommand, args, gcsfuse.bucket)
}

func FuseMount(path string, command string, args []string) error {
func FuseMount(path string, command string, args []string, bucket string) error {
cmd := exec.Command(command, args...)
klog.V(3).Infof("Mounting fuse with command: %s and args: %s", command, args)

out, err := cmd.CombinedOutput()
stdout, err := cmd.StdoutPipe()
if err != nil {
return fmt.Errorf("Error fuseMount command: %s\nargs: %s\noutput: %s", command, args, out)
return err
}

stderr, err := cmd.StderrPipe()
if err != nil {
return err
}

scannerStdout := bufio.NewScanner(stdout)
go func() {
for scannerStdout.Scan() {
text := scannerStdout.Text()
klog.V(3).Infof("[%s] %s", bucket, text)
}
}()

scannerStderr := bufio.NewScanner(stderr)
go func() {
for scannerStderr.Scan() {
text := scannerStderr.Text()
klog.Errorf("[%s] %s", bucket, text)
}
}()

if err := cmd.Start(); err != nil {
return err
}

return WaitForMount(path, 10*time.Second)
Expand Down

0 comments on commit 297e5c7

Please sign in to comment.