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.

249 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
  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. "path/filepath"
  11. "strings"
  12. "github.com/Unknwon/com"
  13. "github.com/Unknwon/macaron"
  14. "github.com/go-xorm/xorm"
  15. "gopkg.in/ini.v1"
  16. "github.com/gogits/gogs/models"
  17. "github.com/gogits/gogs/modules/auth"
  18. "github.com/gogits/gogs/modules/base"
  19. "github.com/gogits/gogs/modules/cron"
  20. "github.com/gogits/gogs/modules/log"
  21. "github.com/gogits/gogs/modules/mailer"
  22. "github.com/gogits/gogs/modules/middleware"
  23. "github.com/gogits/gogs/modules/setting"
  24. "github.com/gogits/gogs/modules/social"
  25. )
  26. const (
  27. INSTALL base.TplName = "install"
  28. )
  29. func checkRunMode() {
  30. switch setting.Cfg.Section("").Key("RUN_MODE").String() {
  31. case "prod":
  32. macaron.Env = macaron.PROD
  33. setting.ProdMode = true
  34. case "test":
  35. macaron.Env = macaron.TEST
  36. }
  37. log.Info("Run Mode: %s", strings.Title(macaron.Env))
  38. }
  39. func NewServices() {
  40. setting.NewServices()
  41. social.NewOauthService()
  42. }
  43. // GlobalInit is for global configuration reload-able.
  44. func GlobalInit() {
  45. setting.NewConfigContext()
  46. log.Trace("Custom path: %s", setting.CustomPath)
  47. log.Trace("Log path: %s", setting.LogRootPath)
  48. mailer.NewMailerContext()
  49. models.LoadModelsConfig()
  50. NewServices()
  51. if setting.InstallLock {
  52. models.LoadRepoConfig()
  53. models.NewRepoContext()
  54. if err := models.NewEngine(); err != nil {
  55. log.Fatal(4, "Fail to initialize ORM engine: %v", err)
  56. }
  57. models.HasEngine = true
  58. cron.NewCronContext()
  59. log.NewGitLogger(path.Join(setting.LogRootPath, "http.log"))
  60. }
  61. if models.EnableSQLite3 {
  62. log.Info("SQLite3 Enabled")
  63. }
  64. checkRunMode()
  65. }
  66. func InstallInit(ctx *middleware.Context) {
  67. if setting.InstallLock {
  68. ctx.Handle(404, "Install", errors.New("Installation is prohibited"))
  69. return
  70. }
  71. ctx.Data["Title"] = ctx.Tr("install.install")
  72. ctx.Data["PageIsInstall"] = true
  73. ctx.Data["DbOptions"] = []string{"MySQL", "PostgreSQL", "SQLite3"}
  74. }
  75. func Install(ctx *middleware.Context) {
  76. form := auth.InstallForm{}
  77. form.DbHost = models.DbCfg.Host
  78. form.DbUser = models.DbCfg.User
  79. form.DbPasswd = models.DbCfg.Passwd
  80. form.DbName = models.DbCfg.Name
  81. form.DbPath = models.DbCfg.Path
  82. form.RepoRootPath = setting.RepoRootPath
  83. // Note(unknwon): it's hard for Windows users change a running user,
  84. // so just use current one if config says default.
  85. if setting.IsWindows && setting.RunUser == "git" {
  86. form.RunUser = os.Getenv("USER")
  87. if len(form.RunUser) == 0 {
  88. form.RunUser = os.Getenv("USERNAME")
  89. }
  90. } else {
  91. form.RunUser = setting.RunUser
  92. }
  93. form.Domain = setting.Domain
  94. form.HTTPPort = setting.HttpPort
  95. form.AppUrl = setting.AppUrl
  96. curDbOp := ""
  97. if models.EnableSQLite3 {
  98. curDbOp = "SQLite3" // Default when enabled.
  99. }
  100. ctx.Data["CurDbOption"] = curDbOp
  101. auth.AssignForm(form, ctx.Data)
  102. ctx.HTML(200, INSTALL)
  103. }
  104. func InstallPost(ctx *middleware.Context, form auth.InstallForm) {
  105. ctx.Data["CurDbOption"] = form.DbType
  106. if ctx.HasError() {
  107. ctx.HTML(200, INSTALL)
  108. return
  109. }
  110. if _, err := exec.LookPath("git"); err != nil {
  111. ctx.RenderWithErr(ctx.Tr("install.test_git_failed", err), INSTALL, &form)
  112. return
  113. }
  114. // Pass basic check, now test configuration.
  115. // Test database setting.
  116. dbTypes := map[string]string{"MySQL": "mysql", "PostgreSQL": "postgres", "SQLite3": "sqlite3"}
  117. models.DbCfg.Type = dbTypes[form.DbType]
  118. models.DbCfg.Host = form.DbHost
  119. models.DbCfg.User = form.DbUser
  120. models.DbCfg.Passwd = form.DbPasswd
  121. models.DbCfg.Name = form.DbName
  122. models.DbCfg.SSLMode = form.SSLMode
  123. models.DbCfg.Path = form.DbPath
  124. // Set test engine.
  125. var x *xorm.Engine
  126. if err := models.NewTestEngine(x); err != nil {
  127. if strings.Contains(err.Error(), `Unknown database type: sqlite3`) {
  128. ctx.RenderWithErr(ctx.Tr("install.sqlite3_not_available", "http://gogs.io/docs/installation/install_from_binary.html"), INSTALL, &form)
  129. } else {
  130. ctx.RenderWithErr(ctx.Tr("install.invalid_db_setting", err), INSTALL, &form)
  131. }
  132. return
  133. }
  134. // Test repository root path.
  135. if err := os.MkdirAll(form.RepoRootPath, os.ModePerm); err != nil {
  136. ctx.Data["Err_RepoRootPath"] = true
  137. ctx.RenderWithErr(ctx.Tr("install.invalid_repo_path", err), INSTALL, &form)
  138. return
  139. }
  140. // Check run user.
  141. curUser := os.Getenv("USER")
  142. if len(curUser) == 0 {
  143. curUser = os.Getenv("USERNAME")
  144. }
  145. if form.RunUser != curUser {
  146. ctx.Data["Err_RunUser"] = true
  147. ctx.RenderWithErr(ctx.Tr("install.run_user_not_match", form.RunUser, curUser), INSTALL, &form)
  148. return
  149. }
  150. // Check admin password.
  151. if form.AdminPasswd != form.AdminConfirmPasswd {
  152. ctx.Data["Err_AdminPasswd"] = true
  153. ctx.RenderWithErr(ctx.Tr("form.password_not_match"), INSTALL, form)
  154. return
  155. }
  156. if form.AppUrl[len(form.AppUrl)-1] != '/' {
  157. form.AppUrl += "/"
  158. }
  159. // Save settings.
  160. cfg := ini.Empty()
  161. cfg.Section("database").Key("DB_TYPE").SetValue(models.DbCfg.Type)
  162. cfg.Section("database").Key("HOST").SetValue(models.DbCfg.Host)
  163. cfg.Section("database").Key("NAME").SetValue(models.DbCfg.Name)
  164. cfg.Section("database").Key("USER").SetValue(models.DbCfg.User)
  165. cfg.Section("database").Key("PASSWD").SetValue(models.DbCfg.Passwd)
  166. cfg.Section("database").Key("SSL_MODE").SetValue(models.DbCfg.SSLMode)
  167. cfg.Section("database").Key("PATH").SetValue(models.DbCfg.Path)
  168. cfg.Section("repository").Key("ROOT").SetValue(form.RepoRootPath)
  169. cfg.Section("").Key("RUN_USER").SetValue(form.RunUser)
  170. cfg.Section("server").Key("DOMAIN").SetValue(form.Domain)
  171. cfg.Section("server").Key("HTTP_PORT").SetValue(form.HTTPPort)
  172. cfg.Section("server").Key("ROOT_URL").SetValue(form.AppUrl)
  173. if len(strings.TrimSpace(form.SMTPHost)) > 0 {
  174. cfg.Section("mailer").Key("ENABLED").SetValue("true")
  175. cfg.Section("mailer").Key("HOST").SetValue(form.SMTPHost)
  176. cfg.Section("mailer").Key("USER").SetValue(form.SMTPEmail)
  177. cfg.Section("mailer").Key("PASSWD").SetValue(form.SMTPPasswd)
  178. cfg.Section("service").Key("REGISTER_EMAIL_CONFIRM").SetValue(com.ToStr(form.RegisterConfirm == "on"))
  179. cfg.Section("service").Key("ENABLE_NOTIFY_MAIL").SetValue(com.ToStr(form.MailNotify == "on"))
  180. }
  181. cfg.Section("").Key("RUN_MODE").SetValue("prod")
  182. cfg.Section("session").Key("PROVIDER").SetValue("file")
  183. cfg.Section("log").Key("MODE").SetValue("file")
  184. cfg.Section("security").Key("INSTALL_LOCK").SetValue("true")
  185. cfg.Section("security").Key("SECRET_KEY").SetValue(base.GetRandomString(15))
  186. os.MkdirAll(filepath.Dir(setting.CustomConf), os.ModePerm)
  187. if err := cfg.SaveTo(setting.CustomConf); err != nil {
  188. ctx.RenderWithErr(ctx.Tr("install.save_config_failed", err), INSTALL, &form)
  189. return
  190. }
  191. GlobalInit()
  192. // Create admin account.
  193. if err := models.CreateUser(&models.User{Name: form.AdminName, Email: form.AdminEmail, Passwd: form.AdminPasswd,
  194. IsAdmin: true, IsActive: true}); err != nil {
  195. if err != models.ErrUserAlreadyExist {
  196. setting.InstallLock = false
  197. ctx.Data["Err_AdminName"] = true
  198. ctx.Data["Err_AdminEmail"] = true
  199. ctx.RenderWithErr(ctx.Tr("install.invalid_admin_setting", err), 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(ctx.Tr("install.install_success"))
  206. ctx.Redirect(form.AppUrl + "user/login")
  207. }