utils.go 2.4 KB

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