Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simple fix for #405 and #406 #488

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions commands/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ func (a *initFnCmd) init(c *cli.Context) error {

err = runInitImage(initImage, a)
if err != nil {
os.Remove(dir)
return err
}

Expand Down Expand Up @@ -303,22 +304,29 @@ func (a *initFnCmd) init(c *cli.Context) error {
func runInitImage(initImage string, a *initFnCmd) error {
fmt.Println("Building from init-image: " + initImage)

// Run the initImage
var c1ErrB bytes.Buffer
tarR, tarW := io.Pipe()
var stdoutBuf, stderrBuf bytes.Buffer
var errStdout, errStderr error

c1 := exec.Command("docker", "run", "-e", "FN_FUNCTION_NAME="+a.ff.Name, initImage)
c1.Stderr = &c1ErrB
c1.Stdout = tarW
cmd := exec.Command("docker", "run", "-e", "FN_FUNCTION_NAME="+a.ff.Name, initImage)
stdoutIn, _ := cmd.StdoutPipe()
stderrIn, _ := cmd.StderrPipe()

c1Err := c1.Start()
if c1Err != nil {
fmt.Println(c1ErrB.String())
return errors.New("Error running init-image")
stdout := io.Writer(&stdoutBuf)
stderr := io.Writer(&stderrBuf)

errStart := cmd.Start()

_, errStdout = io.Copy(stdout, stdoutIn)
_, errStderr = io.Copy(stderr, stderrIn)

errUntar := untarStream(bytes.NewReader(stdoutBuf.Bytes()))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry for delay here. we can use the io.Pipe and not need to buffer here - this is a good idea because images are on the order of megabytes. we don't actually use stderr it seems so i'm thinking something like:

tarIn, tarOut := io.Pipe()
cmd := exec.Command("...")
cmd.Stderr = ioutil.Discard // honestly, os.Stderr makes sense, we can do w/e
cmd.Stdout = tarOut
err := cmd.Start()
if err != nil {
  return err
}
err = untarStream(tarIn)
if err != nil {
  return err // can format error about init image here, add stderr if we want
}
err = cmd.Wait()
if err != nil {
  return err // can format error about init image here, add stderr if we want
}

this pretty much expects that untar stream will fuck up if the command fucks up. if for some reason it doesn't, then we can get the exit code out of Wait(). in the happy path we expect untar to just work, and Wait() to return a nil error. I think that this makes sense, but double check me :) wdyt?


err := cmd.Wait()
if err != nil || errStdout != nil || errStderr != nil || errStart != nil {
return errors.New("Error, unbale to use the init-image! Make sure that Docker is running and that the init-image exists")
}

err := untarStream(tarR)
if err != nil {
if errUntar != nil {
return errors.New("Error un-tarring the output of the init-image")
}

Expand Down