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.

252 lines
7.8 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
Add support for federated avatars (#3320) * Add support for federated avatars Fixes #3105 Removes avatar fetching duplication code Adds an "Enable Federated Avatar" checkbox in user settings (defaults to unchecked) Moves avatar settings all in the same form, making local and remote avatars mutually exclusive Renames UploadAvatarForm to AvatarForm as it's not anymore only for uploading * Run gofmt on all modified files * Move Avatar form in its own page * Add go-libravatar dependency to vendor/ dir Hopefully helps with accepting the contribution. See also #3214 * Revert "Add go-libravatar dependency to vendor/ dir" This reverts commit a8cb93ae640bbb90f7d25012fc257bda9fae9b82. * Make federated avatar setting a global configuration Removes the per-user setting * Move avatar handling back to base tool, disable federated avatar in offline mode * Format, handle error * Properly set fallback host * Use unsupported github.com mirror for importing go-libravatar * Remove comment showing life exists outside of github.com ... pity, but contribution would not be accepted otherwise * Use Combo for Get and Post methods over /avatar * FEDERATED_AVATAR -> ENABLE_FEDERATED_AVATAR * Fix persistance of federated avatar lookup checkbox at install time * Federated Avatars -> Enable Federated Avatars * Use len(string) == 0 instead of string == "" * Move import line where it belong See https://github.com/Unknwon/go-code-convention/blob/master/en-US/import_packages.md Pity the import url is still the unofficial one, but oh well... * Save a line (and waste much more expensive time) * Remove redundant parens * Remove an empty line * Remove empty lines * Reorder lines to make diff smaller * Remove another newline Unknwon review got me start a fight against newlines * Move DISABLE_GRAVATAR and ENABLE_FEDERATED_AVATAR after OFFLINE_MODE On re-reading the diff I figured what Unknwon meant here: https://github.com/gogits/gogs/pull/3320/files#r73741106 * Remove newlines that weren't there before my intervention
8 years ago
10 years ago
  1. // Copyright 2014 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 admin
  5. import (
  6. "fmt"
  7. "runtime"
  8. "strings"
  9. "time"
  10. "github.com/Unknwon/com"
  11. "gopkg.in/macaron.v1"
  12. "code.gitea.io/gitea/models"
  13. "code.gitea.io/gitea/modules/base"
  14. "code.gitea.io/gitea/modules/context"
  15. "code.gitea.io/gitea/modules/cron"
  16. "code.gitea.io/gitea/modules/process"
  17. "code.gitea.io/gitea/modules/setting"
  18. )
  19. const (
  20. tplDashboard base.TplName = "admin/dashboard"
  21. tplConfig base.TplName = "admin/config"
  22. tplMonitor base.TplName = "admin/monitor"
  23. )
  24. var (
  25. startTime = time.Now()
  26. )
  27. var sysStatus struct {
  28. Uptime string
  29. NumGoroutine int
  30. // General statistics.
  31. MemAllocated string // bytes allocated and still in use
  32. MemTotal string // bytes allocated (even if freed)
  33. MemSys string // bytes obtained from system (sum of XxxSys below)
  34. Lookups uint64 // number of pointer lookups
  35. MemMallocs uint64 // number of mallocs
  36. MemFrees uint64 // number of frees
  37. // Main allocation heap statistics.
  38. HeapAlloc string // bytes allocated and still in use
  39. HeapSys string // bytes obtained from system
  40. HeapIdle string // bytes in idle spans
  41. HeapInuse string // bytes in non-idle span
  42. HeapReleased string // bytes released to the OS
  43. HeapObjects uint64 // total number of allocated objects
  44. // Low-level fixed-size structure allocator statistics.
  45. // Inuse is bytes used now.
  46. // Sys is bytes obtained from system.
  47. StackInuse string // bootstrap stacks
  48. StackSys string
  49. MSpanInuse string // mspan structures
  50. MSpanSys string
  51. MCacheInuse string // mcache structures
  52. MCacheSys string
  53. BuckHashSys string // profiling bucket hash table
  54. GCSys string // GC metadata
  55. OtherSys string // other system allocations
  56. // Garbage collector statistics.
  57. NextGC string // next run in HeapAlloc time (bytes)
  58. LastGC string // last run in absolute time (ns)
  59. PauseTotalNs string
  60. PauseNs string // circular buffer of recent GC pause times, most recent at [(NumGC+255)%256]
  61. NumGC uint32
  62. }
  63. func updateSystemStatus() {
  64. sysStatus.Uptime = base.TimeSincePro(startTime)
  65. m := new(runtime.MemStats)
  66. runtime.ReadMemStats(m)
  67. sysStatus.NumGoroutine = runtime.NumGoroutine()
  68. sysStatus.MemAllocated = base.FileSize(int64(m.Alloc))
  69. sysStatus.MemTotal = base.FileSize(int64(m.TotalAlloc))
  70. sysStatus.MemSys = base.FileSize(int64(m.Sys))
  71. sysStatus.Lookups = m.Lookups
  72. sysStatus.MemMallocs = m.Mallocs
  73. sysStatus.MemFrees = m.Frees
  74. sysStatus.HeapAlloc = base.FileSize(int64(m.HeapAlloc))
  75. sysStatus.HeapSys = base.FileSize(int64(m.HeapSys))
  76. sysStatus.HeapIdle = base.FileSize(int64(m.HeapIdle))
  77. sysStatus.HeapInuse = base.FileSize(int64(m.HeapInuse))
  78. sysStatus.HeapReleased = base.FileSize(int64(m.HeapReleased))
  79. sysStatus.HeapObjects = m.HeapObjects
  80. sysStatus.StackInuse = base.FileSize(int64(m.StackInuse))
  81. sysStatus.StackSys = base.FileSize(int64(m.StackSys))
  82. sysStatus.MSpanInuse = base.FileSize(int64(m.MSpanInuse))
  83. sysStatus.MSpanSys = base.FileSize(int64(m.MSpanSys))
  84. sysStatus.MCacheInuse = base.FileSize(int64(m.MCacheInuse))
  85. sysStatus.MCacheSys = base.FileSize(int64(m.MCacheSys))
  86. sysStatus.BuckHashSys = base.FileSize(int64(m.BuckHashSys))
  87. sysStatus.GCSys = base.FileSize(int64(m.GCSys))
  88. sysStatus.OtherSys = base.FileSize(int64(m.OtherSys))
  89. sysStatus.NextGC = base.FileSize(int64(m.NextGC))
  90. sysStatus.LastGC = fmt.Sprintf("%.1fs", float64(time.Now().UnixNano()-int64(m.LastGC))/1000/1000/1000)
  91. sysStatus.PauseTotalNs = fmt.Sprintf("%.1fs", float64(m.PauseTotalNs)/1000/1000/1000)
  92. sysStatus.PauseNs = fmt.Sprintf("%.3fs", float64(m.PauseNs[(m.NumGC+255)%256])/1000/1000/1000)
  93. sysStatus.NumGC = m.NumGC
  94. }
  95. // Operation Operation types.
  96. type Operation int
  97. const (
  98. cleanInactivateUser Operation = iota + 1
  99. cleanRepoArchives
  100. cleanMissingRepos
  101. gitGCRepos
  102. syncSSHAuthorizedKey
  103. syncRepositoryUpdateHook
  104. reinitMissingRepository
  105. )
  106. // Dashboard show admin panel dashboard
  107. func Dashboard(ctx *context.Context) {
  108. ctx.Data["Title"] = ctx.Tr("admin.dashboard")
  109. ctx.Data["PageIsAdmin"] = true
  110. ctx.Data["PageIsAdminDashboard"] = true
  111. // Run operation.
  112. op, _ := com.StrTo(ctx.Query("op")).Int()
  113. if op > 0 {
  114. var err error
  115. var success string
  116. switch Operation(op) {
  117. case cleanInactivateUser:
  118. success = ctx.Tr("admin.dashboard.delete_inactivate_accounts_success")
  119. err = models.DeleteInactivateUsers()
  120. case cleanRepoArchives:
  121. success = ctx.Tr("admin.dashboard.delete_repo_archives_success")
  122. err = models.DeleteRepositoryArchives()
  123. case cleanMissingRepos:
  124. success = ctx.Tr("admin.dashboard.delete_missing_repos_success")
  125. err = models.DeleteMissingRepositories()
  126. case gitGCRepos:
  127. success = ctx.Tr("admin.dashboard.git_gc_repos_success")
  128. err = models.GitGcRepos()
  129. case syncSSHAuthorizedKey:
  130. success = ctx.Tr("admin.dashboard.resync_all_sshkeys_success")
  131. err = models.RewriteAllPublicKeys()
  132. case syncRepositoryUpdateHook:
  133. success = ctx.Tr("admin.dashboard.resync_all_update_hooks_success")
  134. err = models.RewriteRepositoryUpdateHook()
  135. case reinitMissingRepository:
  136. success = ctx.Tr("admin.dashboard.reinit_missing_repos_success")
  137. err = models.ReinitMissingRepositories()
  138. }
  139. if err != nil {
  140. ctx.Flash.Error(err.Error())
  141. } else {
  142. ctx.Flash.Success(success)
  143. }
  144. ctx.Redirect(setting.AppSubURL + "/admin")
  145. return
  146. }
  147. ctx.Data["Stats"] = models.GetStatistic()
  148. // FIXME: update periodically
  149. updateSystemStatus()
  150. ctx.Data["SysStatus"] = sysStatus
  151. ctx.HTML(200, tplDashboard)
  152. }
  153. // SendTestMail send test mail to confirm mail service is OK
  154. func SendTestMail(ctx *context.Context) {
  155. email := ctx.Query("email")
  156. // Send a test email to the user's email address and redirect back to Config
  157. if err := models.SendTestMail(email); err != nil {
  158. ctx.Flash.Error(ctx.Tr("admin.config.test_mail_failed", email, err))
  159. } else {
  160. ctx.Flash.Info(ctx.Tr("admin.config.test_mail_sent", email))
  161. }
  162. ctx.Redirect(setting.AppSubURL + "/admin/config")
  163. }
  164. // Config show admin config page
  165. func Config(ctx *context.Context) {
  166. ctx.Data["Title"] = ctx.Tr("admin.config")
  167. ctx.Data["PageIsAdmin"] = true
  168. ctx.Data["PageIsAdminConfig"] = true
  169. ctx.Data["AppUrl"] = setting.AppURL
  170. ctx.Data["Domain"] = setting.Domain
  171. ctx.Data["OfflineMode"] = setting.OfflineMode
  172. ctx.Data["DisableRouterLog"] = setting.DisableRouterLog
  173. ctx.Data["RunUser"] = setting.RunUser
  174. ctx.Data["RunMode"] = strings.Title(macaron.Env)
  175. ctx.Data["RepoRootPath"] = setting.RepoRootPath
  176. ctx.Data["StaticRootPath"] = setting.StaticRootPath
  177. ctx.Data["LogRootPath"] = setting.LogRootPath
  178. ctx.Data["ScriptType"] = setting.ScriptType
  179. ctx.Data["ReverseProxyAuthUser"] = setting.ReverseProxyAuthUser
  180. ctx.Data["SSH"] = setting.SSH
  181. ctx.Data["Service"] = setting.Service
  182. ctx.Data["DbCfg"] = models.DbCfg
  183. ctx.Data["Webhook"] = setting.Webhook
  184. ctx.Data["MailerEnabled"] = false
  185. if setting.MailService != nil {
  186. ctx.Data["MailerEnabled"] = true
  187. ctx.Data["Mailer"] = setting.MailService
  188. }
  189. ctx.Data["CacheAdapter"] = setting.CacheAdapter
  190. ctx.Data["CacheInterval"] = setting.CacheInterval
  191. ctx.Data["CacheConn"] = setting.CacheConn
  192. ctx.Data["SessionConfig"] = setting.SessionConfig
  193. ctx.Data["DisableGravatar"] = setting.DisableGravatar
  194. ctx.Data["EnableFederatedAvatar"] = setting.EnableFederatedAvatar
  195. ctx.Data["Git"] = setting.Git
  196. type logger struct {
  197. Mode, Config string
  198. }
  199. loggers := make([]*logger, len(setting.LogModes))
  200. for i := range setting.LogModes {
  201. loggers[i] = &logger{setting.LogModes[i], setting.LogConfigs[i]}
  202. }
  203. ctx.Data["Loggers"] = loggers
  204. ctx.HTML(200, tplConfig)
  205. }
  206. // Monitor show admin monitor page
  207. func Monitor(ctx *context.Context) {
  208. ctx.Data["Title"] = ctx.Tr("admin.monitor")
  209. ctx.Data["PageIsAdmin"] = true
  210. ctx.Data["PageIsAdminMonitor"] = true
  211. ctx.Data["Processes"] = process.Processes
  212. ctx.Data["Entries"] = cron.ListTasks()
  213. ctx.HTML(200, tplMonitor)
  214. }