-
Notifications
You must be signed in to change notification settings - Fork 20
/
start.go
80 lines (74 loc) · 1.83 KB
/
start.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"fmt"
"log"
"os"
"os/exec"
"os/signal"
"syscall"
"github.com/urfave/cli"
)
func startCmd() cli.Command {
return cli.Command{
Name: "start",
Usage: "start a functions server",
Action: start,
Flags: []cli.Flag{
cli.StringFlag{
Name: "log-level",
Usage: "--log-level DEBUG to enable debugging",
},
},
}
}
func start(c *cli.Context) error {
denvs := []string{}
if c.String("log-level") != "" {
denvs = append(denvs, "GIN_MODE="+c.String("log-level"))
}
// Socket mount: docker run --rm -it --name functions -v ${PWD}/data:/app/data -v /var/run/docker.sock:/var/run/docker.sock -p 8080:8080 funcy/functions
// OR dind: docker run --rm -it --name functions -v ${PWD}/data:/app/data --privileged -p 8080:8080 funcy/functions
wd, err := os.Getwd()
if err != nil {
log.Fatalln("Getwd failed:", err)
}
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",
}
for _, v := range denvs {
args = append(args, "-e", v)
}
args = append(args, functionsDockerImage)
cmd := exec.Command("docker", args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Start()
if err != nil {
log.Fatalln("starting command failed:", err)
}
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:
log.Println("interrupt caught, exiting")
err = cmd.Process.Kill()
if err != nil {
log.Println("error: could not kill process:", err)
}
case err := <-done:
if err != nil {
log.Println("error: processed finished with error", err)
} else {
log.Println("process finished gracefully without error")
}
}
return nil
}