rename.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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"
  10. "github.com/uozi-tech/cosy/logger"
  11. "net/http"
  12. "os"
  13. "runtime"
  14. "sync"
  15. )
  16. func Rename(oldName string, newName string) (err error) {
  17. oldPath := nginx.GetConfPath("streams-available", oldName)
  18. newPath := nginx.GetConfPath("streams-available", newName)
  19. if oldPath == newPath {
  20. return
  21. }
  22. // check if dst file exists, do not rename
  23. if helper.FileExists(newPath) {
  24. return ErrDstFileExists
  25. }
  26. s := query.Site
  27. _, _ = s.Where(s.Path.Eq(oldPath)).Update(s.Path, newPath)
  28. err = os.Rename(oldPath, newPath)
  29. if err != nil {
  30. return
  31. }
  32. // recreate a soft link
  33. oldEnabledConfigFilePath := nginx.GetConfPath("streams-enabled", oldName)
  34. if helper.SymbolLinkExists(oldEnabledConfigFilePath) {
  35. _ = os.Remove(oldEnabledConfigFilePath)
  36. newEnabledConfigFilePath := nginx.GetConfPath("streams-enabled", newName)
  37. err = os.Symlink(newPath, newEnabledConfigFilePath)
  38. if err != nil {
  39. return
  40. }
  41. }
  42. // test nginx configuration
  43. output := nginx.TestConf()
  44. if nginx.GetLogLevel(output) > nginx.Warn {
  45. return cosy.WrapErrorWithParams(ErrNginxTestFailed, output)
  46. }
  47. // reload nginx
  48. output = nginx.Reload()
  49. if nginx.GetLogLevel(output) > nginx.Warn {
  50. return cosy.WrapErrorWithParams(ErrNginxReloadFailed, output)
  51. }
  52. go syncRename(oldName, newName)
  53. return
  54. }
  55. func syncRename(oldName, newName string) {
  56. nodes := getSyncNodes(newName)
  57. wg := &sync.WaitGroup{}
  58. wg.Add(len(nodes))
  59. for _, node := range nodes {
  60. go func() {
  61. defer func() {
  62. if err := recover(); err != nil {
  63. buf := make([]byte, 1024)
  64. runtime.Stack(buf, false)
  65. logger.Error(err)
  66. }
  67. }()
  68. defer wg.Done()
  69. client := resty.New()
  70. client.SetBaseURL(node.URL)
  71. resp, err := client.R().
  72. SetHeader("X-Node-Secret", node.Token).
  73. SetBody(map[string]string{
  74. "new_name": newName,
  75. }).
  76. Post(fmt.Sprintf("/api/streams/%s/rename", oldName))
  77. if err != nil {
  78. notification.Error("Rename Remote Stream Error", err.Error(), nil)
  79. return
  80. }
  81. if resp.StatusCode() != http.StatusOK {
  82. notification.Error("Rename Remote Stream Error", "Rename stream %{name} to %{new_name} on %{node} failed",
  83. NewSyncResult(node.Name, oldName, resp).
  84. SetNewName(newName))
  85. return
  86. }
  87. notification.Success("Rename Remote Stream Success", "Rename stream %{name} to %{new_name} on %{node} successfully",
  88. NewSyncResult(node.Name, oldName, resp).
  89. SetNewName(newName))
  90. }()
  91. }
  92. wg.Wait()
  93. }