utils.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2018 The etcd Authors
  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 agent
  15. import (
  16. "net"
  17. "net/url"
  18. "os"
  19. "os/exec"
  20. "path/filepath"
  21. "strconv"
  22. "time"
  23. "github.com/coreos/etcd/pkg/fileutil"
  24. )
  25. // TODO: support separate WAL directory
  26. func archive(baseDir, etcdLogPath, dataDir string) error {
  27. dir := filepath.Join(baseDir, "etcd-failure-archive", time.Now().Format(time.RFC3339))
  28. if existDir(dir) {
  29. dir = filepath.Join(baseDir, "etcd-failure-archive", time.Now().Add(time.Second).Format(time.RFC3339))
  30. }
  31. if err := fileutil.TouchDirAll(dir); err != nil {
  32. return err
  33. }
  34. if err := os.Rename(etcdLogPath, filepath.Join(dir, "etcd.log")); err != nil {
  35. if !os.IsNotExist(err) {
  36. return err
  37. }
  38. }
  39. if err := os.Rename(dataDir, filepath.Join(dir, filepath.Base(dataDir))); err != nil {
  40. if !os.IsNotExist(err) {
  41. return err
  42. }
  43. }
  44. return nil
  45. }
  46. func existDir(fpath string) bool {
  47. st, err := os.Stat(fpath)
  48. if err != nil {
  49. if os.IsNotExist(err) {
  50. return false
  51. }
  52. } else {
  53. return st.IsDir()
  54. }
  55. return false
  56. }
  57. func getURLAndPort(addr string) (urlAddr *url.URL, port int, err error) {
  58. urlAddr, err = url.Parse(addr)
  59. if err != nil {
  60. return nil, -1, err
  61. }
  62. var s string
  63. _, s, err = net.SplitHostPort(urlAddr.Host)
  64. if err != nil {
  65. return nil, -1, err
  66. }
  67. port, err = strconv.Atoi(s)
  68. if err != nil {
  69. return nil, -1, err
  70. }
  71. return urlAddr, port, err
  72. }
  73. func stopWithSig(cmd *exec.Cmd, sig os.Signal) error {
  74. err := cmd.Process.Signal(sig)
  75. if err != nil {
  76. return err
  77. }
  78. errc := make(chan error)
  79. go func() {
  80. _, ew := cmd.Process.Wait()
  81. errc <- ew
  82. close(errc)
  83. }()
  84. select {
  85. case <-time.After(5 * time.Second):
  86. cmd.Process.Kill()
  87. case e := <-errc:
  88. return e
  89. }
  90. err = <-errc
  91. return err
  92. }
  93. func cleanPageCache() error {
  94. // https://www.kernel.org/doc/Documentation/sysctl/vm.txt
  95. // https://github.com/torvalds/linux/blob/master/fs/drop_caches.c
  96. cmd := exec.Command("/bin/sh", "-c", `echo "echo 1 > /proc/sys/vm/drop_caches" | sudo sh`)
  97. return cmd.Run()
  98. }