install.go 12 KB

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