Add gogs fix location command
This commit is contained in:
parent
f160b4f33c
commit
a3e1383cac
|
@ -5,7 +5,7 @@ Gogs(Go Git Service) is a Self Hosted Git Service in the Go Programming Language
|
||||||
|
|
||||||
![Demo](http://gowalker.org/public/gogs_demo.gif)
|
![Demo](http://gowalker.org/public/gogs_demo.gif)
|
||||||
|
|
||||||
##### Current version: 0.4.2 Alpha
|
##### Current version: 0.4.3 Alpha
|
||||||
|
|
||||||
### NOTICES
|
### NOTICES
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@ Gogs(Go Git Service) 是一个由 Go 语言编写的自助 Git 托管服务。
|
||||||
|
|
||||||
![Demo](http://gowalker.org/public/gogs_demo.gif)
|
![Demo](http://gowalker.org/public/gogs_demo.gif)
|
||||||
|
|
||||||
##### 当前版本:0.4.2 Alpha
|
##### 当前版本:0.4.3 Alpha
|
||||||
|
|
||||||
## 开发目的
|
## 开发目的
|
||||||
|
|
||||||
|
|
170
cmd/fix.go
170
cmd/fix.go
|
@ -5,10 +5,16 @@
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/codegangsta/cli"
|
"github.com/codegangsta/cli"
|
||||||
|
|
||||||
"github.com/gogits/gogs/models"
|
"github.com/gogits/gogs/models"
|
||||||
"github.com/gogits/gogs/modules/setting"
|
"github.com/gogits/gogs/modules/setting"
|
||||||
)
|
)
|
||||||
|
@ -16,28 +22,152 @@ import (
|
||||||
var CmdFix = cli.Command{
|
var CmdFix = cli.Command{
|
||||||
Name: "fix",
|
Name: "fix",
|
||||||
Usage: "This command for upgrade from old version",
|
Usage: "This command for upgrade from old version",
|
||||||
Description: `Fix provide upgrade from old version`,
|
|
||||||
Action: runFix,
|
Action: runFix,
|
||||||
|
Subcommands: fixCommands,
|
||||||
Flags: []cli.Flag{},
|
Flags: []cli.Flag{},
|
||||||
}
|
}
|
||||||
|
|
||||||
func runFix(k *cli.Context) {
|
func runFix(ctx *cli.Context) {
|
||||||
workDir, _ := setting.WorkDir()
|
}
|
||||||
newLogger(workDir)
|
|
||||||
|
var fixCommands = []cli.Command{
|
||||||
setting.NewConfigContext()
|
{
|
||||||
models.LoadModelsConfig()
|
Name: "location",
|
||||||
|
Usage: "Change Gogs app location",
|
||||||
if models.UseSQLite3 {
|
Description: `Command location fixes location change of Gogs
|
||||||
os.Chdir(workDir)
|
|
||||||
}
|
gogs fix location <old Gogs path>
|
||||||
|
`,
|
||||||
models.SetEngine()
|
Action: runFixLocation,
|
||||||
|
},
|
||||||
err := models.Fix()
|
}
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
// rewriteAuthorizedKeys replaces old Gogs path to the new one.
|
||||||
} else {
|
func rewriteAuthorizedKeys(sshPath, oldPath, newPath string) error {
|
||||||
fmt.Println("Fix successfully!")
|
fr, err := os.Open(sshPath)
|
||||||
}
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer fr.Close()
|
||||||
|
|
||||||
|
tmpPath := sshPath + ".tmp"
|
||||||
|
fw, err := os.Create(tmpPath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer fw.Close()
|
||||||
|
|
||||||
|
oldPath = "command=\"" + oldPath + " serv"
|
||||||
|
newPath = "command=\"" + newPath + " serv"
|
||||||
|
buf := bufio.NewReader(fr)
|
||||||
|
for {
|
||||||
|
line, errRead := buf.ReadString('\n')
|
||||||
|
line = strings.TrimSpace(line)
|
||||||
|
|
||||||
|
if errRead != nil {
|
||||||
|
if errRead != io.EOF {
|
||||||
|
return errRead
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reached end of file, if nothing to read then break,
|
||||||
|
// otherwise handle the last line.
|
||||||
|
if len(line) == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Still finding the line, copy the line that currently read.
|
||||||
|
if _, err = fw.WriteString(strings.Replace(line, oldPath, newPath, 1) + "\n"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if errRead == io.EOF {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = os.Remove(sshPath); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return os.Rename(tmpPath, sshPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
func rewriteUpdateHook(path, appPath string) error {
|
||||||
|
rp := strings.NewReplacer("\\", "/", " ", "\\ ")
|
||||||
|
if err := ioutil.WriteFile(path, []byte(fmt.Sprintf(models.TPL_UPDATE_HOOK,
|
||||||
|
setting.ScriptType, rp.Replace(appPath))), os.ModePerm); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func walkDir(rootPath, recPath, appPath string, depth int) error {
|
||||||
|
depth++
|
||||||
|
if depth > 3 {
|
||||||
|
return nil
|
||||||
|
} else if depth == 3 {
|
||||||
|
if err := rewriteUpdateHook(path.Join(rootPath, "hooks/update"), appPath); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dir, err := os.Open(rootPath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer dir.Close()
|
||||||
|
|
||||||
|
fis, err := dir.Readdir(0)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, fi := range fis {
|
||||||
|
if strings.Contains(fi.Name(), ".DS_Store") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
relPath := path.Join(recPath, fi.Name())
|
||||||
|
curPath := path.Join(rootPath, fi.Name())
|
||||||
|
if fi.IsDir() {
|
||||||
|
if err = walkDir(curPath, relPath, appPath, depth); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func runFixLocation(ctx *cli.Context) {
|
||||||
|
if len(ctx.Args()) != 1 {
|
||||||
|
fmt.Println("Incorrect arguments number, expect 1")
|
||||||
|
os.Exit(2)
|
||||||
|
}
|
||||||
|
|
||||||
|
execPath, _ := setting.ExecPath()
|
||||||
|
|
||||||
|
oldPath := ctx.Args().First()
|
||||||
|
fmt.Printf("Old location: %s\n", oldPath)
|
||||||
|
fmt.Println("This command should be executed in the new Gogs path")
|
||||||
|
fmt.Printf("Do you want to change Gogs app path from old location to:\n")
|
||||||
|
fmt.Printf("-> %s?\n", execPath)
|
||||||
|
fmt.Print("Press <enter> to continue, use <Ctrl+c> to exit.")
|
||||||
|
fmt.Scanln()
|
||||||
|
|
||||||
|
// Fix in authorized_keys file.
|
||||||
|
sshPath := path.Join(models.SshPath, "authorized_keys")
|
||||||
|
fmt.Printf("Fixing pathes in file: %s\n", sshPath)
|
||||||
|
if err := rewriteAuthorizedKeys(sshPath, oldPath, execPath); err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fix position in gogs-repositories.
|
||||||
|
setting.NewConfigContext()
|
||||||
|
fmt.Printf("Fixing pathes in repositories: %s\n", setting.RepoRootPath)
|
||||||
|
if err := walkDir(setting.RepoRootPath, "", execPath, 0); err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
fmt.Println("Fix position finished!")
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,6 @@ func newLogger(logPath string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
qlog.SetOutput(f)
|
qlog.SetOutput(f)
|
||||||
//qlog.SetOutputLevel(qlog.Ldebug)
|
|
||||||
qlog.Info("Start logging serv...")
|
qlog.Info("Start logging serv...")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
6
gogs.go
6
gogs.go
|
@ -17,7 +17,7 @@ import (
|
||||||
"github.com/gogits/gogs/modules/setting"
|
"github.com/gogits/gogs/modules/setting"
|
||||||
)
|
)
|
||||||
|
|
||||||
const APP_VER = "0.4.2.0608 Alpha"
|
const APP_VER = "0.4.3.0610 Alpha"
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
runtime.GOMAXPROCS(runtime.NumCPU())
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
||||||
|
@ -31,10 +31,10 @@ func main() {
|
||||||
app.Version = APP_VER
|
app.Version = APP_VER
|
||||||
app.Commands = []cli.Command{
|
app.Commands = []cli.Command{
|
||||||
cmd.CmdWeb,
|
cmd.CmdWeb,
|
||||||
// cmd.CmdFix,
|
|
||||||
cmd.CmdDump,
|
|
||||||
cmd.CmdServ,
|
cmd.CmdServ,
|
||||||
cmd.CmdUpdate,
|
cmd.CmdUpdate,
|
||||||
|
cmd.CmdFix,
|
||||||
|
cmd.CmdDump,
|
||||||
}
|
}
|
||||||
app.Flags = append(app.Flags, []cli.Flag{}...)
|
app.Flags = append(app.Flags, []cli.Flag{}...)
|
||||||
app.Run(os.Args)
|
app.Run(os.Args)
|
||||||
|
|
|
@ -46,7 +46,9 @@ func LoadModelsConfig() {
|
||||||
DbCfg.Host = setting.Cfg.MustValue("database", "HOST")
|
DbCfg.Host = setting.Cfg.MustValue("database", "HOST")
|
||||||
DbCfg.Name = setting.Cfg.MustValue("database", "NAME")
|
DbCfg.Name = setting.Cfg.MustValue("database", "NAME")
|
||||||
DbCfg.User = setting.Cfg.MustValue("database", "USER")
|
DbCfg.User = setting.Cfg.MustValue("database", "USER")
|
||||||
DbCfg.Pwd = setting.Cfg.MustValue("database", "PASSWD")
|
if len(DbCfg.Pwd) == 0 {
|
||||||
|
DbCfg.Pwd = setting.Cfg.MustValue("database", "PASSWD")
|
||||||
|
}
|
||||||
DbCfg.SslMode = setting.Cfg.MustValue("database", "SSL_MODE")
|
DbCfg.SslMode = setting.Cfg.MustValue("database", "SSL_MODE")
|
||||||
DbCfg.Path = setting.Cfg.MustValue("database", "PATH", "data/gogs.db")
|
DbCfg.Path = setting.Cfg.MustValue("database", "PATH", "data/gogs.db")
|
||||||
}
|
}
|
||||||
|
@ -67,7 +69,6 @@ func NewTestEngine(x *xorm.Engine) (err error) {
|
||||||
}
|
}
|
||||||
cnnstr := fmt.Sprintf("user=%s password=%s host=%s port=%s dbname=%s sslmode=%s",
|
cnnstr := fmt.Sprintf("user=%s password=%s host=%s port=%s dbname=%s sslmode=%s",
|
||||||
DbCfg.User, DbCfg.Pwd, host, port, DbCfg.Name, DbCfg.SslMode)
|
DbCfg.User, DbCfg.Pwd, host, port, DbCfg.Name, DbCfg.SslMode)
|
||||||
//fmt.Println(cnnstr)
|
|
||||||
x, err = xorm.NewEngine("postgres", cnnstr)
|
x, err = xorm.NewEngine("postgres", cnnstr)
|
||||||
case "sqlite3":
|
case "sqlite3":
|
||||||
if !EnableSQLite3 {
|
if !EnableSQLite3 {
|
||||||
|
|
|
@ -37,7 +37,7 @@ var (
|
||||||
var sshOpLocker = sync.Mutex{}
|
var sshOpLocker = sync.Mutex{}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
sshPath string // SSH directory.
|
SshPath string // SSH directory.
|
||||||
appPath string // Execution(binary) path.
|
appPath string // Execution(binary) path.
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -67,9 +67,9 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine and create .ssh path.
|
// Determine and create .ssh path.
|
||||||
sshPath = filepath.Join(homeDir(), ".ssh")
|
SshPath = filepath.Join(homeDir(), ".ssh")
|
||||||
if err = os.MkdirAll(sshPath, os.ModePerm); err != nil {
|
if err = os.MkdirAll(SshPath, os.ModePerm); err != nil {
|
||||||
qlog.Fatalf("publickey.init(fail to create sshPath(%s)): %v\n", sshPath, err)
|
qlog.Fatalf("publickey.init(fail to create SshPath(%s)): %v\n", SshPath, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,7 +94,7 @@ func saveAuthorizedKeyFile(key *PublicKey) error {
|
||||||
sshOpLocker.Lock()
|
sshOpLocker.Lock()
|
||||||
defer sshOpLocker.Unlock()
|
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)
|
f, err := os.OpenFile(fpath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -216,8 +216,8 @@ func DeletePublicKey(key *PublicKey) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
fpath := filepath.Join(sshPath, "authorized_keys")
|
fpath := filepath.Join(SshPath, "authorized_keys")
|
||||||
tmpPath := filepath.Join(sshPath, "authorized_keys.tmp")
|
tmpPath := filepath.Join(SshPath, "authorized_keys.tmp")
|
||||||
log.Trace("publickey.DeletePublicKey(authorized_keys): %s", fpath)
|
log.Trace("publickey.DeletePublicKey(authorized_keys): %s", fpath)
|
||||||
|
|
||||||
if err = rewriteAuthorizedKeys(key, fpath, tmpPath); err != nil {
|
if err = rewriteAuthorizedKeys(key, fpath, tmpPath); err != nil {
|
||||||
|
|
|
@ -28,6 +28,10 @@ import (
|
||||||
"github.com/gogits/gogs/modules/setting"
|
"github.com/gogits/gogs/modules/setting"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
TPL_UPDATE_HOOK = "#!/usr/bin/env %s\n%s update $1 $2 $3\n"
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrRepoAlreadyExist = errors.New("Repository already exist")
|
ErrRepoAlreadyExist = errors.New("Repository already exist")
|
||||||
ErrRepoNotExist = errors.New("Repository does not exist")
|
ErrRepoNotExist = errors.New("Repository does not exist")
|
||||||
|
@ -450,7 +454,7 @@ func initRepository(f string, user *User, repo *Repository, initReadme bool, rep
|
||||||
rp := strings.NewReplacer("\\", "/", " ", "\\ ")
|
rp := strings.NewReplacer("\\", "/", " ", "\\ ")
|
||||||
// hook/post-update
|
// hook/post-update
|
||||||
if err := createHookUpdate(filepath.Join(repoPath, "hooks", "update"),
|
if err := createHookUpdate(filepath.Join(repoPath, "hooks", "update"),
|
||||||
fmt.Sprintf("#!/usr/bin/env %s\n%s update $1 $2 $3\n", setting.ScriptType,
|
fmt.Sprintf(TPL_UPDATE_HOOK, setting.ScriptType,
|
||||||
rp.Replace(appPath))); err != nil {
|
rp.Replace(appPath))); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -90,8 +90,7 @@ var (
|
||||||
RunUser string
|
RunUser string
|
||||||
)
|
)
|
||||||
|
|
||||||
// WorkDir returns absolute path of work directory.
|
func ExecPath() (string, error) {
|
||||||
func WorkDir() (string, error) {
|
|
||||||
file, err := exec.LookPath(os.Args[0])
|
file, err := exec.LookPath(os.Args[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
@ -100,7 +99,13 @@ func WorkDir() (string, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return path.Dir(strings.Replace(p, "\\", "/", -1)), nil
|
return p, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// WorkDir returns absolute path of work directory.
|
||||||
|
func WorkDir() (string, error) {
|
||||||
|
execPath, err := ExecPath()
|
||||||
|
return path.Dir(strings.Replace(execPath, "\\", "/", -1)), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewConfigContext initializes configuration context.
|
// NewConfigContext initializes configuration context.
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
0.4.2.0608 Alpha
|
0.4.3.0610 Alpha
|
Loading…
Reference in New Issue