1
1
mirror of https://github.com/go-gitea/gitea synced 2025-08-03 08:08:36 +00:00

Move install pages out of main macaron routes (#13195)

* Move install pages out of main macaron loop

Signed-off-by: Andrew Thornton <art27@cantab.net>

* Update templates/post-install.tmpl

Co-authored-by: Lauris BH <lauris@nix.lv>

* remove prefetch

Signed-off-by: Andrew Thornton <art27@cantab.net>
This commit is contained in:
zeripath
2020-10-19 22:03:08 +01:00
committed by GitHub
parent 3ddf3f93d6
commit 2f1353a2f3
10 changed files with 234 additions and 115 deletions

View File

@@ -117,9 +117,46 @@ func InitLocales() {
})
}
// PreInstallInit preloads the configuration to check if we need to run install
func PreInstallInit(ctx context.Context) bool {
setting.NewContext()
if !setting.InstallLock {
log.Trace("AppPath: %s", setting.AppPath)
log.Trace("AppWorkPath: %s", setting.AppWorkPath)
log.Trace("Custom path: %s", setting.CustomPath)
log.Trace("Log path: %s", setting.LogRootPath)
log.Trace("Preparing to run install page")
InitLocales()
if setting.EnableSQLite3 {
log.Info("SQLite3 Supported")
}
setting.InitDBConfig()
svg.Init()
}
return !setting.InstallLock
}
// PostInstallInit rereads the settings and starts up the database
func PostInstallInit(ctx context.Context) {
setting.NewContext()
setting.InitDBConfig()
if setting.InstallLock {
if err := initDBEngine(ctx); err == nil {
log.Info("ORM engine initialization successful!")
} else {
log.Fatal("ORM engine initialization failed: %v", err)
}
svg.Init()
}
}
// GlobalInit is for global configuration reload-able.
func GlobalInit(ctx context.Context) {
setting.NewContext()
if !setting.InstallLock {
log.Fatal("Gitea is not installed")
}
if err := git.Init(ctx); err != nil {
log.Fatal("Git module init failed: %v", err)
}
@@ -134,59 +171,50 @@ func GlobalInit(ctx context.Context) {
NewServices()
if setting.InstallLock {
highlight.NewContext()
external.RegisterParsers()
markup.Init()
if err := initDBEngine(ctx); err == nil {
log.Info("ORM engine initialization successful!")
} else {
log.Fatal("ORM engine initialization failed: %v", err)
}
if err := models.InitOAuth2(); err != nil {
log.Fatal("Failed to initialize OAuth2 support: %v", err)
}
models.NewRepoContext()
// Booting long running goroutines.
cron.NewContext()
issue_indexer.InitIssueIndexer(false)
code_indexer.Init()
if err := stats_indexer.Init(); err != nil {
log.Fatal("Failed to initialize repository stats indexer queue: %v", err)
}
mirror_service.InitSyncMirrors()
webhook.InitDeliverHooks()
if err := pull_service.Init(); err != nil {
log.Fatal("Failed to initialize test pull requests queue: %v", err)
}
if err := task.Init(); err != nil {
log.Fatal("Failed to initialize task scheduler: %v", err)
}
eventsource.GetManager().Init()
highlight.NewContext()
external.RegisterParsers()
markup.Init()
if err := initDBEngine(ctx); err == nil {
log.Info("ORM engine initialization successful!")
} else {
log.Fatal("ORM engine initialization failed: %v", err)
}
if err := models.InitOAuth2(); err != nil {
log.Fatal("Failed to initialize OAuth2 support: %v", err)
}
models.NewRepoContext()
// Booting long running goroutines.
cron.NewContext()
issue_indexer.InitIssueIndexer(false)
code_indexer.Init()
if err := stats_indexer.Init(); err != nil {
log.Fatal("Failed to initialize repository stats indexer queue: %v", err)
}
mirror_service.InitSyncMirrors()
webhook.InitDeliverHooks()
if err := pull_service.Init(); err != nil {
log.Fatal("Failed to initialize test pull requests queue: %v", err)
}
if err := task.Init(); err != nil {
log.Fatal("Failed to initialize task scheduler: %v", err)
}
eventsource.GetManager().Init()
if setting.EnableSQLite3 {
log.Info("SQLite3 Supported")
}
checkRunMode()
// Now because Install will re-run GlobalInit once it has set InstallLock
// we can't tell if the ssh port will remain unused until that's done.
// However, see FIXME comment in install.go
if setting.InstallLock {
if setting.SSH.StartBuiltinServer {
ssh.Listen(setting.SSH.ListenHost, setting.SSH.ListenPort, setting.SSH.ServerCiphers, setting.SSH.ServerKeyExchanges, setting.SSH.ServerMACs)
log.Info("SSH server started on %s:%d. Cipher list (%v), key exchange algorithms (%v), MACs (%v)", setting.SSH.ListenHost, setting.SSH.ListenPort, setting.SSH.ServerCiphers, setting.SSH.ServerKeyExchanges, setting.SSH.ServerMACs)
} else {
ssh.Unused()
}
}
if setting.InstallLock {
sso.Init()
if setting.SSH.StartBuiltinServer {
ssh.Listen(setting.SSH.ListenHost, setting.SSH.ListenPort, setting.SSH.ServerCiphers, setting.SSH.ServerKeyExchanges, setting.SSH.ServerMACs)
log.Info("SSH server started on %s:%d. Cipher list (%v), key exchange algorithms (%v), MACs (%v)", setting.SSH.ListenHost, setting.SSH.ListenPort, setting.SSH.ServerCiphers, setting.SSH.ServerKeyExchanges, setting.SSH.ServerMACs)
} else {
ssh.Unused()
}
sso.Init()
svg.Init()
}

View File

@@ -5,7 +5,7 @@
package routers
import (
"errors"
"net/http"
"os"
"os/exec"
"path/filepath"
@@ -27,13 +27,15 @@ import (
const (
// tplInstall template for installation page
tplInstall base.TplName = "install"
tplInstall base.TplName = "install"
tplPostInstall base.TplName = "post-install"
)
// InstallInit prepare for rendering installation page
func InstallInit(ctx *context.Context) {
if setting.InstallLock {
ctx.NotFound("Install", errors.New("Installation is prohibited"))
ctx.Header().Add("Refresh", "1; url="+setting.AppURL+"user/login")
ctx.HTML(200, tplPostInstall)
return
}
@@ -357,7 +359,8 @@ func InstallPost(ctx *context.Context, form auth.InstallForm) {
return
}
GlobalInit(graceful.GetManager().HammerContext())
// Re-read settings
PostInstallInit(ctx.Req.Context())
// Create admin account
if len(form.AdminName) > 0 {
@@ -380,6 +383,11 @@ func InstallPost(ctx *context.Context, form auth.InstallForm) {
u, _ = models.GetUserByName(u.Name)
}
days := 86400 * setting.LogInRememberDays
ctx.SetCookie(setting.CookieUserName, u.Name, days, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true)
ctx.SetSuperSecureCookie(base.EncodeMD5(u.Rands+u.Passwd),
setting.CookieRememberName, u.Name, days, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true)
// Auto-login for admin
if err = ctx.Session.Set("uid", u.ID); err != nil {
ctx.RenderWithErr(ctx.Tr("install.save_config_failed", err), tplInstall, &form)
@@ -397,12 +405,18 @@ func InstallPost(ctx *context.Context, form auth.InstallForm) {
}
log.Info("First-time run install finished!")
// FIXME: This isn't really enough to completely take account of new configuration
// We should really be restarting:
// - On windows this is probably just a simple restart
// - On linux we can't just use graceful.RestartProcess() everything that was passed in on LISTEN_FDS
// (active or not) needs to be passed out and everything new passed out too.
// This means we need to prevent the cleanup goroutine from running prior to the second GlobalInit
ctx.Flash.Success(ctx.Tr("install.install_success"))
ctx.Redirect(form.AppURL + "user/login")
ctx.Header().Add("Refresh", "1; url="+setting.AppURL+"user/login")
ctx.HTML(200, tplPostInstall)
// Now get the http.Server from this request and shut it down
// NB: This is not our hammerable graceful shutdown this is http.Server.Shutdown
srv := ctx.Req.Context().Value(http.ServerContextKey).(*http.Server)
go func() {
if err := srv.Shutdown(graceful.GetManager().HammerContext()); err != nil {
log.Error("Unable to shutdown the install server! Error: %v", err)
}
}()
}

View File

@@ -301,6 +301,15 @@ func NewMacaron() *macaron.Macaron {
return m
}
// RegisterInstallRoute registers the install routes
func RegisterInstallRoute(m *macaron.Macaron) {
m.Combo("/", routers.InstallInit).Get(routers.Install).
Post(binding.BindIgnErr(auth.InstallForm{}), routers.InstallPost)
m.NotFound(func(ctx *context.Context) {
ctx.Redirect(setting.AppURL, 302)
})
}
// RegisterRoutes routes routes to Macaron
func RegisterRoutes(m *macaron.Macaron) {
reqSignIn := context.Toggle(&context.ToggleOptions{SignInRequired: true})