func checkAccessToken(tokenUrl string) (bool, string) { log.Println("daemon: connecting to", DefaultServerAPI+tokenUrl) req, err := http.NewRequest("GET", DefaultServerAPI+tokenUrl, nil) if len(loginAuthStr) > 0 { req.Header.Set("Authorization", loginAuthStr) } resp, err := http.DefaultClient.Do(req) if err != nil { return false, "" } defer resp.Body.Close() body, _ := ioutil.ReadAll(resp.Body) type tokenDs struct { Valid bool `json:"valid"` } tkresp := tokenDs{} result := &ds.Result{Data: &tkresp} if err = json.Unmarshal(body, &result); err != nil { log.Println(err) return false, "" } log.Trace(string(body)) return tkresp.Valid, result.Msg }
func jobRmAllHandler(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { log.Trace("from", req.RemoteAddr, req.Method, req.URL.RequestURI(), req.Proto) DatahubJob = make([]ds.JobInfo, 0) msg, code, httpcode := "Remove all jobs OK.", cmd.ResultOK, http.StatusOK if err := removeAllJobDB(); err != nil { msg, code, httpcode = fmt.Sprintln("Remove all jobs error.", err), cmd.ErrorRemoveAllJobs, http.StatusOK } r, _ := buildResp(code, msg, nil) w.WriteHeader(httpcode) w.Write(r) }
func GetItemslocationInDatapool(itemslocation map[string]string, dpname string, dpid int, dpconn string) error { sql := fmt.Sprintf("SELECT DISTINCT ITEMDESC, REPOSITORY, DATAITEM FROM DH_DP_RPDM_MAP WHERE DPID=%v AND STATUS='A';", dpid) log.Debug(sql) rows, err := g_ds.QueryRows(sql) if err != nil { l := log.Errorf("datapool name %s, dpid %v, dpconn %v, error:%v", dpname, dpid, dpconn, err) logq.LogPutqueue(l) return err } var location, repo, item string for rows.Next() { rows.Scan(&location, &repo, &item) log.Debug(location, repo, item) itemslocation[location] = repo + "/" + item } log.Trace(itemslocation) return err }
func jobDetailHandler(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { log.Trace("from", req.RemoteAddr, req.Method, req.URL.RequestURI(), req.Proto) jobid := ps.ByName("id") var job []ds.JobInfo for _, v := range DatahubJob { if v.ID == jobid { if v.Stat == "downloading" { v.Dlsize, _ = GetFileSize(v.Path) } job = append(job, v) } } r, _ := buildResp(0, "ok", job) w.WriteHeader(http.StatusOK) w.Write(r) return }
func jobRmHandler(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { log.Trace("from", req.RemoteAddr, req.Method, req.URL.RequestURI(), req.Proto) jobid := ps.ByName("id") msg, code, httpcode := fmt.Sprintf("job '%s' not found.", jobid), 4404, http.StatusNotFound for idx, v := range DatahubJob { if v.ID == jobid { removeJobDB(&DatahubJob[idx]) DatahubJob = append(DatahubJob[:idx], DatahubJob[idx+1:]...) msg, code, httpcode = fmt.Sprintf("job %s deleted.", jobid), 0, http.StatusOK break } } r, _ := buildResp(code, msg, nil) w.WriteHeader(httpcode) w.Write(r) return }
func (fs *fsdriver) GetDpOtherData(allotherdata *[]ds.DpOtherData, itemslocation map[string]string, dpconn string) (err error) { dir, err := ioutil.ReadDir(dpconn) if err != nil { return } obj := ds.DpOtherData{} for _, fi := range dir { if fi.IsDir() { obj.Dir = fi.Name() //To find other data that is not in itemslocation. if _, ok := itemslocation[obj.Dir]; !ok { obj.FileNum = GetFileCount(dpconn + "/" + obj.Dir) log.Trace(obj) *allotherdata = append(*allotherdata, obj) } } else { continue } } return }
func jobHandler(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { log.Trace("from", req.RemoteAddr, req.Method, req.URL.RequestURI(), req.Proto) req.ParseForm() all, _ := strconv.Atoi(req.Form.Get("all")) if all == 1 { for i := len(DatahubJob) - 1; i >= 0; i-- { if DatahubJob[i].Stat == "downloading" { DatahubJob[i].Dlsize, _ = GetFileSize(DatahubJob[i].Path) } } //r, _ := buildResp(0, "ok", joblist) r, _ := buildResp(0, "ok", DatahubJob) w.WriteHeader(http.StatusOK) w.Write(r) } else { var job []ds.JobInfo for _, v := range DatahubJob { if v.Stat == "downloading" { v.Dlsize, _ = GetFileSize(v.Path) job = append(job, v) } } //r, _ := buildResp(0, "ok", joblist) r, _ := buildResp(0, "ok", job) w.WriteHeader(http.StatusOK) w.Write(r) } return }
func HeartBeat() { getEp := false g_DaemonRole = GetDaemonRoleByPubRecord() for { if len(DaemonID) == 0 { log.Warn("No daemonid. You'd better start datahub with the parameter \"--token\".") return } heartbeatbody := Beatbody{Daemonid: DaemonID, Role: g_DaemonRole} if getEp == false && len(EntryPoint) == 0 { EntryPoint = getEntryPoint() getEp = true } if len(EntryPoint) != 0 { heartbeatbody.Entrypoint = append(heartbeatbody.Entrypoint, EntryPoint) } logQueue := logq.LogGetqueue() if len(logQueue) > 0 { heartbeatbody.Log = logQueue } errortags := checkErrortagsmap(&Errortagsmap) if len(errortags) != 0 { heartbeatbody.Errortag = errortags } heartbeatbody.CliEntrypoint = CLIEntrypoint + "?daemonauth=" + DaemonAuthrization jsondata, err := json.Marshal(heartbeatbody) if err != nil { l := log.Error(err) logq.LogPutqueue(l) } url := DefaultServerAPI + "/heartbeat" log.Trace("connecting to", url, string(jsondata)) req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsondata)) /* if len(loginAuthStr) > 0 { req.Header.Set("Authorization", loginAuthStr) } */ resp, err := http.DefaultClient.Do(req) if err != nil { l := log.Error(err) logq.LogPutqueue(l) time.Sleep(10 * time.Second) continue } defer resp.Body.Close() body, _ := ioutil.ReadAll(resp.Body) log.Tracef("HeartBeat http statuscode:%v, http body:%s", resp.StatusCode, body) result := ds.Result{} result.Data = &Event{} if err := json.Unmarshal(body, &result); err == nil { if result.Code == 0 { EntryPointStatus = "available" } else { EntryPointStatus = "not available" } } if result.Data != nil && result.Data.(*Event).Type == TYPE_ITEM_DEL { deleteItemsAccordingToHeartbeat(body) } time.Sleep(heartbeatTimeout) } }