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