install.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. // Copyright 2015 - Present, The Gogs Authors. All rights reserved.
  2. // Copyright 2018 - Present, Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package routes
  7. import (
  8. "gitote/gitote/models"
  9. "gitote/gitote/pkg/context"
  10. "gitote/gitote/pkg/cron"
  11. "gitote/gitote/pkg/form"
  12. "gitote/gitote/pkg/mailer"
  13. "gitote/gitote/pkg/markup"
  14. "gitote/gitote/pkg/setting"
  15. "gitote/gitote/pkg/ssh"
  16. "gitote/gitote/pkg/template/highlight"
  17. "gitote/gitote/pkg/tool"
  18. "gitote/gitote/pkg/user"
  19. "net/mail"
  20. "os"
  21. "os/exec"
  22. "path/filepath"
  23. "strings"
  24. raven "github.com/getsentry/raven-go"
  25. "github.com/go-xorm/xorm"
  26. "gitlab.com/gitote/com"
  27. "gitlab.com/gitote/git-module"
  28. log "gopkg.in/clog.v1"
  29. "gopkg.in/ini.v1"
  30. "gopkg.in/macaron.v1"
  31. )
  32. const (
  33. // InstallTPL page template
  34. InstallTPL = "install"
  35. )
  36. // checkRunMode check if it is runned or not in production
  37. func checkRunMode() {
  38. if setting.ProdMode {
  39. macaron.Env = macaron.PROD
  40. macaron.ColorLog = false
  41. } else {
  42. git.Debug = true
  43. }
  44. log.Info("Run Mode: %s", strings.Title(macaron.Env))
  45. }
  46. // NewServices initializes new services
  47. func NewServices() {
  48. setting.NewServices()
  49. mailer.NewContext()
  50. }
  51. // GlobalInit is for global configuration reload-able.
  52. func GlobalInit() {
  53. setting.NewContext()
  54. log.Trace("Custom path: %s", setting.CustomPath)
  55. log.Trace("Log path: %s", setting.LogRootPath)
  56. models.LoadConfigs()
  57. NewServices()
  58. if setting.InstallLock {
  59. highlight.NewContext()
  60. markup.NewSanitizer()
  61. if err := models.NewEngine(); err != nil {
  62. raven.CaptureErrorAndWait(err, nil)
  63. log.Fatal(2, "Fail to initialize ORM engine: %v", err)
  64. }
  65. models.HasEngine = true
  66. models.LoadAuthSources()
  67. models.LoadRepoConfig()
  68. models.NewRepoContext()
  69. // Booting long running goroutines.
  70. cron.NewContext()
  71. models.InitSyncMirrors()
  72. models.InitDeliverHooks()
  73. models.InitTestPullRequests()
  74. }
  75. if models.EnableSQLite3 {
  76. log.Info("SQLite3 Supported")
  77. }
  78. if setting.SupportMiniWinService {
  79. log.Info("Builtin Windows Service Supported")
  80. }
  81. checkRunMode()
  82. if !setting.InstallLock {
  83. return
  84. }
  85. if setting.SSH.StartBuiltinServer {
  86. ssh.Listen(setting.SSH.ListenHost, setting.SSH.ListenPort, setting.SSH.ServerCiphers)
  87. log.Info("SSH server started on %s:%v", setting.SSH.ListenHost, setting.SSH.ListenPort)
  88. log.Trace("SSH server cipher list: %v", setting.SSH.ServerCiphers)
  89. }
  90. if setting.SSH.RewriteAuthorizedKeysAtStart {
  91. if err := models.RewriteAuthorizedKeys(); err != nil {
  92. log.Warn("Failed to rewrite authorized_keys file: %v", err)
  93. }
  94. }
  95. }
  96. // InstallInit initialize the install
  97. func InstallInit(c *context.Context) {
  98. if setting.InstallLock {
  99. c.NotFound()
  100. return
  101. }
  102. c.Title("install.install")
  103. c.PageIs("Install")
  104. dbOpts := []string{"MySQL", "PostgreSQL", "MSSQL"}
  105. if models.EnableSQLite3 {
  106. dbOpts = append(dbOpts, "SQLite3")
  107. }
  108. c.Data["DbOptions"] = dbOpts
  109. }
  110. // Install shows install page
  111. func Install(c *context.Context) {
  112. f := form.Install{}
  113. // Database settings
  114. f.DbHost = models.DbCfg.Host
  115. f.DbUser = models.DbCfg.User
  116. f.DbName = models.DbCfg.Name
  117. f.DbPath = models.DbCfg.Path
  118. c.Data["CurDbOption"] = "MySQL"
  119. switch models.DbCfg.Type {
  120. case "postgres":
  121. c.Data["CurDbOption"] = "PostgreSQL"
  122. case "mssql":
  123. c.Data["CurDbOption"] = "MSSQL"
  124. case "sqlite3":
  125. if models.EnableSQLite3 {
  126. c.Data["CurDbOption"] = "SQLite3"
  127. }
  128. }
  129. // Application general settings
  130. f.RepoRootPath = setting.RepoRootPath
  131. // Note(yoginth): it's hard for Windows users change a running user,
  132. // so just use current one if config says default.
  133. if setting.IsWindows && setting.RunUser == "git" {
  134. f.RunUser = user.CurrentUsername()
  135. } else {
  136. f.RunUser = setting.RunUser
  137. }
  138. f.Domain = setting.Domain
  139. f.SSHPort = setting.SSH.Port
  140. f.UseBuiltinSSHServer = setting.SSH.StartBuiltinServer
  141. f.HTTPPort = setting.HTTPPort
  142. f.AppURL = setting.AppURL
  143. f.LogRootPath = setting.LogRootPath
  144. // E-mail service settings
  145. if setting.MailService != nil {
  146. f.SMTPHost = setting.MailService.Host
  147. f.SMTPFrom = setting.MailService.From
  148. f.SMTPUser = setting.MailService.User
  149. }
  150. f.RegisterConfirm = setting.Service.RegisterEmailConfirm
  151. f.MailNotify = setting.Service.EnableNotifyMail
  152. // Server and other services settings
  153. f.OfflineMode = setting.OfflineMode
  154. f.DisableGravatar = setting.DisableGravatar
  155. f.EnableFederatedAvatar = setting.EnableFederatedAvatar
  156. f.DisableRegistration = setting.Service.DisableRegistration
  157. f.EnableCaptcha = setting.Service.EnableCaptcha
  158. f.RequireSignInView = setting.Service.RequireSignInView
  159. form.Assign(f, c.Data)
  160. c.Success(InstallTPL)
  161. }
  162. // InstallPost install gitote
  163. func InstallPost(c *context.Context, f form.Install) {
  164. c.Data["CurDbOption"] = f.DbType
  165. if c.HasError() {
  166. if c.HasValue("Err_SMTPEmail") {
  167. c.FormErr("SMTP")
  168. }
  169. if c.HasValue("Err_AdminName") ||
  170. c.HasValue("Err_AdminPasswd") ||
  171. c.HasValue("Err_AdminEmail") {
  172. c.FormErr("Admin")
  173. }
  174. c.Success(InstallTPL)
  175. return
  176. }
  177. if _, err := exec.LookPath("git"); err != nil {
  178. c.RenderWithErr(c.Tr("install.test_git_failed", err), InstallTPL, &f)
  179. return
  180. }
  181. // Pass basic check, now test configuration.
  182. // Test database setting.
  183. dbTypes := map[string]string{"MySQL": "mysql", "PostgreSQL": "postgres", "MSSQL": "mssql", "SQLite3": "sqlite3", "TiDB": "tidb"}
  184. models.DbCfg.Type = dbTypes[f.DbType]
  185. models.DbCfg.Host = f.DbHost
  186. models.DbCfg.User = f.DbUser
  187. models.DbCfg.Passwd = f.DbPasswd
  188. models.DbCfg.Name = f.DbName
  189. models.DbCfg.SSLMode = f.SSLMode
  190. models.DbCfg.Path = f.DbPath
  191. if models.DbCfg.Type == "sqlite3" && len(models.DbCfg.Path) == 0 {
  192. c.FormErr("DbPath")
  193. c.RenderWithErr(c.Tr("install.err_empty_db_path"), InstallTPL, &f)
  194. return
  195. }
  196. // Set test engine.
  197. var x *xorm.Engine
  198. if err := models.NewTestEngine(x); err != nil {
  199. if strings.Contains(err.Error(), `Unknown database type: sqlite3`) {
  200. c.FormErr("DbType")
  201. c.RenderWithErr(c.Tr("install.sqlite3_not_available", "https://gitlab.com/gitote/docs/tree/master/installation/install_from_binary.md"), InstallTPL, &f)
  202. } else {
  203. c.FormErr("DbSetting")
  204. c.RenderWithErr(c.Tr("install.invalid_db_setting", err), InstallTPL, &f)
  205. }
  206. return
  207. }
  208. // Test repository root path.
  209. f.RepoRootPath = strings.Replace(f.RepoRootPath, "\\", "/", -1)
  210. if err := os.MkdirAll(f.RepoRootPath, os.ModePerm); err != nil {
  211. c.FormErr("RepoRootPath")
  212. c.RenderWithErr(c.Tr("install.invalid_repo_path", err), InstallTPL, &f)
  213. return
  214. }
  215. // Test log root path.
  216. f.LogRootPath = strings.Replace(f.LogRootPath, "\\", "/", -1)
  217. if err := os.MkdirAll(f.LogRootPath, os.ModePerm); err != nil {
  218. c.FormErr("LogRootPath")
  219. c.RenderWithErr(c.Tr("install.invalid_log_root_path", err), InstallTPL, &f)
  220. return
  221. }
  222. currentUser, match := setting.IsRunUserMatchCurrentUser(f.RunUser)
  223. if !match {
  224. c.FormErr("RunUser")
  225. c.RenderWithErr(c.Tr("install.run_user_not_match", f.RunUser, currentUser), InstallTPL, &f)
  226. return
  227. }
  228. // Check host address and port
  229. if len(f.SMTPHost) > 0 && !strings.Contains(f.SMTPHost, ":") {
  230. c.FormErr("SMTP", "SMTPHost")
  231. c.RenderWithErr(c.Tr("install.smtp_host_missing_port"), InstallTPL, &f)
  232. return
  233. }
  234. // Make sure FROM field is valid
  235. if len(f.SMTPFrom) > 0 {
  236. _, err := mail.ParseAddress(f.SMTPFrom)
  237. if err != nil {
  238. c.FormErr("SMTP", "SMTPFrom")
  239. c.RenderWithErr(c.Tr("install.invalid_smtp_from", err), InstallTPL, &f)
  240. return
  241. }
  242. }
  243. // Check logic loophole between disable self-registration and no admin account.
  244. if f.DisableRegistration && len(f.AdminName) == 0 {
  245. c.FormErr("Services", "Admin")
  246. c.RenderWithErr(c.Tr("install.no_admin_and_disable_registration"), InstallTPL, f)
  247. return
  248. }
  249. // Check admin password.
  250. if len(f.AdminName) > 0 && len(f.AdminPasswd) == 0 {
  251. c.FormErr("Admin", "AdminPasswd")
  252. c.RenderWithErr(c.Tr("install.err_empty_admin_password"), InstallTPL, f)
  253. return
  254. }
  255. if f.AdminPasswd != f.AdminConfirmPasswd {
  256. c.FormErr("Admin", "AdminPasswd")
  257. c.RenderWithErr(c.Tr("form.password_not_match"), InstallTPL, f)
  258. return
  259. }
  260. if f.AppURL[len(f.AppURL)-1] != '/' {
  261. f.AppURL += "/"
  262. }
  263. // Save settings.
  264. cfg := ini.Empty()
  265. if com.IsFile(setting.CustomConf) {
  266. // Keeps custom settings if there is already something.
  267. if err := cfg.Append(setting.CustomConf); err != nil {
  268. raven.CaptureErrorAndWait(err, nil)
  269. log.Error(2, "Fail to load custom conf '%s': %v", setting.CustomConf, err)
  270. }
  271. }
  272. cfg.Section("database").Key("DB_TYPE").SetValue(models.DbCfg.Type)
  273. cfg.Section("database").Key("HOST").SetValue(models.DbCfg.Host)
  274. cfg.Section("database").Key("NAME").SetValue(models.DbCfg.Name)
  275. cfg.Section("database").Key("USER").SetValue(models.DbCfg.User)
  276. cfg.Section("database").Key("PASSWD").SetValue(models.DbCfg.Passwd)
  277. cfg.Section("database").Key("SSL_MODE").SetValue(models.DbCfg.SSLMode)
  278. cfg.Section("database").Key("PATH").SetValue(models.DbCfg.Path)
  279. cfg.Section("repository").Key("ROOT").SetValue(f.RepoRootPath)
  280. cfg.Section("").Key("RUN_USER").SetValue(f.RunUser)
  281. cfg.Section("server").Key("DOMAIN").SetValue(f.Domain)
  282. cfg.Section("server").Key("HTTP_PORT").SetValue(f.HTTPPort)
  283. cfg.Section("server").Key("ROOT_URL").SetValue(f.AppURL)
  284. if f.SSHPort == 0 {
  285. cfg.Section("server").Key("DISABLE_SSH").SetValue("true")
  286. } else {
  287. cfg.Section("server").Key("DISABLE_SSH").SetValue("false")
  288. cfg.Section("server").Key("SSH_PORT").SetValue(com.ToStr(f.SSHPort))
  289. cfg.Section("server").Key("START_SSH_SERVER").SetValue(com.ToStr(f.UseBuiltinSSHServer))
  290. }
  291. if len(strings.TrimSpace(f.SMTPHost)) > 0 {
  292. cfg.Section("mailer").Key("ENABLED").SetValue("true")
  293. cfg.Section("mailer").Key("HOST").SetValue(f.SMTPHost)
  294. cfg.Section("mailer").Key("FROM").SetValue(f.SMTPFrom)
  295. cfg.Section("mailer").Key("USER").SetValue(f.SMTPUser)
  296. cfg.Section("mailer").Key("PASSWD").SetValue(f.SMTPPasswd)
  297. } else {
  298. cfg.Section("mailer").Key("ENABLED").SetValue("false")
  299. }
  300. cfg.Section("service").Key("REGISTER_EMAIL_CONFIRM").SetValue(com.ToStr(f.RegisterConfirm))
  301. cfg.Section("service").Key("ENABLE_NOTIFY_MAIL").SetValue(com.ToStr(f.MailNotify))
  302. cfg.Section("server").Key("OFFLINE_MODE").SetValue(com.ToStr(f.OfflineMode))
  303. cfg.Section("picture").Key("DISABLE_GRAVATAR").SetValue(com.ToStr(f.DisableGravatar))
  304. cfg.Section("picture").Key("ENABLE_FEDERATED_AVATAR").SetValue(com.ToStr(f.EnableFederatedAvatar))
  305. cfg.Section("service").Key("DISABLE_REGISTRATION").SetValue(com.ToStr(f.DisableRegistration))
  306. cfg.Section("service").Key("ENABLE_CAPTCHA").SetValue(com.ToStr(f.EnableCaptcha))
  307. cfg.Section("service").Key("REQUIRE_SIGNIN_VIEW").SetValue(com.ToStr(f.RequireSignInView))
  308. cfg.Section("").Key("RUN_MODE").SetValue("prod")
  309. cfg.Section("session").Key("PROVIDER").SetValue("file")
  310. mode := "file"
  311. if f.EnableConsoleMode {
  312. mode = "console, file"
  313. }
  314. cfg.Section("log").Key("MODE").SetValue(mode)
  315. cfg.Section("log").Key("LEVEL").SetValue("Info")
  316. cfg.Section("log").Key("ROOT_PATH").SetValue(f.LogRootPath)
  317. cfg.Section("security").Key("INSTALL_LOCK").SetValue("true")
  318. secretKey, err := tool.RandomString(15)
  319. if err != nil {
  320. c.RenderWithErr(c.Tr("install.secret_key_failed", err), InstallTPL, &f)
  321. return
  322. }
  323. cfg.Section("security").Key("SECRET_KEY").SetValue(secretKey)
  324. os.MkdirAll(filepath.Dir(setting.CustomConf), os.ModePerm)
  325. if err := cfg.SaveTo(setting.CustomConf); err != nil {
  326. c.RenderWithErr(c.Tr("install.save_config_failed", err), InstallTPL, &f)
  327. return
  328. }
  329. GlobalInit()
  330. // Create admin account
  331. if len(f.AdminName) > 0 {
  332. u := &models.User{
  333. Name: f.AdminName,
  334. Email: f.AdminEmail,
  335. Passwd: f.AdminPasswd,
  336. IsAdmin: true,
  337. IsActive: true,
  338. }
  339. if err := models.CreateUser(u); err != nil {
  340. if !models.IsErrUserAlreadyExist(err) {
  341. setting.InstallLock = false
  342. c.FormErr("AdminName", "AdminEmail")
  343. c.RenderWithErr(c.Tr("install.invalid_admin_setting", err), InstallTPL, &f)
  344. return
  345. }
  346. log.Info("Admin account already exist")
  347. u, _ = models.GetUserByName(u.Name)
  348. }
  349. // Auto-login for admin
  350. c.Session.Set("uid", u.ID)
  351. c.Session.Set("uname", u.Name)
  352. }
  353. log.Info("First-time run install finished!")
  354. c.Flash.Success(c.Tr("install.install_success"))
  355. c.Redirect(f.AppURL + "login")
  356. }