torrent.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright 2019 Yunion
  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 torrent
  15. import (
  16. "fmt"
  17. "os"
  18. "time"
  19. "yunion.io/x/log"
  20. "yunion.io/x/pkg/util/httputils"
  21. identity_apis "yunion.io/x/onecloud/pkg/apis/identity"
  22. "yunion.io/x/onecloud/pkg/appsrv"
  23. "yunion.io/x/onecloud/pkg/image/options"
  24. "yunion.io/x/onecloud/pkg/mcclient/auth"
  25. "yunion.io/x/onecloud/pkg/util/sysutils"
  26. )
  27. type STorrentProcessState struct {
  28. process *os.Process
  29. seeding bool
  30. }
  31. var (
  32. torrentTable = make(map[string]*STorrentProcessState)
  33. seedTaskWorkerMan *appsrv.SWorkerManager
  34. )
  35. const (
  36. TORRENT_TRACKER_SERVICE = "torrent-tracker"
  37. )
  38. func init() {
  39. seedTaskWorkerMan = appsrv.NewWorkerManager("seedTaskWorkerManager", 1, 1024, false)
  40. }
  41. func (stat *STorrentProcessState) StopAndWait() error {
  42. err := stat.process.Kill()
  43. if err != nil {
  44. log.Errorf("kill error %s", err)
  45. return err
  46. }
  47. _, err = stat.process.Wait()
  48. if err != nil {
  49. log.Errorf("wait error %s", err)
  50. return err
  51. }
  52. return nil
  53. }
  54. func GetTrackers() []string {
  55. urls, err := auth.GetServiceURLs(TORRENT_TRACKER_SERVICE, options.Options.Region, "", "", httputils.POST)
  56. if err != nil {
  57. log.Errorf("fail to get torrent-tracker")
  58. return nil
  59. }
  60. return urls
  61. }
  62. type torrentTask struct {
  63. torrentpath string
  64. imageId string
  65. format string
  66. }
  67. func (t *torrentTask) Run() {
  68. log.Infof("Start seed %s ...", t.torrentpath)
  69. err := seedTorrent(t.torrentpath, t.imageId, t.format)
  70. if err == nil {
  71. time.Sleep(10 * time.Second)
  72. }
  73. }
  74. func (t *torrentTask) Dump() string {
  75. return fmt.Sprintf("torrentpath: %s imageId: %s.%s", t.torrentpath, t.imageId, t.format)
  76. }
  77. func SeedTorrent(torrentpath string, imageId, format string) error {
  78. task := &torrentTask{
  79. torrentpath: torrentpath,
  80. imageId: imageId,
  81. format: format,
  82. }
  83. seedTaskWorkerMan.Run(task, nil, nil)
  84. return nil
  85. }
  86. func seedTorrent(torrentpath string, imageId, format string) error {
  87. url, err := auth.GetServiceURL("image", options.Options.Region, "", identity_apis.EndpointInterfacePublic, httputils.POST)
  88. if err != nil {
  89. return err
  90. }
  91. args := []string{
  92. options.Options.TorrentClientPath,
  93. options.Options.FilesystemStoreDatadir,
  94. torrentpath,
  95. "--callback-url",
  96. fmt.Sprintf("%s/images/%s/update-torrent-status?format=%s", url, imageId, format),
  97. }
  98. proc, err := sysutils.Start(false, args...)
  99. if err != nil {
  100. return err
  101. }
  102. torrentTable[torrentpath] = &STorrentProcessState{
  103. process: proc,
  104. seeding: false,
  105. }
  106. return nil
  107. }
  108. func SetTorrentSeeding(filepath string, seeding bool) {
  109. if _, ok := torrentTable[filepath]; ok {
  110. torrentTable[filepath].seeding = seeding
  111. }
  112. }
  113. func GetTorrentSeeding(filepath string) bool {
  114. if t, ok := torrentTable[filepath]; ok {
  115. return t.seeding
  116. }
  117. return false
  118. }
  119. func RemoveTorrent(filepath string) {
  120. if t, ok := torrentTable[filepath]; ok {
  121. t.StopAndWait()
  122. delete(torrentTable, filepath)
  123. }
  124. }
  125. func StopTorrents() {
  126. for k := range torrentTable {
  127. torrentTable[k].StopAndWait()
  128. }
  129. }