install.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. "path/filepath"
  11. "strings"
  12. "github.com/Unknwon/com"
  13. "github.com/Unknwon/macaron"
  14. "github.com/go-xorm/xorm"
  15. "gopkg.in/ini.v1"
  16. "github.com/gogits/gogs/models"
  17. "github.com/gogits/gogs/models/cron"
  18. "github.com/gogits/gogs/modules/auth"
  19. "github.com/gogits/gogs/modules/base"
  20. "github.com/gogits/gogs/modules/log"
  21. "github.com/gogits/gogs/modules/mailer"
  22. "github.com/gogits/gogs/modules/middleware"
  23. "github.com/gogits/gogs/modules/setting"
  24. "github.com/gogits/gogs/modules/social"
  25. "github.com/gogits/gogs/modules/user"
  26. )
  27. const (
  28. INSTALL base.TplName = "install"
  29. )
  30. func checkRunMode() {
  31. switch setting.Cfg.Section("").Key("RUN_MODE").String() {
  32. case "prod":
  33. macaron.Env = macaron.PROD
  34. macaron.ColorLog = false
  35. setting.ProdMode = true
  36. case "test":
  37. macaron.Env = macaron.TEST
  38. }
  39. log.Info("Run Mode: %s", strings.Title(macaron.Env))
  40. }
  41. func NewServices() {
  42. setting.NewServices()
  43. social.NewOauthService()
  44. }
  45. // GlobalInit is for global configuration reload-able.
  46. func GlobalInit() {
  47. setting.NewConfigContext()
  48. log.Trace("Custom path: %s", setting.CustomPath)
  49. log.Trace("Log path: %s", setting.LogRootPath)
  50. mailer.NewMailerContext()
  51. models.LoadModelsConfig()
  52. NewServices()
  53. if setting.InstallLock {
  54. models.LoadRepoConfig()
  55. models.NewRepoContext()
  56. if err := models.NewEngine(); err != nil {
  57. log.Fatal(4, "Fail to initialize ORM engine: %v", err)
  58. }
  59. models.HasEngine = true
  60. cron.NewCronContext()
  61. models.InitDeliverHooks()
  62. log.NewGitLogger(path.Join(setting.LogRootPath, "http.log"))
  63. }
  64. if models.EnableSQLite3 {
  65. log.Info("SQLite3 Supported")
  66. }
  67. checkRunMode()
  68. }
  69. func InstallInit(ctx *middleware.Context) {
  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. ctx.Data["DbOptions"] = []string{"MySQL", "PostgreSQL", "SQLite3"}
  77. }
  78. func Install(ctx *middleware.Context) {
  79. form := auth.InstallForm{}
  80. // Database settings
  81. form.DbHost = models.DbCfg.Host
  82. form.DbUser = models.DbCfg.User
  83. form.DbName = models.DbCfg.Name
  84. form.DbPath = models.DbCfg.Path
  85. if models.EnableSQLite3 {
  86. ctx.Data["CurDbOption"] = "SQLite3" // Default when enabled.
  87. } else {
  88. ctx.Data["CurDbOption"] = "MySQL"
  89. }
  90. // Application general settings
  91. form.AppName = setting.AppName
  92. form.RepoRootPath = setting.RepoRootPath
  93. // Note(unknwon): it's hard for Windows users change a running user,
  94. // so just use current one if config says default.
  95. if setting.IsWindows && setting.RunUser == "git" {
  96. form.RunUser = user.CurrentUsername()
  97. } else {
  98. form.RunUser = setting.RunUser
  99. }
  100. form.Domain = setting.Domain
  101. form.SSHPort = setting.SSHPort
  102. form.HTTPPort = setting.HttpPort
  103. form.AppUrl = setting.AppUrl
  104. // E-mail service settings
  105. if setting.MailService != nil {
  106. form.SMTPHost = setting.MailService.Host
  107. form.SMTPFrom = setting.MailService.From
  108. form.SMTPEmail = setting.MailService.User
  109. }
  110. form.RegisterConfirm = setting.Service.RegisterEmailConfirm
  111. form.MailNotify = setting.Service.EnableNotifyMail
  112. // Server and other services settings
  113. form.OfflineMode = setting.OfflineMode
  114. form.DisableRegistration = setting.Service.DisableRegistration
  115. form.RequireSignInView = setting.Service.RequireSignInView
  116. auth.AssignForm(form, ctx.Data)
  117. ctx.HTML(200, INSTALL)
  118. }
  119. func InstallPost(ctx *middleware.Context, form auth.InstallForm) {
  120. ctx.Data["CurDbOption"] = form.DbType
  121. if ctx.HasError() {
  122. if ctx.HasValue("Err_SMTPEmail") {
  123. ctx.Data["Err_SMTP"] = true
  124. }
  125. if ctx.HasValue("Err_AdminName") ||
  126. ctx.HasValue("Err_AdminPasswd") ||
  127. ctx.HasValue("Err_AdminEmail") {
  128. ctx.Data["Err_Admin"] = true
  129. }
  130. ctx.HTML(200, INSTALL)
  131. return
  132. }
  133. if _, err := exec.LookPath("git"); err != nil {
  134. ctx.RenderWithErr(ctx.Tr("install.test_git_failed", err), INSTALL, &form)
  135. return
  136. }
  137. // Pass basic check, now test configuration.
  138. // Test database setting.
  139. dbTypes := map[string]string{"MySQL": "mysql", "PostgreSQL": "postgres", "SQLite3": "sqlite3"}
  140. models.DbCfg.Type = dbTypes[form.DbType]
  141. models.DbCfg.Host = form.DbHost
  142. models.DbCfg.User = form.DbUser
  143. models.DbCfg.Passwd = form.DbPasswd
  144. models.DbCfg.Name = form.DbName
  145. models.DbCfg.SSLMode = form.SSLMode
  146. models.DbCfg.Path = form.DbPath
  147. if models.DbCfg.Type == "sqlite3" && len(models.DbCfg.Path) == 0 {
  148. ctx.Data["Err_DbPath"] = true
  149. ctx.RenderWithErr(ctx.Tr("install.err_empty_sqlite_path"), INSTALL, &form)
  150. return
  151. }
  152. // Set test engine.
  153. var x *xorm.Engine
  154. if err := models.NewTestEngine(x); err != nil {
  155. if strings.Contains(err.Error(), `Unknown database type: sqlite3`) {
  156. ctx.Data["Err_DbType"] = true
  157. ctx.RenderWithErr(ctx.Tr("install.sqlite3_not_available", "http://gogs.io/docs/installation/install_from_binary.html"), INSTALL, &form)
  158. } else {
  159. ctx.Data["Err_DbSetting"] = true
  160. ctx.RenderWithErr(ctx.Tr("install.invalid_db_setting", err), INSTALL, &form)
  161. }
  162. return
  163. }
  164. // Test repository root path.
  165. if err := os.MkdirAll(form.RepoRootPath, os.ModePerm); err != nil {
  166. ctx.Data["Err_RepoRootPath"] = true
  167. ctx.RenderWithErr(ctx.Tr("install.invalid_repo_path", err), INSTALL, &form)
  168. return
  169. }
  170. // Check run user.
  171. curUser := user.CurrentUsername()
  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.AdminConfirmPasswd {
  179. ctx.Data["Err_Admin"] = true
  180. ctx.Data["Err_AdminPasswd"] = true
  181. ctx.RenderWithErr(ctx.Tr("form.password_not_match"), INSTALL, form)
  182. return
  183. }
  184. if form.AppUrl[len(form.AppUrl)-1] != '/' {
  185. form.AppUrl += "/"
  186. }
  187. // Save settings.
  188. cfg := ini.Empty()
  189. if com.IsFile(setting.CustomConf) {
  190. // Keeps custom settings if there is already something.
  191. if err := cfg.Append(setting.CustomConf); err != nil {
  192. log.Error(4, "Fail to load custom conf '%s': %v", setting.CustomConf, err)
  193. }
  194. }
  195. cfg.Section("database").Key("DB_TYPE").SetValue(models.DbCfg.Type)
  196. cfg.Section("database").Key("HOST").SetValue(models.DbCfg.Host)
  197. cfg.Section("database").Key("NAME").SetValue(models.DbCfg.Name)
  198. cfg.Section("database").Key("USER").SetValue(models.DbCfg.User)
  199. cfg.Section("database").Key("PASSWD").SetValue(models.DbCfg.Passwd)
  200. cfg.Section("database").Key("SSL_MODE").SetValue(models.DbCfg.SSLMode)
  201. cfg.Section("database").Key("PATH").SetValue(models.DbCfg.Path)
  202. cfg.Section("").Key("APP_NAME").SetValue(form.AppName)
  203. cfg.Section("repository").Key("ROOT").SetValue(form.RepoRootPath)
  204. cfg.Section("").Key("RUN_USER").SetValue(form.RunUser)
  205. cfg.Section("server").Key("DOMAIN").SetValue(form.Domain)
  206. cfg.Section("server").Key("HTTP_PORT").SetValue(form.HTTPPort)
  207. cfg.Section("server").Key("ROOT_URL").SetValue(form.AppUrl)
  208. if form.SSHPort == 0 {
  209. cfg.Section("server").Key("DISABLE_SSH").SetValue("true")
  210. } else {
  211. cfg.Section("server").Key("DISABLE_SSH").SetValue("false")
  212. cfg.Section("server").Key("SSH_PORT").SetValue(com.ToStr(form.SSHPort))
  213. }
  214. if len(strings.TrimSpace(form.SMTPHost)) > 0 {
  215. cfg.Section("mailer").Key("ENABLED").SetValue("true")
  216. cfg.Section("mailer").Key("HOST").SetValue(form.SMTPHost)
  217. cfg.Section("mailer").Key("FROM").SetValue(form.SMTPFrom)
  218. cfg.Section("mailer").Key("USER").SetValue(form.SMTPEmail)
  219. cfg.Section("mailer").Key("PASSWD").SetValue(form.SMTPPasswd)
  220. } else {
  221. cfg.Section("mailer").Key("ENABLED").SetValue("false")
  222. }
  223. cfg.Section("service").Key("REGISTER_EMAIL_CONFIRM").SetValue(com.ToStr(form.RegisterConfirm))
  224. cfg.Section("service").Key("ENABLE_NOTIFY_MAIL").SetValue(com.ToStr(form.MailNotify))
  225. cfg.Section("server").Key("OFFLINE_MODE").SetValue(com.ToStr(form.OfflineMode))
  226. cfg.Section("service").Key("DISABLE_REGISTRATION").SetValue(com.ToStr(form.DisableRegistration))
  227. cfg.Section("service").Key("REQUIRE_SIGNIN_VIEW").SetValue(com.ToStr(form.RequireSignInView))
  228. cfg.Section("").Key("RUN_MODE").SetValue("prod")
  229. cfg.Section("session").Key("PROVIDER").SetValue("file")
  230. cfg.Section("log").Key("MODE").SetValue("file")
  231. cfg.Section("log").Key("LEVEL").SetValue("Info")
  232. cfg.Section("security").Key("INSTALL_LOCK").SetValue("true")
  233. cfg.Section("security").Key("SECRET_KEY").SetValue(base.GetRandomString(15))
  234. os.MkdirAll(filepath.Dir(setting.CustomConf), os.ModePerm)
  235. if err := cfg.SaveTo(setting.CustomConf); err != nil {
  236. ctx.RenderWithErr(ctx.Tr("install.save_config_failed", err), INSTALL, &form)
  237. return
  238. }
  239. GlobalInit()
  240. // Create admin account.
  241. if len(form.AdminName) > 0 {
  242. if err := models.CreateUser(&models.User{
  243. Name: form.AdminName,
  244. Email: form.AdminEmail,
  245. Passwd: form.AdminPasswd,
  246. IsAdmin: true,
  247. IsActive: true,
  248. }); err != nil {
  249. if !models.IsErrUserAlreadyExist(err) {
  250. setting.InstallLock = false
  251. ctx.Data["Err_AdminName"] = true
  252. ctx.Data["Err_AdminEmail"] = true
  253. ctx.RenderWithErr(ctx.Tr("install.invalid_admin_setting", err), INSTALL, &form)
  254. return
  255. }
  256. log.Info("Admin account already exist")
  257. }
  258. }
  259. log.Info("First-time run install finished!")
  260. ctx.Flash.Success(ctx.Tr("install.install_success"))
  261. ctx.Redirect(form.AppUrl + "user/login")
  262. }