You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

246 lines
6.0 KiB

8 years ago
8 years ago
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "fmt"
  7. "strings"
  8. "time"
  9. "github.com/Unknwon/com"
  10. "github.com/go-xorm/xorm"
  11. "gopkg.in/ini.v1"
  12. "code.gitea.io/gitea/modules/log"
  13. "code.gitea.io/gitea/modules/process"
  14. "code.gitea.io/gitea/modules/setting"
  15. "code.gitea.io/gitea/modules/sync"
  16. )
  17. var MirrorQueue = sync.NewUniqueQueue(setting.Repository.MirrorQueueLength)
  18. // Mirror represents mirror information of a repository.
  19. type Mirror struct {
  20. ID int64 `xorm:"pk autoincr"`
  21. RepoID int64
  22. Repo *Repository `xorm:"-"`
  23. Interval int // Hour.
  24. EnablePrune bool `xorm:"NOT NULL DEFAULT true"`
  25. Updated time.Time `xorm:"-"`
  26. UpdatedUnix int64
  27. NextUpdate time.Time `xorm:"-"`
  28. NextUpdateUnix int64
  29. address string `xorm:"-"`
  30. }
  31. func (m *Mirror) BeforeInsert() {
  32. m.UpdatedUnix = time.Now().Unix()
  33. m.NextUpdateUnix = m.NextUpdate.Unix()
  34. }
  35. func (m *Mirror) BeforeUpdate() {
  36. m.UpdatedUnix = time.Now().Unix()
  37. m.NextUpdateUnix = m.NextUpdate.Unix()
  38. }
  39. func (m *Mirror) AfterSet(colName string, _ xorm.Cell) {
  40. var err error
  41. switch colName {
  42. case "repo_id":
  43. m.Repo, err = GetRepositoryByID(m.RepoID)
  44. if err != nil {
  45. log.Error(3, "GetRepositoryByID[%d]: %v", m.ID, err)
  46. }
  47. case "updated_unix":
  48. m.Updated = time.Unix(m.UpdatedUnix, 0).Local()
  49. case "next_updated_unix":
  50. m.NextUpdate = time.Unix(m.NextUpdateUnix, 0).Local()
  51. }
  52. }
  53. // ScheduleNextUpdate calculates and sets next update time.
  54. func (m *Mirror) ScheduleNextUpdate() {
  55. m.NextUpdate = time.Now().Add(time.Duration(m.Interval) * time.Hour)
  56. }
  57. func (m *Mirror) readAddress() {
  58. if len(m.address) > 0 {
  59. return
  60. }
  61. cfg, err := ini.Load(m.Repo.GitConfigPath())
  62. if err != nil {
  63. log.Error(4, "Load: %v", err)
  64. return
  65. }
  66. m.address = cfg.Section("remote \"origin\"").Key("url").Value()
  67. }
  68. // HandleCloneUserCredentials replaces user credentials from HTTP/HTTPS URL
  69. // with placeholder <credentials>.
  70. // It will fail for any other forms of clone addresses.
  71. func HandleCloneUserCredentials(url string, mosaics bool) string {
  72. i := strings.Index(url, "@")
  73. if i == -1 {
  74. return url
  75. }
  76. start := strings.Index(url, "://")
  77. if start == -1 {
  78. return url
  79. }
  80. if mosaics {
  81. return url[:start+3] + "<credentials>" + url[i:]
  82. }
  83. return url[:start+3] + url[i+1:]
  84. }
  85. // Address returns mirror address from Git repository config without credentials.
  86. func (m *Mirror) Address() string {
  87. m.readAddress()
  88. return HandleCloneUserCredentials(m.address, false)
  89. }
  90. // FullAddress returns mirror address from Git repository config.
  91. func (m *Mirror) FullAddress() string {
  92. m.readAddress()
  93. return m.address
  94. }
  95. // SaveAddress writes new address to Git repository config.
  96. func (m *Mirror) SaveAddress(addr string) error {
  97. configPath := m.Repo.GitConfigPath()
  98. cfg, err := ini.Load(configPath)
  99. if err != nil {
  100. return fmt.Errorf("Load: %v", err)
  101. }
  102. cfg.Section("remote \"origin\"").Key("url").SetValue(addr)
  103. return cfg.SaveToIndent(configPath, "\t")
  104. }
  105. // runSync returns true if sync finished without error.
  106. func (m *Mirror) runSync() bool {
  107. repoPath := m.Repo.RepoPath()
  108. wikiPath := m.Repo.WikiPath()
  109. timeout := time.Duration(setting.Git.Timeout.Mirror) * time.Second
  110. gitArgs := []string{"remote", "update"}
  111. if m.EnablePrune {
  112. gitArgs = append(gitArgs, "--prune")
  113. }
  114. if _, stderr, err := process.ExecDir(
  115. timeout, repoPath, fmt.Sprintf("Mirror.runSync: %s", repoPath),
  116. "git", gitArgs...); err != nil {
  117. desc := fmt.Sprintf("Fail to update mirror repository '%s': %s", repoPath, stderr)
  118. log.Error(4, desc)
  119. if err = CreateRepositoryNotice(desc); err != nil {
  120. log.Error(4, "CreateRepositoryNotice: %v", err)
  121. }
  122. return false
  123. }
  124. if m.Repo.HasWiki() {
  125. if _, stderr, err := process.ExecDir(
  126. timeout, wikiPath, fmt.Sprintf("Mirror.runSync: %s", wikiPath),
  127. "git", "remote", "update", "--prune"); err != nil {
  128. desc := fmt.Sprintf("Fail to update mirror wiki repository '%s': %s", wikiPath, stderr)
  129. log.Error(4, desc)
  130. if err = CreateRepositoryNotice(desc); err != nil {
  131. log.Error(4, "CreateRepositoryNotice: %v", err)
  132. }
  133. return false
  134. }
  135. }
  136. return true
  137. }
  138. func getMirrorByRepoID(e Engine, repoID int64) (*Mirror, error) {
  139. m := &Mirror{RepoID: repoID}
  140. has, err := e.Get(m)
  141. if err != nil {
  142. return nil, err
  143. } else if !has {
  144. return nil, ErrMirrorNotExist
  145. }
  146. return m, nil
  147. }
  148. // GetMirrorByRepoID returns mirror information of a repository.
  149. func GetMirrorByRepoID(repoID int64) (*Mirror, error) {
  150. return getMirrorByRepoID(x, repoID)
  151. }
  152. func updateMirror(e Engine, m *Mirror) error {
  153. _, err := e.Id(m.ID).AllCols().Update(m)
  154. return err
  155. }
  156. func UpdateMirror(m *Mirror) error {
  157. return updateMirror(x, m)
  158. }
  159. func DeleteMirrorByRepoID(repoID int64) error {
  160. _, err := x.Delete(&Mirror{RepoID: repoID})
  161. return err
  162. }
  163. // MirrorUpdate checks and updates mirror repositories.
  164. func MirrorUpdate() {
  165. if taskStatusTable.IsRunning(mirrorUpdate) {
  166. return
  167. }
  168. taskStatusTable.Start(mirrorUpdate)
  169. defer taskStatusTable.Stop(mirrorUpdate)
  170. log.Trace("Doing: MirrorUpdate")
  171. if err := x.
  172. Where("next_update_unix<=?", time.Now().Unix()).
  173. Iterate(new(Mirror), func(idx int, bean interface{}) error {
  174. m := bean.(*Mirror)
  175. if m.Repo == nil {
  176. log.Error(4, "Disconnected mirror repository found: %d", m.ID)
  177. return nil
  178. }
  179. MirrorQueue.Add(m.RepoID)
  180. return nil
  181. }); err != nil {
  182. log.Error(4, "MirrorUpdate: %v", err)
  183. }
  184. }
  185. // SyncMirrors checks and syncs mirrors.
  186. // TODO: sync more mirrors at same time.
  187. func SyncMirrors() {
  188. // Start listening on new sync requests.
  189. for repoID := range MirrorQueue.Queue() {
  190. log.Trace("SyncMirrors [repo_id: %v]", repoID)
  191. MirrorQueue.Remove(repoID)
  192. m, err := GetMirrorByRepoID(com.StrTo(repoID).MustInt64())
  193. if err != nil {
  194. log.Error(4, "GetMirrorByRepoID [%d]: %v", repoID, err)
  195. continue
  196. }
  197. if !m.runSync() {
  198. continue
  199. }
  200. m.ScheduleNextUpdate()
  201. if err = UpdateMirror(m); err != nil {
  202. log.Error(4, "UpdateMirror [%d]: %v", repoID, err)
  203. continue
  204. }
  205. }
  206. }
  207. func InitSyncMirrors() {
  208. go SyncMirrors()
  209. }