install.go 5.4 KB

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