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