install.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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/go-xorm/xorm"
  14. "gopkg.in/ini.v1"
  15. "gopkg.in/macaron.v1"
  16. "github.com/gogits/git-module"
  17. "github.com/gogits/gogs/models"
  18. "github.com/gogits/gogs/modules/auth"
  19. "github.com/gogits/gogs/modules/base"
  20. "github.com/gogits/gogs/modules/context"
  21. "github.com/gogits/gogs/modules/cron"
  22. "github.com/gogits/gogs/modules/log"
  23. "github.com/gogits/gogs/modules/mailer"
  24. "github.com/gogits/gogs/modules/markdown"
  25. "github.com/gogits/gogs/modules/setting"
  26. "github.com/gogits/gogs/modules/ssh"
  27. "github.com/gogits/gogs/modules/template/highlight"
  28. "github.com/gogits/gogs/modules/user"
  29. )
  30. const (
  31. INSTALL base.TplName = "install"
  32. )
  33. func checkRunMode() {
  34. switch setting.Cfg.Section("").Key("RUN_MODE").String() {
  35. case "prod":
  36. macaron.Env = macaron.PROD
  37. macaron.ColorLog = false
  38. setting.ProdMode = true
  39. default:
  40. git.Debug = true
  41. }
  42. log.Info("Run Mode: %s", strings.Title(macaron.Env))
  43. }
  44. func NewServices() {
  45. setting.NewServices()
  46. mailer.NewContext()
  47. }
  48. // GlobalInit is for global configuration reload-able.
  49. func GlobalInit() {
  50. setting.NewContext()
  51. highlight.NewContext()
  52. log.Trace("Custom path: %s", setting.CustomPath)
  53. log.Trace("Log path: %s", setting.LogRootPath)
  54. models.LoadConfigs()
  55. NewServices()
  56. if setting.InstallLock {
  57. models.LoadRepoConfig()
  58. models.NewRepoContext()
  59. if err := models.NewEngine(); err != nil {
  60. log.Fatal(4, "Fail to initialize ORM engine: %v", err)
  61. }
  62. models.HasEngine = true
  63. cron.NewContext()
  64. models.InitDeliverHooks()
  65. models.InitTestPullRequests()
  66. log.NewGitLogger(path.Join(setting.LogRootPath, "http.log"))
  67. }
  68. if models.EnableSQLite3 {
  69. log.Info("SQLite3 Supported")
  70. }
  71. if models.EnableTidb {
  72. log.Info("TiDB Supported")
  73. }
  74. if setting.SupportMiniWinService {
  75. log.Info("Builtin Windows Service Supported")
  76. }
  77. checkRunMode()
  78. if setting.SSH.StartBuiltinServer {
  79. ssh.Listen(setting.SSH.ListenPort)
  80. log.Info("SSH server started on :%v", setting.SSH.ListenPort)
  81. }
  82. // Build Sanitizer
  83. markdown.BuildSanitizer()
  84. }
  85. func InstallInit(ctx *context.Context) {
  86. if setting.InstallLock {
  87. ctx.Handle(404, "Install", errors.New("Installation is prohibited"))
  88. return
  89. }
  90. ctx.Data["Title"] = ctx.Tr("install.install")
  91. ctx.Data["PageIsInstall"] = true
  92. dbOpts := []string{"MySQL", "PostgreSQL"}
  93. if models.EnableSQLite3 {
  94. dbOpts = append(dbOpts, "SQLite3")
  95. }
  96. if models.EnableTidb {
  97. dbOpts = append(dbOpts, "TiDB")
  98. }
  99. ctx.Data["DbOptions"] = dbOpts
  100. }
  101. func Install(ctx *context.Context) {
  102. form := auth.InstallForm{}
  103. // Database settings
  104. form.DbHost = models.DbCfg.Host
  105. form.DbUser = models.DbCfg.User
  106. form.DbName = models.DbCfg.Name
  107. form.DbPath = models.DbCfg.Path
  108. ctx.Data["CurDbOption"] = "MySQL"
  109. switch models.DbCfg.Type {
  110. case "postgres":
  111. ctx.Data["CurDbOption"] = "PostgreSQL"
  112. case "sqlite3":
  113. if models.EnableSQLite3 {
  114. ctx.Data["CurDbOption"] = "SQLite3"
  115. }
  116. case "tidb":
  117. if models.EnableTidb {
  118. ctx.Data["CurDbOption"] = "TiDB"
  119. }
  120. }
  121. // Application general settings
  122. form.AppName = setting.AppName
  123. form.RepoRootPath = setting.RepoRootPath
  124. // Note(unknwon): it's hard for Windows users change a running user,
  125. // so just use current one if config says default.
  126. if setting.IsWindows && setting.RunUser == "git" {
  127. form.RunUser = user.CurrentUsername()
  128. } else {
  129. form.RunUser = setting.RunUser
  130. }
  131. form.Domain = setting.Domain
  132. form.SSHPort = setting.SSH.Port
  133. form.HTTPPort = setting.HttpPort
  134. form.AppUrl = setting.AppUrl
  135. form.LogRootPath = setting.LogRootPath
  136. // E-mail service settings
  137. if setting.MailService != nil {
  138. form.SMTPHost = setting.MailService.Host
  139. form.SMTPFrom = setting.MailService.From
  140. form.SMTPEmail = setting.MailService.User
  141. }
  142. form.RegisterConfirm = setting.Service.RegisterEmailConfirm
  143. form.MailNotify = setting.Service.EnableNotifyMail
  144. // Server and other services settings
  145. form.OfflineMode = setting.OfflineMode
  146. form.DisableGravatar = setting.DisableGravatar
  147. form.EnableFederatedAvatar = setting.EnableFederatedAvatar
  148. form.DisableRegistration = setting.Service.DisableRegistration
  149. form.EnableCaptcha = setting.Service.EnableCaptcha
  150. form.RequireSignInView = setting.Service.RequireSignInView
  151. auth.AssignForm(form, ctx.Data)
  152. ctx.HTML(200, INSTALL)
  153. }
  154. func InstallPost(ctx *context.Context, form auth.InstallForm) {
  155. ctx.Data["CurDbOption"] = form.DbType
  156. if ctx.HasError() {
  157. if ctx.HasValue("Err_SMTPEmail") {
  158. ctx.Data["Err_SMTP"] = true
  159. }
  160. if ctx.HasValue("Err_AdminName") ||
  161. ctx.HasValue("Err_AdminPasswd") ||
  162. ctx.HasValue("Err_AdminEmail") {
  163. ctx.Data["Err_Admin"] = true
  164. }
  165. ctx.HTML(200, INSTALL)
  166. return
  167. }
  168. if _, err := exec.LookPath("git"); err != nil {
  169. ctx.RenderWithErr(ctx.Tr("install.test_git_failed", err), INSTALL, &form)
  170. return
  171. }
  172. // Pass basic check, now test configuration.
  173. // Test database setting.
  174. dbTypes := map[string]string{"MySQL": "mysql", "PostgreSQL": "postgres", "SQLite3": "sqlite3", "TiDB": "tidb"}
  175. models.DbCfg.Type = dbTypes[form.DbType]
  176. models.DbCfg.Host = form.DbHost
  177. models.DbCfg.User = form.DbUser
  178. models.DbCfg.Passwd = form.DbPasswd
  179. models.DbCfg.Name = form.DbName
  180. models.DbCfg.SSLMode = form.SSLMode
  181. models.DbCfg.Path = form.DbPath
  182. if (models.DbCfg.Type == "sqlite3" || models.DbCfg.Type == "tidb") &&
  183. len(models.DbCfg.Path) == 0 {
  184. ctx.Data["Err_DbPath"] = true
  185. ctx.RenderWithErr(ctx.Tr("install.err_empty_db_path"), INSTALL, &form)
  186. return
  187. } else if models.DbCfg.Type == "tidb" &&
  188. strings.ContainsAny(path.Base(models.DbCfg.Path), ".-") {
  189. ctx.Data["Err_DbPath"] = true
  190. ctx.RenderWithErr(ctx.Tr("install.err_invalid_tidb_name"), INSTALL, &form)
  191. return
  192. }
  193. // Set test engine.
  194. var x *xorm.Engine
  195. if err := models.NewTestEngine(x); err != nil {
  196. if strings.Contains(err.Error(), `Unknown database type: sqlite3`) {
  197. ctx.Data["Err_DbType"] = true
  198. ctx.RenderWithErr(ctx.Tr("install.sqlite3_not_available", "https://gogs.io/docs/installation/install_from_binary.html"), INSTALL, &form)
  199. } else {
  200. ctx.Data["Err_DbSetting"] = true
  201. ctx.RenderWithErr(ctx.Tr("install.invalid_db_setting", err), INSTALL, &form)
  202. }
  203. return
  204. }
  205. // Test repository root path.
  206. form.RepoRootPath = strings.Replace(form.RepoRootPath, "\\", "/", -1)
  207. if err := os.MkdirAll(form.RepoRootPath, os.ModePerm); err != nil {
  208. ctx.Data["Err_RepoRootPath"] = true
  209. ctx.RenderWithErr(ctx.Tr("install.invalid_repo_path", err), INSTALL, &form)
  210. return
  211. }
  212. // Test log root path.
  213. form.LogRootPath = strings.Replace(form.LogRootPath, "\\", "/", -1)
  214. if err := os.MkdirAll(form.LogRootPath, os.ModePerm); err != nil {
  215. ctx.Data["Err_LogRootPath"] = true
  216. ctx.RenderWithErr(ctx.Tr("install.invalid_log_root_path", err), INSTALL, &form)
  217. return
  218. }
  219. currentUser, match := setting.IsRunUserMatchCurrentUser(form.RunUser)
  220. if !match {
  221. ctx.Data["Err_RunUser"] = true
  222. ctx.RenderWithErr(ctx.Tr("install.run_user_not_match", form.RunUser, currentUser), INSTALL, &form)
  223. return
  224. }
  225. // Check logic loophole between disable self-registration and no admin account.
  226. if form.DisableRegistration && len(form.AdminName) == 0 {
  227. ctx.Data["Err_Services"] = true
  228. ctx.Data["Err_Admin"] = true
  229. ctx.RenderWithErr(ctx.Tr("install.no_admin_and_disable_registration"), INSTALL, form)
  230. return
  231. }
  232. // Check admin password.
  233. if len(form.AdminName) > 0 && len(form.AdminPasswd) == 0 {
  234. ctx.Data["Err_Admin"] = true
  235. ctx.Data["Err_AdminPasswd"] = true
  236. ctx.RenderWithErr(ctx.Tr("install.err_empty_admin_password"), INSTALL, form)
  237. return
  238. }
  239. if form.AdminPasswd != form.AdminConfirmPasswd {
  240. ctx.Data["Err_Admin"] = true
  241. ctx.Data["Err_AdminPasswd"] = true
  242. ctx.RenderWithErr(ctx.Tr("form.password_not_match"), INSTALL, form)
  243. return
  244. }
  245. if form.AppUrl[len(form.AppUrl)-1] != '/' {
  246. form.AppUrl += "/"
  247. }
  248. // Save settings.
  249. cfg := ini.Empty()
  250. if com.IsFile(setting.CustomConf) {
  251. // Keeps custom settings if there is already something.
  252. if err := cfg.Append(setting.CustomConf); err != nil {
  253. log.Error(4, "Fail to load custom conf '%s': %v", setting.CustomConf, err)
  254. }
  255. }
  256. cfg.Section("database").Key("DB_TYPE").SetValue(models.DbCfg.Type)
  257. cfg.Section("database").Key("HOST").SetValue(models.DbCfg.Host)
  258. cfg.Section("database").Key("NAME").SetValue(models.DbCfg.Name)
  259. cfg.Section("database").Key("USER").SetValue(models.DbCfg.User)
  260. cfg.Section("database").Key("PASSWD").SetValue(models.DbCfg.Passwd)
  261. cfg.Section("database").Key("SSL_MODE").SetValue(models.DbCfg.SSLMode)
  262. cfg.Section("database").Key("PATH").SetValue(models.DbCfg.Path)
  263. cfg.Section("").Key("APP_NAME").SetValue(form.AppName)
  264. cfg.Section("repository").Key("ROOT").SetValue(form.RepoRootPath)
  265. cfg.Section("").Key("RUN_USER").SetValue(form.RunUser)
  266. cfg.Section("server").Key("DOMAIN").SetValue(form.Domain)
  267. cfg.Section("server").Key("HTTP_PORT").SetValue(form.HTTPPort)
  268. cfg.Section("server").Key("ROOT_URL").SetValue(form.AppUrl)
  269. if form.SSHPort == 0 {
  270. cfg.Section("server").Key("DISABLE_SSH").SetValue("true")
  271. } else {
  272. cfg.Section("server").Key("DISABLE_SSH").SetValue("false")
  273. cfg.Section("server").Key("SSH_PORT").SetValue(com.ToStr(form.SSHPort))
  274. }
  275. if len(strings.TrimSpace(form.SMTPHost)) > 0 {
  276. cfg.Section("mailer").Key("ENABLED").SetValue("true")
  277. cfg.Section("mailer").Key("HOST").SetValue(form.SMTPHost)
  278. cfg.Section("mailer").Key("FROM").SetValue(form.SMTPFrom)
  279. cfg.Section("mailer").Key("USER").SetValue(form.SMTPEmail)
  280. cfg.Section("mailer").Key("PASSWD").SetValue(form.SMTPPasswd)
  281. } else {
  282. cfg.Section("mailer").Key("ENABLED").SetValue("false")
  283. }
  284. cfg.Section("service").Key("REGISTER_EMAIL_CONFIRM").SetValue(com.ToStr(form.RegisterConfirm))
  285. cfg.Section("service").Key("ENABLE_NOTIFY_MAIL").SetValue(com.ToStr(form.MailNotify))
  286. cfg.Section("server").Key("OFFLINE_MODE").SetValue(com.ToStr(form.OfflineMode))
  287. cfg.Section("picture").Key("DISABLE_GRAVATAR").SetValue(com.ToStr(form.DisableGravatar))
  288. cfg.Section("picture").Key("ENABLE_FEDERATED_AVATAR").SetValue(com.ToStr(form.EnableFederatedAvatar))
  289. cfg.Section("service").Key("DISABLE_REGISTRATION").SetValue(com.ToStr(form.DisableRegistration))
  290. cfg.Section("service").Key("ENABLE_CAPTCHA").SetValue(com.ToStr(form.EnableCaptcha))
  291. cfg.Section("service").Key("REQUIRE_SIGNIN_VIEW").SetValue(com.ToStr(form.RequireSignInView))
  292. cfg.Section("").Key("RUN_MODE").SetValue("prod")
  293. cfg.Section("session").Key("PROVIDER").SetValue("file")
  294. cfg.Section("log").Key("MODE").SetValue("file")
  295. cfg.Section("log").Key("LEVEL").SetValue("Info")
  296. cfg.Section("log").Key("ROOT_PATH").SetValue(form.LogRootPath)
  297. cfg.Section("security").Key("INSTALL_LOCK").SetValue("true")
  298. cfg.Section("security").Key("SECRET_KEY").SetValue(base.GetRandomString(15))
  299. os.MkdirAll(filepath.Dir(setting.CustomConf), os.ModePerm)
  300. if err := cfg.SaveTo(setting.CustomConf); err != nil {
  301. ctx.RenderWithErr(ctx.Tr("install.save_config_failed", err), INSTALL, &form)
  302. return
  303. }
  304. GlobalInit()
  305. // Create admin account
  306. if len(form.AdminName) > 0 {
  307. u := &models.User{
  308. Name: form.AdminName,
  309. Email: form.AdminEmail,
  310. Passwd: form.AdminPasswd,
  311. IsAdmin: true,
  312. IsActive: true,
  313. }
  314. if err := models.CreateUser(u); err != nil {
  315. if !models.IsErrUserAlreadyExist(err) {
  316. setting.InstallLock = false
  317. ctx.Data["Err_AdminName"] = true
  318. ctx.Data["Err_AdminEmail"] = true
  319. ctx.RenderWithErr(ctx.Tr("install.invalid_admin_setting", err), INSTALL, &form)
  320. return
  321. }
  322. log.Info("Admin account already exist")
  323. u, _ = models.GetUserByName(u.Name)
  324. }
  325. // Auto-login for admin
  326. ctx.Session.Set("uid", u.ID)
  327. ctx.Session.Set("uname", u.Name)
  328. }
  329. log.Info("First-time run install finished!")
  330. ctx.Flash.Success(ctx.Tr("install.install_success"))
  331. ctx.Redirect(form.AppUrl + "user/login")
  332. }