2013-01-19 16:07:19 -08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-02-27 13:47:59 +01:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2013-04-10 12:24:15 +02:00
|
|
|
"fmt"
|
2014-02-27 13:47:59 +01:00
|
|
|
"io/ioutil"
|
2014-02-01 03:38:39 -08:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2014-10-01 06:07:24 -07:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2014-07-24 22:19:50 +00:00
|
|
|
"github.com/docker/docker/api"
|
|
|
|
"github.com/docker/docker/api/client"
|
|
|
|
"github.com/docker/docker/dockerversion"
|
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
2014-10-30 14:48:30 +02:00
|
|
|
"github.com/docker/docker/pkg/reexec"
|
2014-07-24 22:19:50 +00:00
|
|
|
"github.com/docker/docker/utils"
|
2013-01-23 23:14:46 -08:00
|
|
|
)
|
|
|
|
|
2014-02-27 13:47:59 +01:00
|
|
|
const (
|
2014-09-26 14:24:04 -07:00
|
|
|
defaultTrustKeyFile = "key.json"
|
|
|
|
defaultCaFile = "ca.pem"
|
|
|
|
defaultKeyFile = "key.pem"
|
|
|
|
defaultCertFile = "cert.pem"
|
2014-02-27 13:47:59 +01:00
|
|
|
)
|
|
|
|
|
2013-01-19 16:07:19 -08:00
|
|
|
func main() {
|
2014-08-08 13:18:18 -07:00
|
|
|
if reexec.Init() {
|
2013-03-13 00:29:40 -07:00
|
|
|
return
|
2013-01-25 11:26:18 -08:00
|
|
|
}
|
2014-10-24 10:12:35 -07:00
|
|
|
|
2013-03-13 00:29:40 -07:00
|
|
|
flag.Parse()
|
2014-08-10 01:18:32 +00:00
|
|
|
// FIXME: validate daemon flags here
|
2013-10-04 19:25:15 -07:00
|
|
|
|
2013-08-06 20:51:12 -07:00
|
|
|
if *flVersion {
|
|
|
|
showVersion()
|
|
|
|
return
|
|
|
|
}
|
2014-10-01 06:07:24 -07:00
|
|
|
|
|
|
|
if *flLogLevel != "" {
|
|
|
|
lvl, err := log.ParseLevel(*flLogLevel)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Unable to parse logging level: %s", *flLogLevel)
|
|
|
|
}
|
|
|
|
initLogging(lvl)
|
|
|
|
} else {
|
|
|
|
initLogging(log.InfoLevel)
|
|
|
|
}
|
|
|
|
|
|
|
|
// -D, --debug, -l/--log-level=debug processing
|
|
|
|
// When/if -D is removed this block can be deleted
|
2014-08-01 11:34:06 -06:00
|
|
|
if *flDebug {
|
|
|
|
os.Setenv("DEBUG", "1")
|
2014-10-01 06:07:24 -07:00
|
|
|
initLogging(log.DebugLevel)
|
2014-08-01 11:34:06 -06:00
|
|
|
}
|
|
|
|
|
2014-08-11 22:30:01 +00:00
|
|
|
if len(flHosts) == 0 {
|
2013-12-20 19:30:41 -05:00
|
|
|
defaultHost := os.Getenv("DOCKER_HOST")
|
|
|
|
if defaultHost == "" || *flDaemon {
|
|
|
|
// If we do not have a host, default to unix socket
|
2014-01-29 19:26:54 +00:00
|
|
|
defaultHost = fmt.Sprintf("unix://%s", api.DEFAULTUNIXSOCKET)
|
2013-12-20 19:30:41 -05:00
|
|
|
}
|
2014-08-26 20:32:10 +04:00
|
|
|
defaultHost, err := api.ValidateHost(defaultHost)
|
|
|
|
if err != nil {
|
2014-02-17 11:35:26 -08:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-08-11 22:30:01 +00:00
|
|
|
flHosts = append(flHosts, defaultHost)
|
2013-06-19 12:31:54 +00:00
|
|
|
}
|
|
|
|
|
2014-08-01 11:34:06 -06:00
|
|
|
if *flDaemon {
|
|
|
|
mainDaemon()
|
|
|
|
return
|
2014-06-27 15:55:20 +02:00
|
|
|
}
|
|
|
|
|
2014-08-11 22:30:01 +00:00
|
|
|
if len(flHosts) > 1 {
|
2014-08-01 11:34:06 -06:00
|
|
|
log.Fatal("Please specify only one -H")
|
2013-04-02 10:58:16 -07:00
|
|
|
}
|
2014-08-11 22:30:01 +00:00
|
|
|
protoAddrParts := strings.SplitN(flHosts[0], "://", 2)
|
2014-02-27 13:47:59 +01:00
|
|
|
|
2014-08-01 11:34:06 -06:00
|
|
|
var (
|
|
|
|
cli *client.DockerCli
|
|
|
|
tlsConfig tls.Config
|
|
|
|
)
|
|
|
|
tlsConfig.InsecureSkipVerify = true
|
|
|
|
|
2014-11-20 07:29:04 -08:00
|
|
|
// Regardless of whether the user sets it to true or false, if they
|
|
|
|
// specify --tlsverify at all then we need to turn on tls
|
|
|
|
if flag.IsSet("-tlsverify") {
|
|
|
|
*flTls = true
|
|
|
|
}
|
|
|
|
|
2014-08-01 11:34:06 -06:00
|
|
|
// If we should verify the server, we need to load a trusted ca
|
|
|
|
if *flTlsVerify {
|
|
|
|
certPool := x509.NewCertPool()
|
|
|
|
file, err := ioutil.ReadFile(*flCa)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Couldn't read ca cert %s: %s", *flCa, err)
|
2013-06-19 12:31:54 +00:00
|
|
|
}
|
2014-08-01 11:34:06 -06:00
|
|
|
certPool.AppendCertsFromPEM(file)
|
|
|
|
tlsConfig.RootCAs = certPool
|
|
|
|
tlsConfig.InsecureSkipVerify = false
|
|
|
|
}
|
2014-02-27 13:47:59 +01:00
|
|
|
|
2014-08-01 11:34:06 -06:00
|
|
|
// If tls is enabled, try to load and send client certificates
|
|
|
|
if *flTls || *flTlsVerify {
|
|
|
|
_, errCert := os.Stat(*flCert)
|
|
|
|
_, errKey := os.Stat(*flKey)
|
|
|
|
if errCert == nil && errKey == nil {
|
2014-02-27 13:47:59 +01:00
|
|
|
*flTls = true
|
2014-08-01 11:34:06 -06:00
|
|
|
cert, err := tls.LoadX509KeyPair(*flCert, *flKey)
|
2014-02-27 13:47:59 +01:00
|
|
|
if err != nil {
|
2014-08-01 11:34:06 -06:00
|
|
|
log.Fatalf("Couldn't load X509 key pair: %s. Key encrypted?", err)
|
2014-02-27 13:47:59 +01:00
|
|
|
}
|
2014-08-01 11:34:06 -06:00
|
|
|
tlsConfig.Certificates = []tls.Certificate{cert}
|
2014-02-27 13:47:59 +01:00
|
|
|
}
|
2014-10-15 22:39:51 -04:00
|
|
|
// Avoid fallback to SSL protocols < TLS1.0
|
|
|
|
tlsConfig.MinVersion = tls.VersionTLS10
|
2014-08-01 11:34:06 -06:00
|
|
|
}
|
2014-02-27 13:47:59 +01:00
|
|
|
|
2014-08-01 11:34:06 -06:00
|
|
|
if *flTls || *flTlsVerify {
|
2014-10-14 17:16:45 +00:00
|
|
|
cli = client.NewDockerCli(os.Stdin, os.Stdout, os.Stderr, nil, protoAddrParts[0], protoAddrParts[1], &tlsConfig)
|
2014-08-01 11:34:06 -06:00
|
|
|
} else {
|
2014-10-14 17:16:45 +00:00
|
|
|
cli = client.NewDockerCli(os.Stdin, os.Stdout, os.Stderr, nil, protoAddrParts[0], protoAddrParts[1], nil)
|
2014-08-01 11:34:06 -06:00
|
|
|
}
|
2014-03-19 22:03:11 +00:00
|
|
|
|
2014-08-10 04:33:19 +00:00
|
|
|
if err := cli.Cmd(flag.Args()...); err != nil {
|
2014-08-01 11:34:06 -06:00
|
|
|
if sterr, ok := err.(*utils.StatusError); ok {
|
|
|
|
if sterr.Status != "" {
|
2014-11-05 08:26:22 -08:00
|
|
|
log.Println(sterr.Status)
|
2013-09-09 21:26:35 +00:00
|
|
|
}
|
2014-08-01 11:34:06 -06:00
|
|
|
os.Exit(sterr.StatusCode)
|
2013-03-13 00:29:40 -07:00
|
|
|
}
|
2014-08-01 11:34:06 -06:00
|
|
|
log.Fatal(err)
|
2013-03-13 00:29:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-06 20:51:12 -07:00
|
|
|
func showVersion() {
|
2014-02-11 16:05:45 -08:00
|
|
|
fmt.Printf("Docker version %s, build %s\n", dockerversion.VERSION, dockerversion.GITCOMMIT)
|
2013-08-06 20:51:12 -07:00
|
|
|
}
|