Files
Gogs/internal/route/admin/admin.go

257 lines
7.5 KiB
Go
Raw Normal View History

2014-03-20 07:50:26 -04:00
// Copyright 2014 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package admin
import (
2014-03-22 07:42:24 -04:00
"fmt"
"runtime"
2014-03-21 03:27:59 -04:00
"strings"
2014-03-22 07:42:24 -04:00
"time"
2014-03-21 03:27:59 -04:00
"github.com/json-iterator/go"
"github.com/unknwon/com"
2014-03-21 03:27:59 -04:00
"gogs.io/gogs/internal/conf"
"gogs.io/gogs/internal/context"
"gogs.io/gogs/internal/cron"
"gogs.io/gogs/internal/db"
"gogs.io/gogs/internal/mailer"
"gogs.io/gogs/internal/process"
"gogs.io/gogs/internal/tool"
2014-03-20 07:50:26 -04:00
)
const (
2017-04-05 09:17:21 -04:00
DASHBOARD = "admin/dashboard"
CONFIG = "admin/config"
MONITOR = "admin/monitor"
)
// initTime is the time when the application was initialized.
var initTime = time.Now()
2014-03-22 09:21:57 -04:00
2014-03-22 07:42:24 -04:00
var sysStatus struct {
2014-03-22 09:21:57 -04:00
Uptime string
2014-03-22 07:42:24 -04:00
NumGoroutine int
// General statistics.
MemAllocated string // bytes allocated and still in use
MemTotal string // bytes allocated (even if freed)
MemSys string // bytes obtained from system (sum of XxxSys below)
Lookups uint64 // number of pointer lookups
MemMallocs uint64 // number of mallocs
MemFrees uint64 // number of frees
// Main allocation heap statistics.
HeapAlloc string // bytes allocated and still in use
HeapSys string // bytes obtained from system
HeapIdle string // bytes in idle spans
HeapInuse string // bytes in non-idle span
HeapReleased string // bytes released to the OS
HeapObjects uint64 // total number of allocated objects
// Low-level fixed-size structure allocator statistics.
// Inuse is bytes used now.
// Sys is bytes obtained from system.
StackInuse string // bootstrap stacks
StackSys string
MSpanInuse string // mspan structures
MSpanSys string
MCacheInuse string // mcache structures
MCacheSys string
BuckHashSys string // profiling bucket hash table
GCSys string // GC metadata
OtherSys string // other system allocations
// Garbage collector statistics.
NextGC string // next run in HeapAlloc time (bytes)
LastGC string // last run in absolute time (ns)
PauseTotalNs string
PauseNs string // circular buffer of recent GC pause times, most recent at [(NumGC+255)%256]
NumGC uint32
}
func updateSystemStatus() {
sysStatus.Uptime = tool.TimeSincePro(initTime)
2014-03-22 09:21:57 -04:00
2014-03-22 07:42:24 -04:00
m := new(runtime.MemStats)
runtime.ReadMemStats(m)
sysStatus.NumGoroutine = runtime.NumGoroutine()
sysStatus.MemAllocated = tool.FileSize(int64(m.Alloc))
sysStatus.MemTotal = tool.FileSize(int64(m.TotalAlloc))
sysStatus.MemSys = tool.FileSize(int64(m.Sys))
2014-03-22 07:42:24 -04:00
sysStatus.Lookups = m.Lookups
sysStatus.MemMallocs = m.Mallocs
sysStatus.MemFrees = m.Frees
sysStatus.HeapAlloc = tool.FileSize(int64(m.HeapAlloc))
sysStatus.HeapSys = tool.FileSize(int64(m.HeapSys))
sysStatus.HeapIdle = tool.FileSize(int64(m.HeapIdle))
sysStatus.HeapInuse = tool.FileSize(int64(m.HeapInuse))
sysStatus.HeapReleased = tool.FileSize(int64(m.HeapReleased))
2014-03-22 07:42:24 -04:00
sysStatus.HeapObjects = m.HeapObjects
sysStatus.StackInuse = tool.FileSize(int64(m.StackInuse))
sysStatus.StackSys = tool.FileSize(int64(m.StackSys))
sysStatus.MSpanInuse = tool.FileSize(int64(m.MSpanInuse))
sysStatus.MSpanSys = tool.FileSize(int64(m.MSpanSys))
sysStatus.MCacheInuse = tool.FileSize(int64(m.MCacheInuse))
sysStatus.MCacheSys = tool.FileSize(int64(m.MCacheSys))
sysStatus.BuckHashSys = tool.FileSize(int64(m.BuckHashSys))
sysStatus.GCSys = tool.FileSize(int64(m.GCSys))
sysStatus.OtherSys = tool.FileSize(int64(m.OtherSys))
sysStatus.NextGC = tool.FileSize(int64(m.NextGC))
2014-03-22 07:42:24 -04:00
sysStatus.LastGC = fmt.Sprintf("%.1fs", float64(time.Now().UnixNano()-int64(m.LastGC))/1000/1000/1000)
2014-03-22 09:21:57 -04:00
sysStatus.PauseTotalNs = fmt.Sprintf("%.1fs", float64(m.PauseTotalNs)/1000/1000/1000)
sysStatus.PauseNs = fmt.Sprintf("%.3fs", float64(m.PauseNs[(m.NumGC+255)%256])/1000/1000/1000)
2014-03-22 07:42:24 -04:00
sysStatus.NumGC = m.NumGC
}
2014-05-06 13:47:47 -04:00
// Operation types.
2014-06-21 00:51:41 -04:00
type AdminOperation int
2014-05-06 13:47:47 -04:00
const (
2015-09-17 16:11:44 -04:00
CLEAN_INACTIVATE_USER AdminOperation = iota + 1
2014-11-18 19:05:33 -05:00
CLEAN_REPO_ARCHIVES
2015-11-18 15:37:48 -05:00
CLEAN_MISSING_REPOS
2014-11-30 02:26:29 -05:00
GIT_GC_REPOS
SYNC_SSH_AUTHORIZED_KEY
SYNC_REPOSITORY_HOOKS
REINIT_MISSING_REPOSITORY
2014-05-06 13:47:47 -04:00
)
2017-06-03 07:26:09 -04:00
func Dashboard(c *context.Context) {
c.Title("admin.dashboard")
c.PageIs("Admin")
c.PageIs("AdminDashboard")
2014-05-06 13:47:47 -04:00
// Run operation.
2017-06-03 07:26:09 -04:00
op, _ := com.StrTo(c.Query("op")).Int()
2014-05-06 13:47:47 -04:00
if op > 0 {
var err error
var success string
2014-06-21 00:51:41 -04:00
switch AdminOperation(op) {
case CLEAN_INACTIVATE_USER:
2017-06-03 07:26:09 -04:00
success = c.Tr("admin.dashboard.delete_inactivate_accounts_success")
err = db.DeleteInactivateUsers()
2014-11-18 19:05:33 -05:00
case CLEAN_REPO_ARCHIVES:
2017-06-03 07:26:09 -04:00
success = c.Tr("admin.dashboard.delete_repo_archives_success")
err = db.DeleteRepositoryArchives()
2015-11-18 15:37:48 -05:00
case CLEAN_MISSING_REPOS:
2017-06-03 07:26:09 -04:00
success = c.Tr("admin.dashboard.delete_missing_repos_success")
err = db.DeleteMissingRepositories()
2014-11-30 02:26:29 -05:00
case GIT_GC_REPOS:
2017-06-03 07:26:09 -04:00
success = c.Tr("admin.dashboard.git_gc_repos_success")
err = db.GitGcRepos()
case SYNC_SSH_AUTHORIZED_KEY:
2017-06-03 07:26:09 -04:00
success = c.Tr("admin.dashboard.resync_all_sshkeys_success")
err = db.RewriteAuthorizedKeys()
case SYNC_REPOSITORY_HOOKS:
2017-06-03 07:26:09 -04:00
success = c.Tr("admin.dashboard.resync_all_hooks_success")
err = db.SyncRepositoryHooks()
case REINIT_MISSING_REPOSITORY:
2017-06-03 07:26:09 -04:00
success = c.Tr("admin.dashboard.reinit_missing_repos_success")
err = db.ReinitMissingRepositories()
2014-05-06 13:47:47 -04:00
}
if err != nil {
2017-06-03 07:26:09 -04:00
c.Flash.Error(err.Error())
2014-05-06 13:47:47 -04:00
} else {
2017-06-03 07:26:09 -04:00
c.Flash.Success(success)
2014-05-06 13:47:47 -04:00
}
c.SubURLRedirect("/admin")
2014-05-06 13:47:47 -04:00
return
}
c.Data["GitVersion"] = conf.Git.Version
c.Data["GoVersion"] = runtime.Version()
c.Data["BuildTime"] = conf.BuildTime
c.Data["BuildCommit"] = conf.BuildCommit
c.Data["Stats"] = db.GetStatistic()
2014-11-18 19:05:33 -05:00
// FIXME: update periodically
2014-03-22 07:42:24 -04:00
updateSystemStatus()
2017-06-03 07:26:09 -04:00
c.Data["SysStatus"] = sysStatus
c.Success(DASHBOARD)
2014-03-20 07:50:26 -04:00
}
2017-06-03 07:26:09 -04:00
func SendTestMail(c *context.Context) {
email := c.Query("email")
2016-02-24 23:59:17 -05:00
// Send a test email to the user's email address and redirect back to Config
if err := mailer.SendTestMail(email); err != nil {
2017-06-03 07:26:09 -04:00
c.Flash.Error(c.Tr("admin.config.test_mail_failed", email, err))
2016-02-24 23:59:17 -05:00
} else {
2017-06-03 07:26:09 -04:00
c.Flash.Info(c.Tr("admin.config.test_mail_sent", email))
2016-02-24 23:59:17 -05:00
}
2016-02-18 17:13:12 -05:00
c.Redirect(conf.Server.Subpath + "/admin/config")
2016-02-18 17:13:12 -05:00
}
2017-06-03 07:26:09 -04:00
func Config(c *context.Context) {
c.Title("admin.config")
c.PageIs("Admin")
c.PageIs("AdminConfig")
2014-03-21 03:27:59 -04:00
c.Data["App"] = conf.App
c.Data["Server"] = conf.Server
c.Data["SSH"] = conf.SSH
c.Data["LogRootPath"] = conf.LogRootPath
c.Data["ReverseProxyAuthUser"] = conf.ReverseProxyAuthUser
c.Data["RepoRootPath"] = conf.RepoRootPath
c.Data["ScriptType"] = conf.ScriptType
c.Data["Repository"] = conf.Repository
c.Data["HTTP"] = conf.HTTP
c.Data["DbCfg"] = db.DbCfg
c.Data["Service"] = conf.Service
c.Data["Webhook"] = conf.Webhook
2014-06-08 04:45:34 -04:00
2017-06-03 07:26:09 -04:00
c.Data["MailerEnabled"] = false
if conf.MailService != nil {
2017-06-03 07:26:09 -04:00
c.Data["MailerEnabled"] = true
c.Data["Mailer"] = conf.MailService
2014-03-21 04:13:32 -04:00
}
2014-03-21 03:27:59 -04:00
c.Data["CacheAdapter"] = conf.CacheAdapter
c.Data["CacheInterval"] = conf.CacheInterval
c.Data["CacheConn"] = conf.CacheConn
2014-03-21 10:09:57 -04:00
c.Data["SessionConfig"] = conf.SessionConfig
2014-03-22 09:21:57 -04:00
c.Data["DisableGravatar"] = conf.DisableGravatar
c.Data["EnableFederatedAvatar"] = conf.EnableFederatedAvatar
2014-03-22 06:42:19 -04:00
c.Data["Git"] = conf.Git
2016-08-10 11:01:42 -07:00
type logger struct {
Mode, Config string
}
loggers := make([]*logger, len(conf.LogModes))
for i := range conf.LogModes {
2017-02-09 19:29:59 -05:00
loggers[i] = &logger{
Mode: strings.Title(conf.LogModes[i]),
2017-02-09 19:29:59 -05:00
}
result, _ := jsoniter.MarshalIndent(conf.LogConfigs[i], "", " ")
2017-02-09 19:29:59 -05:00
loggers[i].Config = string(result)
}
2017-06-03 07:26:09 -04:00
c.Data["Loggers"] = loggers
2014-03-21 12:13:13 -04:00
2017-06-03 07:26:09 -04:00
c.HTML(200, CONFIG)
2014-03-21 01:48:10 -04:00
}
2014-06-13 13:01:52 -04:00
2017-06-03 07:26:09 -04:00
func Monitor(c *context.Context) {
c.Data["Title"] = c.Tr("admin.monitor")
c.Data["PageIsAdmin"] = true
c.Data["PageIsAdminMonitor"] = true
c.Data["Processes"] = process.Processes
c.Data["Entries"] = cron.ListTasks()
c.HTML(200, MONITOR)
2014-06-13 13:01:52 -04:00
}