Skip to content

simple fix for #405 and #406 #488

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 3 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
36 changes: 24 additions & 12 deletions commands/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,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 @@ -315,23 +316,34 @@ 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)

err := cmd.Start() // can start ever fail?

go func() {
_, errStdout = io.Copy(stdout, stdoutIn)
}()

go func() {
_, errStderr = io.Copy(stderr, stderrIn)
}()

err = cmd.Wait()
if err != nil || errStdout != nil || errStderr != 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)
err = untarStream(bytes.NewReader(stdoutBuf.Bytes()))
if err != nil {
return errors.New("Error un-tarring the output of the init-image")
return errors.New("Error un-tarring the output of the init-image " + err.Error())
}

return nil
Expand Down