From f147ad619a68440ef6c30d797a8217e687e8d51d Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 19 Jun 2014 01:08:03 -0400 Subject: [PATCH] Basic process manager --- cmd/serve.go | 6 +- gogs.go | 2 +- models/fix.go | 6 - models/git_diff.go | 13 +- models/publickey.go | 3 +- models/repo.go | 268 +++++++++++++++------------ modules/process/manager.go | 89 +++++++++ routers/admin/admin.go | 5 +- templates/VERSION | 2 +- templates/admin/monitor/process.tmpl | 38 ++++ 10 files changed, 289 insertions(+), 143 deletions(-) delete mode 100644 models/fix.go create mode 100644 modules/process/manager.go create mode 100644 templates/admin/monitor/process.tmpl diff --git a/cmd/serve.go b/cmd/serve.go index 3a17bf9f9..a21500c77 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -32,7 +32,7 @@ func newLogger(logPath string) { f, err := os.OpenFile(logPath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, os.ModePerm) if err != nil { - qlog.Fatal(err) + qlog.Fatal("Fail to open log file(%s): %v", logPath, err) } qlog.SetOutput(f) @@ -185,8 +185,8 @@ func runServ(k *cli.Context) { gitcmd.Stdout = os.Stdout gitcmd.Stdin = os.Stdin gitcmd.Stderr = os.Stderr - - if err = gitcmd.Run(); err != nil { + err = gitcmd.Run() + if err != nil { println("Gogs: internal error:", err) qlog.Fatalf("Fail to execute git command: %v", err) } diff --git a/gogs.go b/gogs.go index b4129e90f..7e1459e54 100644 --- a/gogs.go +++ b/gogs.go @@ -17,7 +17,7 @@ import ( "github.com/gogits/gogs/modules/setting" ) -const APP_VER = "0.4.4.0613 Alpha" +const APP_VER = "0.4.4.0619 Alpha" func init() { runtime.GOMAXPROCS(runtime.NumCPU()) diff --git a/models/fix.go b/models/fix.go deleted file mode 100644 index 9fc141bd2..000000000 --- a/models/fix.go +++ /dev/null @@ -1,6 +0,0 @@ -package models - -func Fix() error { - _, err := orm.Exec("alter table repository drop column num_releases") - return err -} diff --git a/models/git_diff.go b/models/git_diff.go index 5b5a46a12..ed114b750 100644 --- a/models/git_diff.go +++ b/models/git_diff.go @@ -6,6 +6,7 @@ package models import ( "bufio" + "fmt" "io" "os" "os/exec" @@ -15,6 +16,7 @@ import ( "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/log" + "github.com/gogits/gogs/modules/process" ) // Diff line types. @@ -67,7 +69,7 @@ func (diff *Diff) NumFiles() int { const DIFF_HEAD = "diff --git " -func ParsePatch(cmd *exec.Cmd, reader io.Reader) (*Diff, error) { +func ParsePatch(pid int64, cmd *exec.Cmd, reader io.Reader) (*Diff, error) { scanner := bufio.NewScanner(reader) var ( curFile *DiffFile @@ -169,11 +171,8 @@ func ParsePatch(cmd *exec.Cmd, reader io.Reader) (*Diff, error) { } // In case process became zombie. - if !cmd.ProcessState.Exited() { - log.Debug("git_diff.ParsePatch: process doesn't exit and now will be killed") - if err := cmd.Process.Kill(); err != nil { - log.Error("git_diff.ParsePatch: fail to kill zombie process: %v", err) - } + if err := process.Kill(pid); err != nil { + log.Error("git_diff.ParsePatch(Kill): %v", err) } return diff, nil } @@ -207,5 +206,5 @@ func GetDiff(repoPath, commitid string) (*Diff, error) { wr.Close() }() defer rd.Close() - return ParsePatch(cmd, rd) + return ParsePatch(process.Add(fmt.Sprintf("GetDiff(%s)", repoPath), cmd), cmd, rd) } diff --git a/models/publickey.go b/models/publickey.go index 76dc0cc74..72b45c5b8 100644 --- a/models/publickey.go +++ b/models/publickey.go @@ -22,6 +22,7 @@ import ( qlog "github.com/qiniu/log" "github.com/gogits/gogs/modules/log" + "github.com/gogits/gogs/modules/process" ) const ( @@ -121,7 +122,7 @@ func AddPublicKey(key *PublicKey) (err error) { if err = ioutil.WriteFile(tmpPath, []byte(key.Content), os.ModePerm); err != nil { return err } - stdout, stderr, err := com.ExecCmd("ssh-keygen", "-l", "-f", tmpPath) + stdout, stderr, err := process.Exec("AddPublicKey", "ssh-keygen", "-l", "-f", tmpPath) if err != nil { return errors.New("ssh-keygen -l -f: " + stderr) } else if len(stdout) < 2 { diff --git a/models/repo.go b/models/repo.go index 0f0181353..6b98d0e40 100644 --- a/models/repo.go +++ b/models/repo.go @@ -9,7 +9,6 @@ import ( "fmt" "io/ioutil" "os" - "os/exec" "path" "path/filepath" "sort" @@ -25,6 +24,7 @@ import ( "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/bin" "github.com/gogits/gogs/modules/log" + "github.com/gogits/gogs/modules/process" "github.com/gogits/gogs/modules/setting" ) @@ -88,13 +88,13 @@ func NewRepoContext() { zip.Verbose = false // Check if server has basic git setting. - stdout, stderr, err := com.ExecCmd("git", "config", "--get", "user.name") + stdout, stderr, err := process.Exec("NewRepoContext(get setting)", "git", "config", "--get", "user.name") if strings.Contains(stderr, "fatal:") { log.Fatal("repo.NewRepoContext(fail to get git user.name): %s", stderr) } else if err != nil || len(strings.TrimSpace(stdout)) == 0 { - if _, stderr, err = com.ExecCmd("git", "config", "--global", "user.email", "gogitservice@gmail.com"); err != nil { + if _, stderr, err = process.Exec("NewRepoContext(set email)", "git", "config", "--global", "user.email", "gogitservice@gmail.com"); err != nil { log.Fatal("repo.NewRepoContext(fail to set git user.email): %s", stderr) - } else if _, stderr, err = com.ExecCmd("git", "config", "--global", "user.name", "Gogs"); err != nil { + } else if _, stderr, err = process.Exec("NewRepoContext(set name)", "git", "config", "--global", "user.name", "Gogs"); err != nil { log.Fatal("repo.NewRepoContext(fail to set git user.name): %s", stderr) } } @@ -190,7 +190,9 @@ type Mirror struct { // MirrorRepository creates a mirror repository from source. func MirrorRepository(repoId int64, userName, repoName, repoPath, url string) error { - _, stderr, err := com.ExecCmd("git", "clone", "--mirror", url, repoPath) + // TODO: need timeout. + _, stderr, err := process.Exec(fmt.Sprintf("MirrorRepository: %s/%s", userName, repoName), + "git", "clone", "--mirror", url, repoPath) if err != nil { return errors.New("git clone --mirror: " + stderr) } @@ -231,9 +233,11 @@ func MirrorUpdate() { return nil } + // TODO: need timeout. repoPath := filepath.Join(setting.RepoRootPath, m.RepoName+".git") - _, stderr, err := com.ExecCmdDir(repoPath, "git", "remote", "update") - if err != nil { + if _, stderr, err := process.ExecDir( + repoPath, fmt.Sprintf("MirrorUpdate: %s", repoPath), + "git", "remote", "update"); err != nil { return errors.New("git remote update: " + stderr) } else if err = git.UnpackRefs(repoPath); err != nil { return err @@ -268,133 +272,34 @@ func MigrateRepository(user *User, name, desc string, private, mirror bool, url return repo, UpdateRepository(repo) } + // TODO: need timeout. // Clone from local repository. - _, stderr, err := com.ExecCmd("git", "clone", repoPath, tmpDir) + _, stderr, err := process.Exec( + fmt.Sprintf("MigrateRepository(git clone): %s", repoPath), + "git", "clone", repoPath, tmpDir) if err != nil { return repo, errors.New("git clone: " + stderr) } + // TODO: need timeout. // Pull data from source. - _, stderr, err = com.ExecCmdDir(tmpDir, "git", "pull", url) - if err != nil { + if _, stderr, err = process.ExecDir( + tmpDir, fmt.Sprintf("MigrateRepository(git pull): %s", repoPath), + "git", "pull", url); err != nil { return repo, errors.New("git pull: " + stderr) } + // TODO: need timeout. // Push data to local repository. - if _, stderr, err = com.ExecCmdDir(tmpDir, "git", "push", "origin", "master"); err != nil { + if _, stderr, err = process.ExecDir( + tmpDir, fmt.Sprintf("MigrateRepository(git push): %s", repoPath), + "git", "push", "origin", "master"); err != nil { return repo, errors.New("git push: " + stderr) } return repo, UpdateRepository(repo) } -// CreateRepository creates a repository for given user or orgnaziation. -func CreateRepository(user *User, name, desc, lang, license string, private, mirror, initReadme bool) (*Repository, error) { - if !IsLegalName(name) { - return nil, ErrRepoNameIllegal - } - - isExist, err := IsRepositoryExist(user, name) - if err != nil { - return nil, err - } else if isExist { - return nil, ErrRepoAlreadyExist - } - - repo := &Repository{ - OwnerId: user.Id, - Name: name, - LowerName: strings.ToLower(name), - Description: desc, - IsPrivate: private, - IsBare: lang == "" && license == "" && !initReadme, - } - if !repo.IsBare { - repo.DefaultBranch = "master" - } - - repoPath := RepoPath(user.Name, repo.Name) - - sess := orm.NewSession() - defer sess.Close() - sess.Begin() - - if _, err = sess.Insert(repo); err != nil { - if err2 := os.RemoveAll(repoPath); err2 != nil { - log.Error("repo.CreateRepository(repo): %v", err) - return nil, errors.New(fmt.Sprintf( - "delete repo directory %s/%s failed(1): %v", user.Name, repo.Name, err2)) - } - sess.Rollback() - return nil, err - } - - mode := AU_WRITABLE - if mirror { - mode = AU_READABLE - } - access := Access{ - UserName: user.LowerName, - RepoName: strings.ToLower(path.Join(user.Name, repo.Name)), - Mode: mode, - } - if _, err = sess.Insert(&access); err != nil { - sess.Rollback() - if err2 := os.RemoveAll(repoPath); err2 != nil { - log.Error("repo.CreateRepository(access): %v", err) - return nil, errors.New(fmt.Sprintf( - "delete repo directory %s/%s failed(2): %v", user.Name, repo.Name, err2)) - } - return nil, err - } - - rawSql := "UPDATE `user` SET num_repos = num_repos + 1 WHERE id = ?" - if _, err = sess.Exec(rawSql, user.Id); err != nil { - sess.Rollback() - if err2 := os.RemoveAll(repoPath); err2 != nil { - log.Error("repo.CreateRepository(repo count): %v", err) - return nil, errors.New(fmt.Sprintf( - "delete repo directory %s/%s failed(3): %v", user.Name, repo.Name, err2)) - } - return nil, err - } - - if err = sess.Commit(); err != nil { - sess.Rollback() - if err2 := os.RemoveAll(repoPath); err2 != nil { - log.Error("repo.CreateRepository(commit): %v", err) - return nil, errors.New(fmt.Sprintf( - "delete repo directory %s/%s failed(3): %v", user.Name, repo.Name, err2)) - } - return nil, err - } - - if err = WatchRepo(user.Id, repo.Id, true); err != nil { - log.Error("repo.CreateRepository(WatchRepo): %v", err) - } - - if err = NewRepoAction(user, repo); err != nil { - log.Error("repo.CreateRepository(NewRepoAction): %v", err) - } - - // No need for init for mirror. - if mirror { - return repo, nil - } - - if err = initRepository(repoPath, user, repo, initReadme, lang, license); err != nil { - return nil, err - } - - c := exec.Command("git", "update-server-info") - c.Dir = repoPath - if err = c.Run(); err != nil { - log.Error("repo.CreateRepository(exec update-server-info): %v", err) - } - - return repo, nil -} - // extractGitBareZip extracts git-bare.zip to repository path. func extractGitBareZip(repoPath string) error { z, err := zip.Open(path.Join(setting.RepoRootPath, "git-bare.zip")) @@ -409,15 +314,22 @@ func extractGitBareZip(repoPath string) error { // initRepoCommit temporarily changes with work directory. func initRepoCommit(tmpPath string, sig *git.Signature) (err error) { var stderr string - if _, stderr, err = com.ExecCmdDir(tmpPath, "git", "add", "--all"); err != nil { + if _, stderr, err = process.ExecDir( + tmpPath, fmt.Sprintf("initRepoCommit(git add): %s", tmpPath), + "git", "add", "--all"); err != nil { return errors.New("git add: " + stderr) } - if _, stderr, err = com.ExecCmdDir(tmpPath, "git", "commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email), + + if _, stderr, err = process.ExecDir( + tmpPath, fmt.Sprintf("initRepoCommit(git commit): %s", tmpPath), + "git", "commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email), "-m", "Init commit"); err != nil { return errors.New("git commit: " + stderr) } - if _, stderr, err = com.ExecCmdDir(tmpPath, "git", "push", "origin", "master"); err != nil { + if _, stderr, err = process.ExecDir( + tmpPath, fmt.Sprintf("initRepoCommit(git push): %s", tmpPath), + "git", "push", "origin", "master"); err != nil { return errors.New("git push: " + stderr) } return nil @@ -475,9 +387,11 @@ func initRepository(f string, user *User, repo *Repository, initReadme bool, rep tmpDir := filepath.Join(os.TempDir(), base.ToStr(time.Now().Nanosecond())) os.MkdirAll(tmpDir, os.ModePerm) - _, stderr, err := com.ExecCmd("git", "clone", repoPath, tmpDir) + _, stderr, err := process.Exec( + fmt.Sprintf("initRepository(git clone): %s", repoPath), + "git", "clone", repoPath, tmpDir) if err != nil { - return errors.New("git clone: " + stderr) + return errors.New("initRepository(git clone): " + stderr) } // README @@ -540,6 +454,114 @@ func initRepository(f string, user *User, repo *Repository, initReadme bool, rep return initRepoCommit(tmpDir, user.NewGitSig()) } +// CreateRepository creates a repository for given user or orgnaziation. +func CreateRepository(user *User, name, desc, lang, license string, private, mirror, initReadme bool) (*Repository, error) { + if !IsLegalName(name) { + return nil, ErrRepoNameIllegal + } + + isExist, err := IsRepositoryExist(user, name) + if err != nil { + return nil, err + } else if isExist { + return nil, ErrRepoAlreadyExist + } + + repo := &Repository{ + OwnerId: user.Id, + Name: name, + LowerName: strings.ToLower(name), + Description: desc, + IsPrivate: private, + IsBare: lang == "" && license == "" && !initReadme, + } + if !repo.IsBare { + repo.DefaultBranch = "master" + } + + repoPath := RepoPath(user.Name, repo.Name) + + sess := orm.NewSession() + defer sess.Close() + sess.Begin() + + if _, err = sess.Insert(repo); err != nil { + if err2 := os.RemoveAll(repoPath); err2 != nil { + log.Error("repo.CreateRepository(repo): %v", err) + return nil, errors.New(fmt.Sprintf( + "delete repo directory %s/%s failed(1): %v", user.Name, repo.Name, err2)) + } + sess.Rollback() + return nil, err + } + + mode := AU_WRITABLE + if mirror { + mode = AU_READABLE + } + access := Access{ + UserName: user.LowerName, + RepoName: strings.ToLower(path.Join(user.Name, repo.Name)), + Mode: mode, + } + if _, err = sess.Insert(&access); err != nil { + sess.Rollback() + if err2 := os.RemoveAll(repoPath); err2 != nil { + log.Error("repo.CreateRepository(access): %v", err) + return nil, errors.New(fmt.Sprintf( + "delete repo directory %s/%s failed(2): %v", user.Name, repo.Name, err2)) + } + return nil, err + } + + rawSql := "UPDATE `user` SET num_repos = num_repos + 1 WHERE id = ?" + if _, err = sess.Exec(rawSql, user.Id); err != nil { + sess.Rollback() + if err2 := os.RemoveAll(repoPath); err2 != nil { + log.Error("repo.CreateRepository(repo count): %v", err) + return nil, errors.New(fmt.Sprintf( + "delete repo directory %s/%s failed(3): %v", user.Name, repo.Name, err2)) + } + return nil, err + } + + if err = sess.Commit(); err != nil { + sess.Rollback() + if err2 := os.RemoveAll(repoPath); err2 != nil { + log.Error("repo.CreateRepository(commit): %v", err) + return nil, errors.New(fmt.Sprintf( + "delete repo directory %s/%s failed(3): %v", user.Name, repo.Name, err2)) + } + return nil, err + } + + if err = WatchRepo(user.Id, repo.Id, true); err != nil { + log.Error("repo.CreateRepository(WatchRepo): %v", err) + } + + if err = NewRepoAction(user, repo); err != nil { + log.Error("repo.CreateRepository(NewRepoAction): %v", err) + } + + // No need for init for mirror. + if mirror { + return repo, nil + } + + if err = initRepository(repoPath, user, repo, initReadme, lang, license); err != nil { + return nil, err + } + + _, stderr, err := process.ExecDir( + repoPath, fmt.Sprintf("CreateRepository(git update-server-info): %s", repoPath), + "git", "update-server-info") + if err != nil { + return nil, errors.New("CreateRepository(git update-server-info): " + stderr) + } + + return repo, nil +} + // GetRepositoriesWithUsers returns given number of repository objects with offset. // It also auto-gets corresponding users. func GetRepositoriesWithUsers(num, offset int) ([]*Repository, error) { diff --git a/modules/process/manager.go b/modules/process/manager.go new file mode 100644 index 000000000..173b2aa4e --- /dev/null +++ b/modules/process/manager.go @@ -0,0 +1,89 @@ +// Copyright 2014 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package process + +import ( + "bytes" + "fmt" + "os/exec" + "time" + + "github.com/gogits/gogs/modules/log" +) + +// Process represents a working process inherit from Gogs. +type Process struct { + Pid int64 // Process ID, not system one. + Description string + Start time.Time + Cmd *exec.Cmd +} + +// List of existing processes. +var ( + curPid int64 = 1 + Processes []*Process +) + +// Add adds a existing process and returns its PID. +func Add(desc string, cmd *exec.Cmd) int64 { + pid := curPid + Processes = append(Processes, &Process{ + Pid: pid, + Description: desc, + Start: time.Now(), + Cmd: cmd, + }) + curPid++ + return pid +} + +func ExecDir(dir, desc, cmdName string, args ...string) (string, string, error) { + bufOut := new(bytes.Buffer) + bufErr := new(bytes.Buffer) + + cmd := exec.Command(cmdName, args...) + cmd.Dir = dir + cmd.Stdout = bufOut + cmd.Stderr = bufErr + + pid := Add(desc, cmd) + err := cmd.Run() + if errKill := Kill(pid); errKill != nil { + log.Error("Exec: %v", pid, desc, errKill) + } + return bufOut.String(), bufErr.String(), err +} + +// Exec starts executing a command and record its process. +func Exec(desc, cmdName string, args ...string) (string, string, error) { + return ExecDir("", desc, cmdName, args...) +} + +// Remove removes a process from list. +func Remove(pid int64) { + for i, proc := range Processes { + if proc.Pid == pid { + Processes = append(Processes[:i], Processes[i+1:]...) + return + } + } +} + +// Kill kills and removes a process from list. +func Kill(pid int64) error { + for i, proc := range Processes { + if proc.Pid == pid { + if proc.Cmd.Process != nil && proc.Cmd.ProcessState != nil && !proc.Cmd.ProcessState.Exited() { + if err := proc.Cmd.Process.Kill(); err != nil { + return fmt.Errorf("fail to kill process(%d/%s): %v", proc.Pid, proc.Description, err) + } + } + Processes = append(Processes[:i], Processes[i+1:]...) + return nil + } + } + return nil +} diff --git a/routers/admin/admin.go b/routers/admin/admin.go index d4b49a9e1..a14ffae0e 100644 --- a/routers/admin/admin.go +++ b/routers/admin/admin.go @@ -16,6 +16,7 @@ import ( "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/cron" "github.com/gogits/gogs/modules/middleware" + "github.com/gogits/gogs/modules/process" "github.com/gogits/gogs/modules/setting" ) @@ -238,10 +239,12 @@ func Monitor(ctx *middleware.Context) { switch tab { case "process": ctx.Data["PageIsMonitorProcess"] = true + ctx.Data["Processes"] = process.Processes + ctx.HTML(200, "admin/monitor/process") default: ctx.Data["PageIsMonitorCron"] = true ctx.Data["Entries"] = cron.ListEntries() + ctx.HTML(200, "admin/monitor/cron") } - ctx.HTML(200, "admin/monitor/cron") } diff --git a/templates/VERSION b/templates/VERSION index c2606e903..d86ece155 100644 --- a/templates/VERSION +++ b/templates/VERSION @@ -1 +1 @@ -0.4.4.0613 Alpha \ No newline at end of file +0.4.4.0619 Alpha \ No newline at end of file diff --git a/templates/admin/monitor/process.tmpl b/templates/admin/monitor/process.tmpl new file mode 100644 index 000000000..2d60ff689 --- /dev/null +++ b/templates/admin/monitor/process.tmpl @@ -0,0 +1,38 @@ +{{template "base/head" .}} +{{template "base/navbar" .}} +
+ {{template "admin/nav" .}} +
+ +
+
+ {{if .PageIsMonitorProcess}} + + + + + + + + + + + {{range .Processes}} + + + + + + + {{end}} + +
PidDescriptionStart TimeExecution Time
{{.Pid}}{{.Description}}{{.Start}}{{TimeSince .Start}}
+ {{end}} +
+
+
+
+{{template "base/footer" .}} \ No newline at end of file