github.com/codemac/docker@v1.2.1-0.20150518222241-6a18412d5b9c/docker/docker.go (about) 1 package main 2 3 import ( 4 "crypto/tls" 5 "crypto/x509" 6 "fmt" 7 "io/ioutil" 8 "os" 9 "runtime" 10 "strings" 11 12 "github.com/Sirupsen/logrus" 13 "github.com/docker/docker/api/client" 14 "github.com/docker/docker/autogen/dockerversion" 15 "github.com/docker/docker/opts" 16 flag "github.com/docker/docker/pkg/mflag" 17 "github.com/docker/docker/pkg/reexec" 18 "github.com/docker/docker/pkg/term" 19 ) 20 21 const ( 22 defaultTrustKeyFile = "key.json" 23 defaultCaFile = "ca.pem" 24 defaultKeyFile = "key.pem" 25 defaultCertFile = "cert.pem" 26 ) 27 28 func main() { 29 if reexec.Init() { 30 return 31 } 32 33 // Set terminal emulation based on platform as required. 34 stdin, stdout, stderr := term.StdStreams() 35 36 initLogging(stderr) 37 38 flag.Parse() 39 // FIXME: validate daemon flags here 40 41 if *flVersion { 42 showVersion() 43 return 44 } 45 46 if *flLogLevel != "" { 47 lvl, err := logrus.ParseLevel(*flLogLevel) 48 if err != nil { 49 fmt.Fprintf(os.Stderr, "Unable to parse logging level: %s\n", *flLogLevel) 50 os.Exit(1) 51 } 52 setLogLevel(lvl) 53 } else { 54 setLogLevel(logrus.InfoLevel) 55 } 56 57 if *flDebug { 58 os.Setenv("DEBUG", "1") 59 setLogLevel(logrus.DebugLevel) 60 } 61 62 if len(flHosts) == 0 { 63 defaultHost := os.Getenv("DOCKER_HOST") 64 if defaultHost == "" || *flDaemon { 65 if runtime.GOOS != "windows" { 66 // If we do not have a host, default to unix socket 67 defaultHost = fmt.Sprintf("unix://%s", opts.DefaultUnixSocket) 68 } else { 69 // If we do not have a host, default to TCP socket on Windows 70 defaultHost = fmt.Sprintf("tcp://%s:%d", opts.DefaultHTTPHost, opts.DefaultHTTPPort) 71 } 72 } 73 defaultHost, err := opts.ValidateHost(defaultHost) 74 if err != nil { 75 if *flDaemon { 76 logrus.Fatal(err) 77 } else { 78 fmt.Fprint(os.Stderr, err) 79 } 80 os.Exit(1) 81 } 82 flHosts = append(flHosts, defaultHost) 83 } 84 85 setDefaultConfFlag(flTrustKey, defaultTrustKeyFile) 86 87 if *flDaemon { 88 if *flHelp { 89 flag.Usage() 90 return 91 } 92 mainDaemon() 93 return 94 } 95 96 if len(flHosts) > 1 { 97 fmt.Fprintf(os.Stderr, "Please specify only one -H") 98 os.Exit(0) 99 } 100 protoAddrParts := strings.SplitN(flHosts[0], "://", 2) 101 102 var ( 103 cli *client.DockerCli 104 tlsConfig tls.Config 105 ) 106 tlsConfig.InsecureSkipVerify = true 107 108 // Regardless of whether the user sets it to true or false, if they 109 // specify --tlsverify at all then we need to turn on tls 110 if flag.IsSet("-tlsverify") { 111 *flTls = true 112 } 113 114 // If we should verify the server, we need to load a trusted ca 115 if *flTlsVerify { 116 certPool := x509.NewCertPool() 117 file, err := ioutil.ReadFile(*flCa) 118 if err != nil { 119 fmt.Fprintf(os.Stderr, "Couldn't read ca cert %s: %s\n", *flCa, err) 120 os.Exit(1) 121 } 122 certPool.AppendCertsFromPEM(file) 123 tlsConfig.RootCAs = certPool 124 tlsConfig.InsecureSkipVerify = false 125 } 126 127 // If tls is enabled, try to load and send client certificates 128 if *flTls || *flTlsVerify { 129 _, errCert := os.Stat(*flCert) 130 _, errKey := os.Stat(*flKey) 131 if errCert == nil && errKey == nil { 132 *flTls = true 133 cert, err := tls.LoadX509KeyPair(*flCert, *flKey) 134 if err != nil { 135 fmt.Fprintf(os.Stderr, "Couldn't load X509 key pair: %q. Make sure the key is encrypted\n", err) 136 os.Exit(1) 137 } 138 tlsConfig.Certificates = []tls.Certificate{cert} 139 } 140 // Avoid fallback to SSL protocols < TLS1.0 141 tlsConfig.MinVersion = tls.VersionTLS10 142 } 143 144 if *flTls || *flTlsVerify { 145 cli = client.NewDockerCli(stdin, stdout, stderr, *flTrustKey, protoAddrParts[0], protoAddrParts[1], &tlsConfig) 146 } else { 147 cli = client.NewDockerCli(stdin, stdout, stderr, *flTrustKey, protoAddrParts[0], protoAddrParts[1], nil) 148 } 149 150 if err := cli.Cmd(flag.Args()...); err != nil { 151 if sterr, ok := err.(client.StatusError); ok { 152 if sterr.Status != "" { 153 fmt.Fprintln(cli.Err(), sterr.Status) 154 os.Exit(1) 155 } 156 os.Exit(sterr.StatusCode) 157 } 158 fmt.Fprintln(cli.Err(), err) 159 os.Exit(1) 160 } 161 } 162 163 func showVersion() { 164 fmt.Printf("Docker version %s, build %s\n", dockerversion.VERSION, dockerversion.GITCOMMIT) 165 }