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.
gitea-fork-majority-judgment/routers/install.go

246 lines
6.9 KiB

// 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 routers
import (
"errors"
"os"
10 years ago
"os/exec"
10 years ago
"path"
"strings"
"github.com/Unknwon/com"
"github.com/Unknwon/goconfig"
"github.com/Unknwon/macaron"
10 years ago
"github.com/go-xorm/xorm"
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/auth"
"github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/cron"
"github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/mailer"
"github.com/gogits/gogs/modules/middleware"
10 years ago
"github.com/gogits/gogs/modules/setting"
10 years ago
"github.com/gogits/gogs/modules/social"
)
const (
INSTALL base.TplName = "install"
)
func checkRunMode() {
10 years ago
switch setting.Cfg.MustValue("", "RUN_MODE") {
case "prod":
macaron.Env = macaron.PROD
10 years ago
setting.ProdMode = true
case "test":
macaron.Env = macaron.TEST
}
log.Info("Run Mode: %s", strings.Title(macaron.Env))
}
10 years ago
func NewServices() {
10 years ago
setting.NewServices()
10 years ago
social.NewOauthService()
}
// GlobalInit is for global configuration reload-able.
func GlobalInit() {
10 years ago
setting.NewConfigContext()
10 years ago
log.Trace("Custom path: %s", setting.CustomPath)
10 years ago
log.Trace("Log path: %s", setting.LogRootPath)
mailer.NewMailerContext()
models.LoadModelsConfig()
NewServices()
10 years ago
10 years ago
if setting.InstallLock {
models.LoadRepoConfig()
models.NewRepoContext()
10 years ago
if err := models.NewEngine(); err != nil {
log.Fatal(4, "Fail to initialize ORM engine: %v", err)
10 years ago
}
models.HasEngine = true
cron.NewCronContext()
10 years ago
log.NewGitLogger(path.Join(setting.LogRootPath, "http.log"))
}
if models.EnableSQLite3 {
log.Info("SQLite3 Enabled")
}
checkRunMode()
}
10 years ago
func renderDbOption(ctx *middleware.Context) {
ctx.Data["DbOptions"] = []string{"MySQL", "PostgreSQL", "SQLite3"}
}
// @router /install [get]
10 years ago
func Install(ctx *middleware.Context, form auth.InstallForm) {
10 years ago
if setting.InstallLock {
ctx.Handle(404, "install.Install", errors.New("Installation is prohibited"))
return
}
ctx.Data["Title"] = "Install"
ctx.Data["PageIsInstall"] = true
10 years ago
// Get and assign values to install form.
if len(form.Host) == 0 {
form.Host = models.DbCfg.Host
}
if len(form.User) == 0 {
form.User = models.DbCfg.User
}
if len(form.Passwd) == 0 {
form.Passwd = models.DbCfg.Pwd
}
if len(form.DatabaseName) == 0 {
form.DatabaseName = models.DbCfg.Name
}
if len(form.DatabasePath) == 0 {
form.DatabasePath = models.DbCfg.Path
}
if len(form.RepoRootPath) == 0 {
10 years ago
form.RepoRootPath = setting.RepoRootPath
}
if len(form.RunUser) == 0 {
10 years ago
form.RunUser = setting.RunUser
}
if len(form.Domain) == 0 {
10 years ago
form.Domain = setting.Domain
}
if len(form.AppUrl) == 0 {
10 years ago
form.AppUrl = setting.AppUrl
}
10 years ago
renderDbOption(ctx)
10 years ago
curDbOp := ""
10 years ago
if models.EnableSQLite3 {
10 years ago
curDbOp = "SQLite3" // Default when enabled.
10 years ago
}
10 years ago
ctx.Data["CurDbOption"] = curDbOp
10 years ago
auth.AssignForm(form, ctx.Data)
ctx.HTML(200, INSTALL)
}
10 years ago
func InstallPost(ctx *middleware.Context, form auth.InstallForm) {
10 years ago
if setting.InstallLock {
ctx.Handle(404, "install.InstallPost", errors.New("Installation is prohibited"))
return
}
ctx.Data["Title"] = "Install"
ctx.Data["PageIsInstall"] = true
10 years ago
renderDbOption(ctx)
10 years ago
ctx.Data["CurDbOption"] = form.Database
10 years ago
if ctx.HasError() {
ctx.HTML(200, INSTALL)
return
}
10 years ago
if _, err := exec.LookPath("git"); err != nil {
ctx.RenderWithErr("Fail to test 'git' command: "+err.Error(), INSTALL, &form)
10 years ago
return
}
// Pass basic check, now test configuration.
// Test database setting.
10 years ago
dbTypes := map[string]string{"MySQL": "mysql", "PostgreSQL": "postgres", "SQLite3": "sqlite3"}
models.DbCfg.Type = dbTypes[form.Database]
models.DbCfg.Host = form.Host
models.DbCfg.User = form.User
models.DbCfg.Pwd = form.Passwd
models.DbCfg.Name = form.DatabaseName
models.DbCfg.SslMode = form.SslMode
models.DbCfg.Path = form.DatabasePath
10 years ago
// Set test engine.
10 years ago
var x *xorm.Engine
if err := models.NewTestEngine(x); err != nil {
// NOTE: should use core.QueryDriver (github.com/go-xorm/core)
10 years ago
if strings.Contains(err.Error(), `Unknown database type: sqlite3`) {
10 years ago
ctx.RenderWithErr("Your release version does not support SQLite3, please download the official binary version "+
"from http://gogs.io/docs/installation/install_from_binary.md, NOT the gobuild version.", INSTALL, &form)
10 years ago
} else {
ctx.RenderWithErr("Database setting is not correct: "+err.Error(), INSTALL, &form)
10 years ago
}
return
}
// Test repository root path.
if err := os.MkdirAll(form.RepoRootPath, os.ModePerm); err != nil {
ctx.RenderWithErr("Repository root path is invalid: "+err.Error(), INSTALL, &form)
return
}
10 years ago
// Check run user.
10 years ago
curUser := os.Getenv("USER")
10 years ago
if len(curUser) == 0 {
10 years ago
curUser = os.Getenv("USERNAME")
10 years ago
}
// Does not check run user when the install lock is off.
if form.RunUser != curUser {
ctx.RenderWithErr("Run user isn't the current user: "+form.RunUser+" -> "+curUser, INSTALL, &form)
10 years ago
return
}
// Save settings.
10 years ago
setting.Cfg.SetValue("database", "DB_TYPE", models.DbCfg.Type)
setting.Cfg.SetValue("database", "HOST", models.DbCfg.Host)
setting.Cfg.SetValue("database", "NAME", models.DbCfg.Name)
setting.Cfg.SetValue("database", "USER", models.DbCfg.User)
setting.Cfg.SetValue("database", "PASSWD", models.DbCfg.Pwd)
setting.Cfg.SetValue("database", "SSL_MODE", models.DbCfg.SslMode)
setting.Cfg.SetValue("database", "PATH", models.DbCfg.Path)
setting.Cfg.SetValue("repository", "ROOT", form.RepoRootPath)
setting.Cfg.SetValue("", "RUN_USER", form.RunUser)
setting.Cfg.SetValue("server", "DOMAIN", form.Domain)
setting.Cfg.SetValue("server", "ROOT_URL", form.AppUrl)
10 years ago
if len(strings.TrimSpace(form.SmtpHost)) > 0 {
10 years ago
setting.Cfg.SetValue("mailer", "ENABLED", "true")
setting.Cfg.SetValue("mailer", "HOST", form.SmtpHost)
setting.Cfg.SetValue("mailer", "USER", form.SmtpEmail)
setting.Cfg.SetValue("mailer", "PASSWD", form.SmtpPasswd)
setting.Cfg.SetValue("service", "REGISTER_EMAIL_CONFIRM", com.ToStr(form.RegisterConfirm == "on"))
setting.Cfg.SetValue("service", "ENABLE_NOTIFY_MAIL", com.ToStr(form.MailNotify == "on"))
}
10 years ago
setting.Cfg.SetValue("", "RUN_MODE", "prod")
10 years ago
10 years ago
setting.Cfg.SetValue("security", "INSTALL_LOCK", "true")
10 years ago
os.MkdirAll("custom/conf", os.ModePerm)
10 years ago
if err := goconfig.SaveConfigFile(setting.Cfg, path.Join(setting.CustomPath, "conf/app.ini")); err != nil {
ctx.RenderWithErr("Fail to save configuration: "+err.Error(), INSTALL, &form)
return
}
GlobalInit()
10 years ago
// Create admin account.
if err := models.CreateUser(&models.User{Name: form.AdminName, Email: form.AdminEmail, Passwd: form.AdminPasswd,
10 years ago
IsAdmin: true, IsActive: true}); err != nil {
if err != models.ErrUserAlreadyExist {
10 years ago
setting.InstallLock = false
ctx.RenderWithErr("Admin account setting is invalid: "+err.Error(), INSTALL, &form)
10 years ago
return
}
log.Info("Admin account already exist")
10 years ago
}
log.Info("First-time run install finished!")
ctx.Flash.Success("Welcome! We're glad that you choose Gogs, have fun and take care.")
ctx.Redirect("/user/login")
}