func Edit(rend render.Render, req *http.Request, params martini.Params) { id := helper.Int64(params["id"]) host, errResponse := getClientWithNoBusyOrJSONError(id) if host == nil { rend.JSON(200, errResponse) return } body := JSON.FormRequest(req.Body) c := &model.WebServer{} if err := JSON.ParseToStruct(JSON.Stringify(body), c); err != nil { rend.JSON(200, helper.Error(helper.ParamsError)) return } keys := JSON.GetKeys(body) c.Id = id if err := client.Edit(c, keys...); err != nil { rend.JSON(200, helper.Error(err)) return } rend.JSON(200, helper.Success(c)) }
func Update(rend render.Render, req *http.Request, params martini.Params) { id := helper.Int64(params["id"]) host, errResponse := getClientWithNoBusyOrJSONError(id) if host == nil { rend.JSON(200, errResponse) return } //未部署列表 unDeployFileList, err := host.GetUnDeployFiles() if err != nil || unDeployFileList == nil { rend.JSON(200, helper.Error(err)) return } //选择要上传的文件 body := JSON.FormRequest(req.Body) files := body["files"] upFile := rpc.UploadFileList{} JSON.ParseToStruct(JSON.Stringify(files), &upFile) uploadFiles := &rpc.UpdateArgs{ Id: host.Id, ResPath: config.ResServer(), FileList: upFile, DeployPath: host.DeployPath, } //上传 completeUploadList, err := host.CallRpc("Update", uploadFiles) if err != nil { rend.JSON(200, helper.Error(err)) return } //上传ok就删除对应路径记录 helper.Map(completeUploadList, func(key, value interface{}) bool { p := key.(string) v := reflect.ValueOf(value) if v.Kind() == reflect.Bool { delete(unDeployFileList, p) } return false }) //更新记录 host.WebServer.UnDeployList = JSON.Stringify(unDeployFileList) err = client.Edit(host.WebServer, "UnDeployList") if err != nil { fmt.Println(err) } rend.JSON(200, helper.Success(completeUploadList)) }
func Update(rend render.Render, req *http.Request) { if master.IsLock() { rend.JSON(200, helper.Error(helper.BusyError)) return } result, err := update() if err != nil { rend.JSON(200, helper.Error(err, result)) return } rend.JSON(200, helper.Success(result)) }
func getClientWithAliveOrJSONError(id int64) (*client.HostClient, JSON.Type) { host, err := getClientOrJSONError(id) if host != nil { switch host.Status { case status.Alive: return host, nil case status.Die: err = helper.Error(helper.OfflineError) break case status.Busy: err = helper.Error(helper.BusyError) break } } return nil, err }
func Add(rend render.Render, req *http.Request) { c := &model.WebServer{} body := JSON.FormRequest(req.Body) if err := JSON.ParseToStruct(JSON.Stringify(body), c); err != nil { rend.JSON(200, helper.Error(helper.ParamsError, err)) return } if _, err := client.Add(c); err != nil { rend.JSON(200, helper.Error(err)) return } rend.JSON(200, helper.Success(c)) }
func getClientOrJSONError(id int64) (*client.HostClient, JSON.Type) { host := client.FindFromCache(id) if host == nil { return nil, helper.Error(helper.EmptyError, "Client is not found") } return host, nil }
func ShowError(rend render.Render) { if master.Error { rend.JSON(200, helper.Success(master.ErrorLog)) } else { rend.JSON(200, helper.Error()) } }
func Refresh(rend render.Render) { _, err := client.Fetch() if err != nil { rend.JSON(200, helper.Error(err)) return } rend.JSON(200, helper.Success(fillClientList())) }
func Add(rend render.Render, req *http.Request) { params, _ := jason.NewObjectFromReader(req.Body) name, _ := params.GetString("name") result, err := group.Add(name) if err != nil { rend.JSON(200, helper.Error(err)) return } rend.JSON(200, helper.Success(result)) }
func GetLastVersion(rend render.Render) { version, err := master.GetLastVersion() if err != nil { rend.JSON(200, helper.Error(err)) return } result := JSON.Parse(version) result["list"] = JSON.Parse(version.List) rend.JSON(200, helper.Success(result)) }
func Move(rend render.Render, params martini.Params) { id := helper.Int64(params["id"]) gid := helper.Int64(params["gid"]) c := model.WebServer{Id: id, Group: gid} if err := client.Edit(&c, "Group"); err != nil { rend.JSON(200, helper.Error(err)) return } rend.JSON(200, helper.Success()) }
func getClientWithNoBusyOrJSONError(id int64) (*client.HostClient, JSON.Type) { host, err := getClientOrJSONError(id) if host != nil { switch host.Status { case status.Busy: err = helper.Error(helper.BusyError) break default: return host, nil } } return nil, err }
func Emit(client *socketClient, msg *Message) { syncLock.Lock() method := msg.Method if method == "broadcast" { BroadCast(client, msg) } else if _, found := onEmitCallback[method]; method != "" && found { client.out <- &Message{method, onEmitCallback[method]()} } else { client.out <- &Message{method, helper.Error("method undefined")} } syncLock.Unlock() }
func Compile(rend render.Render, req *http.Request) { if master.IsLock() { rend.JSON(200, helper.Error(helper.BusyError)) return } master.Lock() master.SetBusy() master.SetMessage("ready to compile") task.Trigger("master.Compile") rend.JSON(200, helper.Success()) }
//多终端Rpc调用 //TODO //队列调用,最大同时调用数 func BatchCallRpc(clients HostMap, method string, params ...rpc.RpcInterface) JSON.Type { results := JSON.Type{} helper.AsyncMap(clients, func(key, value interface{}) bool { c := value.(*HostClient) result, err := c.CallRpc(method, params...) if err != nil { results[helper.Itoa64(c.Id)] = helper.Error(err) } else { results[helper.Itoa64(c.Id)] = result } return false }) return results }
func Check(rend render.Render, req *http.Request) { body, _ := jason.NewObjectFromReader(req.Body) clientsId, _ := body.GetInt64Array("clientsId") clientList := client.List(clientsId) results := JSON.Type{} for _, c := range clientList { result, err := c.CallRpc("CheckDeployPath", rpc.CheckDeployPathArgs{c.Id, c.DeployPath}) if err != nil { results[helper.Itoa64(c.Id)] = helper.Error(err) } else { results[helper.Itoa64(c.Id)] = result } } rend.JSON(200, helper.Success(results)) }
func Del(rend render.Render, params martini.Params) { id := helper.Int64(params["id"]) host, errResponse := getClientWithNoBusyOrJSONError(id) if host == nil { rend.JSON(200, errResponse) return } c := model.WebServer{Id: id} if err := client.Del(&c); err != nil { rend.JSON(200, helper.Error(err)) return } rend.JSON(200, helper.Success()) }
func GetUnDeployFiles(rend render.Render, params martini.Params) { id := helper.Int64(params["id"]) host, errResponse := getClientWithNoBusyOrJSONError(id) if host == nil { rend.JSON(200, errResponse) return } result, err := host.GetUnDeployFiles() if err != nil || result == nil || helper.Cap(result) == 0 { rend.JSON(200, helper.Error(err)) return } rend.JSON(200, helper.Success(result)) }
func ShowLog(rend render.Render, params martini.Params) { id := helper.Int64(params["id"]) host, errResponse := getClientOrJSONError(id) if host == nil { rend.JSON(200, errResponse) return } result, err := host.CallRpc("ShowLog", &rpc.SimpleArgs{Id: host.Id}) if err != nil { rend.JSON(200, helper.Error(err)) return } rend.JSON(200, helper.Success(result)) }
func GetBackupList(rend render.Render, params martini.Params) { id := helper.Int64(params["id"]) host, errResponse := getClientWithNoBusyOrJSONError(id) if host == nil { rend.JSON(200, errResponse) return } result, err := host.CallRpc("GetBackupList") if err != nil { rend.JSON(200, helper.Error(err)) return } rend.JSON(200, helper.Success(result)) }
func Deploy(rend render.Render, req *http.Request, params martini.Params) { id := helper.Int64(params["id"]) host, errResponse := getClientWithAliveOrJSONError(id) if host == nil { rend.JSON(200, errResponse) return } host.SetBusy() result, err := host.Deploy() if err != nil { rend.JSON(200, helper.Error(err)) return } rend.JSON(200, helper.Success(result)) }
func RemoveBackup(rend render.Render, req *http.Request, params martini.Params) { id := helper.Int64(params["id"]) host, errResponse := getClientWithAliveOrJSONError(id) if host == nil { rend.JSON(200, errResponse) return } body, _ := jason.NewObjectFromReader(req.Body) path, _ := body.GetString("path") _, err := host.RemoveBackup(path) if err != nil { rend.JSON(200, helper.Error(err)) return } rend.JSON(200, helper.Success()) }
func Add(rend render.Render, req *http.Request) { cfg := model.Config{ Name: "hahaha", Content: JSON.Stringify(JSON.Type{ "Name": "languid", "XX": "jeremy", "isTest": true, "clients": []int{1, 2, 3, 4, 5}, }), } if _, err := db.Orm().Insert(&cfg); err != nil { rend.JSON(200, helper.Error(err)) return } result := JSON.Parse(cfg) result["Content"] = JSON.Parse(result["Content"]) rend.JSON(200, helper.Success(result)) }