rename.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package stream
  2. import (
  3. "fmt"
  4. "github.com/0xJacky/Nginx-UI/internal/helper"
  5. "github.com/0xJacky/Nginx-UI/internal/nginx"
  6. "github.com/0xJacky/Nginx-UI/internal/notification"
  7. "github.com/0xJacky/Nginx-UI/query"
  8. "github.com/go-resty/resty/v2"
  9. "github.com/uozi-tech/cosy/logger"
  10. "net/http"
  11. "os"
  12. "runtime"
  13. "sync"
  14. )
  15. func Rename(oldName string, newName string) (err error) {
  16. oldPath := nginx.GetConfPath("streams-available", oldName)
  17. newPath := nginx.GetConfPath("streams-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("streams-enabled", oldName)
  33. if helper.SymbolLinkExists(oldEnabledConfigFilePath) {
  34. _ = os.Remove(oldEnabledConfigFilePath)
  35. newEnabledConfigFilePath := nginx.GetConfPath("streams-enabled", newName)
  36. err = os.Symlink(newPath, newEnabledConfigFilePath)
  37. if err != nil {
  38. return
  39. }
  40. }
  41. // test nginx configuration
  42. output := nginx.TestConf()
  43. if nginx.GetLogLevel(output) > nginx.Warn {
  44. return fmt.Errorf("%s", output)
  45. }
  46. // reload nginx
  47. output = nginx.Reload()
  48. if nginx.GetLogLevel(output) > nginx.Warn {
  49. return fmt.Errorf("%s", output)
  50. }
  51. go syncRename(oldName, newName)
  52. return
  53. }
  54. func syncRename(oldName, newName string) {
  55. nodes := getSyncNodes(newName)
  56. wg := &sync.WaitGroup{}
  57. wg.Add(len(nodes))
  58. for _, node := range nodes {
  59. go func() {
  60. defer func() {
  61. if err := recover(); err != nil {
  62. buf := make([]byte, 1024)
  63. runtime.Stack(buf, false)
  64. logger.Error(err)
  65. }
  66. }()
  67. defer wg.Done()
  68. client := resty.New()
  69. client.SetBaseURL(node.URL)
  70. resp, err := client.R().
  71. SetHeader("X-Node-Secret", node.Token).
  72. SetBody(map[string]string{
  73. "new_name": newName,
  74. }).
  75. Post(fmt.Sprintf("/api/streams/%s/rename", oldName))
  76. if err != nil {
  77. notification.Error("Rename Remote Stream Error", err.Error(), nil)
  78. return
  79. }
  80. if resp.StatusCode() != http.StatusOK {
  81. notification.Error("Rename Remote Stream Error", "Rename stream %{name} to %{new_name} on %{node} failed",
  82. NewSyncResult(node.Name, oldName, resp).
  83. SetNewName(newName))
  84. return
  85. }
  86. notification.Success("Rename Remote Stream Success", "Rename stream %{name} to %{new_name} on %{node} successfully",
  87. NewSyncResult(node.Name, oldName, resp).
  88. SetNewName(newName))
  89. }()
  90. }
  91. wg.Wait()
  92. }