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

install.go 6.2 kB

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