From b293b6eaa6b305bbac2147f76d5722607e8aa04b Mon Sep 17 00:00:00 2001 From: Unknwon Date: Sun, 1 Feb 2015 12:41:03 -0500 Subject: cmd: CMD option for port number of `gogs web` to prevent first time run conflict - routers: use new binding convention to simplify code - templates: able to set HTTP port number in install page --- cmd/web.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'cmd') diff --git a/cmd/web.go b/cmd/web.go index 241abf2c9c..e6fb2925cf 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -53,7 +53,9 @@ var CmdWeb = cli.Command{ Description: `Gogs web server is the only thing you need to run, and it takes care of all the other things for you`, Action: runWeb, - Flags: []cli.Flag{}, + Flags: []cli.Flag{ + cli.StringFlag{"port, p", "3000", "Temporary port number to prevent conflict", ""}, + }, } type VerChecker struct { @@ -162,7 +164,7 @@ func newMacaron() *macaron.Macaron { return m } -func runWeb(*cli.Context) { +func runWeb(ctx *cli.Context) { routers.GlobalInit() checkVersion() @@ -179,9 +181,9 @@ func runWeb(*cli.Context) { // Routers. m.Get("/", ignSignIn, routers.Home) m.Get("/explore", ignSignIn, routers.Explore) - // FIXME: when i'm binding form here??? - m.Get("/install", bindIgnErr(auth.InstallForm{}), routers.Install) - m.Post("/install", bindIgnErr(auth.InstallForm{}), routers.InstallPost) + m.Combo("/install", routers.InstallInit). + Get(routers.Install). + Post(bindIgnErr(auth.InstallForm{}), routers.InstallPost) m.Group("", func() { m.Get("/pulls", user.Pulls) m.Get("/issues", user.Issues) @@ -460,6 +462,12 @@ func runWeb(*cli.Context) { // Not found handler. m.NotFound(routers.NotFound) + // Flag for port number in case first time run conflict. + if ctx.IsSet("port") { + setting.AppUrl = strings.Replace(setting.AppUrl, setting.HttpPort, ctx.String("port"), 1) + setting.HttpPort = ctx.String("port") + } + var err error listenAddr := fmt.Sprintf("%s:%s", setting.HttpAddr, setting.HttpPort) log.Info("Listen: %v://%s%s", setting.Protocol, listenAddr, setting.AppSubUrl) -- cgit v1.2.3 From 89ea3e1acce645460980a639b440c6a139f8e10a Mon Sep 17 00:00:00 2001 From: Unknwon Date: Sun, 1 Feb 2015 14:39:58 -0500 Subject: routers: save partial config when install --- cmd/web.go | 4 ++-- routers/install.go | 52 +++++++++++++++++++++++++++------------------------- 2 files changed, 29 insertions(+), 27 deletions(-) (limited to 'cmd') diff --git a/cmd/web.go b/cmd/web.go index e6fb2925cf..8e75e8becb 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -77,13 +77,13 @@ func checkVersion() { // Check dependency version. checkers := []VerChecker{ - {"github.com/Unknwon/macaron", macaron.Version, "0.5.0"}, + {"github.com/Unknwon/macaron", macaron.Version, "0.5.1"}, {"github.com/macaron-contrib/binding", binding.Version, "0.0.4"}, {"github.com/macaron-contrib/cache", cache.Version, "0.0.7"}, {"github.com/macaron-contrib/csrf", csrf.Version, "0.0.1"}, {"github.com/macaron-contrib/i18n", i18n.Version, "0.0.5"}, {"github.com/macaron-contrib/session", session.Version, "0.1.6"}, - {"gopkg.in/ini.v1", ini.Version, "1.0.1"}, + {"gopkg.in/ini.v1", ini.Version, "1.2.0"}, } for _, c := range checkers { ver := strings.Join(strings.Split(c.Version(), ".")[:3], ".") diff --git a/routers/install.go b/routers/install.go index 58e6cf664b..9c3f134d45 100644 --- a/routers/install.go +++ b/routers/install.go @@ -14,6 +14,7 @@ import ( "github.com/Unknwon/com" "github.com/Unknwon/macaron" "github.com/go-xorm/xorm" + "gopkg.in/ini.v1" "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/auth" @@ -186,41 +187,42 @@ func InstallPost(ctx *middleware.Context, form auth.InstallForm) { } // Save settings. - setting.Cfg.Section("database").Key("DB_TYPE").SetValue(models.DbCfg.Type) - setting.Cfg.Section("database").Key("HOST").SetValue(models.DbCfg.Host) - setting.Cfg.Section("database").Key("NAME").SetValue(models.DbCfg.Name) - setting.Cfg.Section("database").Key("USER").SetValue(models.DbCfg.User) - setting.Cfg.Section("database").Key("PASSWD").SetValue(models.DbCfg.Passwd) - setting.Cfg.Section("database").Key("SSL_MODE").SetValue(models.DbCfg.SSLMode) - setting.Cfg.Section("database").Key("PATH").SetValue(models.DbCfg.Path) - - setting.Cfg.Section("repository").Key("ROOT").SetValue(form.RepoRootPath) - setting.Cfg.Section("").Key("RUN_USER").SetValue(form.RunUser) - setting.Cfg.Section("server").Key("DOMAIN").SetValue(form.Domain) - setting.Cfg.Section("server").Key("HTTP_PORT").SetValue(form.HTTPPort) - setting.Cfg.Section("server").Key("ROOT_URL").SetValue(form.AppUrl) + cfg := ini.Empty() + cfg.Section("database").Key("DB_TYPE").SetValue(models.DbCfg.Type) + cfg.Section("database").Key("HOST").SetValue(models.DbCfg.Host) + cfg.Section("database").Key("NAME").SetValue(models.DbCfg.Name) + cfg.Section("database").Key("USER").SetValue(models.DbCfg.User) + cfg.Section("database").Key("PASSWD").SetValue(models.DbCfg.Passwd) + cfg.Section("database").Key("SSL_MODE").SetValue(models.DbCfg.SSLMode) + cfg.Section("database").Key("PATH").SetValue(models.DbCfg.Path) + + cfg.Section("repository").Key("ROOT").SetValue(form.RepoRootPath) + cfg.Section("").Key("RUN_USER").SetValue(form.RunUser) + cfg.Section("server").Key("DOMAIN").SetValue(form.Domain) + cfg.Section("server").Key("HTTP_PORT").SetValue(form.HTTPPort) + cfg.Section("server").Key("ROOT_URL").SetValue(form.AppUrl) if len(strings.TrimSpace(form.SMTPHost)) > 0 { - setting.Cfg.Section("mailer").Key("ENABLED").SetValue("true") - setting.Cfg.Section("mailer").Key("HOST").SetValue(form.SMTPHost) - setting.Cfg.Section("mailer").Key("USER").SetValue(form.SMTPEmail) - setting.Cfg.Section("mailer").Key("PASSWD").SetValue(form.SMTPPasswd) + cfg.Section("mailer").Key("ENABLED").SetValue("true") + cfg.Section("mailer").Key("HOST").SetValue(form.SMTPHost) + cfg.Section("mailer").Key("USER").SetValue(form.SMTPEmail) + cfg.Section("mailer").Key("PASSWD").SetValue(form.SMTPPasswd) - setting.Cfg.Section("service").Key("REGISTER_EMAIL_CONFIRM").SetValue(com.ToStr(form.RegisterConfirm == "on")) - setting.Cfg.Section("service").Key("ENABLE_NOTIFY_MAIL").SetValue(com.ToStr(form.MailNotify == "on")) + cfg.Section("service").Key("REGISTER_EMAIL_CONFIRM").SetValue(com.ToStr(form.RegisterConfirm == "on")) + cfg.Section("service").Key("ENABLE_NOTIFY_MAIL").SetValue(com.ToStr(form.MailNotify == "on")) } - setting.Cfg.Section("").Key("RUN_MODE").SetValue("prod") + cfg.Section("").Key("RUN_MODE").SetValue("prod") - setting.Cfg.Section("session").Key("PROVIDER").SetValue("file") + cfg.Section("session").Key("PROVIDER").SetValue("file") - setting.Cfg.Section("log").Key("MODE").SetValue("file") + cfg.Section("log").Key("MODE").SetValue("file") - setting.Cfg.Section("security").Key("INSTALL_LOCK").SetValue("true") - setting.Cfg.Section("security").Key("SECRET_KEY").SetValue(base.GetRandomString(15)) + cfg.Section("security").Key("INSTALL_LOCK").SetValue("true") + cfg.Section("security").Key("SECRET_KEY").SetValue(base.GetRandomString(15)) os.MkdirAll("custom/conf", os.ModePerm) - if err := setting.Cfg.SaveTo(path.Join(setting.CustomPath, "conf/app.ini")); err != nil { + if err := cfg.SaveTo(path.Join(setting.CustomPath, "conf/app.ini")); err != nil { ctx.RenderWithErr(ctx.Tr("install.save_config_failed", err), INSTALL, &form) return } -- cgit v1.2.3 From 0a2068d7fbd447de1135d3b16e9f26de5dd18d56 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Sun, 1 Feb 2015 17:21:56 -0500 Subject: models: code fix on #818 --- cmd/fix.go | 2 +- conf/locale/locale_en-US.ini | 4 ++-- models/publickey.go | 52 +++++++++++++++++++++++++++++--------------- 3 files changed, 37 insertions(+), 21 deletions(-) (limited to 'cmd') diff --git a/cmd/fix.go b/cmd/fix.go index 5122ff32c7..eff85d6282 100644 --- a/cmd/fix.go +++ b/cmd/fix.go @@ -164,7 +164,7 @@ func runFixLocation(ctx *cli.Context) { fmt.Scanln() // Fix in authorized_keys file. - sshPath := path.Join(models.SshPath, "authorized_keys") + sshPath := path.Join(models.SSHPath, "authorized_keys") if com.IsFile(sshPath) { fmt.Printf("Fixing pathes in file: %s\n", sshPath) if err := rewriteAuthorizedKeys(sshPath, oldPath, execPath); err != nil { diff --git a/conf/locale/locale_en-US.ini b/conf/locale/locale_en-US.ini index c0979d1bdb..660fb253bb 100644 --- a/conf/locale/locale_en-US.ini +++ b/conf/locale/locale_en-US.ini @@ -514,8 +514,8 @@ dashboard.delete_repo_archives = Delete all repositories archives dashboard.delete_repo_archives_success = All repositories archives have been deleted successfully. dashboard.git_gc_repos = Do garbage collection on repositories dashboard.git_gc_repos_success = All repositories have done garbage collection successfully. -dashboard.resync_all_sshkeys = Do resync .ssh/autorized_key file -dashboard.resync_all_sshkeys_success = All keys are synced again. +dashboard.resync_all_sshkeys = Rewrite '.ssh/autorized_key' file(caution: non-Gogs keys will be lost) +dashboard.resync_all_sshkeys_success = All public keys have been rewritten successfully. dashboard.server_uptime = Server Uptime dashboard.current_goroutine = Current Goroutines dashboard.current_memory_usage = Current Memory Usage diff --git a/models/publickey.go b/models/publickey.go index 41233d0c39..67ab4242f2 100644 --- a/models/publickey.go +++ b/models/publickey.go @@ -33,7 +33,7 @@ const ( ) var ( - ErrKeyAlreadyExist = errors.New("Public key already exist") + ErrKeyAlreadyExist = errors.New("Public key already exists") ErrKeyNotExist = errors.New("Public key does not exist") ErrKeyUnableVerify = errors.New("Unable to verify public key") ) @@ -41,7 +41,7 @@ var ( var sshOpLocker = sync.Mutex{} var ( - SshPath string // SSH directory. + SSHPath string // SSH directory. appPath string // Execution(binary) path. ) @@ -72,9 +72,9 @@ func init() { appPath = strings.Replace(appPath, "\\", "/", -1) // Determine and create .ssh path. - SshPath = filepath.Join(homeDir(), ".ssh") - if err = os.MkdirAll(SshPath, 0700); err != nil { - log.Fatal(4, "fail to create SshPath(%s): %v\n", SshPath, err) + SSHPath = filepath.Join(homeDir(), ".ssh") + if err = os.MkdirAll(SSHPath, 0700); err != nil { + log.Fatal(4, "fail to create '%s': %v", SSHPath, err) } } @@ -248,12 +248,13 @@ func saveAuthorizedKeyFile(keys ...*PublicKey) error { sshOpLocker.Lock() defer sshOpLocker.Unlock() - fpath := filepath.Join(SshPath, "authorized_keys") + fpath := filepath.Join(SSHPath, "authorized_keys") f, err := os.OpenFile(fpath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600) if err != nil { return err } defer f.Close() + finfo, err := f.Stat() if err != nil { return err @@ -270,8 +271,7 @@ func saveAuthorizedKeyFile(keys ...*PublicKey) error { } for _, key := range keys { - _, err = f.WriteString(key.GetAuthorizedString()) - if err != nil { + if _, err = f.WriteString(key.GetAuthorizedString()); err != nil { return err } } @@ -418,8 +418,8 @@ func DeletePublicKey(key *PublicKey) error { return err } - fpath := filepath.Join(SshPath, "authorized_keys") - tmpPath := filepath.Join(SshPath, "authorized_keys.tmp") + fpath := filepath.Join(SSHPath, "authorized_keys") + tmpPath := filepath.Join(SSHPath, "authorized_keys.tmp") if err = rewriteAuthorizedKeys(key, fpath, tmpPath); err != nil { return err } else if err = os.Remove(fpath); err != nil { @@ -428,20 +428,36 @@ func DeletePublicKey(key *PublicKey) error { return os.Rename(tmpPath, fpath) } -// RewriteAllPublicKeys remove any authorized key and re-write all key from database again +// RewriteAllPublicKeys removes any authorized key and rewrite all keys from database again. func RewriteAllPublicKeys() error { - keys := make([]*PublicKey, 0, 5) - err := x.Find(&keys) + sshOpLocker.Lock() + defer sshOpLocker.Unlock() + + tmpPath := filepath.Join(SSHPath, "authorized_keys.tmp") + f, err := os.Create(tmpPath) if err != nil { return err } + defer os.Remove(tmpPath) - fpath := filepath.Join(SshPath, "authorized_keys") - if _, err := os.Stat(fpath); os.IsNotExist(err) { - return saveAuthorizedKeyFile(keys...) + err = x.Iterate(new(PublicKey), func(idx int, bean interface{}) (err error) { + _, err = f.WriteString((bean.(*PublicKey)).GetAuthorizedString()) + return err + }) + f.Close() + if err != nil { + return err } - if err := os.Remove(fpath); err != nil { + + fpath := filepath.Join(SSHPath, "authorized_keys") + if com.IsExist(fpath) { + if err = os.Remove(fpath); err != nil { + return err + } + } + if err = os.Rename(tmpPath, fpath); err != nil { return err } - return saveAuthorizedKeyFile(keys...) + + return nil } -- cgit v1.2.3 From 4dfffee9fb716672c7170013a4f69ca759f2dd51 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Wed, 4 Feb 2015 19:14:09 -0500 Subject: cmd: update csrf version requirement --- cmd/web.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'cmd') diff --git a/cmd/web.go b/cmd/web.go index 8e75e8becb..55b6bf0874 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -80,7 +80,7 @@ func checkVersion() { {"github.com/Unknwon/macaron", macaron.Version, "0.5.1"}, {"github.com/macaron-contrib/binding", binding.Version, "0.0.4"}, {"github.com/macaron-contrib/cache", cache.Version, "0.0.7"}, - {"github.com/macaron-contrib/csrf", csrf.Version, "0.0.1"}, + {"github.com/macaron-contrib/csrf", csrf.Version, "0.0.3"}, {"github.com/macaron-contrib/i18n", i18n.Version, "0.0.5"}, {"github.com/macaron-contrib/session", session.Version, "0.1.6"}, {"gopkg.in/ini.v1", ini.Version, "1.2.0"}, -- cgit v1.2.3