diff --git a/cron/plugin.go b/cron/plugin.go index 1a2857ac..fee101e4 100644 --- a/cron/plugin.go +++ b/cron/plugin.go @@ -7,6 +7,10 @@ import ( "log" "strings" "time" + "github.com/toolkits/file" + "os/exec" + "fmt" + "errors" ) func SyncMinePlugins() { @@ -21,7 +25,9 @@ func SyncMinePlugins() { if g.Config().Heartbeat.Addr == "" { return } - + if err := UpdatePlugin(); err != nil { + log.Fatalln(err.Error()) + } go syncMinePlugins() } @@ -82,3 +88,31 @@ func syncMinePlugins() { } } +//如果插件启用,则用git地址下载插件 +func UpdatePlugin() error { + if !g.Config().Plugin.Enabled { + return nil + } + dir := g.Config().Plugin.Dir + parentDir := file.Dir(dir) + file.InsureDir(parentDir) + + if file.IsExist(dir) { + // git pull + cmd := exec.Command("git", "pull") + cmd.Dir = dir + err := cmd.Run() + if err != nil { + return errors.New(fmt.Sprintf("git pull in dir:%s fail. error: %s", dir, err)) + } + } else { + // git clone + cmd := exec.Command("git", "clone", g.Config().Plugin.Git, file.Basename(dir)) + cmd.Dir = parentDir + err := cmd.Run() + if err != nil { + return errors.New((fmt.Sprintf("git clone plugin url:%s into dir:%s fail. error: %s", g.Config().Plugin.Git, dir, err))) + } + } + return nil +} diff --git a/http/plugin.go b/http/plugin.go index ba7e7919..56bad852 100644 --- a/http/plugin.go +++ b/http/plugin.go @@ -7,6 +7,7 @@ import ( "github.com/toolkits/file" "net/http" "os/exec" + "github.com/open-falcon/agent/cron" ) func configPluginRoutes() { @@ -15,31 +16,10 @@ func configPluginRoutes() { w.Write([]byte("plugin not enabled")) return } - - dir := g.Config().Plugin.Dir - parentDir := file.Dir(dir) - file.InsureDir(parentDir) - - if file.IsExist(dir) { - // git pull - cmd := exec.Command("git", "pull") - cmd.Dir = dir - err := cmd.Run() - if err != nil { - w.Write([]byte(fmt.Sprintf("git pull in dir:%s fail. error: %s", dir, err))) - return - } - } else { - // git clone - cmd := exec.Command("git", "clone", g.Config().Plugin.Git, file.Basename(dir)) - cmd.Dir = parentDir - err := cmd.Run() - if err != nil { - w.Write([]byte(fmt.Sprintf("git clone in dir:%s fail. error: %s", parentDir, err))) - return - } + if err := cron.UpdatePlugin(); err != nil { + w.Write([]byte(err.Error())) + return } - w.Write([]byte("success")) })