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

admin.go 8.9 kB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
12 years ago
10 years ago
12 years ago
12 years ago
12 years ago
12 years ago
11 years ago
12 years ago
11 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 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
9 years ago
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. "os"
  8. "runtime"
  9. "strings"
  10. "time"
  11. "github.com/Unknwon/com"
  12. "gopkg.in/macaron.v1"
  13. "code.gitea.io/gitea/models"
  14. "code.gitea.io/gitea/modules/base"
  15. "code.gitea.io/gitea/modules/context"
  16. "code.gitea.io/gitea/modules/cron"
  17. "code.gitea.io/gitea/modules/process"
  18. "code.gitea.io/gitea/modules/setting"
  19. )
  20. const (
  21. tplDashboard base.TplName = "admin/dashboard"
  22. tplConfig base.TplName = "admin/config"
  23. tplMonitor base.TplName = "admin/monitor"
  24. )
  25. var (
  26. startTime = time.Now()
  27. )
  28. var sysStatus struct {
  29. Uptime string
  30. NumGoroutine int
  31. // General statistics.
  32. MemAllocated string // bytes allocated and still in use
  33. MemTotal string // bytes allocated (even if freed)
  34. MemSys string // bytes obtained from system (sum of XxxSys below)
  35. Lookups uint64 // number of pointer lookups
  36. MemMallocs uint64 // number of mallocs
  37. MemFrees uint64 // number of frees
  38. // Main allocation heap statistics.
  39. HeapAlloc string // bytes allocated and still in use
  40. HeapSys string // bytes obtained from system
  41. HeapIdle string // bytes in idle spans
  42. HeapInuse string // bytes in non-idle span
  43. HeapReleased string // bytes released to the OS
  44. HeapObjects uint64 // total number of allocated objects
  45. // Low-level fixed-size structure allocator statistics.
  46. // Inuse is bytes used now.
  47. // Sys is bytes obtained from system.
  48. StackInuse string // bootstrap stacks
  49. StackSys string
  50. MSpanInuse string // mspan structures
  51. MSpanSys string
  52. MCacheInuse string // mcache structures
  53. MCacheSys string
  54. BuckHashSys string // profiling bucket hash table
  55. GCSys string // GC metadata
  56. OtherSys string // other system allocations
  57. // Garbage collector statistics.
  58. NextGC string // next run in HeapAlloc time (bytes)
  59. LastGC string // last run in absolute time (ns)
  60. PauseTotalNs string
  61. PauseNs string // circular buffer of recent GC pause times, most recent at [(NumGC+255)%256]
  62. NumGC uint32
  63. }
  64. func updateSystemStatus() {
  65. sysStatus.Uptime = base.TimeSincePro(startTime, "en")
  66. m := new(runtime.MemStats)
  67. runtime.ReadMemStats(m)
  68. sysStatus.NumGoroutine = runtime.NumGoroutine()
  69. sysStatus.MemAllocated = base.FileSize(int64(m.Alloc))
  70. sysStatus.MemTotal = base.FileSize(int64(m.TotalAlloc))
  71. sysStatus.MemSys = base.FileSize(int64(m.Sys))
  72. sysStatus.Lookups = m.Lookups
  73. sysStatus.MemMallocs = m.Mallocs
  74. sysStatus.MemFrees = m.Frees
  75. sysStatus.HeapAlloc = base.FileSize(int64(m.HeapAlloc))
  76. sysStatus.HeapSys = base.FileSize(int64(m.HeapSys))
  77. sysStatus.HeapIdle = base.FileSize(int64(m.HeapIdle))
  78. sysStatus.HeapInuse = base.FileSize(int64(m.HeapInuse))
  79. sysStatus.HeapReleased = base.FileSize(int64(m.HeapReleased))
  80. sysStatus.HeapObjects = m.HeapObjects
  81. sysStatus.StackInuse = base.FileSize(int64(m.StackInuse))
  82. sysStatus.StackSys = base.FileSize(int64(m.StackSys))
  83. sysStatus.MSpanInuse = base.FileSize(int64(m.MSpanInuse))
  84. sysStatus.MSpanSys = base.FileSize(int64(m.MSpanSys))
  85. sysStatus.MCacheInuse = base.FileSize(int64(m.MCacheInuse))
  86. sysStatus.MCacheSys = base.FileSize(int64(m.MCacheSys))
  87. sysStatus.BuckHashSys = base.FileSize(int64(m.BuckHashSys))
  88. sysStatus.GCSys = base.FileSize(int64(m.GCSys))
  89. sysStatus.OtherSys = base.FileSize(int64(m.OtherSys))
  90. sysStatus.NextGC = base.FileSize(int64(m.NextGC))
  91. sysStatus.LastGC = fmt.Sprintf("%.1fs", float64(time.Now().UnixNano()-int64(m.LastGC))/1000/1000/1000)
  92. sysStatus.PauseTotalNs = fmt.Sprintf("%.1fs", float64(m.PauseTotalNs)/1000/1000/1000)
  93. sysStatus.PauseNs = fmt.Sprintf("%.3fs", float64(m.PauseNs[(m.NumGC+255)%256])/1000/1000/1000)
  94. sysStatus.NumGC = m.NumGC
  95. }
  96. // Operation Operation types.
  97. type Operation int
  98. const (
  99. cleanInactivateUser Operation = iota + 1
  100. cleanRepoArchives
  101. cleanMissingRepos
  102. gitGCRepos
  103. syncSSHAuthorizedKey
  104. syncRepositoryUpdateHook
  105. reinitMissingRepository
  106. syncExternalUsers
  107. gitFsck
  108. )
  109. // Dashboard show admin panel dashboard
  110. func Dashboard(ctx *context.Context) {
  111. ctx.Data["Title"] = ctx.Tr("admin.dashboard")
  112. ctx.Data["PageIsAdmin"] = true
  113. ctx.Data["PageIsAdminDashboard"] = true
  114. // Run operation.
  115. op, _ := com.StrTo(ctx.Query("op")).Int()
  116. if op > 0 {
  117. var err error
  118. var success string
  119. switch Operation(op) {
  120. case cleanInactivateUser:
  121. success = ctx.Tr("admin.dashboard.delete_inactivate_accounts_success")
  122. err = models.DeleteInactivateUsers()
  123. case cleanRepoArchives:
  124. success = ctx.Tr("admin.dashboard.delete_repo_archives_success")
  125. err = models.DeleteRepositoryArchives()
  126. case cleanMissingRepos:
  127. success = ctx.Tr("admin.dashboard.delete_missing_repos_success")
  128. err = models.DeleteMissingRepositories(ctx.User)
  129. case gitGCRepos:
  130. success = ctx.Tr("admin.dashboard.git_gc_repos_success")
  131. err = models.GitGcRepos()
  132. case syncSSHAuthorizedKey:
  133. success = ctx.Tr("admin.dashboard.resync_all_sshkeys_success")
  134. err = models.RewriteAllPublicKeys()
  135. case syncRepositoryUpdateHook:
  136. success = ctx.Tr("admin.dashboard.resync_all_hooks_success")
  137. err = models.SyncRepositoryHooks()
  138. case reinitMissingRepository:
  139. success = ctx.Tr("admin.dashboard.reinit_missing_repos_success")
  140. err = models.ReinitMissingRepositories()
  141. case syncExternalUsers:
  142. success = ctx.Tr("admin.dashboard.sync_external_users_started")
  143. go models.SyncExternalUsers()
  144. case gitFsck:
  145. success = ctx.Tr("admin.dashboard.git_fsck_started")
  146. go models.GitFsck()
  147. }
  148. if err != nil {
  149. ctx.Flash.Error(err.Error())
  150. } else {
  151. ctx.Flash.Success(success)
  152. }
  153. ctx.Redirect(setting.AppSubURL + "/admin")
  154. return
  155. }
  156. ctx.Data["Stats"] = models.GetStatistic()
  157. // FIXME: update periodically
  158. updateSystemStatus()
  159. ctx.Data["SysStatus"] = sysStatus
  160. ctx.HTML(200, tplDashboard)
  161. }
  162. // SendTestMail send test mail to confirm mail service is OK
  163. func SendTestMail(ctx *context.Context) {
  164. email := ctx.Query("email")
  165. // Send a test email to the user's email address and redirect back to Config
  166. if err := models.SendTestMail(email); err != nil {
  167. ctx.Flash.Error(ctx.Tr("admin.config.test_mail_failed", email, err))
  168. } else {
  169. ctx.Flash.Info(ctx.Tr("admin.config.test_mail_sent", email))
  170. }
  171. ctx.Redirect(setting.AppSubURL + "/admin/config")
  172. }
  173. // Config show admin config page
  174. func Config(ctx *context.Context) {
  175. ctx.Data["Title"] = ctx.Tr("admin.config")
  176. ctx.Data["PageIsAdmin"] = true
  177. ctx.Data["PageIsAdminConfig"] = true
  178. ctx.Data["CustomConf"] = setting.CustomConf
  179. ctx.Data["AppUrl"] = setting.AppURL
  180. ctx.Data["Domain"] = setting.Domain
  181. ctx.Data["OfflineMode"] = setting.OfflineMode
  182. ctx.Data["DisableRouterLog"] = setting.DisableRouterLog
  183. ctx.Data["RunUser"] = setting.RunUser
  184. ctx.Data["RunMode"] = strings.Title(macaron.Env)
  185. ctx.Data["GitVersion"] = setting.Git.Version
  186. ctx.Data["RepoRootPath"] = setting.RepoRootPath
  187. ctx.Data["CustomRootPath"] = setting.CustomPath
  188. ctx.Data["StaticRootPath"] = setting.StaticRootPath
  189. ctx.Data["LogRootPath"] = setting.LogRootPath
  190. ctx.Data["ScriptType"] = setting.ScriptType
  191. ctx.Data["ReverseProxyAuthUser"] = setting.ReverseProxyAuthUser
  192. ctx.Data["ReverseProxyAuthEmail"] = setting.ReverseProxyAuthEmail
  193. ctx.Data["SSH"] = setting.SSH
  194. ctx.Data["Service"] = setting.Service
  195. ctx.Data["DbCfg"] = models.DbCfg
  196. ctx.Data["Webhook"] = setting.Webhook
  197. ctx.Data["MailerEnabled"] = false
  198. if setting.MailService != nil {
  199. ctx.Data["MailerEnabled"] = true
  200. ctx.Data["Mailer"] = setting.MailService
  201. }
  202. ctx.Data["CacheAdapter"] = setting.CacheService.Adapter
  203. ctx.Data["CacheInterval"] = setting.CacheService.Interval
  204. ctx.Data["CacheConn"] = setting.CacheService.Conn
  205. ctx.Data["SessionConfig"] = setting.SessionConfig
  206. ctx.Data["DisableGravatar"] = setting.DisableGravatar
  207. ctx.Data["EnableFederatedAvatar"] = setting.EnableFederatedAvatar
  208. ctx.Data["Git"] = setting.Git
  209. type envVar struct {
  210. Name, Value string
  211. }
  212. envVars := map[string]*envVar{}
  213. if len(os.Getenv("GITEA_WORK_DIR")) > 0 {
  214. envVars["GITEA_WORK_DIR"] = &envVar{"GITEA_WORK_DIR", os.Getenv("GITEA_WORK_DIR")}
  215. }
  216. if len(os.Getenv("GITEA_CUSTOM")) > 0 {
  217. envVars["GITEA_CUSTOM"] = &envVar{"GITEA_CUSTOM", os.Getenv("GITEA_CUSTOM")}
  218. }
  219. ctx.Data["EnvVars"] = envVars
  220. type logger struct {
  221. Mode, Config string
  222. }
  223. loggers := make([]*logger, len(setting.LogModes))
  224. for i := range setting.LogModes {
  225. loggers[i] = &logger{setting.LogModes[i], setting.LogConfigs[i]}
  226. }
  227. ctx.Data["Loggers"] = loggers
  228. ctx.HTML(200, tplConfig)
  229. }
  230. // Monitor show admin monitor page
  231. func Monitor(ctx *context.Context) {
  232. ctx.Data["Title"] = ctx.Tr("admin.monitor")
  233. ctx.Data["PageIsAdmin"] = true
  234. ctx.Data["PageIsAdminMonitor"] = true
  235. ctx.Data["Processes"] = process.GetManager().Processes
  236. ctx.Data["Entries"] = cron.ListTasks()
  237. ctx.HTML(200, tplMonitor)
  238. }