1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-12-21 12:44:49 -05:00

Basic process manager

This commit is contained in:
Unknown 2014-06-19 01:08:03 -04:00
parent 8d3276cab0
commit f147ad619a
10 changed files with 326 additions and 180 deletions

View file

@ -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)
}

View file

@ -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())

View file

@ -1,6 +0,0 @@
package models
func Fix() error {
_, err := orm.Exec("alter table repository drop column num_releases")
return err
}

View file

@ -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)
}

View file

@ -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 {

View file

@ -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,26 +272,188 @@ 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)
}
// extractGitBareZip extracts git-bare.zip to repository path.
func extractGitBareZip(repoPath string) error {
z, err := zip.Open(path.Join(setting.RepoRootPath, "git-bare.zip"))
if err != nil {
return err
}
defer z.Close()
return z.ExtractTo(repoPath)
}
// initRepoCommit temporarily changes with work directory.
func initRepoCommit(tmpPath string, sig *git.Signature) (err error) {
var stderr string
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 = 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 = process.ExecDir(
tmpPath, fmt.Sprintf("initRepoCommit(git push): %s", tmpPath),
"git", "push", "origin", "master"); err != nil {
return errors.New("git push: " + stderr)
}
return nil
}
func createHookUpdate(hookPath, content string) error {
pu, err := os.OpenFile(hookPath, os.O_CREATE|os.O_WRONLY, 0777)
if err != nil {
return err
}
defer pu.Close()
_, err = pu.WriteString(content)
return err
}
// SetRepoEnvs sets environment variables for command update.
func SetRepoEnvs(userId int64, userName, repoName, repoUserName string) {
os.Setenv("userId", base.ToStr(userId))
os.Setenv("userName", userName)
os.Setenv("repoName", repoName)
os.Setenv("repoUserName", repoUserName)
}
// InitRepository initializes README and .gitignore if needed.
func initRepository(f string, user *User, repo *Repository, initReadme bool, repoLang, license string) error {
repoPath := RepoPath(user.Name, repo.Name)
// Create bare new repository.
if err := extractGitBareZip(repoPath); err != nil {
return err
}
rp := strings.NewReplacer("\\", "/", " ", "\\ ")
// hook/post-update
if err := createHookUpdate(filepath.Join(repoPath, "hooks", "update"),
fmt.Sprintf(TPL_UPDATE_HOOK, setting.ScriptType,
rp.Replace(appPath))); err != nil {
return err
}
// Initialize repository according to user's choice.
fileName := map[string]string{}
if initReadme {
fileName["readme"] = "README.md"
}
if repoLang != "" {
fileName["gitign"] = ".gitignore"
}
if license != "" {
fileName["license"] = "LICENSE"
}
// Clone to temprory path and do the init commit.
tmpDir := filepath.Join(os.TempDir(), base.ToStr(time.Now().Nanosecond()))
os.MkdirAll(tmpDir, os.ModePerm)
_, stderr, err := process.Exec(
fmt.Sprintf("initRepository(git clone): %s", repoPath),
"git", "clone", repoPath, tmpDir)
if err != nil {
return errors.New("initRepository(git clone): " + stderr)
}
// README
if initReadme {
defaultReadme := repo.Name + "\n" + strings.Repeat("=",
utf8.RuneCountInString(repo.Name)) + "\n\n" + repo.Description
if err := ioutil.WriteFile(filepath.Join(tmpDir, fileName["readme"]),
[]byte(defaultReadme), 0644); err != nil {
return err
}
}
// .gitignore
if repoLang != "" {
filePath := "conf/gitignore/" + repoLang
targetPath := path.Join(tmpDir, fileName["gitign"])
data, err := bin.Asset(filePath)
if err == nil {
if err = ioutil.WriteFile(targetPath, data, os.ModePerm); err != nil {
return err
}
} else {
// Check custom files.
filePath = path.Join(setting.CustomPath, "conf/gitignore", repoLang)
if com.IsFile(filePath) {
if err := com.Copy(filePath, targetPath); err != nil {
return err
}
}
}
}
// LICENSE
if license != "" {
filePath := "conf/license/" + license
targetPath := path.Join(tmpDir, fileName["license"])
data, err := bin.Asset(filePath)
if err == nil {
if err = ioutil.WriteFile(targetPath, data, os.ModePerm); err != nil {
return err
}
} else {
// Check custom files.
filePath = path.Join(setting.CustomPath, "conf/license", license)
if com.IsFile(filePath) {
if err := com.Copy(filePath, targetPath); err != nil {
return err
}
}
}
}
if len(fileName) == 0 {
return nil
}
SetRepoEnvs(user.Id, user.Name, repo.Name, user.Name)
// Apply changes and commit.
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) {
@ -386,160 +552,16 @@ func CreateRepository(user *User, name, desc, lang, license string, private, mir
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)
_, 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
}
// extractGitBareZip extracts git-bare.zip to repository path.
func extractGitBareZip(repoPath string) error {
z, err := zip.Open(path.Join(setting.RepoRootPath, "git-bare.zip"))
if err != nil {
return err
}
defer z.Close()
return z.ExtractTo(repoPath)
}
// 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 {
return errors.New("git add: " + stderr)
}
if _, stderr, err = com.ExecCmdDir(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 {
return errors.New("git push: " + stderr)
}
return nil
}
func createHookUpdate(hookPath, content string) error {
pu, err := os.OpenFile(hookPath, os.O_CREATE|os.O_WRONLY, 0777)
if err != nil {
return err
}
defer pu.Close()
_, err = pu.WriteString(content)
return err
}
// SetRepoEnvs sets environment variables for command update.
func SetRepoEnvs(userId int64, userName, repoName, repoUserName string) {
os.Setenv("userId", base.ToStr(userId))
os.Setenv("userName", userName)
os.Setenv("repoName", repoName)
os.Setenv("repoUserName", repoUserName)
}
// InitRepository initializes README and .gitignore if needed.
func initRepository(f string, user *User, repo *Repository, initReadme bool, repoLang, license string) error {
repoPath := RepoPath(user.Name, repo.Name)
// Create bare new repository.
if err := extractGitBareZip(repoPath); err != nil {
return err
}
rp := strings.NewReplacer("\\", "/", " ", "\\ ")
// hook/post-update
if err := createHookUpdate(filepath.Join(repoPath, "hooks", "update"),
fmt.Sprintf(TPL_UPDATE_HOOK, setting.ScriptType,
rp.Replace(appPath))); err != nil {
return err
}
// Initialize repository according to user's choice.
fileName := map[string]string{}
if initReadme {
fileName["readme"] = "README.md"
}
if repoLang != "" {
fileName["gitign"] = ".gitignore"
}
if license != "" {
fileName["license"] = "LICENSE"
}
// Clone to temprory path and do the init commit.
tmpDir := filepath.Join(os.TempDir(), base.ToStr(time.Now().Nanosecond()))
os.MkdirAll(tmpDir, os.ModePerm)
_, stderr, err := com.ExecCmd("git", "clone", repoPath, tmpDir)
if err != nil {
return errors.New("git clone: " + stderr)
}
// README
if initReadme {
defaultReadme := repo.Name + "\n" + strings.Repeat("=",
utf8.RuneCountInString(repo.Name)) + "\n\n" + repo.Description
if err := ioutil.WriteFile(filepath.Join(tmpDir, fileName["readme"]),
[]byte(defaultReadme), 0644); err != nil {
return err
}
}
// .gitignore
if repoLang != "" {
filePath := "conf/gitignore/" + repoLang
targetPath := path.Join(tmpDir, fileName["gitign"])
data, err := bin.Asset(filePath)
if err == nil {
if err = ioutil.WriteFile(targetPath, data, os.ModePerm); err != nil {
return err
}
} else {
// Check custom files.
filePath = path.Join(setting.CustomPath, "conf/gitignore", repoLang)
if com.IsFile(filePath) {
if err := com.Copy(filePath, targetPath); err != nil {
return err
}
}
}
}
// LICENSE
if license != "" {
filePath := "conf/license/" + license
targetPath := path.Join(tmpDir, fileName["license"])
data, err := bin.Asset(filePath)
if err == nil {
if err = ioutil.WriteFile(targetPath, data, os.ModePerm); err != nil {
return err
}
} else {
// Check custom files.
filePath = path.Join(setting.CustomPath, "conf/license", license)
if com.IsFile(filePath) {
if err := com.Copy(filePath, targetPath); err != nil {
return err
}
}
}
}
if len(fileName) == 0 {
return nil
}
SetRepoEnvs(user.Id, user.Name, repo.Name, user.Name)
// Apply changes and commit.
return initRepoCommit(tmpDir, user.NewGitSig())
}
// GetRepositoriesWithUsers returns given number of repository objects with offset.
// It also auto-gets corresponding users.
func GetRepositoriesWithUsers(num, offset int) ([]*Repository, error) {

View file

@ -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
}

