install.go 6.9 KB

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