2013-01-19 19:07:19 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-02-27 07:47:59 -05:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2013-04-10 06:24:15 -04:00
|
|
|
"fmt"
|
2014-02-27 07:47:59 -05:00
|
|
|
"io/ioutil"
|
2014-02-01 06:38:39 -05:00
|
|
|
"os"
|
2015-04-23 16:45:34 -04:00
|
|
|
"runtime"
|
2014-02-01 06:38:39 -05:00
|
|
|
"strings"
|
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/api/client"
|
2015-02-04 16:22:38 -05:00
|
|
|
"github.com/docker/docker/autogen/dockerversion"
|
2015-04-13 10:17:14 -04:00
|
|
|
"github.com/docker/docker/opts"
|
2014-07-24 18:19:50 -04:00
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
2014-10-30 08:48:30 -04:00
|
|
|
"github.com/docker/docker/pkg/reexec"
|
2015-01-23 20:33:49 -05:00
|
|
|
"github.com/docker/docker/pkg/term"
|
2015-05-28 20:59:07 -04:00
|
|
|
"github.com/docker/docker/utils"
|
2013-01-24 02:14:46 -05:00
|
|
|
)
|
|
|
|
|
2014-02-27 07:47:59 -05:00
|
|
|
const (
|
2014-09-26 17:24:04 -04:00
|
|
|
defaultTrustKeyFile = "key.json"
|
|
|
|
defaultCaFile = "ca.pem"
|
|
|
|
defaultKeyFile = "key.pem"
|
|
|
|
defaultCertFile = "cert.pem"
|
2014-02-27 07:47:59 -05:00
|
|
|
)
|
|
|
|
|
2013-01-19 19:07:19 -05:00
|
|
|
func main() {
|
2015-03-19 21:07:56 -04:00
|
|
|
if reexec.Init() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-05 19:41:48 -05:00
|
|
|
// Set terminal emulation based on platform as required.
|
2015-03-22 12:55:21 -04:00
|
|
|
stdin, stdout, stderr := term.StdStreams()
|
2015-03-05 19:41:48 -05:00
|
|
|
|
|
|
|
initLogging(stderr)
|
|
|
|
|
2013-03-13 03:29:40 -04:00
|
|
|
flag.Parse()
|
2014-08-09 21:18:32 -04:00
|
|
|
// FIXME: validate daemon flags here
|
2013-10-04 22:25:15 -04:00
|
|
|
|
2013-08-06 23:51:12 -04:00
|
|
|
if *flVersion {
|
|
|
|
showVersion()
|
|
|
|
return
|
|
|
|
}
|
2014-10-01 09:07:24 -04:00
|
|
|
|
|
|
|
if *flLogLevel != "" {
|
2015-03-26 18:22:04 -04:00
|
|
|
lvl, err := logrus.ParseLevel(*flLogLevel)
|
2014-10-01 09:07:24 -04:00
|
|
|
if err != nil {
|
2015-05-09 14:33:06 -04:00
|
|
|
fmt.Fprintf(os.Stderr, "Unable to parse logging level: %s\n", *flLogLevel)
|
|
|
|
os.Exit(1)
|
2014-10-01 09:07:24 -04:00
|
|
|
}
|
2015-03-05 19:41:48 -05:00
|
|
|
setLogLevel(lvl)
|
2014-10-01 09:07:24 -04:00
|
|
|
} else {
|
2015-03-26 18:22:04 -04:00
|
|
|
setLogLevel(logrus.InfoLevel)
|
2014-10-01 09:07:24 -04:00
|
|
|
}
|
|
|
|
|
2014-08-01 13:34:06 -04:00
|
|
|
if *flDebug {
|
|
|
|
os.Setenv("DEBUG", "1")
|
2015-05-14 13:07:21 -04:00
|
|
|
setLogLevel(logrus.DebugLevel)
|
2014-08-01 13:34:06 -04:00
|
|
|
}
|
|
|
|
|
2014-08-11 18:30:01 -04:00
|
|
|
if len(flHosts) == 0 {
|
2013-12-20 19:30:41 -05:00
|
|
|
defaultHost := os.Getenv("DOCKER_HOST")
|
|
|
|
if defaultHost == "" || *flDaemon {
|
2015-04-23 16:45:34 -04:00
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
// If we do not have a host, default to unix socket
|
|
|
|
defaultHost = fmt.Sprintf("unix://%s", opts.DefaultUnixSocket)
|
|
|
|
} else {
|
|
|
|
// If we do not have a host, default to TCP socket on Windows
|
|
|
|
defaultHost = fmt.Sprintf("tcp://%s:%d", opts.DefaultHTTPHost, opts.DefaultHTTPPort)
|
|
|
|
}
|
2013-12-20 19:30:41 -05:00
|
|
|
}
|
2015-04-13 10:17:14 -04:00
|
|
|
defaultHost, err := opts.ValidateHost(defaultHost)
|
2014-08-26 12:32:10 -04:00
|
|
|
if err != nil {
|
2015-05-09 14:33:06 -04:00
|
|
|
if *flDaemon {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
} else {
|
|
|
|
fmt.Fprint(os.Stderr, err)
|
|
|
|
}
|
|
|
|
os.Exit(1)
|
2014-02-17 14:35:26 -05:00
|
|
|
}
|
2014-08-11 18:30:01 -04:00
|
|
|
flHosts = append(flHosts, defaultHost)
|
2013-06-19 08:31:54 -04:00
|
|
|
}
|
|
|
|
|
2015-01-21 11:14:30 -05:00
|
|
|
setDefaultConfFlag(flTrustKey, defaultTrustKeyFile)
|
|
|
|
|
2014-08-01 13:34:06 -04:00
|
|
|
if *flDaemon {
|
2015-03-02 16:45:45 -05:00
|
|
|
if *flHelp {
|
|
|
|
flag.Usage()
|
|
|
|
return
|
|
|
|
}
|
2014-08-01 13:34:06 -04:00
|
|
|
mainDaemon()
|
|
|
|
return
|
2014-06-27 09:55:20 -04:00
|
|
|
}
|
|
|
|
|
2014-08-11 18:30:01 -04:00
|
|
|
if len(flHosts) > 1 {
|
2015-05-09 14:33:06 -04:00
|
|
|
fmt.Fprintf(os.Stderr, "Please specify only one -H")
|
|
|
|
os.Exit(0)
|
2013-04-02 13:58:16 -04:00
|
|
|
}
|
2014-08-11 18:30:01 -04:00
|
|
|
protoAddrParts := strings.SplitN(flHosts[0], "://", 2)
|
2014-02-27 07:47:59 -05:00
|
|
|
|
2014-08-01 13:34:06 -04:00
|
|
|
var (
|
|
|
|
cli *client.DockerCli
|
|
|
|
tlsConfig tls.Config
|
|
|
|
)
|
|
|
|
tlsConfig.InsecureSkipVerify = true
|
|
|
|
|
2014-11-20 10:29:04 -05: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 13:34:06 -04: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 {
|
2015-05-09 14:33:06 -04:00
|
|
|
fmt.Fprintf(os.Stderr, "Couldn't read ca cert %s: %s\n", *flCa, err)
|
|
|
|
os.Exit(1)
|
2013-06-19 08:31:54 -04:00
|
|
|
}
|
2014-08-01 13:34:06 -04:00
|
|
|
certPool.AppendCertsFromPEM(file)
|
|
|
|
tlsConfig.RootCAs = certPool
|
|
|
|
tlsConfig.InsecureSkipVerify = false
|
|
|
|
}
|
2014-02-27 07:47:59 -05:00
|
|
|
|
2014-08-01 13:34:06 -04: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 07:47:59 -05:00
|
|
|
*flTls = true
|
2014-08-01 13:34:06 -04:00
|
|
|
cert, err := tls.LoadX509KeyPair(*flCert, *flKey)
|
2014-02-27 07:47:59 -05:00
|
|
|
if err != nil {
|
2015-05-09 14:33:06 -04:00
|
|
|
fmt.Fprintf(os.Stderr, "Couldn't load X509 key pair: %q. Make sure the key is encrypted\n", err)
|
|
|
|
os.Exit(1)
|
2014-02-27 07:47:59 -05:00
|
|
|
}
|
2014-08-01 13:34:06 -04:00
|
|
|
tlsConfig.Certificates = []tls.Certificate{cert}
|
2014-02-27 07:47:59 -05:00
|
|
|
}
|
2014-10-15 22:39:51 -04:00
|
|
|
// Avoid fallback to SSL protocols < TLS1.0
|
|
|
|
tlsConfig.MinVersion = tls.VersionTLS10
|
2014-08-01 13:34:06 -04:00
|
|
|
}
|
2014-02-27 07:47:59 -05:00
|
|
|
|
2014-08-01 13:34:06 -04:00
|
|
|
if *flTls || *flTlsVerify {
|
2015-01-23 20:33:49 -05:00
|
|
|
cli = client.NewDockerCli(stdin, stdout, stderr, *flTrustKey, protoAddrParts[0], protoAddrParts[1], &tlsConfig)
|
2014-08-01 13:34:06 -04:00
|
|
|
} else {
|
2015-01-23 20:33:49 -05:00
|
|
|
cli = client.NewDockerCli(stdin, stdout, stderr, *flTrustKey, protoAddrParts[0], protoAddrParts[1], nil)
|
2014-08-01 13:34:06 -04:00
|
|
|
}
|
2014-03-19 18:03:11 -04:00
|
|
|
|
2014-08-10 00:33:19 -04:00
|
|
|
if err := cli.Cmd(flag.Args()...); err != nil {
|
2015-04-15 04:57:52 -04:00
|
|
|
if sterr, ok := err.(client.StatusError); ok {
|
2014-08-01 13:34:06 -04:00
|
|
|
if sterr.Status != "" {
|
2015-05-09 14:33:06 -04:00
|
|
|
fmt.Fprintln(cli.Err(), sterr.Status)
|
|
|
|
os.Exit(1)
|
2013-09-09 17:26:35 -04:00
|
|
|
}
|
2014-08-01 13:34:06 -04:00
|
|
|
os.Exit(sterr.StatusCode)
|
2013-03-13 03:29:40 -04:00
|
|
|
}
|
2015-05-09 14:33:06 -04:00
|
|
|
fmt.Fprintln(cli.Err(), err)
|
|
|
|
os.Exit(1)
|
2013-03-13 03:29:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-06 23:51:12 -04:00
|
|
|
func showVersion() {
|
2015-05-28 20:59:07 -04:00
|
|
|
if utils.ExperimentalBuild() {
|
|
|
|
fmt.Printf("Docker version %s, build %s, experimental\n", dockerversion.VERSION, dockerversion.GITCOMMIT)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("Docker version %s, build %s\n", dockerversion.VERSION, dockerversion.GITCOMMIT)
|
|
|
|
}
|
2013-08-06 23:51:12 -04:00
|
|
|
}
|