2015-02-13 14:46:14 -05:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-03-31 16:37:49 -04:00
|
|
|
"net"
|
2015-02-13 14:46:14 -05:00
|
|
|
"net/http"
|
|
|
|
|
2015-03-31 16:37:49 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-02-13 14:46:14 -05:00
|
|
|
"github.com/docker/docker/engine"
|
2015-02-13 18:08:42 -05:00
|
|
|
"github.com/docker/docker/pkg/systemd"
|
2015-02-13 14:46:14 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewServer sets up the required Server and does protocol specific checking.
|
|
|
|
func NewServer(proto, addr string, job *engine.Job) (Server, error) {
|
2015-03-31 16:37:49 -04:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
l net.Listener
|
|
|
|
r = createRouter(
|
|
|
|
job.Eng,
|
|
|
|
job.GetenvBool("Logging"),
|
|
|
|
job.GetenvBool("EnableCors"),
|
|
|
|
job.Getenv("CorsHeaders"),
|
|
|
|
job.Getenv("Version"),
|
|
|
|
)
|
|
|
|
)
|
2015-02-13 14:46:14 -05:00
|
|
|
switch proto {
|
|
|
|
case "fd":
|
2015-03-31 16:37:49 -04:00
|
|
|
ls, err := systemd.ListenFD(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
chErrors := make(chan error, len(ls))
|
|
|
|
// We don't want to start serving on these sockets until the
|
|
|
|
// daemon is initialized and installed. Otherwise required handlers
|
|
|
|
// won't be ready.
|
|
|
|
<-activationLock
|
|
|
|
// Since ListenFD will return one or more sockets we have
|
|
|
|
// to create a go func to spawn off multiple serves
|
|
|
|
for i := range ls {
|
|
|
|
listener := ls[i]
|
|
|
|
go func() {
|
|
|
|
httpSrv := http.Server{Handler: r}
|
|
|
|
chErrors <- httpSrv.Serve(listener)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
for i := 0; i < len(ls); i++ {
|
|
|
|
if err := <-chErrors; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, nil
|
2015-02-13 14:46:14 -05:00
|
|
|
case "tcp":
|
2015-03-31 16:37:49 -04:00
|
|
|
if !job.GetenvBool("TlsVerify") {
|
|
|
|
logrus.Infof("/!\\ DON'T BIND ON ANY IP ADDRESS WITHOUT setting -tlsverify IF YOU DON'T KNOW WHAT YOU'RE DOING /!\\")
|
|
|
|
}
|
|
|
|
if l, err = NewTcpSocket(addr, tlsConfigFromJob(job)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := allocateDaemonPort(addr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-02-13 14:46:14 -05:00
|
|
|
case "unix":
|
2015-03-31 16:37:49 -04:00
|
|
|
if l, err = NewUnixSocket(addr, job.Getenv("SocketGroup")); err != nil {
|
|
|
|
return nil, err
|
2015-02-13 18:08:42 -05:00
|
|
|
}
|
2015-03-31 16:37:49 -04:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("Invalid protocol format: %q", proto)
|
2015-02-13 18:08:42 -05:00
|
|
|
}
|
2015-03-31 16:37:49 -04:00
|
|
|
return &HttpServer{
|
|
|
|
&http.Server{
|
|
|
|
Addr: addr,
|
|
|
|
Handler: r,
|
|
|
|
},
|
|
|
|
l,
|
|
|
|
}, nil
|
2015-02-13 18:08:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called through eng.Job("acceptconnections")
|
2015-03-25 03:44:12 -04:00
|
|
|
func AcceptConnections(job *engine.Job) error {
|
2015-02-13 18:08:42 -05:00
|
|
|
// Tell the init daemon we are accepting requests
|
|
|
|
go systemd.SdNotify("READY=1")
|
|
|
|
// close the lock so the listeners start accepting connections
|
2015-03-31 18:12:15 -04:00
|
|
|
select {
|
|
|
|
case <-activationLock:
|
|
|
|
default:
|
2015-02-13 18:08:42 -05:00
|
|
|
close(activationLock)
|
|
|
|
}
|
2015-03-25 03:44:12 -04:00
|
|
|
return nil
|
2015-02-13 18:08:42 -05:00
|
|
|
}
|