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