install.go 6.5 KB

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