etcd.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package etcdmain
  15. import (
  16. "crypto/tls"
  17. "encoding/json"
  18. "fmt"
  19. "io/ioutil"
  20. "net"
  21. "net/http"
  22. _ "net/http/pprof"
  23. "os"
  24. "path"
  25. "reflect"
  26. "runtime"
  27. "strings"
  28. "time"
  29. "github.com/coreos/etcd/discovery"
  30. "github.com/coreos/etcd/etcdserver"
  31. "github.com/coreos/etcd/etcdserver/api/v2http"
  32. "github.com/coreos/etcd/pkg/cors"
  33. "github.com/coreos/etcd/pkg/fileutil"
  34. pkgioutil "github.com/coreos/etcd/pkg/ioutil"
  35. "github.com/coreos/etcd/pkg/osutil"
  36. runtimeutil "github.com/coreos/etcd/pkg/runtime"
  37. "github.com/coreos/etcd/pkg/transport"
  38. "github.com/coreos/etcd/pkg/types"
  39. "github.com/coreos/etcd/proxy/httpproxy"
  40. "github.com/coreos/etcd/rafthttp"
  41. "github.com/coreos/etcd/version"
  42. "github.com/coreos/go-systemd/daemon"
  43. systemdutil "github.com/coreos/go-systemd/util"
  44. "github.com/coreos/pkg/capnslog"
  45. "github.com/prometheus/client_golang/prometheus"
  46. )
  47. type dirType string
  48. var plog = capnslog.NewPackageLogger("github.com/coreos/etcd", "etcdmain")
  49. const (
  50. // the owner can make/remove files inside the directory
  51. privateDirMode = 0700
  52. // internal fd usage includes disk usage and transport usage.
  53. // To read/write snapshot, snap pkg needs 1. In normal case, wal pkg needs
  54. // at most 2 to read/lock/write WALs. One case that it needs to 2 is to
  55. // read all logs after some snapshot index, which locates at the end of
  56. // the second last and the head of the last. For purging, it needs to read
  57. // directory, so it needs 1. For fd monitor, it needs 1.
  58. // For transport, rafthttp builds two long-polling connections and at most
  59. // four temporary connections with each member. There are at most 9 members
  60. // in a cluster, so it should reserve 96.
  61. // For the safety, we set the total reserved number to 150.
  62. reservedInternalFDNum = 150
  63. )
  64. var (
  65. dirMember = dirType("member")
  66. dirProxy = dirType("proxy")
  67. dirEmpty = dirType("empty")
  68. )
  69. func Main() {
  70. checkSupportArch()
  71. cfg := NewConfig()
  72. err := cfg.Parse(os.Args[1:])
  73. if err != nil {
  74. plog.Errorf("error verifying flags, %v. See 'etcd --help'.", err)
  75. switch err {
  76. case errUnsetAdvertiseClientURLsFlag:
  77. plog.Errorf("When listening on specific address(es), this etcd process must advertise accessible url(s) to each connected client.")
  78. }
  79. os.Exit(1)
  80. }
  81. setupLogging(cfg)
  82. var stopped <-chan struct{}
  83. plog.Infof("etcd Version: %s\n", version.Version)
  84. plog.Infof("Git SHA: %s\n", version.GitSHA)
  85. plog.Infof("Go Version: %s\n", runtime.Version())
  86. plog.Infof("Go OS/Arch: %s/%s\n", runtime.GOOS, runtime.GOARCH)
  87. GoMaxProcs := runtime.GOMAXPROCS(0)
  88. plog.Infof("setting maximum number of CPUs to %d, total number of available CPUs is %d", GoMaxProcs, runtime.NumCPU())
  89. // TODO: check whether fields are set instead of whether fields have default value
  90. if cfg.name != defaultName && cfg.initialCluster == initialClusterFromName(defaultName) {
  91. cfg.initialCluster = initialClusterFromName(cfg.name)
  92. }
  93. if cfg.dir == "" {
  94. cfg.dir = fmt.Sprintf("%v.etcd", cfg.name)
  95. plog.Warningf("no data-dir provided, using default data-dir ./%s", cfg.dir)
  96. }
  97. which := identifyDataDirOrDie(cfg.dir)
  98. if which != dirEmpty {
  99. plog.Noticef("the server is already initialized as %v before, starting as etcd %v...", which, which)
  100. switch which {
  101. case dirMember:
  102. stopped, err = startEtcd(cfg)
  103. case dirProxy:
  104. err = startProxy(cfg)
  105. default:
  106. plog.Panicf("unhandled dir type %v", which)
  107. }
  108. } else {
  109. shouldProxy := cfg.isProxy()
  110. if !shouldProxy {
  111. stopped, err = startEtcd(cfg)
  112. if derr, ok := err.(*etcdserver.DiscoveryError); ok && derr.Err == discovery.ErrFullCluster {
  113. if cfg.shouldFallbackToProxy() {
  114. plog.Noticef("discovery cluster full, falling back to %s", fallbackFlagProxy)
  115. shouldProxy = true
  116. }
  117. }
  118. }
  119. if shouldProxy {
  120. err = startProxy(cfg)
  121. }
  122. }
  123. if err != nil {
  124. if derr, ok := err.(*etcdserver.DiscoveryError); ok {
  125. switch derr.Err {
  126. case discovery.ErrDuplicateID:
  127. plog.Errorf("member %q has previously registered with discovery service token (%s).", cfg.name, cfg.durl)
  128. plog.Errorf("But etcd could not find valid cluster configuration in the given data dir (%s).", cfg.dir)
  129. plog.Infof("Please check the given data dir path if the previous bootstrap succeeded")
  130. plog.Infof("or use a new discovery token if the previous bootstrap failed.")
  131. case discovery.ErrDuplicateName:
  132. plog.Errorf("member with duplicated name has registered with discovery service token(%s).", cfg.durl)
  133. plog.Errorf("please check (cURL) the discovery token for more information.")
  134. plog.Errorf("please do not reuse the discovery token and generate a new one to bootstrap the cluster.")
  135. default:
  136. plog.Errorf("%v", err)
  137. plog.Infof("discovery token %s was used, but failed to bootstrap the cluster.", cfg.durl)
  138. plog.Infof("please generate a new discovery token and try to bootstrap again.")
  139. }
  140. os.Exit(1)
  141. }
  142. if strings.Contains(err.Error(), "include") && strings.Contains(err.Error(), "--initial-cluster") {
  143. plog.Infof("%v", err)
  144. if cfg.initialCluster == initialClusterFromName(cfg.name) {
  145. plog.Infof("forgot to set --initial-cluster flag?")
  146. }
  147. if types.URLs(cfg.apurls).String() == defaultInitialAdvertisePeerURLs {
  148. plog.Infof("forgot to set --initial-advertise-peer-urls flag?")
  149. }
  150. if cfg.initialCluster == initialClusterFromName(cfg.name) && len(cfg.durl) == 0 {
  151. plog.Infof("if you want to use discovery service, please set --discovery flag.")
  152. }
  153. os.Exit(1)
  154. }
  155. plog.Fatalf("%v", err)
  156. }
  157. osutil.HandleInterrupts()
  158. if systemdutil.IsRunningSystemd() {
  159. // At this point, the initialization of etcd is done.
  160. // The listeners are listening on the TCP ports and ready
  161. // for accepting connections.
  162. // The http server is probably ready for serving incoming
  163. // connections. If it is not, the connection might be pending
  164. // for less than one second.
  165. err := daemon.SdNotify("READY=1")
  166. if err != nil {
  167. plog.Errorf("failed to notify systemd for readiness: %v", err)
  168. if err == daemon.SdNotifyNoSocket {
  169. plog.Errorf("forgot to set Type=notify in systemd service file?")
  170. }
  171. }
  172. }
  173. <-stopped
  174. osutil.Exit(0)
  175. }
  176. // startEtcd launches the etcd server and HTTP handlers for client/server communication.
  177. func startEtcd(cfg *config) (<-chan struct{}, error) {
  178. urlsmap, token, err := getPeerURLsMapAndToken(cfg, "etcd")
  179. if err != nil {
  180. return nil, fmt.Errorf("error setting up initial cluster: %v", err)
  181. }
  182. if cfg.peerAutoTLS && cfg.peerTLSInfo.Empty() {
  183. var phosts []string
  184. for _, u := range cfg.lpurls {
  185. phosts = append(phosts, u.Host)
  186. }
  187. cfg.peerTLSInfo, err = transport.SelfCert(cfg.dir, phosts)
  188. if err != nil {
  189. plog.Fatalf("could not get certs (%v)", err)
  190. }
  191. } else if cfg.peerAutoTLS {
  192. plog.Warningf("ignoring peer auto TLS since certs given")
  193. }
  194. if !cfg.peerTLSInfo.Empty() {
  195. plog.Infof("peerTLS: %s", cfg.peerTLSInfo)
  196. }
  197. var plns []net.Listener
  198. for _, u := range cfg.lpurls {
  199. if u.Scheme == "http" {
  200. if !cfg.peerTLSInfo.Empty() {
  201. plog.Warningf("The scheme of peer url %s is HTTP while peer key/cert files are presented. Ignored peer key/cert files.", u.String())
  202. }
  203. if cfg.peerTLSInfo.ClientCertAuth {
  204. plog.Warningf("The scheme of peer url %s is HTTP while client cert auth (--peer-client-cert-auth) is enabled. Ignored client cert auth for this url.", u.String())
  205. }
  206. }
  207. var (
  208. l net.Listener
  209. tlscfg *tls.Config
  210. )
  211. if !cfg.peerTLSInfo.Empty() {
  212. tlscfg, err = cfg.peerTLSInfo.ServerConfig()
  213. if err != nil {
  214. return nil, err
  215. }
  216. }
  217. l, err = rafthttp.NewListener(u, tlscfg)
  218. if err != nil {
  219. return nil, err
  220. }
  221. urlStr := u.String()
  222. plog.Info("listening for peers on ", urlStr)
  223. defer func() {
  224. if err != nil {
  225. l.Close()
  226. plog.Info("stopping listening for peers on ", urlStr)
  227. }
  228. }()
  229. plns = append(plns, l)
  230. }
  231. var ctlscfg *tls.Config
  232. if !cfg.clientTLSInfo.Empty() {
  233. plog.Infof("clientTLS: %s", cfg.clientTLSInfo)
  234. ctlscfg, err = cfg.clientTLSInfo.ServerConfig()
  235. if err != nil {
  236. return nil, err
  237. }
  238. }
  239. sctxs := make(map[string]*serveCtx)
  240. for _, u := range cfg.lcurls {
  241. if u.Scheme == "http" {
  242. if !cfg.clientTLSInfo.Empty() {
  243. plog.Warningf("The scheme of client url %s is HTTP while peer key/cert files are presented. Ignored key/cert files.", u.String())
  244. }
  245. if cfg.clientTLSInfo.ClientCertAuth {
  246. plog.Warningf("The scheme of client url %s is HTTP while client cert auth (--client-cert-auth) is enabled. Ignored client cert auth for this url.", u.String())
  247. }
  248. }
  249. if u.Scheme == "https" && ctlscfg == nil {
  250. return nil, fmt.Errorf("TLS key/cert (--cert-file, --key-file) must be provided for client url %s with HTTPs scheme", u.String())
  251. }
  252. ctx := &serveCtx{host: u.Host}
  253. if u.Scheme == "https" {
  254. ctx.secure = true
  255. } else {
  256. ctx.insecure = true
  257. }
  258. if sctxs[u.Host] != nil {
  259. if ctx.secure {
  260. sctxs[u.Host].secure = true
  261. }
  262. if ctx.insecure {
  263. sctxs[u.Host].insecure = true
  264. }
  265. continue
  266. }
  267. var l net.Listener
  268. l, err = net.Listen("tcp", u.Host)
  269. if err != nil {
  270. return nil, err
  271. }
  272. var fdLimit uint64
  273. if fdLimit, err = runtimeutil.FDLimit(); err == nil {
  274. if fdLimit <= reservedInternalFDNum {
  275. plog.Fatalf("file descriptor limit[%d] of etcd process is too low, and should be set higher than %d to ensure internal usage", fdLimit, reservedInternalFDNum)
  276. }
  277. l = transport.LimitListener(l, int(fdLimit-reservedInternalFDNum))
  278. }
  279. l, err = transport.NewKeepAliveListener(l, "tcp", nil)
  280. ctx.l = l
  281. if err != nil {
  282. return nil, err
  283. }
  284. plog.Info("listening for client requests on ", u.Host)
  285. defer func() {
  286. if err != nil {
  287. l.Close()
  288. plog.Info("stopping listening for client requests on ", u.Host)
  289. }
  290. }()
  291. sctxs[u.Host] = ctx
  292. }
  293. srvcfg := &etcdserver.ServerConfig{
  294. Name: cfg.name,
  295. ClientURLs: cfg.acurls,
  296. PeerURLs: cfg.apurls,
  297. DataDir: cfg.dir,
  298. DedicatedWALDir: cfg.walDir,
  299. SnapCount: cfg.snapCount,
  300. MaxSnapFiles: cfg.maxSnapFiles,
  301. MaxWALFiles: cfg.maxWalFiles,
  302. InitialPeerURLsMap: urlsmap,
  303. InitialClusterToken: token,
  304. DiscoveryURL: cfg.durl,
  305. DiscoveryProxy: cfg.dproxy,
  306. NewCluster: cfg.isNewCluster(),
  307. ForceNewCluster: cfg.forceNewCluster,
  308. PeerTLSInfo: cfg.peerTLSInfo,
  309. TickMs: cfg.TickMs,
  310. ElectionTicks: cfg.electionTicks(),
  311. AutoCompactionRetention: cfg.autoCompactionRetention,
  312. QuotaBackendBytes: cfg.quotaBackendBytes,
  313. StrictReconfigCheck: cfg.strictReconfigCheck,
  314. EnablePprof: cfg.enablePprof,
  315. }
  316. var s *etcdserver.EtcdServer
  317. s, err = etcdserver.NewServer(srvcfg)
  318. if err != nil {
  319. return nil, err
  320. }
  321. s.Start()
  322. osutil.RegisterInterruptHandler(s.Stop)
  323. if cfg.corsInfo.String() != "" {
  324. plog.Infof("cors = %s", cfg.corsInfo)
  325. }
  326. ch := http.Handler(&cors.CORSHandler{
  327. Handler: v2http.NewClientHandler(s, srvcfg.ReqTimeout()),
  328. Info: cfg.corsInfo,
  329. })
  330. ph := v2http.NewPeerHandler(s)
  331. // Start the peer server in a goroutine
  332. for _, l := range plns {
  333. go func(l net.Listener) {
  334. plog.Fatal(servePeerHTTP(l, ph))
  335. }(l)
  336. }
  337. // Start a client server goroutine for each listen address
  338. for _, sctx := range sctxs {
  339. go func(sctx *serveCtx) {
  340. // read timeout does not work with http close notify
  341. // TODO: https://github.com/golang/go/issues/9524
  342. plog.Fatal(serve(sctx, s, ctlscfg, ch))
  343. }(sctx)
  344. }
  345. return s.StopNotify(), nil
  346. }
  347. // startProxy launches an HTTP proxy for client communication which proxies to other etcd nodes.
  348. func startProxy(cfg *config) error {
  349. pt, err := transport.NewTimeoutTransport(cfg.peerTLSInfo, time.Duration(cfg.proxyDialTimeoutMs)*time.Millisecond, time.Duration(cfg.proxyReadTimeoutMs)*time.Millisecond, time.Duration(cfg.proxyWriteTimeoutMs)*time.Millisecond)
  350. if err != nil {
  351. return err
  352. }
  353. pt.MaxIdleConnsPerHost = httpproxy.DefaultMaxIdleConnsPerHost
  354. tr, err := transport.NewTimeoutTransport(cfg.peerTLSInfo, time.Duration(cfg.proxyDialTimeoutMs)*time.Millisecond, time.Duration(cfg.proxyReadTimeoutMs)*time.Millisecond, time.Duration(cfg.proxyWriteTimeoutMs)*time.Millisecond)
  355. if err != nil {
  356. return err
  357. }
  358. cfg.dir = path.Join(cfg.dir, "proxy")
  359. err = os.MkdirAll(cfg.dir, privateDirMode)
  360. if err != nil {
  361. return err
  362. }
  363. var peerURLs []string
  364. clusterfile := path.Join(cfg.dir, "cluster")
  365. b, err := ioutil.ReadFile(clusterfile)
  366. switch {
  367. case err == nil:
  368. if cfg.durl != "" {
  369. plog.Warningf("discovery token ignored since the proxy has already been initialized. Valid cluster file found at %q", clusterfile)
  370. }
  371. if cfg.dnsCluster != "" {
  372. plog.Warningf("DNS SRV discovery ignored since the proxy has already been initialized. Valid cluster file found at %q", clusterfile)
  373. }
  374. urls := struct{ PeerURLs []string }{}
  375. err = json.Unmarshal(b, &urls)
  376. if err != nil {
  377. return err
  378. }
  379. peerURLs = urls.PeerURLs
  380. plog.Infof("proxy: using peer urls %v from cluster file %q", peerURLs, clusterfile)
  381. case os.IsNotExist(err):
  382. var urlsmap types.URLsMap
  383. urlsmap, _, err = getPeerURLsMapAndToken(cfg, "proxy")
  384. if err != nil {
  385. return fmt.Errorf("error setting up initial cluster: %v", err)
  386. }
  387. if cfg.durl != "" {
  388. var s string
  389. s, err = discovery.GetCluster(cfg.durl, cfg.dproxy)
  390. if err != nil {
  391. return err
  392. }
  393. if urlsmap, err = types.NewURLsMap(s); err != nil {
  394. return err
  395. }
  396. }
  397. peerURLs = urlsmap.URLs()
  398. plog.Infof("proxy: using peer urls %v ", peerURLs)
  399. default:
  400. return err
  401. }
  402. clientURLs := []string{}
  403. uf := func() []string {
  404. gcls, gerr := etcdserver.GetClusterFromRemotePeers(peerURLs, tr)
  405. // TODO: remove the 2nd check when we fix GetClusterFromRemotePeers
  406. // GetClusterFromRemotePeers should not return nil error with an invalid empty cluster
  407. if gerr != nil {
  408. plog.Warningf("proxy: %v", gerr)
  409. return []string{}
  410. }
  411. if len(gcls.Members()) == 0 {
  412. return clientURLs
  413. }
  414. clientURLs = gcls.ClientURLs()
  415. urls := struct{ PeerURLs []string }{gcls.PeerURLs()}
  416. b, jerr := json.Marshal(urls)
  417. if jerr != nil {
  418. plog.Warningf("proxy: error on marshal peer urls %s", jerr)
  419. return clientURLs
  420. }
  421. err = pkgioutil.WriteAndSyncFile(clusterfile+".bak", b, 0600)
  422. if err != nil {
  423. plog.Warningf("proxy: error on writing urls %s", err)
  424. return clientURLs
  425. }
  426. err = os.Rename(clusterfile+".bak", clusterfile)
  427. if err != nil {
  428. plog.Warningf("proxy: error on updating clusterfile %s", err)
  429. return clientURLs
  430. }
  431. if !reflect.DeepEqual(gcls.PeerURLs(), peerURLs) {
  432. plog.Noticef("proxy: updated peer urls in cluster file from %v to %v", peerURLs, gcls.PeerURLs())
  433. }
  434. peerURLs = gcls.PeerURLs()
  435. return clientURLs
  436. }
  437. ph := httpproxy.NewHandler(pt, uf, time.Duration(cfg.proxyFailureWaitMs)*time.Millisecond, time.Duration(cfg.proxyRefreshIntervalMs)*time.Millisecond)
  438. ph = &cors.CORSHandler{
  439. Handler: ph,
  440. Info: cfg.corsInfo,
  441. }
  442. if cfg.isReadonlyProxy() {
  443. ph = httpproxy.NewReadonlyHandler(ph)
  444. }
  445. // Start a proxy server goroutine for each listen address
  446. for _, u := range cfg.lcurls {
  447. var (
  448. l net.Listener
  449. tlscfg *tls.Config
  450. )
  451. if !cfg.clientTLSInfo.Empty() {
  452. tlscfg, err = cfg.clientTLSInfo.ServerConfig()
  453. if err != nil {
  454. return err
  455. }
  456. }
  457. l, err := transport.NewListener(u.Host, u.Scheme, tlscfg)
  458. if err != nil {
  459. return err
  460. }
  461. host := u.String()
  462. go func() {
  463. plog.Info("proxy: listening for client requests on ", host)
  464. mux := http.NewServeMux()
  465. mux.Handle("/metrics", prometheus.Handler())
  466. mux.Handle("/", ph)
  467. plog.Fatal(http.Serve(l, mux))
  468. }()
  469. }
  470. return nil
  471. }
  472. // getPeerURLsMapAndToken sets up an initial peer URLsMap and cluster token for bootstrap or discovery.
  473. func getPeerURLsMapAndToken(cfg *config, which string) (urlsmap types.URLsMap, token string, err error) {
  474. switch {
  475. case cfg.durl != "":
  476. urlsmap = types.URLsMap{}
  477. // If using discovery, generate a temporary cluster based on
  478. // self's advertised peer URLs
  479. urlsmap[cfg.name] = cfg.apurls
  480. token = cfg.durl
  481. case cfg.dnsCluster != "":
  482. var clusterStr string
  483. clusterStr, token, err = discovery.SRVGetCluster(cfg.name, cfg.dnsCluster, cfg.initialClusterToken, cfg.apurls)
  484. if err != nil {
  485. return nil, "", err
  486. }
  487. urlsmap, err = types.NewURLsMap(clusterStr)
  488. // only etcd member must belong to the discovered cluster.
  489. // proxy does not need to belong to the discovered cluster.
  490. if which == "etcd" {
  491. if _, ok := urlsmap[cfg.name]; !ok {
  492. return nil, "", fmt.Errorf("cannot find local etcd member %q in SRV records", cfg.name)
  493. }
  494. }
  495. default:
  496. // We're statically configured, and cluster has appropriately been set.
  497. urlsmap, err = types.NewURLsMap(cfg.initialCluster)
  498. token = cfg.initialClusterToken
  499. }
  500. return urlsmap, token, err
  501. }
  502. // identifyDataDirOrDie returns the type of the data dir.
  503. // Dies if the datadir is invalid.
  504. func identifyDataDirOrDie(dir string) dirType {
  505. names, err := fileutil.ReadDir(dir)
  506. if err != nil {
  507. if os.IsNotExist(err) {
  508. return dirEmpty
  509. }
  510. plog.Fatalf("error listing data dir: %s", dir)
  511. }
  512. var m, p bool
  513. for _, name := range names {
  514. switch dirType(name) {
  515. case dirMember:
  516. m = true
  517. case dirProxy:
  518. p = true
  519. default:
  520. plog.Warningf("found invalid file/dir %s under data dir %s (Ignore this if you are upgrading etcd)", name, dir)
  521. }
  522. }
  523. if m && p {
  524. plog.Fatal("invalid datadir. Both member and proxy directories exist.")
  525. }
  526. if m {
  527. return dirMember
  528. }
  529. if p {
  530. return dirProxy
  531. }
  532. return dirEmpty
  533. }
  534. func setupLogging(cfg *config) {
  535. capnslog.SetGlobalLogLevel(capnslog.INFO)
  536. if cfg.debug {
  537. capnslog.SetGlobalLogLevel(capnslog.DEBUG)
  538. }
  539. if cfg.logPkgLevels != "" {
  540. repoLog := capnslog.MustRepoLogger("github.com/coreos/etcd")
  541. settings, err := repoLog.ParseLogLevelConfig(cfg.logPkgLevels)
  542. if err != nil {
  543. plog.Warningf("couldn't parse log level string: %s, continuing with default levels", err.Error())
  544. return
  545. }
  546. repoLog.SetLogLevel(settings)
  547. }
  548. }
  549. func checkSupportArch() {
  550. // TODO qualify arm64
  551. if runtime.GOARCH == "amd64" {
  552. return
  553. }
  554. if env, ok := os.LookupEnv("ETCD_UNSUPPORTED_ARCH"); ok && env == runtime.GOARCH {
  555. plog.Warningf("running etcd on unsupported architecture %q since ETCD_UNSUPPORTED_ARCH is set", env)
  556. return
  557. }
  558. plog.Errorf("etcd on unsupported platform without ETCD_UNSUPPORTED_ARCH=%s set.", runtime.GOARCH)
  559. os.Exit(1)
  560. }