From 122041eb266b3ca1db3b5d5fbe8efbf22f038e98 Mon Sep 17 00:00:00 2001 From: David Delabassee Date: Mon, 26 Nov 2018 16:48:31 +0100 Subject: [PATCH 1/4] simple fix for #405 and #406 --- commands/init.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/commands/init.go b/commands/init.go index ca8ed2ec..c81633d4 100644 --- a/commands/init.go +++ b/commands/init.go @@ -315,6 +315,11 @@ func (a *initFnCmd) init(c *cli.Context) error { func runInitImage(initImage string, a *initFnCmd) error { fmt.Println("Building from init-image: " + initImage) + cmdCheck := exec.Command("docker", "inspect", "--type=image", initImage) + if cmdCheck.Run() != nil { + return errors.New("Error, can't pull the init-image! Make sure that Docker is running and that the init-image exists.") + } + // Run the initImage var c1ErrB bytes.Buffer tarR, tarW := io.Pipe() From 0f86cf5737e1d164bdd2dc31f0ac45d6c2b9e99c Mon Sep 17 00:00:00 2001 From: David Delabassee Date: Wed, 28 Nov 2018 10:54:33 +0100 Subject: [PATCH 2/4] handle failures to cope with wrong init-image and docker down --- commands/init.go | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/commands/init.go b/commands/init.go index c81633d4..8b4d257a 100644 --- a/commands/init.go +++ b/commands/init.go @@ -315,28 +315,34 @@ func (a *initFnCmd) init(c *cli.Context) error { func runInitImage(initImage string, a *initFnCmd) error { fmt.Println("Building from init-image: " + initImage) - cmdCheck := exec.Command("docker", "inspect", "--type=image", initImage) - if cmdCheck.Run() != nil { - return errors.New("Error, can't pull the init-image! Make sure that Docker is running and that the init-image exists.") - } + var stdoutBuf, stderrBuf bytes.Buffer + var errStdout, errStderr error + + cmd := exec.Command("docker", "run", "-e", "FN_FUNCTION_NAME="+a.ff.Name, initImage) + stdoutIn, _ := cmd.StdoutPipe() + stderrIn, _ := cmd.StderrPipe() + + stdout := io.Writer(&stdoutBuf) + stderr := io.Writer(&stderrBuf) + + err := cmd.Start() // can start ever fail? - // Run the initImage - var c1ErrB bytes.Buffer - tarR, tarW := io.Pipe() + go func() { + _, errStdout = io.Copy(stdout, stdoutIn) + }() - c1 := exec.Command("docker", "run", "-e", "FN_FUNCTION_NAME="+a.ff.Name, initImage) - c1.Stderr = &c1ErrB - c1.Stdout = tarW + go func() { + _, errStderr = io.Copy(stderr, stderrIn) + }() - c1Err := c1.Start() - if c1Err != nil { - fmt.Println(c1ErrB.String()) - return errors.New("Error running init-image") + 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 From ad6fb6d9e6d1cbc054e3b3694cd6abbde92cf7cb Mon Sep 17 00:00:00 2001 From: David Delabassee Date: Wed, 28 Nov 2018 16:53:26 +0100 Subject: [PATCH 3/4] remove the empty func dir when init-imag fails --- commands/init.go | 1 + 1 file changed, 1 insertion(+) diff --git a/commands/init.go b/commands/init.go index 8b4d257a..29636946 100644 --- a/commands/init.go +++ b/commands/init.go @@ -249,6 +249,7 @@ func (a *initFnCmd) init(c *cli.Context) error { err = runInitImage(initImage, a) if err != nil { + os.Remove(dir) return err } From 16a4573d99435c292866b6d2994de7017920ffcb Mon Sep 17 00:00:00 2001 From: David Delabassee Date: Thu, 29 Nov 2018 09:59:25 +0100 Subject: [PATCH 4/4] error handling simplification --- commands/init.go | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/commands/init.go b/commands/init.go index 29636946..0ae935b2 100644 --- a/commands/init.go +++ b/commands/init.go @@ -326,24 +326,20 @@ func runInitImage(initImage string, a *initFnCmd) error { stdout := io.Writer(&stdoutBuf) stderr := io.Writer(&stderrBuf) - err := cmd.Start() // can start ever fail? + errStart := cmd.Start() - go func() { - _, errStdout = io.Copy(stdout, stdoutIn) - }() + _, errStdout = io.Copy(stdout, stdoutIn) + _, errStderr = io.Copy(stderr, stderrIn) - go func() { - _, errStderr = io.Copy(stderr, stderrIn) - }() + errUntar := untarStream(bytes.NewReader(stdoutBuf.Bytes())) - 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 := 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(bytes.NewReader(stdoutBuf.Bytes())) - if err != nil { - return errors.New("Error un-tarring the output of the init-image " + err.Error()) + if errUntar != nil { + return errors.New("Error un-tarring the output of the init-image") } return nil