View file

@ -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")
}

View file

@ -1 +1 @@
0.4.4.0613 Alpha
0.4.4.0619 Alpha

View file

@ -0,0 +1,38 @@
{{template "base/head" .}}
{{template "base/navbar" .}}
<div id="body" class="container" data-page="admin">
{{template "admin/nav" .}}
<div id="admin-container" class="col-md-10">
<ul class="nav nav-tabs">
<li{{if .PageIsMonitorCron}} class="active"{{end}}><a href="/admin/monitor">Cron Tasks</a></li>
<li{{if .PageIsMonitorProcess}} class="active"{{end}}><a href="/admin/monitor?tab=process">Processes</a></li>
</ul>
<div class="panel panel-default">
<div class="panel-body">
{{if .PageIsMonitorProcess}}
<table class="table table-striped">
<thead>
<tr>
<th>Pid</th>
<th>Description</th>
<th>Start Time</th>
<th>Execution Time</th>
</tr>
</thead>
<tbody>
{{range .Processes}}
<tr>
<td>{{.Pid}}</td>
<td>{{.Description}}</td>
<td>{{.Start}}</td>
<td>{{TimeSince .Start}}</td>
</tr>
{{end}}
</tbody>
</table>
{{end}}
</div>
</div>
</div>
</div>
{{template "base/footer" .}}