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.

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