install.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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/macaron"
  13. "github.com/go-xorm/xorm"
  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/cron"
  18. "github.com/gogits/gogs/modules/log"
  19. "github.com/gogits/gogs/modules/mailer"
  20. "github.com/gogits/gogs/modules/middleware"
  21. "github.com/gogits/gogs/modules/setting"
  22. "github.com/gogits/gogs/modules/social"
  23. )
  24. const (
  25. INSTALL base.TplName = "install"
  26. )
  27. func checkRunMode() {
  28. switch setting.Cfg.Section("").Key("RUN_MODE").String() {
  29. case "prod":
  30. macaron.Env = macaron.PROD
  31. setting.ProdMode = true
  32. case "test":
  33. macaron.Env = macaron.TEST
  34. }
  35. log.Info("Run Mode: %s", strings.Title(macaron.Env))
  36. }
  37. func NewServices() {
  38. setting.NewServices()
  39. social.NewOauthService()
  40. }
  41. // GlobalInit is for global configuration reload-able.
  42. func GlobalInit() {
  43. setting.NewConfigContext()
  44. log.Trace("Custom path: %s", setting.CustomPath)
  45. log.Trace("Log path: %s", setting.LogRootPath)
  46. mailer.NewMailerContext()
  47. models.LoadModelsConfig()
  48. NewServices()
  49. if setting.InstallLock {
  50. models.LoadRepoConfig()
  51. models.NewRepoContext()
  52. if err := models.NewEngine(); err != nil {
  53. log.Fatal(4, "Fail to initialize ORM engine: %v", err)
  54. }
  55. models.HasEngine = true
  56. cron.NewCronContext()
  57. log.NewGitLogger(path.Join(setting.LogRootPath, "http.log"))
  58. }
  59. if models.EnableSQLite3 {
  60. log.Info("SQLite3 Enabled")
  61. }
  62. checkRunMode()
  63. }
  64. func InstallInit(ctx *middleware.Context) {
  65. if setting.InstallLock {
  66. ctx.Handle(404, "Install", errors.New("Installation is prohibited"))
  67. return
  68. }
  69. ctx.Data["Title"] = ctx.Tr("install.install")
  70. ctx.Data["PageIsInstall"] = true
  71. ctx.Data["DbOptions"] = []string{"MySQL", "PostgreSQL", "SQLite3"}
  72. }
  73. func Install(ctx *middleware.Context) {
  74. form := auth.InstallForm{}
  75. form.DbHost = models.DbCfg.Host
  76. form.DbUser = models.DbCfg.User
  77. form.DbPasswd = models.DbCfg.Passwd
  78. form.DbName = models.DbCfg.Name
  79. form.DbPath = models.DbCfg.Path
  80. form.RepoRootPath = setting.RepoRootPath
  81. // Note(unknwon): it's hard for Windows users change a running user,
  82. // so just use current one if config says default.
  83. if setting.IsWindows && setting.RunUser == "git" {
  84. form.RunUser = os.Getenv("USER")
  85. if len(form.RunUser) == 0 {
  86. form.RunUser = os.Getenv("USERNAME")
  87. }
  88. } else {
  89. form.RunUser = setting.RunUser
  90. }
  91. form.Domain = setting.Domain
  92. form.HTTPPort = setting.HttpPort
  93. form.AppUrl = setting.AppUrl
  94. curDbOp := ""
  95. if models.EnableSQLite3 {
  96. curDbOp = "SQLite3" // Default when enabled.
  97. }
  98. ctx.Data["CurDbOption"] = curDbOp
  99. auth.AssignForm(form, ctx.Data)
  100. ctx.HTML(200, INSTALL)
  101. }
  102. func InstallPost(ctx *middleware.Context, form auth.InstallForm) {
  103. ctx.Data["CurDbOption"] = form.DbType
  104. if ctx.HasError() {
  105. ctx.HTML(200, INSTALL)
  106. return
  107. }
  108. if _, err := exec.LookPath("git"); err != nil {
  109. ctx.RenderWithErr(ctx.Tr("install.test_git_failed", err), INSTALL, &form)
  110. return
  111. }
  112. // Pass basic check, now test configuration.
  113. // Test database setting.
  114. dbTypes := map[string]string{"MySQL": "mysql", "PostgreSQL": "postgres", "SQLite3": "sqlite3"}
  115. models.DbCfg.Type = dbTypes[form.DbType]
  116. models.DbCfg.Host = form.DbHost
  117. models.DbCfg.User = form.DbUser
  118. models.DbCfg.Passwd = form.DbPasswd
  119. models.DbCfg.Name = form.DbName
  120. models.DbCfg.SSLMode = form.SSLMode
  121. models.DbCfg.Path = form.DbPath
  122. // Set test engine.
  123. var x *xorm.Engine
  124. if err := models.NewTestEngine(x); err != nil {
  125. if strings.Contains(err.Error(), `Unknown database type: sqlite3`) {
  126. ctx.RenderWithErr(ctx.Tr("install.sqlite3_not_available", "http://gogs.io/docs/installation/install_from_binary.html"), INSTALL, &form)
  127. } else {
  128. ctx.RenderWithErr(ctx.Tr("install.invalid_db_setting", err), INSTALL, &form)
  129. }
  130. return
  131. }
  132. // Test repository root path.
  133. if err := os.MkdirAll(form.RepoRootPath, os.ModePerm); err != nil {
  134. ctx.Data["Err_RepoRootPath"] = true
  135. ctx.RenderWithErr(ctx.Tr("install.invalid_repo_path", err), INSTALL, &form)
  136. return
  137. }
  138. // Check run user.
  139. curUser := os.Getenv("USER")
  140. if len(curUser) == 0 {
  141. curUser = os.Getenv("USERNAME")
  142. }
  143. if form.RunUser != curUser {
  144. ctx.Data["Err_RunUser"] = true
  145. ctx.RenderWithErr(ctx.Tr("install.run_user_not_match", form.RunUser, curUser), INSTALL, &form)
  146. return
  147. }
  148. // Check admin password.
  149. if form.AdminPasswd != form.AdminConfirmPasswd {
  150. ctx.Data["Err_AdminPasswd"] = true
  151. ctx.RenderWithErr(ctx.Tr("form.password_not_match"), INSTALL, form)
  152. return
  153. }
  154. if form.AppUrl[len(form.AppUrl)-1] != '/' {
  155. form.AppUrl += "/"
  156. }
  157. // Save settings.
  158. setting.Cfg.Section("database").Key("DB_TYPE").SetValue(models.DbCfg.Type)
  159. setting.Cfg.Section("database").Key("HOST").SetValue(models.DbCfg.Host)
  160. setting.Cfg.Section("database").Key("NAME").SetValue(models.DbCfg.Name)
  161. setting.Cfg.Section("database").Key("USER").SetValue(models.DbCfg.User)
  162. setting.Cfg.Section("database").Key("PASSWD").SetValue(models.DbCfg.Passwd)
  163. setting.Cfg.Section("database").Key("SSL_MODE").SetValue(models.DbCfg.SSLMode)
  164. setting.Cfg.Section("database").Key("PATH").SetValue(models.DbCfg.Path)
  165. setting.Cfg.Section("repository").Key("ROOT").SetValue(form.RepoRootPath)
  166. setting.Cfg.Section("").Key("RUN_USER").SetValue(form.RunUser)
  167. setting.Cfg.Section("server").Key("DOMAIN").SetValue(form.Domain)
  168. setting.Cfg.Section("server").Key("HTTP_PORT").SetValue(form.HTTPPort)
  169. setting.Cfg.Section("server").Key("ROOT_URL").SetValue(form.AppUrl)
  170. if len(strings.TrimSpace(form.SMTPHost)) > 0 {
  171. setting.Cfg.Section("mailer").Key("ENABLED").SetValue("true")
  172. setting.Cfg.Section("mailer").Key("HOST").SetValue(form.SMTPHost)
  173. setting.Cfg.Section("mailer").Key("USER").SetValue(form.SMTPEmail)
  174. setting.Cfg.Section("mailer").Key("PASSWD").SetValue(form.SMTPPasswd)
  175. setting.Cfg.Section("service").Key("REGISTER_EMAIL_CONFIRM").SetValue(com.ToStr(form.RegisterConfirm == "on"))
  176. setting.Cfg.Section("service").Key("ENABLE_NOTIFY_MAIL").SetValue(com.ToStr(form.MailNotify == "on"))
  177. }
  178. setting.Cfg.Section("").Key("RUN_MODE").SetValue("prod")
  179. setting.Cfg.Section("session").Key("PROVIDER").SetValue("file")
  180. setting.Cfg.Section("log").Key("MODE").SetValue("file")
  181. setting.Cfg.Section("security").Key("INSTALL_LOCK").SetValue("true")
  182. setting.Cfg.Section("security").Key("SECRET_KEY").SetValue(base.GetRandomString(15))
  183. os.MkdirAll("custom/conf", os.ModePerm)
  184. if err := setting.Cfg.SaveTo(path.Join(setting.CustomPath, "conf/app.ini")); err != nil {
  185. ctx.RenderWithErr(ctx.Tr("install.save_config_failed", err), INSTALL, &form)
  186. return
  187. }
  188. GlobalInit()
  189. // Create admin account.
  190. if err := models.CreateUser(&models.User{Name: form.AdminName, Email: form.AdminEmail, Passwd: form.AdminPasswd,
  191. IsAdmin: true, IsActive: true}); err != nil {
  192. if err != models.ErrUserAlreadyExist {
  193. setting.InstallLock = false
  194. ctx.Data["Err_AdminName"] = true
  195. ctx.Data["Err_AdminEmail"] = true
  196. ctx.RenderWithErr(ctx.Tr("install.invalid_admin_setting", err), INSTALL, &form)
  197. return
  198. }
  199. log.Info("Admin account already exist")
  200. }
  201. log.Info("First-time run install finished!")
  202. ctx.Flash.Success(ctx.Tr("install.install_success"))
  203. ctx.Redirect(form.AppUrl + "user/login")
  204. }