install.go 6.8 KB

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