mirror of https://github.com/gogits/gogs.git
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.
259 lines
7.7 KiB
259 lines
7.7 KiB
11 years ago
|
// 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 (
|
||
8 years ago
|
"encoding/json"
|
||
11 years ago
|
"fmt"
|
||
|
"runtime"
|
||
11 years ago
|
"strings"
|
||
11 years ago
|
"time"
|
||
11 years ago
|
|
||
11 years ago
|
"github.com/Unknwon/com"
|
||
9 years ago
|
"gopkg.in/macaron.v1"
|
||
11 years ago
|
|
||
11 years ago
|
"github.com/gogits/gogs/models"
|
||
8 years ago
|
"github.com/gogits/gogs/pkg/context"
|
||
|
"github.com/gogits/gogs/pkg/cron"
|
||
|
"github.com/gogits/gogs/pkg/mailer"
|
||
|
"github.com/gogits/gogs/pkg/process"
|
||
|
"github.com/gogits/gogs/pkg/setting"
|
||
8 years ago
|
"github.com/gogits/gogs/pkg/tool"
|
||
11 years ago
|
)
|
||
|
|
||
11 years ago
|
const (
|
||
8 years ago
|
DASHBOARD = "admin/dashboard"
|
||
|
CONFIG = "admin/config"
|
||
|
MONITOR = "admin/monitor"
|
||
11 years ago
|
)
|
||
|
|
||
11 years ago
|
var (
|
||
|
startTime = time.Now()
|
||
|
)
|
||
11 years ago
|
|
||
11 years ago
|
var sysStatus struct {
|
||
11 years ago
|
Uptime string
|
||
11 years ago
|
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() {
|
||
8 years ago
|
sysStatus.Uptime = tool.TimeSincePro(startTime)
|
||
11 years ago
|
|
||
11 years ago
|
m := new(runtime.MemStats)
|
||
|
runtime.ReadMemStats(m)
|
||
|
sysStatus.NumGoroutine = runtime.NumGoroutine()
|
||
|
|
||
8 years ago
|
sysStatus.MemAllocated = tool.FileSize(int64(m.Alloc))
|
||
|
sysStatus.MemTotal = tool.FileSize(int64(m.TotalAlloc))
|
||
|
sysStatus.MemSys = tool.FileSize(int64(m.Sys))
|
||
11 years ago
|
sysStatus.Lookups = m.Lookups
|
||
|
sysStatus.MemMallocs = m.Mallocs
|
||
|
sysStatus.MemFrees = m.Frees
|
||
|
|
||
8 years ago
|
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))
|
||
11 years ago
|
sysStatus.HeapObjects = m.HeapObjects
|
||
|
|
||
8 years ago
|
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))
|
||
11 years ago
|
sysStatus.LastGC = fmt.Sprintf("%.1fs", float64(time.Now().UnixNano()-int64(m.LastGC))/1000/1000/1000)
|
||
11 years ago
|
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)
|
||
11 years ago
|
sysStatus.NumGC = m.NumGC
|
||
|
}
|
||
|
|
||
11 years ago
|
// Operation types.
|
||
11 years ago
|
type AdminOperation int
|
||
|
|
||
11 years ago
|
const (
|
||
9 years ago
|
CLEAN_INACTIVATE_USER AdminOperation = iota + 1
|
||
10 years ago
|
CLEAN_REPO_ARCHIVES
|
||
9 years ago
|
CLEAN_MISSING_REPOS
|
||
10 years ago
|
GIT_GC_REPOS
|
||
10 years ago
|
SYNC_SSH_AUTHORIZED_KEY
|
||
8 years ago
|
SYNC_REPOSITORY_HOOKS
|
||
9 years ago
|
REINIT_MISSING_REPOSITORY
|
||
11 years ago
|
)
|
||
|
|
||
8 years ago
|
func Dashboard(c *context.Context) {
|
||
|
c.Data["Title"] = c.Tr("admin.dashboard")
|
||
|
c.Data["PageIsAdmin"] = true
|
||
|
c.Data["PageIsAdminDashboard"] = true
|
||
11 years ago
|
|
||
|
// Run operation.
|
||
8 years ago
|
op, _ := com.StrTo(c.Query("op")).Int()
|
||
11 years ago
|
if op > 0 {
|
||
|
var err error
|
||
|
var success string
|
||
|
|
||
11 years ago
|
switch AdminOperation(op) {
|
||
|
case CLEAN_INACTIVATE_USER:
|
||
8 years ago
|
success = c.Tr("admin.dashboard.delete_inactivate_accounts_success")
|
||
11 years ago
|
err = models.DeleteInactivateUsers()
|
||
10 years ago
|
case CLEAN_REPO_ARCHIVES:
|
||
8 years ago
|
success = c.Tr("admin.dashboard.delete_repo_archives_success")
|
||
10 years ago
|
err = models.DeleteRepositoryArchives()
|
||
9 years ago
|
case CLEAN_MISSING_REPOS:
|
||
8 years ago
|
success = c.Tr("admin.dashboard.delete_missing_repos_success")
|
||
9 years ago
|
err = models.DeleteMissingRepositories()
|
||
10 years ago
|
case GIT_GC_REPOS:
|
||
8 years ago
|
success = c.Tr("admin.dashboard.git_gc_repos_success")
|
||
10 years ago
|
err = models.GitGcRepos()
|
||
10 years ago
|
case SYNC_SSH_AUTHORIZED_KEY:
|
||
8 years ago
|
success = c.Tr("admin.dashboard.resync_all_sshkeys_success")
|
||
10 years ago
|
err = models.RewriteAllPublicKeys()
|
||
8 years ago
|
case SYNC_REPOSITORY_HOOKS:
|
||
8 years ago
|
success = c.Tr("admin.dashboard.resync_all_hooks_success")
|
||
8 years ago
|
err = models.SyncRepositoryHooks()
|
||
9 years ago
|
case REINIT_MISSING_REPOSITORY:
|
||
8 years ago
|
success = c.Tr("admin.dashboard.reinit_missing_repos_success")
|
||
9 years ago
|
err = models.ReinitMissingRepositories()
|
||
11 years ago
|
}
|
||
|
|
||
|
if err != nil {
|
||
8 years ago
|
c.Flash.Error(err.Error())
|
||
11 years ago
|
} else {
|
||
8 years ago
|
c.Flash.Success(success)
|
||
11 years ago
|
}
|
||
8 years ago
|
c.Redirect(setting.AppSubURL + "/admin")
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
c.Data["Stats"] = models.GetStatistic()
|
||
10 years ago
|
// FIXME: update periodically
|
||
11 years ago
|
updateSystemStatus()
|
||
8 years ago
|
c.Data["SysStatus"] = sysStatus
|
||
|
c.HTML(200, DASHBOARD)
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
func SendTestMail(c *context.Context) {
|
||
|
email := c.Query("email")
|
||
9 years ago
|
// Send a test email to the user's email address and redirect back to Config
|
||
8 years ago
|
if err := mailer.SendTestMail(email); err != nil {
|
||
8 years ago
|
c.Flash.Error(c.Tr("admin.config.test_mail_failed", email, err))
|
||
9 years ago
|
} else {
|
||
8 years ago
|
c.Flash.Info(c.Tr("admin.config.test_mail_sent", email))
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
c.Redirect(setting.AppSubURL + "/admin/config")
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func Config(c *context.Context) {
|
||
|
c.Data["Title"] = c.Tr("admin.config")
|
||
|
c.Data["PageIsAdmin"] = true
|
||
|
c.Data["PageIsAdminConfig"] = true
|
||
11 years ago
|
|
||
8 years ago
|
c.Data["AppURL"] = setting.AppURL
|
||
|
c.Data["Domain"] = setting.Domain
|
||
|
c.Data["OfflineMode"] = setting.OfflineMode
|
||
|
c.Data["DisableRouterLog"] = setting.DisableRouterLog
|
||
|
c.Data["RunUser"] = setting.RunUser
|
||
|
c.Data["RunMode"] = strings.Title(macaron.Env)
|
||
|
c.Data["StaticRootPath"] = setting.StaticRootPath
|
||
|
c.Data["LogRootPath"] = setting.LogRootPath
|
||
|
c.Data["ReverseProxyAuthUser"] = setting.ReverseProxyAuthUser
|
||
11 years ago
|
|
||
8 years ago
|
c.Data["SSH"] = setting.SSH
|
||
9 years ago
|
|
||
8 years ago
|
c.Data["RepoRootPath"] = setting.RepoRootPath
|
||
|
c.Data["ScriptType"] = setting.ScriptType
|
||
|
c.Data["Repository"] = setting.Repository
|
||
8 years ago
|
|
||
8 years ago
|
c.Data["Service"] = setting.Service
|
||
|
c.Data["DbCfg"] = models.DbCfg
|
||
|
c.Data["Webhook"] = setting.Webhook
|
||
11 years ago
|
|
||
8 years ago
|
c.Data["MailerEnabled"] = false
|
||
11 years ago
|
if setting.MailService != nil {
|
||
8 years ago
|
c.Data["MailerEnabled"] = true
|
||
|
c.Data["Mailer"] = setting.MailService
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
8 years ago
|
c.Data["CacheAdapter"] = setting.CacheAdapter
|
||
|
c.Data["CacheInterval"] = setting.CacheInterval
|
||
|
c.Data["CacheConn"] = setting.CacheConn
|
||
11 years ago
|
|
||
8 years ago
|
c.Data["SessionConfig"] = setting.SessionConfig
|
||
11 years ago
|
|
||
8 years ago
|
c.Data["DisableGravatar"] = setting.DisableGravatar
|
||
|
c.Data["EnableFederatedAvatar"] = setting.EnableFederatedAvatar
|
||
11 years ago
|
|
||
8 years ago
|
c.Data["GitVersion"] = setting.Git.Version
|
||
|
c.Data["Git"] = setting.Git
|
||
9 years ago
|
|
||
11 years ago
|
type logger struct {
|
||
|
Mode, Config string
|
||
|
}
|
||
11 years ago
|
loggers := make([]*logger, len(setting.LogModes))
|
||
|
for i := range setting.LogModes {
|
||
8 years ago
|
loggers[i] = &logger{
|
||
|
Mode: strings.Title(setting.LogModes[i]),
|
||
|
}
|
||
|
|
||
8 years ago
|
result, _ := json.MarshalIndent(setting.LogConfigs[i], "", " ")
|
||
8 years ago
|
loggers[i].Config = string(result)
|
||
11 years ago
|
}
|
||
8 years ago
|
c.Data["Loggers"] = loggers
|
||
11 years ago
|
|
||
8 years ago
|
c.HTML(200, CONFIG)
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
8 years ago
|
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)
|
||
11 years ago
|
}
|