2013-01-20 08:07:19 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2014-02-27 20:47:59 +08:00
|
|
|
"crypto/tls"
|
|
|
|
|
"crypto/x509"
|
2013-04-10 18:24:15 +08:00
|
|
|
"fmt"
|
2014-02-27 20:47:59 +08:00
|
|
|
"io/ioutil"
|
2014-02-01 19:38:39 +08:00
|
|
|
"os"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2014-10-01 21:07:24 +08:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2014-07-25 06:19:50 +08: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 20:48:30 +08:00
|
|
|
"github.com/docker/docker/pkg/reexec"
|
2014-07-25 06:19:50 +08:00
|
|
|
"github.com/docker/docker/utils"
|
2013-01-24 15:14:46 +08:00
|
|
|
)
|
|
|
|
|
|
2014-02-27 20:47:59 +08:00
|
|
|
const (
|
2014-09-27 05:24:04 +08:00
|
|
|
defaultTrustKeyFile = "key.json"
|
|
|
|
|
defaultCaFile = "ca.pem"
|
|
|
|
|
defaultKeyFile = "key.pem"
|
|
|
|
|
defaultCertFile = "cert.pem"
|
2014-02-27 20:47:59 +08:00
|
|
|
)
|
|
|
|
|
|
2013-01-20 08:07:19 +08:00
|
|
|
func main() {
|
2014-08-09 04:18:18 +08:00
|
|
|
if reexec.Init() {
|
2013-03-13 15:29:40 +08:00
|
|
|
return
|
2013-01-26 03:26:18 +08:00
|
|
|
}
|
2014-10-25 01:12:35 +08:00
|
|
|
|
2013-03-13 15:29:40 +08:00
|
|
|
flag.Parse()
|
2014-08-10 09:18:32 +08:00
|
|
|
// FIXME: validate daemon flags here
|
2013-10-05 10:25:15 +08:00
|
|
|
|
2013-08-07 11:51:12 +08:00
|
|
|
if *flVersion {
|
|
|
|
|
showVersion()
|
|
|
|
|
return
|
|
|
|
|
}
|
2014-10-01 21:07:24 +08: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-02 01:34:06 +08:00
|
|
|
if *flDebug {
|
|
|
|
|
os.Setenv("DEBUG", "1")
|
2014-10-01 21:07:24 +08:00
|
|
|
initLogging(log.DebugLevel)
|
2014-08-02 01:34:06 +08:00
|
|
|
}
|
|
|
|
|
|
2014-08-12 06:30:01 +08:00
|
|
|
if len(flHosts) == 0 {
|
2013-12-21 08:30:41 +08:00
|
|
|
defaultHost := os.Getenv("DOCKER_HOST")
|
|
|
|
|
if defaultHost == "" || *flDaemon {
|
|
|
|
|
// If we do not have a host, default to unix socket
|
2014-01-30 03:26:54 +08:00
|
|
|
defaultHost = fmt.Sprintf("unix://%s", api.DEFAULTUNIXSOCKET)
|
2013-12-21 08:30:41 +08:00
|
|
|
}
|
2014-08-27 00:32:10 +08:00
|
|
|
defaultHost, err := api.ValidateHost(defaultHost)
|
|
|
|
|
if err != nil {
|
2014-02-18 03:35:26 +08:00
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
2014-08-12 06:30:01 +08:00
|
|
|
flHosts = append(flHosts, defaultHost)
|
2013-06-19 20:31:54 +08:00
|
|
|
}
|
|
|
|
|
|
2015-01-22 00:14:30 +08:00
|
|
|
setDefaultConfFlag(flTrustKey, defaultTrustKeyFile)
|
|
|
|
|
|
2014-08-02 01:34:06 +08:00
|
|
|
if *flDaemon {
|
|
|
|
|
mainDaemon()
|
|
|
|
|
return
|
2014-06-27 21:55:20 +08:00
|
|
|
}
|
|
|
|
|
|
2014-08-12 06:30:01 +08:00
|
|
|
if len(flHosts) > 1 {
|
2014-08-02 01:34:06 +08:00
|
|
|
log.Fatal("Please specify only one -H")
|
2013-04-03 01:58:16 +08:00
|
|
|
}
|
2014-08-12 06:30:01 +08:00
|
|
|
protoAddrParts := strings.SplitN(flHosts[0], "://", 2)
|
2014-02-27 20:47:59 +08:00
|
|
|
|
2014-08-02 01:34:06 +08:00
|
|
|
var (
|
|
|
|
|
cli *client.DockerCli
|
|
|
|
|
tlsConfig tls.Config
|
|
|
|
|
)
|
|
|
|
|
tlsConfig.InsecureSkipVerify = true
|
|
|
|
|
|
2014-11-20 23: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-02 01:34:06 +08: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 20:31:54 +08:00
|
|
|
}
|
2014-08-02 01:34:06 +08:00
|
|
|
certPool.AppendCertsFromPEM(file)
|
|
|
|
|
tlsConfig.RootCAs = certPool
|
|
|
|
|
tlsConfig.InsecureSkipVerify = false
|
|
|
|
|
}
|
2014-02-27 20:47:59 +08:00
|
|
|
|
2014-08-02 01:34:06 +08: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 20:47:59 +08:00
|
|
|
*flTls = true
|
2014-08-02 01:34:06 +08:00
|
|
|
cert, err := tls.LoadX509KeyPair(*flCert, *flKey)
|
2014-02-27 20:47:59 +08:00
|
|
|
if err != nil {
|
2014-08-02 01:34:06 +08:00
|
|
|
log.Fatalf("Couldn't load X509 key pair: %s. Key encrypted?", err)
|
2014-02-27 20:47:59 +08:00
|
|
|
}
|
2014-08-02 01:34:06 +08:00
|
|
|
tlsConfig.Certificates = []tls.Certificate{cert}
|
2014-02-27 20:47:59 +08:00
|
|
|
}
|
2014-10-16 10:39:51 +08:00
|
|
|
// Avoid fallback to SSL protocols < TLS1.0
|
|
|
|
|
tlsConfig.MinVersion = tls.VersionTLS10
|
2014-08-02 01:34:06 +08:00
|
|
|
}
|
2014-02-27 20:47:59 +08:00
|
|
|
|
2014-08-02 01:34:06 +08:00
|
|
|
if *flTls || *flTlsVerify {
|
2015-01-30 05:46:12 +08:00
|
|
|
cli = client.NewDockerCli(os.Stdin, os.Stdout, os.Stderr, *flTrustKey, protoAddrParts[0], protoAddrParts[1], &tlsConfig)
|
2014-08-02 01:34:06 +08:00
|
|
|
} else {
|
2015-01-30 05:46:12 +08:00
|
|
|
cli = client.NewDockerCli(os.Stdin, os.Stdout, os.Stderr, *flTrustKey, protoAddrParts[0], protoAddrParts[1], nil)
|
2014-08-02 01:34:06 +08:00
|
|
|
}
|
2014-03-20 06:03:11 +08:00
|
|
|
|
2014-08-10 12:33:19 +08:00
|
|
|
if err := cli.Cmd(flag.Args()...); err != nil {
|
2014-08-02 01:34:06 +08:00
|
|
|
if sterr, ok := err.(*utils.StatusError); ok {
|
|
|
|
|
if sterr.Status != "" {
|
2014-11-06 00:26:22 +08:00
|
|
|
log.Println(sterr.Status)
|
2013-09-10 05:26:35 +08:00
|
|
|
}
|
2014-08-02 01:34:06 +08:00
|
|
|
os.Exit(sterr.StatusCode)
|
2013-03-13 15:29:40 +08:00
|
|
|
}
|
2014-08-02 01:34:06 +08:00
|
|
|
log.Fatal(err)
|
2013-03-13 15:29:40 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-07 11:51:12 +08:00
|
|
|
func showVersion() {
|
2014-02-12 08:05:45 +08:00
|
|
|
fmt.Printf("Docker version %s, build %s\n", dockerversion.VERSION, dockerversion.GITCOMMIT)
|
2013-08-07 11:51:12 +08:00
|
|
|
}
|