install.go 6.1 KB

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