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.

admin.go 9.1KB

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