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.

209 lines
5.9 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  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. "os/exec"
  9. "strings"
  10. "github.com/Unknwon/goconfig"
  11. "github.com/go-martini/martini"
  12. "github.com/lunny/xorm"
  13. qlog "github.com/qiniu/log"
  14. "github.com/gogits/gogs/models"
  15. "github.com/gogits/gogs/modules/auth"
  16. "github.com/gogits/gogs/modules/base"
  17. "github.com/gogits/gogs/modules/log"
  18. "github.com/gogits/gogs/modules/mailer"
  19. "github.com/gogits/gogs/modules/middleware"
  20. )
  21. // Check run mode(Default of martini is Dev).
  22. func checkRunMode() {
  23. switch base.Cfg.MustValue("", "RUN_MODE") {
  24. case "prod":
  25. martini.Env = martini.Prod
  26. base.IsProdMode = true
  27. case "test":
  28. martini.Env = martini.Test
  29. }
  30. log.Info("Run Mode: %s", strings.Title(martini.Env))
  31. }
  32. // GlobalInit is for global configuration reload-able.
  33. func GlobalInit() {
  34. base.NewConfigContext()
  35. mailer.NewMailerContext()
  36. models.LoadModelsConfig()
  37. models.LoadRepoConfig()
  38. models.NewRepoContext()
  39. if base.InstallLock {
  40. if err := models.NewEngine(); err != nil {
  41. qlog.Fatal(err)
  42. }
  43. models.HasEngine = true
  44. }
  45. base.NewServices()
  46. checkRunMode()
  47. }
  48. func Install(ctx *middleware.Context, form auth.InstallForm) {
  49. if base.InstallLock {
  50. ctx.Handle(404, "install.Install", errors.New("Installation is prohibited"))
  51. return
  52. }
  53. ctx.Data["Title"] = "Install"
  54. ctx.Data["PageIsInstall"] = true
  55. // Get and assign value to install form.
  56. if len(form.Host) == 0 {
  57. form.Host = models.DbCfg.Host
  58. }
  59. if len(form.User) == 0 {
  60. form.User = models.DbCfg.User
  61. }
  62. if len(form.Passwd) == 0 {
  63. form.Passwd = models.DbCfg.Pwd
  64. }
  65. if len(form.DatabaseName) == 0 {
  66. form.DatabaseName = models.DbCfg.Name
  67. }
  68. if len(form.DatabasePath) == 0 {
  69. form.DatabasePath = models.DbCfg.Path
  70. }
  71. if len(form.RepoRootPath) == 0 {
  72. form.RepoRootPath = base.RepoRootPath
  73. }
  74. if len(form.RunUser) == 0 {
  75. form.RunUser = base.RunUser
  76. }
  77. if len(form.Domain) == 0 {
  78. form.Domain = base.Domain
  79. }
  80. if len(form.AppUrl) == 0 {
  81. form.AppUrl = base.AppUrl
  82. }
  83. auth.AssignForm(form, ctx.Data)
  84. ctx.HTML(200, "install")
  85. }
  86. func InstallPost(ctx *middleware.Context, form auth.InstallForm) {
  87. if base.InstallLock {
  88. ctx.Handle(404, "install.Install", errors.New("Installation is prohibited"))
  89. return
  90. }
  91. ctx.Data["Title"] = "Install"
  92. ctx.Data["PageIsInstall"] = true
  93. if ctx.HasError() {
  94. ctx.HTML(200, "install")
  95. return
  96. }
  97. if _, err := exec.LookPath("git"); err != nil {
  98. ctx.RenderWithErr("Fail to test 'git' command: "+err.Error(), "install", &form)
  99. return
  100. }
  101. // Pass basic check, now test configuration.
  102. // Test database setting.
  103. dbTypes := map[string]string{"mysql": "mysql", "pgsql": "postgres", "sqlite": "sqlite3"}
  104. models.DbCfg.Type = dbTypes[form.Database]
  105. models.DbCfg.Host = form.Host
  106. models.DbCfg.User = form.User
  107. models.DbCfg.Pwd = form.Passwd
  108. models.DbCfg.Name = form.DatabaseName
  109. models.DbCfg.SslMode = form.SslMode
  110. models.DbCfg.Path = form.DatabasePath
  111. // Set test engine.
  112. var x *xorm.Engine
  113. if err := models.NewTestEngine(x); err != nil {
  114. if strings.Contains(err.Error(), `Unknown database type: sqlite3`) {
  115. ctx.RenderWithErr("Your release version does not support SQLite3, please download the official binary version "+
  116. "from https://github.com/gogits/gogs/wiki/Install-from-binary, NOT the gobuild version.", "install", &form)
  117. } else {
  118. ctx.RenderWithErr("Database setting is not correct: "+err.Error(), "install", &form)
  119. }
  120. return
  121. }
  122. // Test repository root path.
  123. if err := os.MkdirAll(form.RepoRootPath, os.ModePerm); err != nil {
  124. ctx.RenderWithErr("Repository root path is invalid: "+err.Error(), "install", &form)
  125. return
  126. }
  127. // Check run user.
  128. curUser := os.Getenv("USERNAME")
  129. if len(curUser) == 0 {
  130. curUser = os.Getenv("USER")
  131. }
  132. // Does not check run user when the install lock is off.
  133. if form.RunUser != curUser {
  134. ctx.RenderWithErr("Run user isn't the current user: "+form.RunUser+" -> "+curUser, "install", &form)
  135. return
  136. }
  137. // Save settings.
  138. base.Cfg.SetValue("database", "DB_TYPE", models.DbCfg.Type)
  139. base.Cfg.SetValue("database", "HOST", models.DbCfg.Host)
  140. base.Cfg.SetValue("database", "NAME", models.DbCfg.Name)
  141. base.Cfg.SetValue("database", "USER", models.DbCfg.User)
  142. base.Cfg.SetValue("database", "PASSWD", models.DbCfg.Pwd)
  143. base.Cfg.SetValue("database", "SSL_MODE", models.DbCfg.SslMode)
  144. base.Cfg.SetValue("database", "PATH", models.DbCfg.Path)
  145. base.Cfg.SetValue("repository", "ROOT", form.RepoRootPath)
  146. base.Cfg.SetValue("", "RUN_USER", form.RunUser)
  147. base.Cfg.SetValue("server", "DOMAIN", form.Domain)
  148. base.Cfg.SetValue("server", "ROOT_URL", form.AppUrl)
  149. if len(strings.TrimSpace(form.SmtpHost)) > 0 {
  150. base.Cfg.SetValue("mailer", "ENABLED", "true")
  151. base.Cfg.SetValue("mailer", "HOST", form.SmtpHost)
  152. base.Cfg.SetValue("mailer", "USER", form.SmtpEmail)
  153. base.Cfg.SetValue("mailer", "PASSWD", form.SmtpPasswd)
  154. base.Cfg.SetValue("service", "REGISTER_EMAIL_CONFIRM", base.ToStr(form.RegisterConfirm == "on"))
  155. base.Cfg.SetValue("service", "ENABLE_NOTIFY_MAIL", base.ToStr(form.MailNotify == "on"))
  156. }
  157. base.Cfg.SetValue("", "RUN_MODE", "prod")
  158. base.Cfg.SetValue("security", "INSTALL_LOCK", "true")
  159. os.MkdirAll("custom/conf", os.ModePerm)
  160. if err := goconfig.SaveConfigFile(base.Cfg, "custom/conf/app.ini"); err != nil {
  161. ctx.RenderWithErr("Fail to save configuration: "+err.Error(), "install", &form)
  162. return
  163. }
  164. GlobalInit()
  165. // Create admin account.
  166. if _, err := models.RegisterUser(&models.User{Name: form.AdminName, Email: form.AdminEmail, Passwd: form.AdminPasswd,
  167. IsAdmin: true, IsActive: true}); err != nil {
  168. if err != models.ErrUserAlreadyExist {
  169. base.InstallLock = false
  170. ctx.RenderWithErr("Admin account setting is invalid: "+err.Error(), "install", &form)
  171. return
  172. }
  173. log.Info("Admin account already exist")
  174. }
  175. log.Info("First-time run install finished!")
  176. ctx.Flash.Success("Welcome! We're glad that you choose Gogs, have fun and take care.")
  177. ctx.Redirect("/user/login")
  178. }