install.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 renderDbOption(ctx *middleware.Context) {
  65. ctx.Data["DbOptions"] = []string{"MySQL", "PostgreSQL", "SQLite3"}
  66. }
  67. // @router /install [get]
  68. func Install(ctx *middleware.Context, form auth.InstallForm) {
  69. if setting.InstallLock {
  70. ctx.Handle(404, "Install", errors.New("Installation is prohibited"))
  71. return
  72. }
  73. ctx.Data["Title"] = ctx.Tr("install.install")
  74. ctx.Data["PageIsInstall"] = true
  75. // FIXME: when i'm ckeching length here? should they all be 0 no matter when?
  76. // Get and assign values to install form.
  77. if len(form.DbHost) == 0 {
  78. form.DbHost = models.DbCfg.Host
  79. }
  80. if len(form.DbUser) == 0 {
  81. form.DbUser = models.DbCfg.User
  82. }
  83. if len(form.DbPasswd) == 0 {
  84. form.DbPasswd = 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. // Note: it's not normall to use SSH in windows so current user can be first option(not git).
  97. if setting.IsWindows && setting.RunUser == "git" {
  98. form.RunUser = os.Getenv("USER")
  99. if len(form.RunUser) == 0 {
  100. form.RunUser = os.Getenv("USERNAME")
  101. }
  102. } else {
  103. form.RunUser = setting.RunUser
  104. }
  105. }
  106. if len(form.Domain) == 0 {
  107. form.Domain = setting.Domain
  108. }
  109. if len(form.AppUrl) == 0 {
  110. form.AppUrl = setting.AppUrl
  111. }
  112. renderDbOption(ctx)
  113. curDbOp := ""
  114. if models.EnableSQLite3 {
  115. curDbOp = "SQLite3" // Default when enabled.
  116. }
  117. ctx.Data["CurDbOption"] = curDbOp
  118. auth.AssignForm(form, ctx.Data)
  119. ctx.HTML(200, INSTALL)
  120. }
  121. func InstallPost(ctx *middleware.Context, form auth.InstallForm) {
  122. if setting.InstallLock {
  123. ctx.Handle(404, "InstallPost", errors.New("Installation is prohibited"))
  124. return
  125. }
  126. ctx.Data["Title"] = ctx.Tr("install.install")
  127. ctx.Data["PageIsInstall"] = true
  128. renderDbOption(ctx)
  129. ctx.Data["CurDbOption"] = form.Database
  130. if ctx.HasError() {
  131. ctx.HTML(200, INSTALL)
  132. return
  133. }
  134. if _, err := exec.LookPath("git"); err != nil {
  135. ctx.RenderWithErr(ctx.Tr("install.test_git_failed", err), INSTALL, &form)
  136. return
  137. }
  138. // Pass basic check, now test configuration.
  139. // Test database setting.
  140. dbTypes := map[string]string{"MySQL": "mysql", "PostgreSQL": "postgres", "SQLite3": "sqlite3"}
  141. models.DbCfg.Type = dbTypes[form.Database]
  142. models.DbCfg.Host = form.DbHost
  143. models.DbCfg.User = form.DbUser
  144. models.DbCfg.Pwd = form.DbPasswd
  145. models.DbCfg.Name = form.DatabaseName
  146. models.DbCfg.SslMode = form.SslMode
  147. models.DbCfg.Path = form.DatabasePath
  148. // Set test engine.
  149. var x *xorm.Engine
  150. if err := models.NewTestEngine(x); err != nil {
  151. // FIXME: should use core.QueryDriver (github.com/go-xorm/core)
  152. if strings.Contains(err.Error(), `Unknown database type: sqlite3`) {
  153. ctx.RenderWithErr(ctx.Tr("install.sqlite3_not_available", "http://gogs.io/docs/installation/install_from_binary.html"), INSTALL, &form)
  154. } else {
  155. ctx.RenderWithErr(ctx.Tr("install.invalid_db_setting", err), INSTALL, &form)
  156. }
  157. return
  158. }
  159. // Test repository root path.
  160. if err := os.MkdirAll(form.RepoRootPath, os.ModePerm); err != nil {
  161. ctx.Data["Err_RepoRootPath"] = true
  162. ctx.RenderWithErr(ctx.Tr("install.invalid_repo_path", err), INSTALL, &form)
  163. return
  164. }
  165. // Check run user.
  166. curUser := os.Getenv("USER")
  167. if len(curUser) == 0 {
  168. curUser = os.Getenv("USERNAME")
  169. }
  170. // Does not check run user when the install lock is off.
  171. if form.RunUser != curUser {
  172. ctx.Data["Err_RunUser"] = true
  173. ctx.RenderWithErr(ctx.Tr("install.run_user_not_match", form.RunUser, curUser), INSTALL, &form)
  174. return
  175. }
  176. // Check admin password.
  177. if form.AdminPasswd != form.ConfirmPasswd {
  178. ctx.Data["Err_AdminPasswd"] = true
  179. ctx.RenderWithErr(ctx.Tr("form.password_not_match"), INSTALL, form)
  180. return
  181. }
  182. // Save settings.
  183. setting.Cfg.Section("database").Key("DB_TYPE").SetValue(models.DbCfg.Type)
  184. setting.Cfg.Section("database").Key("HOST").SetValue(models.DbCfg.Host)
  185. setting.Cfg.Section("database").Key("NAME").SetValue(models.DbCfg.Name)
  186. setting.Cfg.Section("database").Key("USER").SetValue(models.DbCfg.User)
  187. setting.Cfg.Section("database").Key("PASSWD").SetValue(models.DbCfg.Pwd)
  188. setting.Cfg.Section("database").Key("SSL_MODE").SetValue(models.DbCfg.SslMode)
  189. setting.Cfg.Section("database").Key("PATH").SetValue(models.DbCfg.Path)
  190. setting.Cfg.Section("repository").Key("ROOT").SetValue(form.RepoRootPath)
  191. setting.Cfg.Section("").Key("RUN_USER").SetValue(form.RunUser)
  192. setting.Cfg.Section("server").Key("DOMAIN").SetValue(form.Domain)
  193. setting.Cfg.Section("server").Key("ROOT_URL").SetValue(form.AppUrl)
  194. if len(strings.TrimSpace(form.SmtpHost)) > 0 {
  195. setting.Cfg.Section("mailer").Key("ENABLED").SetValue("true")
  196. setting.Cfg.Section("mailer").Key("HOST").SetValue(form.SmtpHost)
  197. setting.Cfg.Section("mailer").Key("USER").SetValue(form.SmtpEmail)
  198. setting.Cfg.Section("mailer").Key("PASSWD").SetValue(form.SmtpPasswd)
  199. setting.Cfg.Section("service").Key("REGISTER_EMAIL_CONFIRM").SetValue(com.ToStr(form.RegisterConfirm == "on"))
  200. setting.Cfg.Section("service").Key("ENABLE_NOTIFY_MAIL").SetValue(com.ToStr(form.MailNotify == "on"))
  201. }
  202. setting.Cfg.Section("").Key("RUN_MODE").SetValue("prod")
  203. setting.Cfg.Section("session").Key("PROVIDER").SetValue("file")
  204. setting.Cfg.Section("log").Key("MODE").SetValue("file")
  205. setting.Cfg.Section("security").Key("INSTALL_LOCK").SetValue("true")
  206. setting.Cfg.Section("security").Key("SECRET_KEY").SetValue(base.GetRandomString(15))
  207. os.MkdirAll("custom/conf", os.ModePerm)
  208. if err := setting.Cfg.SaveTo(path.Join(setting.CustomPath, "conf/app.ini")); err != nil {
  209. ctx.RenderWithErr(ctx.Tr("install.save_config_failed", err), INSTALL, &form)
  210. return
  211. }
  212. GlobalInit()
  213. // Create admin account.
  214. if err := models.CreateUser(&models.User{Name: form.AdminName, Email: form.AdminEmail, Passwd: form.AdminPasswd,
  215. IsAdmin: true, IsActive: true}); err != nil {
  216. if err != models.ErrUserAlreadyExist {
  217. setting.InstallLock = false
  218. ctx.Data["Err_AdminName"] = true
  219. ctx.Data["Err_AdminEmail"] = true
  220. ctx.RenderWithErr(ctx.Tr("install.invalid_admin_setting", err), INSTALL, &form)
  221. return
  222. }
  223. log.Info("Admin account already exist")
  224. }
  225. log.Info("First-time run install finished!")
  226. ctx.Flash.Success(ctx.Tr("install.install_success"))
  227. ctx.Redirect(setting.AppSubUrl + "/user/login")
  228. }