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.

install.go 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. "strings"
  10. "github.com/Unknwon/goconfig"
  11. "github.com/go-martini/martini"
  12. "github.com/lunny/xorm"
  13. qlog "github.com/qiniu/log"
  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/log"
  18. "github.com/gogits/gogs/modules/mailer"
  19. "github.com/gogits/gogs/modules/middleware"
  20. )
  21. // Check run mode(Default of martini is Dev).
  22. func checkRunMode() {
  23. switch base.Cfg.MustValue("", "RUN_MODE") {
  24. case "prod":
  25. martini.Env = martini.Prod
  26. base.IsProdMode = true
  27. case "test":
  28. martini.Env = martini.Test
  29. }
  30. log.Info("Run Mode: %s", strings.Title(martini.Env))
  31. }
  32. // GlobalInit is for global configuration reload-able.
  33. func GlobalInit() {
  34. base.NewConfigContext()
  35. mailer.NewMailerContext()
  36. models.LoadModelsConfig()
  37. models.LoadRepoConfig()
  38. models.NewRepoContext()
  39. if base.InstallLock {
  40. if err := models.NewEngine(); err != nil {
  41. qlog.Fatal(err)
  42. }
  43. models.HasEngine = true
  44. }
  45. base.NewServices()
  46. checkRunMode()
  47. }
  48. func Install(ctx *middleware.Context, form auth.InstallForm) {
  49. if base.InstallLock {
  50. ctx.Handle(404, "install.Install", errors.New("Installation is prohibited"))
  51. return
  52. }
  53. ctx.Data["Title"] = "Install"
  54. ctx.Data["PageIsInstall"] = true
  55. if ctx.Req.Method == "GET" {
  56. // Get and assign value to install form.
  57. if len(form.Host) == 0 {
  58. form.Host = models.DbCfg.Host
  59. }
  60. if len(form.User) == 0 {
  61. form.User = models.DbCfg.User
  62. }
  63. if len(form.Passwd) == 0 {
  64. form.Passwd = models.DbCfg.Pwd
  65. }
  66. if len(form.DatabaseName) == 0 {
  67. form.DatabaseName = models.DbCfg.Name
  68. }
  69. if len(form.DatabasePath) == 0 {
  70. form.DatabasePath = models.DbCfg.Path
  71. }
  72. if len(form.RepoRootPath) == 0 {
  73. form.RepoRootPath = base.RepoRootPath
  74. }
  75. if len(form.RunUser) == 0 {
  76. form.RunUser = base.RunUser
  77. }
  78. if len(form.Domain) == 0 {
  79. form.Domain = base.Domain
  80. }
  81. if len(form.AppUrl) == 0 {
  82. form.AppUrl = base.AppUrl
  83. }
  84. auth.AssignForm(form, ctx.Data)
  85. ctx.HTML(200, "install")
  86. return
  87. }
  88. if ctx.HasError() {
  89. ctx.HTML(200, "install")
  90. return
  91. }
  92. if _, err := exec.LookPath("git"); err != nil {
  93. ctx.RenderWithErr("Fail to test 'git' command: "+err.Error(), "install", &form)
  94. return
  95. }
  96. // Pass basic check, now test configuration.
  97. // Test database setting.
  98. dbTypes := map[string]string{"mysql": "mysql", "pgsql": "postgres", "sqlite": "sqlite3"}
  99. models.DbCfg.Type = dbTypes[form.Database]
  100. models.DbCfg.Host = form.Host
  101. models.DbCfg.User = form.User
  102. models.DbCfg.Pwd = form.Passwd
  103. models.DbCfg.Name = form.DatabaseName
  104. models.DbCfg.SslMode = form.SslMode
  105. models.DbCfg.Path = form.DatabasePath
  106. // Set test engine.
  107. var x *xorm.Engine
  108. if err := models.NewTestEngine(x); err != nil {
  109. if strings.Contains(err.Error(), `Unknown database type: sqlite3`) {
  110. ctx.RenderWithErr("Your release version does not support SQLite3, please download the official binary version "+
  111. "from https://github.com/gogits/gogs/wiki/Install-from-binary, NOT the gobuild version.", "install", &form)
  112. } else {
  113. ctx.RenderWithErr("Database setting is not correct: "+err.Error(), "install", &form)
  114. }
  115. return
  116. }
  117. // Test repository root path.
  118. if err := os.MkdirAll(form.RepoRootPath, os.ModePerm); err != nil {
  119. ctx.RenderWithErr("Repository root path is invalid: "+err.Error(), "install", &form)
  120. return
  121. }
  122. // Check run user.
  123. curUser := os.Getenv("USERNAME")
  124. if len(curUser) == 0 {
  125. curUser = os.Getenv("USER")
  126. }
  127. // Does not check run user when the install lock is off.
  128. if form.RunUser != curUser {
  129. ctx.RenderWithErr("Run user isn't the current user: "+form.RunUser+" -> "+curUser, "install", &form)
  130. return
  131. }
  132. // Save settings.
  133. base.Cfg.SetValue("database", "DB_TYPE", models.DbCfg.Type)
  134. base.Cfg.SetValue("database", "HOST", models.DbCfg.Host)
  135. base.Cfg.SetValue("database", "NAME", models.DbCfg.Name)
  136. base.Cfg.SetValue("database", "USER", models.DbCfg.User)
  137. base.Cfg.SetValue("database", "PASSWD", models.DbCfg.Pwd)
  138. base.Cfg.SetValue("database", "SSL_MODE", models.DbCfg.SslMode)
  139. base.Cfg.SetValue("database", "PATH", models.DbCfg.Path)
  140. base.Cfg.SetValue("repository", "ROOT", form.RepoRootPath)
  141. base.Cfg.SetValue("", "RUN_USER", form.RunUser)
  142. base.Cfg.SetValue("server", "DOMAIN", form.Domain)
  143. base.Cfg.SetValue("server", "ROOT_URL", form.AppUrl)
  144. if len(strings.TrimSpace(form.SmtpHost)) > 0 {
  145. base.Cfg.SetValue("mailer", "ENABLED", "true")
  146. base.Cfg.SetValue("mailer", "HOST", form.SmtpHost)
  147. base.Cfg.SetValue("mailer", "USER", form.SmtpEmail)
  148. base.Cfg.SetValue("mailer", "PASSWD", form.SmtpPasswd)
  149. base.Cfg.SetValue("service", "REGISTER_EMAIL_CONFIRM", base.ToStr(form.RegisterConfirm == "on"))
  150. base.Cfg.SetValue("service", "ENABLE_NOTIFY_MAIL", base.ToStr(form.MailNotify == "on"))
  151. }
  152. base.Cfg.SetValue("", "RUN_MODE", "prod")
  153. base.Cfg.SetValue("security", "INSTALL_LOCK", "true")
  154. os.MkdirAll("custom/conf", os.ModePerm)
  155. if err := goconfig.SaveConfigFile(base.Cfg, "custom/conf/app.ini"); err != nil {
  156. ctx.RenderWithErr("Fail to save configuration: "+err.Error(), "install", &form)
  157. return
  158. }
  159. GlobalInit()
  160. // Create admin account.
  161. if _, err := models.RegisterUser(&models.User{Name: form.AdminName, Email: form.AdminEmail, Passwd: form.AdminPasswd,
  162. IsAdmin: true, IsActive: true}); err != nil {
  163. if err != models.ErrUserAlreadyExist {
  164. base.InstallLock = false
  165. ctx.RenderWithErr("Admin account setting is invalid: "+err.Error(), "install", &form)
  166. return
  167. }
  168. log.Info("Admin account already exist")
  169. }
  170. log.Info("First-time run install finished!")
  171. ctx.Redirect("/user/login")
  172. }