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.
393 lines
12 KiB
393 lines
12 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.
|
||
|
|
||
8 years ago
|
package routes
|
||
11 years ago
|
|
||
11 years ago
|
import (
|
||
8 years ago
|
"net/mail"
|
||
11 years ago
|
"os"
|
||
11 years ago
|
"os/exec"
|
||
10 years ago
|
"path/filepath"
|
||
11 years ago
|
"strings"
|
||
|
|
||
11 years ago
|
"github.com/Unknwon/com"
|
||
11 years ago
|
"github.com/go-xorm/xorm"
|
||
8 years ago
|
log "gopkg.in/clog.v1"
|
||
10 years ago
|
"gopkg.in/ini.v1"
|
||
9 years ago
|
"gopkg.in/macaron.v1"
|
||
11 years ago
|
|
||
9 years ago
|
"github.com/gogits/git-module"
|
||
9 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/form"
|
||
|
"github.com/gogits/gogs/pkg/mailer"
|
||
|
"github.com/gogits/gogs/pkg/markup"
|
||
|
"github.com/gogits/gogs/pkg/setting"
|
||
|
"github.com/gogits/gogs/pkg/ssh"
|
||
|
"github.com/gogits/gogs/pkg/template/highlight"
|
||
8 years ago
|
"github.com/gogits/gogs/pkg/tool"
|
||
8 years ago
|
"github.com/gogits/gogs/pkg/user"
|
||
11 years ago
|
)
|
||
|
|
||
11 years ago
|
const (
|
||
8 years ago
|
INSTALL = "install"
|
||
11 years ago
|
)
|
||
|
|
||
11 years ago
|
func checkRunMode() {
|
||
8 years ago
|
if setting.ProdMode {
|
||
11 years ago
|
macaron.Env = macaron.PROD
|
||
10 years ago
|
macaron.ColorLog = false
|
||
8 years ago
|
} else {
|
||
9 years ago
|
git.Debug = true
|
||
11 years ago
|
}
|
||
11 years ago
|
log.Info("Run Mode: %s", strings.Title(macaron.Env))
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
func NewServices() {
|
||
11 years ago
|
setting.NewServices()
|
||
9 years ago
|
mailer.NewContext()
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
// GlobalInit is for global configuration reload-able.
|
||
|
func GlobalInit() {
|
||
9 years ago
|
setting.NewContext()
|
||
11 years ago
|
log.Trace("Custom path: %s", setting.CustomPath)
|
||
11 years ago
|
log.Trace("Log path: %s", setting.LogRootPath)
|
||
9 years ago
|
models.LoadConfigs()
|
||
11 years ago
|
NewServices()
|
||
11 years ago
|
|
||
11 years ago
|
if setting.InstallLock {
|
||
8 years ago
|
highlight.NewContext()
|
||
8 years ago
|
markup.NewSanitizer()
|
||
11 years ago
|
if err := models.NewEngine(); err != nil {
|
||
8 years ago
|
log.Fatal(2, "Fail to initialize ORM engine: %v", err)
|
||
11 years ago
|
}
|
||
|
models.HasEngine = true
|
||
8 years ago
|
|
||
8 years ago
|
models.LoadRepoConfig()
|
||
|
models.NewRepoContext()
|
||
|
|
||
8 years ago
|
// Booting long running goroutines.
|
||
9 years ago
|
cron.NewContext()
|
||
8 years ago
|
models.InitSyncMirrors()
|
||
10 years ago
|
models.InitDeliverHooks()
|
||
9 years ago
|
models.InitTestPullRequests()
|
||
11 years ago
|
}
|
||
11 years ago
|
if models.EnableSQLite3 {
|
||
10 years ago
|
log.Info("SQLite3 Supported")
|
||
11 years ago
|
}
|
||
9 years ago
|
if setting.SupportMiniWinService {
|
||
|
log.Info("Builtin Windows Service Supported")
|
||
|
}
|
||
11 years ago
|
checkRunMode()
|
||
9 years ago
|
|
||
8 years ago
|
if setting.InstallLock && setting.SSH.StartBuiltinServer {
|
||
8 years ago
|
ssh.Listen(setting.SSH.ListenHost, setting.SSH.ListenPort, setting.SSH.ServerCiphers)
|
||
8 years ago
|
log.Info("SSH server started on %s:%v", setting.SSH.ListenHost, setting.SSH.ListenPort)
|
||
|
log.Trace("SSH server cipher list: %v", setting.SSH.ServerCiphers)
|
||
9 years ago
|
}
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
func InstallInit(c *context.Context) {
|
||
11 years ago
|
if setting.InstallLock {
|
||
8 years ago
|
c.NotFound()
|
||
11 years ago
|
return
|
||
|
}
|
||
11 years ago
|
|
||
8 years ago
|
c.Title("install.install")
|
||
|
c.PageIs("Install")
|
||
11 years ago
|
|
||
8 years ago
|
dbOpts := []string{"MySQL", "PostgreSQL", "MSSQL"}
|
||
9 years ago
|
if models.EnableSQLite3 {
|
||
|
dbOpts = append(dbOpts, "SQLite3")
|
||
|
}
|
||
8 years ago
|
c.Data["DbOptions"] = dbOpts
|
||
10 years ago
|
}
|
||
11 years ago
|
|
||
8 years ago
|
func Install(c *context.Context) {
|
||
8 years ago
|
f := form.Install{}
|
||
10 years ago
|
|
||
10 years ago
|
// Database settings
|
||
8 years ago
|
f.DbHost = models.DbCfg.Host
|
||
|
f.DbUser = models.DbCfg.User
|
||
|
f.DbName = models.DbCfg.Name
|
||
|
f.DbPath = models.DbCfg.Path
|
||
10 years ago
|
|
||
8 years ago
|
c.Data["CurDbOption"] = "MySQL"
|
||
9 years ago
|
switch models.DbCfg.Type {
|
||
|
case "postgres":
|
||
8 years ago
|
c.Data["CurDbOption"] = "PostgreSQL"
|
||
8 years ago
|
case "mssql":
|
||
8 years ago
|
c.Data["CurDbOption"] = "MSSQL"
|
||
9 years ago
|
case "sqlite3":
|
||
|
if models.EnableSQLite3 {
|
||
8 years ago
|
c.Data["CurDbOption"] = "SQLite3"
|
||
9 years ago
|
}
|
||
10 years ago
|
}
|
||
|
|
||
|
// Application general settings
|
||
8 years ago
|
f.AppName = setting.AppName
|
||
|
f.RepoRootPath = setting.RepoRootPath
|
||
10 years ago
|
|
||
|
// Note(unknwon): it's hard for Windows users change a running user,
|
||
|
// so just use current one if config says default.
|
||
|
if setting.IsWindows && setting.RunUser == "git" {
|
||
8 years ago
|
f.RunUser = user.CurrentUsername()
|
||
10 years ago
|
} else {
|
||
8 years ago
|
f.RunUser = setting.RunUser
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
8 years ago
|
f.Domain = setting.Domain
|
||
|
f.SSHPort = setting.SSH.Port
|
||
|
f.UseBuiltinSSHServer = setting.SSH.StartBuiltinServer
|
||
|
f.HTTPPort = setting.HTTPPort
|
||
8 years ago
|
f.AppUrl = setting.AppURL
|
||
8 years ago
|
f.LogRootPath = setting.LogRootPath
|
||
10 years ago
|
|
||
10 years ago
|
// E-mail service settings
|
||
|
if setting.MailService != nil {
|
||
8 years ago
|
f.SMTPHost = setting.MailService.Host
|
||
|
f.SMTPFrom = setting.MailService.From
|
||
|
f.SMTPUser = setting.MailService.User
|
||
11 years ago
|
}
|
||
8 years ago
|
f.RegisterConfirm = setting.Service.RegisterEmailConfirm
|
||
|
f.MailNotify = setting.Service.EnableNotifyMail
|
||
10 years ago
|
|
||
|
// Server and other services settings
|
||
8 years ago
|
f.OfflineMode = setting.OfflineMode
|
||
|
f.DisableGravatar = setting.DisableGravatar
|
||
|
f.EnableFederatedAvatar = setting.EnableFederatedAvatar
|
||
|
f.DisableRegistration = setting.Service.DisableRegistration
|
||
|
f.EnableCaptcha = setting.Service.EnableCaptcha
|
||
|
f.RequireSignInView = setting.Service.RequireSignInView
|
||
|
|
||
8 years ago
|
form.Assign(f, c.Data)
|
||
|
c.Success(INSTALL)
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
func InstallPost(c *context.Context, f form.Install) {
|
||
|
c.Data["CurDbOption"] = f.DbType
|
||
11 years ago
|
|
||
8 years ago
|
if c.HasError() {
|
||
|
if c.HasValue("Err_SMTPEmail") {
|
||
|
c.FormErr("SMTP")
|
||
10 years ago
|
}
|
||
8 years ago
|
if c.HasValue("Err_AdminName") ||
|
||
|
c.HasValue("Err_AdminPasswd") ||
|
||
|
c.HasValue("Err_AdminEmail") {
|
||
|
c.FormErr("Admin")
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
c.Success(INSTALL)
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
11 years ago
|
if _, err := exec.LookPath("git"); err != nil {
|
||
8 years ago
|
c.RenderWithErr(c.Tr("install.test_git_failed", err), INSTALL, &f)
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
11 years ago
|
// Pass basic check, now test configuration.
|
||
|
// Test database setting.
|
||
8 years ago
|
dbTypes := map[string]string{"MySQL": "mysql", "PostgreSQL": "postgres", "MSSQL": "mssql", "SQLite3": "sqlite3", "TiDB": "tidb"}
|
||
8 years ago
|
models.DbCfg.Type = dbTypes[f.DbType]
|
||
|
models.DbCfg.Host = f.DbHost
|
||
|
models.DbCfg.User = f.DbUser
|
||
|
models.DbCfg.Passwd = f.DbPasswd
|
||
|
models.DbCfg.Name = f.DbName
|
||
|
models.DbCfg.SSLMode = f.SSLMode
|
||
|
models.DbCfg.Path = f.DbPath
|
||
11 years ago
|
|
||
8 years ago
|
if models.DbCfg.Type == "sqlite3" && len(models.DbCfg.Path) == 0 {
|
||
8 years ago
|
c.FormErr("DbPath")
|
||
|
c.RenderWithErr(c.Tr("install.err_empty_db_path"), INSTALL, &f)
|
||
9 years ago
|
return
|
||
10 years ago
|
}
|
||
|
|
||
11 years ago
|
// Set test engine.
|
||
11 years ago
|
var x *xorm.Engine
|
||
|
if err := models.NewTestEngine(x); err != nil {
|
||
11 years ago
|
if strings.Contains(err.Error(), `Unknown database type: sqlite3`) {
|
||
8 years ago
|
c.FormErr("DbType")
|
||
|
c.RenderWithErr(c.Tr("install.sqlite3_not_available", "https://gogs.io/docs/installation/install_from_binary.html"), INSTALL, &f)
|
||
11 years ago
|
} else {
|
||
8 years ago
|
c.FormErr("DbSetting")
|
||
|
c.RenderWithErr(c.Tr("install.invalid_db_setting", err), INSTALL, &f)
|
||
11 years ago
|
}
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
|
// Test repository root path.
|
||
8 years ago
|
f.RepoRootPath = strings.Replace(f.RepoRootPath, "\\", "/", -1)
|
||
|
if err := os.MkdirAll(f.RepoRootPath, os.ModePerm); err != nil {
|
||
8 years ago
|
c.FormErr("RepoRootPath")
|
||
|
c.RenderWithErr(c.Tr("install.invalid_repo_path", err), INSTALL, &f)
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
9 years ago
|
// Test log root path.
|
||
8 years ago
|
f.LogRootPath = strings.Replace(f.LogRootPath, "\\", "/", -1)
|
||
|
if err := os.MkdirAll(f.LogRootPath, os.ModePerm); err != nil {
|
||
8 years ago
|
c.FormErr("LogRootPath")
|
||
|
c.RenderWithErr(c.Tr("install.invalid_log_root_path", err), INSTALL, &f)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
currentUser, match := setting.IsRunUserMatchCurrentUser(f.RunUser)
|
||
9 years ago
|
if !match {
|
||
8 years ago
|
c.FormErr("RunUser")
|
||
|
c.RenderWithErr(c.Tr("install.run_user_not_match", f.RunUser, currentUser), INSTALL, &f)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// Check host address and port
|
||
|
if len(f.SMTPHost) > 0 && !strings.Contains(f.SMTPHost, ":") {
|
||
|
c.FormErr("SMTP", "SMTPHost")
|
||
|
c.RenderWithErr(c.Tr("install.smtp_host_missing_port"), INSTALL, &f)
|
||
10 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
// Make sure FROM field is valid
|
||
8 years ago
|
if len(f.SMTPFrom) > 0 {
|
||
|
_, err := mail.ParseAddress(f.SMTPFrom)
|
||
8 years ago
|
if err != nil {
|
||
8 years ago
|
c.FormErr("SMTP", "SMTPFrom")
|
||
|
c.RenderWithErr(c.Tr("install.invalid_smtp_from", err), INSTALL, &f)
|
||
8 years ago
|
return
|
||
|
}
|
||
8 years ago
|
}
|
||
|
|
||
9 years ago
|
// Check logic loophole between disable self-registration and no admin account.
|
||
8 years ago
|
if f.DisableRegistration && len(f.AdminName) == 0 {
|
||
8 years ago
|
c.FormErr("Services", "Admin")
|
||
|
c.RenderWithErr(c.Tr("install.no_admin_and_disable_registration"), INSTALL, f)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
10 years ago
|
// Check admin password.
|
||
8 years ago
|
if len(f.AdminName) > 0 && len(f.AdminPasswd) == 0 {
|
||
8 years ago
|
c.FormErr("Admin", "AdminPasswd")
|
||
|
c.RenderWithErr(c.Tr("install.err_empty_admin_password"), INSTALL, f)
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
if f.AdminPasswd != f.AdminConfirmPasswd {
|
||
8 years ago
|
c.FormErr("Admin", "AdminPasswd")
|
||
|
c.RenderWithErr(c.Tr("form.password_not_match"), INSTALL, f)
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
if f.AppUrl[len(f.AppUrl)-1] != '/' {
|
||
|
f.AppUrl += "/"
|
||
10 years ago
|
}
|
||
|
|
||
11 years ago
|
// Save settings.
|
||
10 years ago
|
cfg := ini.Empty()
|
||
10 years ago
|
if com.IsFile(setting.CustomConf) {
|
||
|
// Keeps custom settings if there is already something.
|
||
|
if err := cfg.Append(setting.CustomConf); err != nil {
|
||
8 years ago
|
log.Error(2, "Fail to load custom conf '%s': %v", setting.CustomConf, err)
|
||
10 years ago
|
}
|
||
|
}
|
||
10 years ago
|
cfg.Section("database").Key("DB_TYPE").SetValue(models.DbCfg.Type)
|
||
|
cfg.Section("database").Key("HOST").SetValue(models.DbCfg.Host)
|
||
|
cfg.Section("database").Key("NAME").SetValue(models.DbCfg.Name)
|
||
|
cfg.Section("database").Key("USER").SetValue(models.DbCfg.User)
|
||
|
cfg.Section("database").Key("PASSWD").SetValue(models.DbCfg.Passwd)
|
||
|
cfg.Section("database").Key("SSL_MODE").SetValue(models.DbCfg.SSLMode)
|
||
|
cfg.Section("database").Key("PATH").SetValue(models.DbCfg.Path)
|
||
|
|
||
8 years ago
|
cfg.Section("").Key("APP_NAME").SetValue(f.AppName)
|
||
|
cfg.Section("repository").Key("ROOT").SetValue(f.RepoRootPath)
|
||
|
cfg.Section("").Key("RUN_USER").SetValue(f.RunUser)
|
||
|
cfg.Section("server").Key("DOMAIN").SetValue(f.Domain)
|
||
|
cfg.Section("server").Key("HTTP_PORT").SetValue(f.HTTPPort)
|
||
|
cfg.Section("server").Key("ROOT_URL").SetValue(f.AppUrl)
|
||
11 years ago
|
|
||
8 years ago
|
if f.SSHPort == 0 {
|
||
9 years ago
|
cfg.Section("server").Key("DISABLE_SSH").SetValue("true")
|
||
|
} else {
|
||
|
cfg.Section("server").Key("DISABLE_SSH").SetValue("false")
|
||
8 years ago
|
cfg.Section("server").Key("SSH_PORT").SetValue(com.ToStr(f.SSHPort))
|
||
|
cfg.Section("server").Key("START_SSH_SERVER").SetValue(com.ToStr(f.UseBuiltinSSHServer))
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
if len(strings.TrimSpace(f.SMTPHost)) > 0 {
|
||
10 years ago
|
cfg.Section("mailer").Key("ENABLED").SetValue("true")
|
||
8 years ago
|
cfg.Section("mailer").Key("HOST").SetValue(f.SMTPHost)
|
||
|
cfg.Section("mailer").Key("FROM").SetValue(f.SMTPFrom)
|
||
|
cfg.Section("mailer").Key("USER").SetValue(f.SMTPUser)
|
||
|
cfg.Section("mailer").Key("PASSWD").SetValue(f.SMTPPasswd)
|
||
10 years ago
|
} else {
|
||
|
cfg.Section("mailer").Key("ENABLED").SetValue("false")
|
||
11 years ago
|
}
|
||
8 years ago
|
cfg.Section("service").Key("REGISTER_EMAIL_CONFIRM").SetValue(com.ToStr(f.RegisterConfirm))
|
||
|
cfg.Section("service").Key("ENABLE_NOTIFY_MAIL").SetValue(com.ToStr(f.MailNotify))
|
||
10 years ago
|
|
||
8 years ago
|
cfg.Section("server").Key("OFFLINE_MODE").SetValue(com.ToStr(f.OfflineMode))
|
||
|
cfg.Section("picture").Key("DISABLE_GRAVATAR").SetValue(com.ToStr(f.DisableGravatar))
|
||
|
cfg.Section("picture").Key("ENABLE_FEDERATED_AVATAR").SetValue(com.ToStr(f.EnableFederatedAvatar))
|
||
|
cfg.Section("service").Key("DISABLE_REGISTRATION").SetValue(com.ToStr(f.DisableRegistration))
|
||
|
cfg.Section("service").Key("ENABLE_CAPTCHA").SetValue(com.ToStr(f.EnableCaptcha))
|
||
|
cfg.Section("service").Key("REQUIRE_SIGNIN_VIEW").SetValue(com.ToStr(f.RequireSignInView))
|
||
11 years ago
|
|
||
10 years ago
|
cfg.Section("").Key("RUN_MODE").SetValue("prod")
|
||
11 years ago
|
|
||
10 years ago
|
cfg.Section("session").Key("PROVIDER").SetValue("file")
|
||
10 years ago
|
|
||
8 years ago
|
mode := "file"
|
||
|
if f.EnableConsoleMode {
|
||
|
mode = "console, file"
|
||
|
}
|
||
|
cfg.Section("log").Key("MODE").SetValue(mode)
|
||
10 years ago
|
cfg.Section("log").Key("LEVEL").SetValue("Info")
|
||
8 years ago
|
cfg.Section("log").Key("ROOT_PATH").SetValue(f.LogRootPath)
|
||
10 years ago
|
|
||
10 years ago
|
cfg.Section("security").Key("INSTALL_LOCK").SetValue("true")
|
||
8 years ago
|
secretKey, err := tool.RandomString(15)
|
||
8 years ago
|
if err != nil {
|
||
8 years ago
|
c.RenderWithErr(c.Tr("install.secret_key_failed", err), INSTALL, &f)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
cfg.Section("security").Key("SECRET_KEY").SetValue(secretKey)
|
||
11 years ago
|
|
||
10 years ago
|
os.MkdirAll(filepath.Dir(setting.CustomConf), os.ModePerm)
|
||
|
if err := cfg.SaveTo(setting.CustomConf); err != nil {
|
||
8 years ago
|
c.RenderWithErr(c.Tr("install.save_config_failed", err), INSTALL, &f)
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
|
GlobalInit()
|
||
|
|
||
9 years ago
|
// Create admin account
|
||
8 years ago
|
if len(f.AdminName) > 0 {
|
||
9 years ago
|
u := &models.User{
|
||
8 years ago
|
Name: f.AdminName,
|
||
|
Email: f.AdminEmail,
|
||
|
Passwd: f.AdminPasswd,
|
||
10 years ago
|
IsAdmin: true,
|
||
|
IsActive: true,
|
||
9 years ago
|
}
|
||
|
if err := models.CreateUser(u); err != nil {
|
||
10 years ago
|
if !models.IsErrUserAlreadyExist(err) {
|
||
|
setting.InstallLock = false
|
||
8 years ago
|
c.FormErr("AdminName", "AdminEmail")
|
||
|
c.RenderWithErr(c.Tr("install.invalid_admin_setting", err), INSTALL, &f)
|
||
10 years ago
|
return
|
||
|
}
|
||
|
log.Info("Admin account already exist")
|
||
9 years ago
|
u, _ = models.GetUserByName(u.Name)
|
||
11 years ago
|
}
|
||
9 years ago
|
|
||
|
// Auto-login for admin
|
||
8 years ago
|
c.Session.Set("uid", u.ID)
|
||
|
c.Session.Set("uname", u.Name)
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
log.Info("First-time run install finished!")
|
||
8 years ago
|
c.Flash.Success(c.Tr("install.install_success"))
|
||
|
c.Redirect(f.AppUrl + "user/login")
|
||
11 years ago
|
}
|