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.

248 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
  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/macaron"
  13. "github.com/go-xorm/xorm"
  14. "gopkg.in/ini.v1"
  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.Section("").Key("RUN_MODE").String() {
  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 InstallInit(ctx *middleware.Context) {
  66. if setting.InstallLock {
  67. ctx.Handle(404, "Install", errors.New("Installation is prohibited"))
  68. return
  69. }
  70. ctx.Data["Title"] = ctx.Tr("install.install")
  71. ctx.Data["PageIsInstall"] = true
  72. ctx.Data["DbOptions"] = []string{"MySQL", "PostgreSQL", "SQLite3"}
  73. }
  74. func Install(ctx *middleware.Context) {
  75. form := auth.InstallForm{}
  76. form.DbHost = models.DbCfg.Host
  77. form.DbUser = models.DbCfg.User
  78. form.DbPasswd = models.DbCfg.Passwd
  79. form.DbName = models.DbCfg.Name
  80. form.DbPath = models.DbCfg.Path
  81. form.RepoRootPath = setting.RepoRootPath
  82. // Note(unknwon): it's hard for Windows users change a running user,
  83. // so just use current one if config says default.
  84. if setting.IsWindows && setting.RunUser == "git" {
  85. form.RunUser = os.Getenv("USER")
  86. if len(form.RunUser) == 0 {
  87. form.RunUser = os.Getenv("USERNAME")
  88. }
  89. } else {
  90. form.RunUser = setting.RunUser
  91. }
  92. form.Domain = setting.Domain
  93. form.HTTPPort = setting.HttpPort
  94. form.AppUrl = setting.AppUrl
  95. curDbOp := ""
  96. if models.EnableSQLite3 {
  97. curDbOp = "SQLite3" // Default when enabled.
  98. }
  99. ctx.Data["CurDbOption"] = curDbOp
  100. auth.AssignForm(form, ctx.Data)
  101. ctx.HTML(200, INSTALL)
  102. }
  103. func InstallPost(ctx *middleware.Context, form auth.InstallForm) {
  104. ctx.Data["CurDbOption"] = form.DbType
  105. if ctx.HasError() {
  106. ctx.HTML(200, INSTALL)
  107. return
  108. }
  109. if _, err := exec.LookPath("git"); err != nil {
  110. ctx.RenderWithErr(ctx.Tr("install.test_git_failed", err), INSTALL, &form)
  111. return
  112. }
  113. // Pass basic check, now test configuration.
  114. // Test database setting.
  115. dbTypes := map[string]string{"MySQL": "mysql", "PostgreSQL": "postgres", "SQLite3": "sqlite3"}
  116. models.DbCfg.Type = dbTypes[form.DbType]
  117. models.DbCfg.Host = form.DbHost
  118. models.DbCfg.User = form.DbUser
  119. models.DbCfg.Passwd = form.DbPasswd
  120. models.DbCfg.Name = form.DbName
  121. models.DbCfg.SSLMode = form.SSLMode
  122. models.DbCfg.Path = form.DbPath
  123. // Set test engine.
  124. var x *xorm.Engine
  125. if err := models.NewTestEngine(x); err != nil {
  126. if strings.Contains(err.Error(), `Unknown database type: sqlite3`) {
  127. ctx.RenderWithErr(ctx.Tr("install.sqlite3_not_available", "http://gogs.io/docs/installation/install_from_binary.html"), INSTALL, &form)
  128. } else {
  129. ctx.RenderWithErr(ctx.Tr("install.invalid_db_setting", err), INSTALL, &form)
  130. }
  131. return
  132. }
  133. // Test repository root path.
  134. if err := os.MkdirAll(form.RepoRootPath, os.ModePerm); err != nil {
  135. ctx.Data["Err_RepoRootPath"] = true
  136. ctx.RenderWithErr(ctx.Tr("install.invalid_repo_path", err), INSTALL, &form)
  137. return
  138. }
  139. // Check run user.
  140. curUser := os.Getenv("USER")
  141. if len(curUser) == 0 {
  142. curUser = os.Getenv("USERNAME")
  143. }
  144. if form.RunUser != curUser {
  145. ctx.Data["Err_RunUser"] = true
  146. ctx.RenderWithErr(ctx.Tr("install.run_user_not_match", form.RunUser, curUser), INSTALL, &form)
  147. return
  148. }
  149. // Check admin password.
  150. if form.AdminPasswd != form.AdminConfirmPasswd {
  151. ctx.Data["Err_AdminPasswd"] = true
  152. ctx.RenderWithErr(ctx.Tr("form.password_not_match"), INSTALL, form)
  153. return
  154. }
  155. if form.AppUrl[len(form.AppUrl)-1] != '/' {
  156. form.AppUrl += "/"
  157. }
  158. // Save settings.
  159. cfg := ini.Empty()
  160. cfg.Section("database").Key("DB_TYPE").SetValue(models.DbCfg.Type)
  161. cfg.Section("database").Key("HOST").SetValue(models.DbCfg.Host)
  162. cfg.Section("database").Key("NAME").SetValue(models.DbCfg.Name)
  163. cfg.Section("database").Key("USER").SetValue(models.DbCfg.User)
  164. cfg.Section("database").Key("PASSWD").SetValue(models.DbCfg.Passwd)
  165. cfg.Section("database").Key("SSL_MODE").SetValue(models.DbCfg.SSLMode)
  166. cfg.Section("database").Key("PATH").SetValue(models.DbCfg.Path)
  167. cfg.Section("repository").Key("ROOT").SetValue(form.RepoRootPath)
  168. cfg.Section("").Key("RUN_USER").SetValue(form.RunUser)
  169. cfg.Section("server").Key("DOMAIN").SetValue(form.Domain)
  170. cfg.Section("server").Key("HTTP_PORT").SetValue(form.HTTPPort)
  171. cfg.Section("server").Key("ROOT_URL").SetValue(form.AppUrl)
  172. if len(strings.TrimSpace(form.SMTPHost)) > 0 {
  173. cfg.Section("mailer").Key("ENABLED").SetValue("true")
  174. cfg.Section("mailer").Key("HOST").SetValue(form.SMTPHost)
  175. cfg.Section("mailer").Key("USER").SetValue(form.SMTPEmail)
  176. cfg.Section("mailer").Key("PASSWD").SetValue(form.SMTPPasswd)
  177. cfg.Section("service").Key("REGISTER_EMAIL_CONFIRM").SetValue(com.ToStr(form.RegisterConfirm == "on"))
  178. cfg.Section("service").Key("ENABLE_NOTIFY_MAIL").SetValue(com.ToStr(form.MailNotify == "on"))
  179. }
  180. cfg.Section("").Key("RUN_MODE").SetValue("prod")
  181. cfg.Section("session").Key("PROVIDER").SetValue("file")
  182. cfg.Section("log").Key("MODE").SetValue("file")
  183. cfg.Section("security").Key("INSTALL_LOCK").SetValue("true")
  184. cfg.Section("security").Key("SECRET_KEY").SetValue(base.GetRandomString(15))
  185. os.MkdirAll("custom/conf", os.ModePerm)
  186. if err := cfg.SaveTo(path.Join(setting.CustomPath, "conf/app.ini")); err != nil {
  187. ctx.RenderWithErr(ctx.Tr("install.save_config_failed", err), INSTALL, &form)
  188. return
  189. }
  190. GlobalInit()
  191. // Create admin account.
  192. if err := models.CreateUser(&models.User{Name: form.AdminName, Email: form.AdminEmail, Passwd: form.AdminPasswd,
  193. IsAdmin: true, IsActive: true}); err != nil {
  194. if err != models.ErrUserAlreadyExist {
  195. setting.InstallLock = false
  196. ctx.Data["Err_AdminName"] = true
  197. ctx.Data["Err_AdminEmail"] = true
  198. ctx.RenderWithErr(ctx.Tr("install.invalid_admin_setting", err), INSTALL, &form)
  199. return
  200. }
  201. log.Info("Admin account already exist")
  202. }
  203. log.Info("First-time run install finished!")
  204. ctx.Flash.Success(ctx.Tr("install.install_success"))
  205. ctx.Redirect(form.AppUrl + "user/login")
  206. }