install.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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.IsProdMode = 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 Install(ctx *middleware.Context, form auth.InstallForm) {
  59. if base.InstallLock {
  60. ctx.Handle(404, "install.Install", errors.New("Installation is prohibited"))
  61. return
  62. }
  63. ctx.Data["Title"] = "Install"
  64. ctx.Data["PageIsInstall"] = true
  65. // Get and assign value to install form.
  66. if len(form.Host) == 0 {
  67. form.Host = models.DbCfg.Host
  68. }
  69. if len(form.User) == 0 {
  70. form.User = models.DbCfg.User
  71. }
  72. if len(form.Passwd) == 0 {
  73. form.Passwd = models.DbCfg.Pwd
  74. }
  75. if len(form.DatabaseName) == 0 {
  76. form.DatabaseName = models.DbCfg.Name
  77. }
  78. if len(form.DatabasePath) == 0 {
  79. form.DatabasePath = models.DbCfg.Path
  80. }
  81. if len(form.RepoRootPath) == 0 {
  82. form.RepoRootPath = base.RepoRootPath
  83. }
  84. if len(form.RunUser) == 0 {
  85. form.RunUser = base.RunUser
  86. }
  87. if len(form.Domain) == 0 {
  88. form.Domain = base.Domain
  89. }
  90. if len(form.AppUrl) == 0 {
  91. form.AppUrl = base.AppUrl
  92. }
  93. auth.AssignForm(form, ctx.Data)
  94. ctx.HTML(200, "install")
  95. }
  96. func InstallPost(ctx *middleware.Context, form auth.InstallForm) {
  97. if base.InstallLock {
  98. ctx.Handle(404, "install.Install", errors.New("Installation is prohibited"))
  99. return
  100. }
  101. ctx.Data["Title"] = "Install"
  102. ctx.Data["PageIsInstall"] = true
  103. if ctx.HasError() {
  104. ctx.HTML(200, "install")
  105. return
  106. }
  107. if _, err := exec.LookPath("git"); err != nil {
  108. ctx.RenderWithErr("Fail to test 'git' command: "+err.Error(), "install", &form)
  109. return
  110. }
  111. // Pass basic check, now test configuration.
  112. // Test database setting.
  113. dbTypes := map[string]string{"mysql": "mysql", "pgsql": "postgres", "sqlite": "sqlite3"}
  114. models.DbCfg.Type = dbTypes[form.Database]
  115. models.DbCfg.Host = form.Host
  116. models.DbCfg.User = form.User
  117. models.DbCfg.Pwd = form.Passwd
  118. models.DbCfg.Name = form.DatabaseName
  119. models.DbCfg.SslMode = form.SslMode
  120. models.DbCfg.Path = form.DatabasePath
  121. // Set test engine.
  122. var x *xorm.Engine
  123. if err := models.NewTestEngine(x); err != nil {
  124. if strings.Contains(err.Error(), `Unknown database type: sqlite3`) {
  125. ctx.RenderWithErr("Your release version does not support SQLite3, please download the official binary version "+
  126. "from https://github.com/gogits/gogs/wiki/Install-from-binary, NOT the gobuild version.", "install", &form)
  127. } else {
  128. ctx.RenderWithErr("Database setting is not correct: "+err.Error(), "install", &form)
  129. }
  130. return
  131. }
  132. // Test repository root path.
  133. if err := os.MkdirAll(form.RepoRootPath, os.ModePerm); err != nil {
  134. ctx.RenderWithErr("Repository root path is invalid: "+err.Error(), "install", &form)
  135. return
  136. }
  137. // Check run user.
  138. curUser := os.Getenv("USER")
  139. if len(curUser) == 0 {
  140. curUser = os.Getenv("USERNAME")
  141. }
  142. // Does not check run user when the install lock is off.
  143. if form.RunUser != curUser {
  144. ctx.RenderWithErr("Run user isn't the current user: "+form.RunUser+" -> "+curUser, "install", &form)
  145. return
  146. }
  147. // Save settings.
  148. base.Cfg.SetValue("database", "DB_TYPE", models.DbCfg.Type)
  149. base.Cfg.SetValue("database", "HOST", models.DbCfg.Host)
  150. base.Cfg.SetValue("database", "NAME", models.DbCfg.Name)
  151. base.Cfg.SetValue("database", "USER", models.DbCfg.User)
  152. base.Cfg.SetValue("database", "PASSWD", models.DbCfg.Pwd)
  153. base.Cfg.SetValue("database", "SSL_MODE", models.DbCfg.SslMode)
  154. base.Cfg.SetValue("database", "PATH", models.DbCfg.Path)
  155. base.Cfg.SetValue("repository", "ROOT", form.RepoRootPath)
  156. base.Cfg.SetValue("", "RUN_USER", form.RunUser)
  157. base.Cfg.SetValue("server", "DOMAIN", form.Domain)
  158. base.Cfg.SetValue("server", "ROOT_URL", form.AppUrl)
  159. if len(strings.TrimSpace(form.SmtpHost)) > 0 {
  160. base.Cfg.SetValue("mailer", "ENABLED", "true")
  161. base.Cfg.SetValue("mailer", "HOST", form.SmtpHost)
  162. base.Cfg.SetValue("mailer", "USER", form.SmtpEmail)
  163. base.Cfg.SetValue("mailer", "PASSWD", form.SmtpPasswd)
  164. base.Cfg.SetValue("service", "REGISTER_EMAIL_CONFIRM", base.ToStr(form.RegisterConfirm == "on"))
  165. base.Cfg.SetValue("service", "ENABLE_NOTIFY_MAIL", base.ToStr(form.MailNotify == "on"))
  166. }
  167. base.Cfg.SetValue("", "RUN_MODE", "prod")
  168. base.Cfg.SetValue("security", "INSTALL_LOCK", "true")
  169. os.MkdirAll("custom/conf", os.ModePerm)
  170. if err := goconfig.SaveConfigFile(base.Cfg, "custom/conf/app.ini"); err != nil {
  171. ctx.RenderWithErr("Fail to save configuration: "+err.Error(), "install", &form)
  172. return
  173. }
  174. GlobalInit()
  175. // Create admin account.
  176. if _, err := models.RegisterUser(&models.User{Name: form.AdminName, Email: form.AdminEmail, Passwd: form.AdminPasswd,
  177. IsAdmin: true, IsActive: true}); err != nil {
  178. if err != models.ErrUserAlreadyExist {
  179. base.InstallLock = false
  180. ctx.RenderWithErr("Admin account setting is invalid: "+err.Error(), "install", &form)
  181. return
  182. }
  183. log.Info("Admin account already exist")
  184. }
  185. log.Info("First-time run install finished!")
  186. ctx.Flash.Success("Welcome! We're glad that you choose Gogs, have fun and take care.")
  187. ctx.Redirect("/user/login")
  188. }