Merge branch 'create_pidfile' of https://github.com/flavio/docker into flavio-create_pidfile
This commit is contained in:
commit
c1703c2b68
1 changed files with 42 additions and 2 deletions
|
@ -2,12 +2,15 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
|
"fmt"
|
||||||
"github.com/dotcloud/docker"
|
"github.com/dotcloud/docker"
|
||||||
"github.com/dotcloud/docker/rcli"
|
"github.com/dotcloud/docker/rcli"
|
||||||
"github.com/dotcloud/docker/term"
|
"github.com/dotcloud/docker/term"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
var GIT_COMMIT string
|
var GIT_COMMIT string
|
||||||
|
@ -22,6 +25,7 @@ func main() {
|
||||||
flDaemon := flag.Bool("d", false, "Daemon mode")
|
flDaemon := flag.Bool("d", false, "Daemon mode")
|
||||||
flDebug := flag.Bool("D", false, "Debug mode")
|
flDebug := flag.Bool("D", false, "Debug mode")
|
||||||
bridgeName := flag.String("b", "", "Attach containers to a pre-existing network bridge")
|
bridgeName := flag.String("b", "", "Attach containers to a pre-existing network bridge")
|
||||||
|
pidfile := flag.String("p", "/var/run/docker.pid", "File containing process PID")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
if *bridgeName != "" {
|
if *bridgeName != "" {
|
||||||
docker.NetworkBridgeIface = *bridgeName
|
docker.NetworkBridgeIface = *bridgeName
|
||||||
|
@ -37,7 +41,7 @@ func main() {
|
||||||
flag.Usage()
|
flag.Usage()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err := daemon(); err != nil {
|
if err := daemon(*pidfile); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -47,7 +51,43 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func daemon() error {
|
func createPidFile(pidfile string) error {
|
||||||
|
if _, err := os.Stat(pidfile); err == nil {
|
||||||
|
return fmt.Errorf("pid file found, ensure docker is not running or delete %s", pidfile)
|
||||||
|
}
|
||||||
|
|
||||||
|
file, err := os.Create(pidfile)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
_, err = fmt.Fprintf(file, "%d", os.Getpid())
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func removePidFile(pidfile string) {
|
||||||
|
if err := os.Remove(pidfile); err != nil {
|
||||||
|
log.Printf("Error removing %s: %s", pidfile, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func daemon(pidfile string) error {
|
||||||
|
if err := createPidFile(pidfile); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
defer removePidFile(pidfile)
|
||||||
|
|
||||||
|
c := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(c, os.Interrupt, os.Kill, os.Signal(syscall.SIGTERM))
|
||||||
|
go func() {
|
||||||
|
sig := <-c
|
||||||
|
log.Printf("Received signal '%v', exiting\n", sig)
|
||||||
|
removePidFile(pidfile)
|
||||||
|
os.Exit(0)
|
||||||
|
}()
|
||||||
|
|
||||||
service, err := docker.NewServer()
|
service, err := docker.NewServer()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
Loading…
Reference in a new issue