You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

install.go 9.0 kB

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