rename.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package site
  2. import (
  3. "fmt"
  4. "net/http"
  5. "os"
  6. "runtime"
  7. "sync"
  8. "github.com/0xJacky/Nginx-UI/internal/helper"
  9. "github.com/0xJacky/Nginx-UI/internal/nginx"
  10. "github.com/0xJacky/Nginx-UI/internal/notification"
  11. "github.com/0xJacky/Nginx-UI/query"
  12. "github.com/go-resty/resty/v2"
  13. "github.com/uozi-tech/cosy/logger"
  14. )
  15. func Rename(oldName string, newName string) (err error) {
  16. oldPath := nginx.GetConfPath("sites-available", oldName)
  17. newPath := nginx.GetConfPath("sites-available", newName)
  18. if oldPath == newPath {
  19. return
  20. }
  21. // check if dst file exists, do not rename
  22. if helper.FileExists(newPath) {
  23. return ErrDstFileExists
  24. }
  25. s := query.Site
  26. _, _ = s.Where(s.Path.Eq(oldPath)).Update(s.Path, newPath)
  27. err = os.Rename(oldPath, newPath)
  28. if err != nil {
  29. return
  30. }
  31. // recreate a soft link
  32. oldEnabledConfigFilePath := nginx.GetConfPath("sites-enabled", oldName)
  33. if helper.SymbolLinkExists(oldEnabledConfigFilePath) {
  34. _ = os.Remove(oldEnabledConfigFilePath)
  35. newEnabledConfigFilePath := nginx.GetConfPath("sites-enabled", newName)
  36. err = os.Symlink(newPath, newEnabledConfigFilePath)
  37. if err != nil {
  38. return
  39. }
  40. }
  41. // test nginx configuration
  42. res := nginx.Control(nginx.TestConfig)
  43. if res.IsError() {
  44. return res.GetError()
  45. }
  46. // reload nginx
  47. res = nginx.Control(nginx.Reload)
  48. if res.IsError() {
  49. return res.GetError()
  50. }
  51. // update ChatGPT history
  52. g := query.ChatGPTLog
  53. _, _ = g.Where(g.Name.Eq(oldName)).Update(g.Name, newName)
  54. // update config history
  55. b := query.ConfigBackup
  56. _, _ = b.Where(b.FilePath.Eq(oldPath)).Updates(map[string]interface{}{
  57. "filepath": newPath,
  58. "name": newName,
  59. })
  60. go syncRename(oldName, newName)
  61. return
  62. }
  63. func syncRename(oldName, newName string) {
  64. nodes := getSyncNodes(newName)
  65. wg := &sync.WaitGroup{}
  66. wg.Add(len(nodes))
  67. for _, node := range nodes {
  68. go func() {
  69. defer func() {
  70. if err := recover(); err != nil {
  71. buf := make([]byte, 1024)
  72. runtime.Stack(buf, false)
  73. logger.Error(err)
  74. }
  75. }()
  76. defer wg.Done()
  77. client := resty.New()
  78. client.SetBaseURL(node.URL)
  79. resp, err := client.R().
  80. SetHeader("X-Node-Secret", node.Token).
  81. SetBody(map[string]string{
  82. "new_name": newName,
  83. }).
  84. Post(fmt.Sprintf("/api/sites/%s/rename", oldName))
  85. if err != nil {
  86. notification.Error("Rename Remote Site Error", err.Error(), nil)
  87. return
  88. }
  89. if resp.StatusCode() != http.StatusOK {
  90. notification.Error("Rename Remote Site Error", "Rename site %{name} to %{new_name} on %{node} failed",
  91. NewSyncResult(node.Name, oldName, resp).
  92. SetNewName(newName))
  93. return
  94. }
  95. notification.Success("Rename Remote Site Success", "Rename site %{name} to %{new_name} on %{node} successfully",
  96. NewSyncResult(node.Name, oldName, resp).
  97. SetNewName(newName))
  98. }()
  99. }
  100. wg.Wait()
  101. }