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.

install.go 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package routers
  5. import (
  6. "errors"
  7. "os"
  8. "strings"
  9. "github.com/Unknwon/goconfig"
  10. "github.com/codegangsta/martini"
  11. "github.com/gogits/gogs/models"
  12. "github.com/gogits/gogs/modules/auth"
  13. "github.com/gogits/gogs/modules/base"
  14. "github.com/gogits/gogs/modules/log"
  15. "github.com/gogits/gogs/modules/mailer"
  16. "github.com/gogits/gogs/modules/middleware"
  17. )
  18. // Check run mode(Default of martini is Dev).
  19. func checkRunMode() {
  20. switch base.Cfg.MustValue("", "RUN_MODE") {
  21. case "prod":
  22. martini.Env = martini.Prod
  23. case "test":
  24. martini.Env = martini.Test
  25. }
  26. log.Info("Run Mode: %s", strings.Title(martini.Env))
  27. }
  28. // GlobalInit is for global configuration reload-able.
  29. func GlobalInit() {
  30. base.NewConfigContext()
  31. mailer.NewMailerContext()
  32. models.LoadModelsConfig()
  33. models.LoadRepoConfig()
  34. models.NewRepoContext()
  35. if err := models.NewEngine(); err != nil && base.InstallLock {
  36. log.Error("%v", err)
  37. os.Exit(2)
  38. }
  39. base.NewServices()
  40. checkRunMode()
  41. }
  42. func Install(ctx *middleware.Context, form auth.InstallForm) {
  43. if base.InstallLock {
  44. ctx.Handle(404, "install.Install", errors.New("Installation is prohibited"))
  45. return
  46. }
  47. ctx.Data["Title"] = "Install"
  48. ctx.Data["PageIsInstall"] = true
  49. // Get and assign value to install form.
  50. if len(form.Host) == 0 {
  51. form.Host = models.DbCfg.Host
  52. }
  53. if len(form.User) == 0 {
  54. form.User = models.DbCfg.User
  55. }
  56. if len(form.Passwd) == 0 {
  57. form.Passwd = models.DbCfg.Pwd
  58. }
  59. if len(form.DatabaseName) == 0 {
  60. form.DatabaseName = models.DbCfg.Name
  61. }
  62. if len(form.DatabasePath) == 0 {
  63. form.DatabasePath = models.DbCfg.Path
  64. }
  65. if len(form.RepoRootPath) == 0 {
  66. form.RepoRootPath = base.RepoRootPath
  67. }
  68. if len(form.RunUser) == 0 {
  69. form.RunUser = base.RunUser
  70. }
  71. if len(form.Domain) == 0 {
  72. form.Domain = base.Domain
  73. }
  74. if len(form.AppUrl) == 0 {
  75. form.AppUrl = base.AppUrl
  76. }
  77. auth.AssignForm(form, ctx.Data)
  78. if ctx.Req.Method == "GET" {
  79. ctx.HTML(200, "install")
  80. return
  81. }
  82. if ctx.HasError() {
  83. ctx.HTML(200, "install")
  84. return
  85. }
  86. // Pass basic check, now test configuration.
  87. // Test database setting.
  88. dbTypes := map[string]string{"mysql": "mysql", "pgsql": "postgres", "sqlite": "sqlite3"}
  89. models.DbCfg.Type = dbTypes[form.Database]
  90. models.DbCfg.Host = form.Host
  91. models.DbCfg.User = form.User
  92. models.DbCfg.Pwd = form.Passwd
  93. models.DbCfg.Name = form.DatabaseName
  94. models.DbCfg.SslMode = form.SslMode
  95. models.DbCfg.Path = form.DatabasePath
  96. if err := models.NewEngine(); err != nil {
  97. ctx.RenderWithErr("Database setting is not correct: "+err.Error(), "install", &form)
  98. return
  99. }
  100. // Test repository root path.
  101. if err := os.MkdirAll(form.RepoRootPath, os.ModePerm); err != nil {
  102. ctx.RenderWithErr("Repository root path is invalid: "+err.Error(), "install", &form)
  103. return
  104. }
  105. // Create admin account.
  106. if _, err := models.RegisterUser(&models.User{Name: form.AdminName, Email: form.AdminEmail, Passwd: form.AdminPasswd,
  107. IsAdmin: true, IsActive: true}); err != nil {
  108. if err != models.ErrUserAlreadyExist {
  109. ctx.RenderWithErr("Admin account setting is invalid: "+err.Error(), "install", &form)
  110. return
  111. }
  112. }
  113. // Save settings.
  114. base.Cfg.SetValue("database", "DB_TYPE", models.DbCfg.Type)
  115. base.Cfg.SetValue("database", "HOST", models.DbCfg.Host)
  116. base.Cfg.SetValue("database", "NAME", models.DbCfg.Name)
  117. base.Cfg.SetValue("database", "USER", models.DbCfg.User)
  118. base.Cfg.SetValue("database", "PASSWD", models.DbCfg.Pwd)
  119. base.Cfg.SetValue("database", "SSL_MODE", models.DbCfg.SslMode)
  120. base.Cfg.SetValue("database", "PATH", models.DbCfg.Path)
  121. base.Cfg.SetValue("repository", "ROOT", form.RepoRootPath)
  122. base.Cfg.SetValue("", "RUN_USER", form.RunUser)
  123. base.Cfg.SetValue("server", "DOMAIN", form.Domain)
  124. base.Cfg.SetValue("server", "ROOT_URL", form.AppUrl)
  125. if len(form.Host) > 0 {
  126. base.Cfg.SetValue("mailer", "ENABLED", "true")
  127. base.Cfg.SetValue("mailer", "HOST", form.SmtpHost)
  128. base.Cfg.SetValue("mailer", "USER", form.SmtpEmail)
  129. base.Cfg.SetValue("mailer", "PASSWD", form.SmtpPasswd)
  130. base.Cfg.SetValue("service", "REGISTER_EMAIL_CONFIRM", base.ToStr(form.RegisterConfirm == "on"))
  131. base.Cfg.SetValue("service", "ENABLE_NOTIFY_MAIL", base.ToStr(form.MailNotify == "on"))
  132. }
  133. base.Cfg.SetValue("security", "INSTALL_LOCK", "true")
  134. if err := goconfig.SaveConfigFile(base.Cfg, "custom/conf/app.ini"); err != nil {
  135. ctx.RenderWithErr("Fail to save configuration: "+err.Error(), "install", &form)
  136. return
  137. }
  138. GlobalInit()
  139. log.Info("First-time run install finished!")
  140. ctx.Redirect("/user/login")
  141. }