Commit 6c595930 authored by ale's avatar ale
Browse files

pass etcd server URLs as-is to the etcd client

parent a6bbeb41
...@@ -4,14 +4,13 @@ import ( ...@@ -4,14 +4,13 @@ import (
"flag" "flag"
"io/ioutil" "io/ioutil"
"log" "log"
"net"
"strings" "strings"
"git.autistici.org/ale/autoradio/Godeps/_workspace/src/github.com/coreos/go-etcd/etcd" "git.autistici.org/ale/autoradio/Godeps/_workspace/src/github.com/coreos/go-etcd/etcd"
) )
var ( var (
etcdMachines = flag.String("etcd-servers", "localhost:2379", "Etcd servers (comma-separated list)") etcdMachines = flag.String("etcd-servers", "http://127.0.0.1:2379", "Etcd server URLs (comma-separated list)")
etcdCertFile = flag.String("etcd-cert", "", "SSL certificate for etcd client") etcdCertFile = flag.String("etcd-cert", "", "SSL certificate for etcd client")
etcdKeyFile = flag.String("etcd-key", "", "SSL private key for etcd client") etcdKeyFile = flag.String("etcd-key", "", "SSL private key for etcd client")
etcdCaFile = flag.String("etcd-ca", "", "SSL CA certificate for etcd client") etcdCaFile = flag.String("etcd-ca", "", "SSL CA certificate for etcd client")
...@@ -25,45 +24,19 @@ func mustLoadFile(path string) string { ...@@ -25,45 +24,19 @@ func mustLoadFile(path string) string {
return string(data) return string(data)
} }
// Resolve a list of etcd host:port specs, returning URLs.
func resolveAll(input []string, proto string) []string {
var result []string
for _, hostport := range input {
host, port, err := net.SplitHostPort(hostport)
if err != nil {
log.Fatalf("Error parsing etcd server spec '%s': %s", hostport, err)
}
addrs, err := net.LookupHost(host)
if err != nil {
log.Fatalf("Error resolving etcd server spec '%s': %s", hostport, err)
}
for _, a := range addrs {
url := proto + "://" + net.JoinHostPort(a, port)
result = append(result, url)
}
}
return result
}
// NewEtcdClient creates a new etcd client. It uses command-line flags // NewEtcdClient creates a new etcd client. It uses command-line flags
// to find servers and connection parameters. If the 'strongReads' // to find servers and connection parameters. If the 'strongReads'
// argument is true, read requests will also be sent to the etcd // argument is true, read requests will also be sent to the etcd
// master and should be able to see the effect of previous writes. // master and should be able to see the effect of previous writes.
func NewEtcdClient(strongReads bool) EtcdClient { func NewEtcdClient(strongReads bool) EtcdClient {
proto := "http" machines := strings.Split(*etcdMachines, ",")
if *etcdCertFile != "" && *etcdKeyFile != "" { if len(machines) < 1 {
proto = "https"
}
// Resolve etcd servers.
machines := resolveAll(strings.Split(*etcdMachines, ","), proto)
if len(machines) == 0 {
log.Fatal("No etcd servers specified!") log.Fatal("No etcd servers specified!")
} }
// Create the etcd client. // Create the etcd client.
var c *etcd.Client var c *etcd.Client
if proto == "https" { if *etcdCertFile != "" && *etcdKeyFile != "" {
var err error var err error
c, err = etcd.NewTLSClient(machines, c, err = etcd.NewTLSClient(machines,
mustLoadFile(*etcdCertFile), mustLoadFile(*etcdCertFile),
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment