mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Merge branch 'clean-up-dockerfile-orphans' into 'master'
Clean up dockerfile orphans See merge request !112
This commit is contained in:
58
fn/common.go
58
fn/common.go
@@ -6,8 +6,10 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"os/signal"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode"
|
"unicode"
|
||||||
@@ -91,11 +93,11 @@ func dockerbuild(verbwriter io.Writer, path string, ff *funcfile) error {
|
|||||||
if helper == nil {
|
if helper == nil {
|
||||||
return fmt.Errorf("Cannot build, no language helper found for %v", *ff.Runtime)
|
return fmt.Errorf("Cannot build, no language helper found for %v", *ff.Runtime)
|
||||||
}
|
}
|
||||||
err := writeTmpDockerfile(helper, dir, ff)
|
dockerfile, err = writeTmpDockerfile(helper, dir, ff)
|
||||||
defer os.Remove(filepath.Join(dir, "Dockerfile"))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
defer os.Remove(dockerfile)
|
||||||
if helper.HasPreBuild() {
|
if helper.HasPreBuild() {
|
||||||
err := helper.PreBuild()
|
err := helper.PreBuild()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -105,18 +107,36 @@ func dockerbuild(verbwriter io.Writer, path string, ff *funcfile) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Building image %v\n", ff.FullName())
|
fmt.Printf("Building image %v\n", ff.FullName())
|
||||||
cmd := exec.Command("docker", "build",
|
|
||||||
"-t", ff.FullName(),
|
cancel := make(chan os.Signal, 3)
|
||||||
// "--no-cache",
|
signal.Notify(cancel, os.Interrupt) // and others perhaps
|
||||||
"--build-arg", "HTTP_PROXY",
|
defer signal.Stop(cancel)
|
||||||
"--build-arg", "HTTPS_PROXY",
|
|
||||||
".")
|
result := make(chan error, 1)
|
||||||
cmd.Dir = dir
|
|
||||||
cmd.Stderr = os.Stderr
|
go func(done chan<- error) {
|
||||||
cmd.Stdout = os.Stdout
|
cmd := exec.Command("docker", "build",
|
||||||
if err := cmd.Run(); err != nil {
|
"-t", ff.FullName(),
|
||||||
return fmt.Errorf("error running docker build: %v", err)
|
"-f", dockerfile,
|
||||||
|
// "--no-cache",
|
||||||
|
"--build-arg", "HTTP_PROXY",
|
||||||
|
"--build-arg", "HTTPS_PROXY",
|
||||||
|
".")
|
||||||
|
cmd.Dir = dir
|
||||||
|
cmd.Stderr = os.Stderr
|
||||||
|
cmd.Stdout = os.Stdout
|
||||||
|
done <- cmd.Run()
|
||||||
|
}(result)
|
||||||
|
|
||||||
|
select {
|
||||||
|
case err := <-result:
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error running docker build: %v", err)
|
||||||
|
}
|
||||||
|
case signal := <-cancel:
|
||||||
|
return fmt.Errorf("build cancelled on signal %v", signal)
|
||||||
}
|
}
|
||||||
|
|
||||||
if helper != nil {
|
if helper != nil {
|
||||||
err := helper.AfterBuild()
|
err := helper.AfterBuild()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -157,14 +177,14 @@ func exists(name string) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeTmpDockerfile(helper langs.LangHelper, dir string, ff *funcfile) error {
|
func writeTmpDockerfile(helper langs.LangHelper, dir string, ff *funcfile) (string, error) {
|
||||||
if ff.Entrypoint == "" && ff.Cmd == "" {
|
if ff.Entrypoint == "" && ff.Cmd == "" {
|
||||||
return errors.New("entrypoint and cmd are missing, you must provide one or the other")
|
return "", errors.New("entrypoint and cmd are missing, you must provide one or the other")
|
||||||
}
|
}
|
||||||
|
|
||||||
fd, err := os.Create(filepath.Join(dir, "Dockerfile"))
|
fd, err := ioutil.TempFile(dir, "Dockerfile")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return "", err
|
||||||
}
|
}
|
||||||
defer fd.Close()
|
defer fd.Close()
|
||||||
|
|
||||||
@@ -192,9 +212,9 @@ func writeTmpDockerfile(helper langs.LangHelper, dir string, ff *funcfile) error
|
|||||||
}
|
}
|
||||||
err = writeLines(fd, dfLines)
|
err = writeLines(fd, dfLines)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return "", err
|
||||||
}
|
}
|
||||||
return err
|
return fd.Name(), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeLines(w io.Writer, lines []string) error {
|
func writeLines(w io.Writer, lines []string) error {
|
||||||
|
|||||||
Reference in New Issue
Block a user