From e6d963f0dd3c81c58fcb13d5f5106f370ee48e63 Mon Sep 17 00:00:00 2001 From: Travis Reeder Date: Mon, 22 May 2017 16:41:51 -0700 Subject: [PATCH] Added fn update too to get latest version. --- fn/main.go | 1 + fn/start.go | 13 +++++++---- fn/update.go | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 fn/update.go diff --git a/fn/main.go b/fn/main.go index 48818fdd8..d26c14a4f 100644 --- a/fn/main.go +++ b/fn/main.go @@ -71,6 +71,7 @@ GLOBAL OPTIONS: } app.Commands = []cli.Command{ startCmd(), + updateCmd(), initFn(), apps(), routes(), diff --git a/fn/start.go b/fn/start.go index 3a1437917..1187450fd 100644 --- a/fn/start.go +++ b/fn/start.go @@ -35,21 +35,26 @@ func startCmd() cli.Command { } func start(c *cli.Context) error { - denvs := "" + denvs := []string{} if c.String("log-level") != "" { - denvs += "-e GIN_MODE=" + c.String("log-level") + denvs = append(denvs, "GIN_MODE="+c.String("log-level")) } // docker run --rm -it --name functions -v ${PWD}/data:/app/data -v /var/run/docker.sock:/var/run/docker.sock -p 8080:8080 treeder/functions wd, err := os.Getwd() if err != nil { logrus.WithError(err).Fatalln("Getwd failed") } - cmd := exec.Command("docker", "run", "--rm", "-i", + args := []string{"run", "--rm", "-i", "--name", "functions", "-v", fmt.Sprintf("%s/data:/app/data", wd), "-v", "/var/run/docker.sock:/var/run/docker.sock", "-p", "8080:8080", - "treeder/functions") + } + for _, v := range denvs { + args = append(args, "-e", v) + } + args = append(args, "treeder/functions") + cmd := exec.Command("docker", args...) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr err = cmd.Start() diff --git a/fn/update.go b/fn/update.go new file mode 100644 index 000000000..f356640ae --- /dev/null +++ b/fn/update.go @@ -0,0 +1,64 @@ +package main + +/* +usage: fn init + +If there's a Dockerfile found, this will generate the basic file with just the image name. exit +It will then try to decipher the runtime based on the files in the current directory, if it can't figure it out, it will ask. +It will then take a best guess for what the entrypoint will be based on the language, it it can't guess, it will ask. + +*/ + +import ( + "os" + "os/exec" + "os/signal" + "syscall" + + "github.com/Sirupsen/logrus" + "github.com/urfave/cli" +) + +func updateCmd() cli.Command { + return cli.Command{ + Name: "update", + Usage: "pulls latest functions server", + Action: update, + } +} + +func update(c *cli.Context) error { + args := []string{"pull", + "treeder/functions:latest", + } + cmd := exec.Command("docker", args...) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + err := cmd.Start() + if err != nil { + logrus.WithError(err).Fatalln("starting command failed") + } + + done := make(chan error, 1) + go func() { + done <- cmd.Wait() + }() + // catch ctrl-c and kill + sigC := make(chan os.Signal, 2) + signal.Notify(sigC, os.Interrupt, syscall.SIGTERM) + select { + case <-sigC: + logrus.Infoln("interrupt caught, exiting") + err = cmd.Process.Kill() + if err != nil { + logrus.WithError(err).Errorln("Could not kill process") + } + case err := <-done: + if err != nil { + logrus.WithError(err).Errorln("processed finished with error") + } else { + logrus.Println("process done gracefully without error") + } + } + return nil +}