install.go 7.6 KB

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