admin.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. package admin
  2. import (
  3. "fmt"
  4. "gitote/gitote/models"
  5. "gitote/gitote/pkg/context"
  6. "gitote/gitote/pkg/cron"
  7. "gitote/gitote/pkg/mailer"
  8. "gitote/gitote/pkg/process"
  9. "gitote/gitote/pkg/setting"
  10. "gitote/gitote/pkg/tool"
  11. "runtime"
  12. "strings"
  13. "time"
  14. "github.com/Unknwon/com"
  15. "github.com/json-iterator/go"
  16. "gopkg.in/macaron.v1"
  17. )
  18. const (
  19. // DASHBOARD page template
  20. DASHBOARD = "admin/dashboard"
  21. // ANALYTICS page template
  22. ANALYTICS = "admin/analytics"
  23. // API page template
  24. API = "admin/api"
  25. // CONFIG page template
  26. CONFIG = "admin/config"
  27. // MONITOR page template
  28. MONITOR = "admin/monitor"
  29. // STATUS page template
  30. STATUS = "admin/status"
  31. )
  32. var (
  33. startTime = time.Now()
  34. )
  35. var sysStatus struct {
  36. Uptime string
  37. NumGoroutine int
  38. // General statistics.
  39. MemAllocated string // bytes allocated and still in use
  40. MemTotal string // bytes allocated (even if freed)
  41. MemSys string // bytes obtained from system (sum of XxxSys below)
  42. Lookups uint64 // number of pointer lookups
  43. MemMallocs uint64 // number of mallocs
  44. MemFrees uint64 // number of frees
  45. // Main allocation heap statistics.
  46. HeapAlloc string // bytes allocated and still in use
  47. HeapSys string // bytes obtained from system
  48. HeapIdle string // bytes in idle spans
  49. HeapInuse string // bytes in non-idle span
  50. HeapReleased string // bytes released to the OS
  51. HeapObjects uint64 // total number of allocated objects
  52. // Low-level fixed-size structure allocator statistics.
  53. // Inuse is bytes used now.
  54. // Sys is bytes obtained from system.
  55. StackInuse string // bootstrap stacks
  56. StackSys string
  57. MSpanInuse string // mspan structures
  58. MSpanSys string
  59. MCacheInuse string // mcache structures
  60. MCacheSys string
  61. BuckHashSys string // profiling bucket hash table
  62. GCSys string // GC metadata
  63. OtherSys string // other system allocations
  64. // Garbage collector statistics.
  65. NextGC string // next run in HeapAlloc time (bytes)
  66. LastGC string // last run in absolute time (ns)
  67. PauseTotalNs string
  68. PauseNs string // circular buffer of recent GC pause times, most recent at [(NumGC+255)%256]
  69. NumGC uint32
  70. }
  71. func updateSystemStatus() {
  72. sysStatus.Uptime = tool.TimeSincePro(startTime)
  73. m := new(runtime.MemStats)
  74. runtime.ReadMemStats(m)
  75. sysStatus.NumGoroutine = runtime.NumGoroutine()
  76. sysStatus.MemAllocated = tool.FileSize(int64(m.Alloc))
  77. sysStatus.MemTotal = tool.FileSize(int64(m.TotalAlloc))
  78. sysStatus.MemSys = tool.FileSize(int64(m.Sys))
  79. sysStatus.Lookups = m.Lookups
  80. sysStatus.MemMallocs = m.Mallocs
  81. sysStatus.MemFrees = m.Frees
  82. sysStatus.HeapAlloc = tool.FileSize(int64(m.HeapAlloc))
  83. sysStatus.HeapSys = tool.FileSize(int64(m.HeapSys))
  84. sysStatus.HeapIdle = tool.FileSize(int64(m.HeapIdle))
  85. sysStatus.HeapInuse = tool.FileSize(int64(m.HeapInuse))
  86. sysStatus.HeapReleased = tool.FileSize(int64(m.HeapReleased))
  87. sysStatus.HeapObjects = m.HeapObjects
  88. sysStatus.StackInuse = tool.FileSize(int64(m.StackInuse))
  89. sysStatus.StackSys = tool.FileSize(int64(m.StackSys))
  90. sysStatus.MSpanInuse = tool.FileSize(int64(m.MSpanInuse))
  91. sysStatus.MSpanSys = tool.FileSize(int64(m.MSpanSys))
  92. sysStatus.MCacheInuse = tool.FileSize(int64(m.MCacheInuse))
  93. sysStatus.MCacheSys = tool.FileSize(int64(m.MCacheSys))
  94. sysStatus.BuckHashSys = tool.FileSize(int64(m.BuckHashSys))
  95. sysStatus.GCSys = tool.FileSize(int64(m.GCSys))
  96. sysStatus.OtherSys = tool.FileSize(int64(m.OtherSys))
  97. sysStatus.NextGC = tool.FileSize(int64(m.NextGC))
  98. sysStatus.LastGC = fmt.Sprintf("%.1fs", float64(time.Now().UnixNano()-int64(m.LastGC))/1000/1000/1000)
  99. sysStatus.PauseTotalNs = fmt.Sprintf("%.1fs", float64(m.PauseTotalNs)/1000/1000/1000)
  100. sysStatus.PauseNs = fmt.Sprintf("%.3fs", float64(m.PauseNs[(m.NumGC+255)%256])/1000/1000/1000)
  101. sysStatus.NumGC = m.NumGC
  102. }
  103. // AdminOperation types.
  104. type AdminOperation int
  105. const (
  106. // CLEAN_INACTIVATE_USER operation value
  107. CLEAN_INACTIVATE_USER AdminOperation = iota + 1
  108. // CLEAN_REPO_ARCHIVES operation value
  109. CLEAN_REPO_ARCHIVES
  110. // CLEAN_MISSING_REPOS operation value
  111. CLEAN_MISSING_REPOS
  112. // GIT_GC_REPOS operation value
  113. GIT_GC_REPOS
  114. // SYNC_SSH_AUTHORIZED_KEY operation value
  115. SYNC_SSH_AUTHORIZED_KEY
  116. // SYNC_REPOSITORY_HOOKS operation value
  117. SYNC_REPOSITORY_HOOKS
  118. // REINIT_MISSING_REPOSITORY operation value
  119. REINIT_MISSING_REPOSITORY
  120. )
  121. // Dashboard shows dashboard page
  122. func Dashboard(c *context.Context) {
  123. c.Data["Title"] = "Dashboard"
  124. c.Data["PageIsAdmin"] = true
  125. c.Data["PageIsAdminDashboard"] = true
  126. // Run operation.
  127. op, _ := com.StrTo(c.Query("op")).Int()
  128. if op > 0 {
  129. var err error
  130. var success string
  131. switch AdminOperation(op) {
  132. case CLEAN_REPO_ARCHIVES:
  133. success = "All repositories archives have been deleted successfully."
  134. err = models.DeleteRepositoryArchives()
  135. case CLEAN_MISSING_REPOS:
  136. success = "All repository records that lost Git files have been deleted successfully."
  137. err = models.DeleteMissingRepositories()
  138. case GIT_GC_REPOS:
  139. success = "All repositories have done garbage collection successfully."
  140. err = models.GitGcRepos()
  141. case SYNC_SSH_AUTHORIZED_KEY:
  142. success = "All public keys have been rewritten successfully."
  143. err = models.RewriteAuthorizedKeys()
  144. case SYNC_REPOSITORY_HOOKS:
  145. success = "All repositories' pre-receive, update and post-receive hooks have been resynced successfully."
  146. err = models.SyncRepositoryHooks()
  147. case REINIT_MISSING_REPOSITORY:
  148. success = "All repository records that lost Git files have been reinitialized successfully."
  149. err = models.ReinitMissingRepositories()
  150. }
  151. if err != nil {
  152. c.Flash.Error(err.Error())
  153. } else {
  154. c.Flash.Success(success)
  155. }
  156. c.Redirect(setting.AppSubURL + "/admin")
  157. return
  158. }
  159. c.Data["Stats"] = models.GetStatistic()
  160. // FIXME: update periodically
  161. updateSystemStatus()
  162. c.Data["SysStatus"] = sysStatus
  163. c.Data["GitVersion"] = setting.Git.Version
  164. c.HTML(200, DASHBOARD)
  165. }
  166. // SendTestMail send mail to verify the email
  167. func SendTestMail(c *context.Context) {
  168. email := c.Query("email")
  169. // Send a test email to the user's email address and redirect back to Config
  170. if err := mailer.SendTestMail(email); err != nil {
  171. c.Flash.Error(c.Tr("admin.config.test_mail_failed", email, err))
  172. } else {
  173. c.Flash.Info(c.Tr("admin.config.test_mail_sent", email))
  174. }
  175. c.Redirect(setting.AppSubURL + "/admin/config")
  176. }
  177. // Config shows configuration page
  178. func Config(c *context.Context) {
  179. c.Data["Title"] = "Configuration"
  180. c.Data["PageIsAdmin"] = true
  181. c.Data["PageIsAdminConfig"] = true
  182. c.Data["AppURL"] = setting.AppURL
  183. c.Data["Domain"] = setting.Domain
  184. c.Data["OfflineMode"] = setting.OfflineMode
  185. c.Data["DisableRouterLog"] = setting.DisableRouterLog
  186. c.Data["RunUser"] = setting.RunUser
  187. c.Data["RunMode"] = strings.Title(macaron.Env)
  188. c.Data["StaticRootPath"] = setting.StaticRootPath
  189. c.Data["LogRootPath"] = setting.LogRootPath
  190. c.Data["ReverseProxyAuthUser"] = setting.ReverseProxyAuthUser
  191. c.Data["SSH"] = setting.SSH
  192. c.Data["RepoRootPath"] = setting.RepoRootPath
  193. c.Data["ScriptType"] = setting.ScriptType
  194. c.Data["Repository"] = setting.Repository
  195. c.Data["HTTP"] = setting.HTTP
  196. c.Data["DbCfg"] = models.DbCfg
  197. c.Data["Service"] = setting.Service
  198. c.Data["Webhook"] = setting.Webhook
  199. c.Data["MailerEnabled"] = false
  200. if setting.MailService != nil {
  201. c.Data["MailerEnabled"] = true
  202. c.Data["Mailer"] = setting.MailService
  203. }
  204. c.Data["CacheAdapter"] = setting.CacheAdapter
  205. c.Data["CacheInterval"] = setting.CacheInterval
  206. c.Data["CacheConn"] = setting.CacheConn
  207. c.Data["SessionConfig"] = setting.SessionConfig
  208. c.Data["DisableGravatar"] = setting.DisableGravatar
  209. c.Data["EnableFederatedAvatar"] = setting.EnableFederatedAvatar
  210. c.Data["GitVersion"] = setting.Git.Version
  211. c.Data["Git"] = setting.Git
  212. type logger struct {
  213. Mode, Config string
  214. }
  215. loggers := make([]*logger, len(setting.LogModes))
  216. for i := range setting.LogModes {
  217. loggers[i] = &logger{
  218. Mode: strings.Title(setting.LogModes[i]),
  219. }
  220. result, _ := jsoniter.MarshalIndent(setting.LogConfigs[i], "", " ")
  221. loggers[i].Config = string(result)
  222. }
  223. c.Data["Loggers"] = loggers
  224. c.HTML(200, CONFIG)
  225. }
  226. // Monitor shows monitor page
  227. func Monitor(c *context.Context) {
  228. c.Data["Title"] = "Monitoring"
  229. c.Data["PageIsAdmin"] = true
  230. c.Data["PageIsAdminMonitor"] = true
  231. c.Data["Processes"] = process.Processes
  232. c.Data["Entries"] = cron.ListTasks()
  233. c.HTML(200, MONITOR)
  234. }
  235. // Status shows status page
  236. func Status(c *context.Context) {
  237. c.Data["Title"] = "Status"
  238. c.Data["PageIsAdminStatus"] = true
  239. c.HTML(200, STATUS)
  240. }
  241. // Analytics shows analytics page
  242. func Analytics(c *context.Context) {
  243. c.Data["Title"] = "Analytics"
  244. c.Data["PageIsAdminAnalytics"] = true
  245. c.HTML(200, ANALYTICS)
  246. }
  247. // AdminAPI shows api configuration
  248. func AdminAPI(c *context.Context) {
  249. c.Data["Title"] = "API"
  250. c.Data["PageIsAdminAPI"] = true
  251. c.HTML(200, API)
  252. }