You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

245 lines
6.9 KiB

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