func svnUp(paths ...string) (int, JSON.Type, error) { var path string if len(paths) > 0 { path = paths[0] } else { path = svnDir } output, err := sh.Command("svn", "up", path).SetTimeout(time.Second * 30).Output() if err != nil { return -1, nil, helper.NewError("svn up command error", err) } lines := strings.Split(helper.Trim(string(output)), "\n") list := JSON.Type{} regLine := regexp.MustCompile(`^([U|D|A])\s+(.*)`) version := getVersion(lines[len(lines)-1]) for _, line := range lines { if matches := regLine.FindAllStringSubmatch(line, -1); matches != nil { for _, match := range matches { a := match[1] path := match[2] path = path[len(svnDir):] list[path] = action.ParseAction(a) } } } return version, list, nil }
//添加更新记录 func UpdateVersion(data *model.Version) error { if _, err := db.Orm().Insert(data); err != nil { return helper.NewError("save version error", err) } LastVersion = *data Version = LastVersion.Version return nil }
//获取最新版本 func GetLastVersion() (model.Version, error) { LastVersion = model.Version{} if err := db.Orm().QueryTable("version").OrderBy("-id").Limit(1).One(&LastVersion); err != nil { return LastVersion, helper.NewError("SvnService.GetLastVersion", err) } Version = LastVersion.Version return LastVersion, nil }
func Send(url string, method string, params interface{}) (interface{}, error) { contentString := `{"method": "` + method + `", "params":[` + JSON.Stringify(params) + `], "id":"` + helper.RandString(10) + `"}` contentBody := []byte(contentString) req, err := http.NewRequest("POST", url, bytes.NewBuffer(contentBody)) defer req.Body.Close() if err != nil { return nil, helper.NewError("create request error:", err) } req.Header.Set("Content-Type", "application/json") transport := http.Transport{ Dial: dial, } client := &http.Client{ Transport: &transport, } resp, err := client.Do(req) if err != nil { return nil, helper.NewError("rpc request error", err) } body, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, err } result := JSON.Parse(string(body)) if result == nil { return nil, nil } if result["error"] != nil { return nil, helper.NewError(method + ":" + result["error"].(string)) } //无返回内容 if result["result"] == nil { return nil, nil } return result["result"].(map[string]interface{})["Response"], nil }
func Active(client *model.WebServer) (int64, error) { created, id, err := db.Orm().ReadOrCreate(client, "Ip", "InternalIp", "Port") if err != nil { return id, err } if created || id > 0 { id = FindOrAppend(client) return id, nil } return id, helper.NewError(helper.AppendString("already exisits client, id: ", id)) }
//客户端启动通知,保存客户端入库 func (h *RpcServer) Active(r *http.Request, args *rpc.ActiveArgs, reply *rpc.RpcReply) error { id, err := client.Active(&model.WebServer{ Ip: args.Ip, InternalIp: args.InternalIp, Port: args.Port, }) if err != nil { return helper.NewError("add client error", err) } reply.Response = id return nil }
func update() (model.Version, error) { now := time.Now() version := model.Version{} num, list, err := svnUp() if err != nil { return version, err } if master.IsChanged(num) == false { return version, helper.NewError("no change") } master.Version = num version = model.Version{ Version: num, Time: now, List: JSON.Stringify(list), } if err := master.UpdateVersion(&version); err != nil { return version, err } master.SetUnDeployFile(list) task.Trigger("client.UpdateHostUnDeployList") webSocket.BroadCastAll(&webSocket.Message{ "svnup", helper.Success(version), }) return version, nil }
func init() { task.New("host.ProcStat", func(this *task.Task) interface{} { if IsConnected == false { return nil } cpu := proc.CPUPercent() mem := proc.MEMPercent() CallRpc("ReportUsage", rpc.UsageArgs{Detail.Id, cpu, mem}) return nil }, time.Second*1) task.New("host.Deploy", func(this *task.Task) interface{} { if IsConnected == false { return nil } if deploying { this.Enable = false return nil } var err error deploying = true this.Enable = false defer func() { deploying = false if err != nil { log.Println(err) broadcastAll(Error, err.Error()) } else { broadcastAll(Message, "deploy complete") time.Sleep(time.Second * 2) broadcastAll(Finish, "") } }() broadcastAll(Start, "mvn compiling..") err = helper.GetCMDOutputWithComplete(sh.Command("sh", shDir+"compile.sh").SetTimeout(time.Second * 60).Output()) if err != nil { err = helper.NewError("mvn compile error!", err) return nil } broadcastAll(Message, "killing java..") if err = helper.GetCMDOutputWithComplete(sh.Command("sh", shDir+"kill.sh").SetTimeout(time.Second * 30).Output()); err != nil { err = helper.NewError("kill java error!", err) return nil } broadcastAll(Message, "backup whole project..") if err = helper.GetCMDOutputWithComplete(sh.Command("sh", shDir+"backup.sh").SetTimeout(time.Second * 30).Output()); err != nil { err = helper.NewError("backup error!", err) return nil } broadcastAll(Message, "execute mvn war:exploded") if err = helper.GetCMDOutputWithComplete(sh.Command("sh", shDir+"exploded.sh").SetTimeout(time.Second * 30).Output()); err != nil { err = helper.NewError("mvn war:exploded error!", err) return nil } broadcastAll(Message, "starting server..") if err = helper.GetCMDOutputWithComplete(sh.Command("sh", shDir+"startup.sh").SetTimeout(time.Second * 30).Output()); err != nil { err = helper.NewError("server start error!", err) return nil } return nil }) }