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 7.0KB

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