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.

257 lines
7.1 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
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", errors.New("Installation is prohibited"))
  72. return
  73. }
  74. ctx.Data["Title"] = ctx.Tr("install.install")
  75. ctx.Data["PageIsInstall"] = true
  76. // Get and assign values to install form.
  77. if len(form.DbHost) == 0 {
  78. form.DbHost = models.DbCfg.Host
  79. }
  80. if len(form.DbUser) == 0 {
  81. form.DbUser = models.DbCfg.User
  82. }
  83. if len(form.DbPasswd) == 0 {
  84. form.DbPasswd = 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, "InstallPost", errors.New("Installation is prohibited"))
  116. return
  117. }
  118. ctx.Data["Title"] = ctx.Tr("install.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(ctx.Tr("install.test_git_failed", err), 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.DbHost
  135. models.DbCfg.User = form.DbUser
  136. models.DbCfg.Pwd = form.DbPasswd
  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(ctx.Tr("install.sqlite3_not_available"), INSTALL, &form)
  146. } else {
  147. ctx.RenderWithErr(ctx.Tr("install.invalid_db_setting", err), INSTALL, &form)
  148. }
  149. return
  150. }
  151. // Test repository root path.
  152. if err := os.MkdirAll(form.RepoRootPath, os.ModePerm); err != nil {
  153. ctx.Data["Err_RepoRootPath"] = true
  154. ctx.RenderWithErr(ctx.Tr("install.invalid_repo_path", err), 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.Data["Err_RunUser"] = true
  165. ctx.RenderWithErr(ctx.Tr("install.run_user_not_match", form.RunUser, curUser), INSTALL, &form)
  166. return
  167. }
  168. // Check admin password.
  169. if form.AdminPasswd != form.ConfirmPasswd {
  170. ctx.Data["Err_AdminPasswd"] = true
  171. ctx.RenderWithErr(ctx.Tr("form.password_not_match"), INSTALL, form)
  172. return
  173. }
  174. // Save settings.
  175. setting.Cfg.SetValue("database", "DB_TYPE", models.DbCfg.Type)
  176. setting.Cfg.SetValue("database", "HOST", models.DbCfg.Host)
  177. setting.Cfg.SetValue("database", "NAME", models.DbCfg.Name)
  178. setting.Cfg.SetValue("database", "USER", models.DbCfg.User)
  179. setting.Cfg.SetValue("database", "PASSWD", models.DbCfg.Pwd)
  180. setting.Cfg.SetValue("database", "SSL_MODE", models.DbCfg.SslMode)
  181. setting.Cfg.SetValue("database", "PATH", models.DbCfg.Path)
  182. setting.Cfg.SetValue("repository", "ROOT", form.RepoRootPath)
  183. setting.Cfg.SetValue("", "RUN_USER", form.RunUser)
  184. setting.Cfg.SetValue("server", "DOMAIN", form.Domain)
  185. setting.Cfg.SetValue("server", "ROOT_URL", form.AppUrl)
  186. if len(strings.TrimSpace(form.SmtpHost)) > 0 {
  187. setting.Cfg.SetValue("mailer", "ENABLED", "true")
  188. setting.Cfg.SetValue("mailer", "HOST", form.SmtpHost)
  189. setting.Cfg.SetValue("mailer", "USER", form.SmtpEmail)
  190. setting.Cfg.SetValue("mailer", "PASSWD", form.SmtpPasswd)
  191. setting.Cfg.SetValue("service", "REGISTER_EMAIL_CONFIRM", com.ToStr(form.RegisterConfirm == "on"))
  192. setting.Cfg.SetValue("service", "ENABLE_NOTIFY_MAIL", com.ToStr(form.MailNotify == "on"))
  193. }
  194. setting.Cfg.SetValue("", "RUN_MODE", "prod")
  195. setting.Cfg.SetValue("log", "MODE", "file")
  196. setting.Cfg.SetValue("security", "INSTALL_LOCK", "true")
  197. os.MkdirAll("custom/conf", os.ModePerm)
  198. if err := goconfig.SaveConfigFile(setting.Cfg, path.Join(setting.CustomPath, "conf/app.ini")); err != nil {
  199. ctx.RenderWithErr(ctx.Tr("install.save_config_failed", err), INSTALL, &form)
  200. return
  201. }
  202. GlobalInit()
  203. // Create admin account.
  204. if err := models.CreateUser(&models.User{Name: form.AdminName, Email: form.AdminEmail, Passwd: form.AdminPasswd,
  205. IsAdmin: true, IsActive: true}); err != nil {
  206. if err != models.ErrUserAlreadyExist {
  207. setting.InstallLock = false
  208. ctx.Data["Err_AdminName"] = true
  209. ctx.Data["Err_AdminEmail"] = true
  210. ctx.RenderWithErr(ctx.Tr("install.invalid_admin_setting", err), INSTALL, &form)
  211. return
  212. }
  213. log.Info("Admin account already exist")
  214. }
  215. log.Info("First-time run install finished!")
  216. ctx.Flash.Success(ctx.Tr("install.install_success"))
  217. ctx.Redirect(setting.AppSubUrl + "/user/login")
  218. }