func getBatchDelTagsName(reponame, itemname, tagname string) ([]string, error) { log.Println("Batch delete tags from db") sqlrpdmid := fmt.Sprintf(`SELECT RPDMID FROM DH_DP_RPDM_MAP WHERE REPOSITORY='%s' AND DATAITEM='%s' AND STATUS='A';`, reponame, itemname) var rpdmId int row, err := g_ds.QueryRow(sqlrpdmid) if err != nil { l := log.Error("select rpdmid from DH_DP_RPDM_MAP error:", err) logq.LogPutqueue(l) return nil, err } row.Scan(&rpdmId) tagname = strings.Replace(tagname, "*", "%", -1) log.Println(tagname) sql := fmt.Sprintf(`SELECT TAGNAME FROM DH_RPDM_TAG_MAP WHERE TAGNAME LIKE '%s' AND RPDMID=%d AND STATUS='A';`, tagname, rpdmId) //var tagnames []string tagsname := make([]string, 0) rows, err := g_ds.QueryRows(sql) if err != nil { l := log.Error("batch delete tag from DH_RPDM_TAG_MAP error:", err) logq.LogPutqueue(l) return nil, err } for rows.Next() { rows.Scan(&tagname) tagsname = append(tagsname, tagname) } log.Println(tagsname) /*if len(tagsname) == 0 { return nil, errors.New("没有匹配的tag") }*/ return tagsname, nil }
func DeleteItemOrTag(repo, item, tag string) (err error) { uri := "/repositories/" if len(tag) == 0 { uri = uri + repo + "/" + item } else { uri = uri + repo + "/" + item + "/" + tag } log.Println(uri) req, err := http.NewRequest("DELETE", DefaultServerAPI+uri, nil) if len(loginAuthStr) > 0 { req.Header.Set("Authorization", loginAuthStr) } if err != nil { return err } //req.Header.Set("User", "admin") resp, err := http.DefaultClient.Do(req) if err != nil { return err } defer resp.Body.Close() if resp.StatusCode == http.StatusOK { return nil } else { body, _ := ioutil.ReadAll(resp.Body) log.Println(resp.StatusCode, string(body)) return errors.New(fmt.Sprintf("%d", resp.StatusCode)) } return err }
func GetTagComment(repo, item, tag string) string { path := "/api/repositories/" + repo + "/" + item + "/" + tag resp, err := commToServerGetRsp("get", path, nil) if err != nil { log.Error(err) return "" } defer resp.Body.Close() if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusBadRequest { err = errors.New("unkown error") log.Error("GET", path, resp.StatusCode) return "" } result := ds.Response{} struComment := &struct { Comment string `json:"comment"` }{} result.Data = struComment respbody, _ := ioutil.ReadAll(resp.Body) log.Println(string(respbody)) unmarshalerr := json.Unmarshal(respbody, &result) if unmarshalerr != nil { log.Error(unmarshalerr) return "" } log.Println(result) return struComment.Comment }
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 RollBackTag(repo, item, tag string) { //Delete /repository/repo/item tag log.Println(repo, "/", item, ":", tag) err := DeleteItemOrTag(repo, item, tag) if err != nil { log.Println("DeleteTag err ", err.Error()) } }
func RollBackItem(repo, item string) { //Delete /repository/repo/item log.Println(repo, "/", item) err := DeleteItemOrTag(repo, item, "") if err != nil { log.Println("DeleteItem err ", err.Error()) } }
func rollbackDelTag(tagid int) error { log.Println("TODO rollback delete tag from db") log.Println(tagid) sql := fmt.Sprintf(`UPDATE DH_RPDM_TAG_MAP SET STATUS='A' WHERE TAGID=%d`, tagid) _, err := g_ds.Update(sql) if err != nil { l := log.Error("rollback delete tag error:", err) logq.LogPutqueue(l) return err } return nil }
func GetDataPoolStatusByID(dpid int) (status string) { sqlGetDpStatus := fmt.Sprintf("SELECT STATUS FROM DH_DP WHERE DPID=%d", dpid) row, err := g_ds.QueryRow(sqlGetDpStatus) if err != nil { log.Println(sqlGetDpStatus) log.Println(err.Error()) return } row.Scan(&status) if status != "A" { log.Println("dpid:", dpid, " status:", status) } return }
func GetSampleData(itempath string) (sample string) { var filename string for _, v := range SampleFiles { filename = itempath + "/" + v if isFileExists(filename) == true { if bytes, err := ioutil.ReadFile(filename); err == nil { sample = string(bytes) return sample } else { l := log.Error(err) logq.LogPutqueue(l) } } } d, err := os.Open(itempath) //ppen dir if err != nil { log.Println(err) return "" } defer d.Close() ff, _ := d.Readdir(10) // return []fileinfo for i, fi := range ff { log.Printf("sample filename %d: %+v\n", i, fi.Name()) filename = strings.ToLower(fi.Name()) if filename != "sample.md" && filename != "meta.md" && filename != PriceFile { f, err := os.Open(itempath + "/" + fi.Name()) log.Println("filename:", itempath+"/"+fi.Name()) if err != nil { continue } defer f.Close() scanner := bufio.NewScanner(f) scanner.Split(bufio.ScanLines) var i = 0 for scanner.Scan() { if i > 9 { break } i++ sample += scanner.Text() + " \n" //md " \n" is a new line //log.Println(scanner.Text()) } break } } log.Debug("sample data:", sample) //need lenth check return sample }
func InsertItemToDb(repo, item, datapool, itemdesc, createTime string) (err error) { log.Println("Insert item to db") dpid := GetDataPoolDpid(datapool) if dpid > 0 { //sqlInsertItem := fmt.Sprintf(`INSERT INTO DH_DP_RPDM_MAP (RPDMID, REPOSITORY, DATAITEM, ITEMDESC, DPID, PUBLISH, CREATE_TIME, STATUS) // VALUES (null, '%s', '%s', '%s', %d, 'Y', datetime('now'), 'A')`, repo, item, itemdesc, dpid) sqlInsertItem := fmt.Sprintf(`INSERT INTO DH_DP_RPDM_MAP (RPDMID, REPOSITORY, DATAITEM, ITEMDESC, DPID, PUBLISH, CREATE_TIME, STATUS) VALUES (null, '%s', '%s', '%s', %d, 'Y', '%s', 'A')`, repo, item, itemdesc, dpid, createTime) _, err = g_ds.Insert(sqlInsertItem) log.Println(sqlInsertItem) } else { err = errors.New("dpid is not found") } return err }
func InsertTagToDb(dpexist bool, p ds.DsPull, tagcom string) (err error) { if dpexist == false { return } DpId := GetDataPoolDpid(p.Datapool) if DpId == 0 { return } rpdmid := GetRepoItemId(p.Repository, p.Dataitem) if rpdmid == 0 { sqlInsertRpdm := fmt.Sprintf(`INSERT INTO DH_DP_RPDM_MAP (RPDMID ,REPOSITORY, DATAITEM, DPID, PUBLISH ,CREATE_TIME ,STATUS, ITEMDESC) VALUES (null, '%s', '%s', %d, 'N', datetime('now'), 'A', '%s')`, p.Repository, p.Dataitem, DpId, p.ItemDesc) g_ds.Insert(sqlInsertRpdm) rpdmid = GetRepoItemId(p.Repository, p.Dataitem) } sqlInsertTag := fmt.Sprintf(`INSERT INTO DH_RPDM_TAG_MAP(TAGID, TAGNAME ,RPDMID ,DETAIL,CREATE_TIME, STATUS, COMMENT) VALUES (null, '%s', %d, '%s', datetime('now'), 'A', '%s')`, p.Tag, rpdmid, p.DestName, tagcom) log.Println(sqlInsertTag) _, err = g_ds.Insert(sqlInsertTag) return err }
func dl(uri, ip string, p ds.DsPull, w http.ResponseWriter, c chan int) error { if len(ip) == 0 { ip = "http://localhost:65535" //TODO return } target := ip + uri log.Println(target) n, err := download(target, p, w, c) if err != nil { log.Printf("[%d bytes returned.]\n", n) log.Println(err) } return err }
func delTagsForDelItem(reponame, itemname string) error { log.Println("Begin to remove tags for remove item from db") sqlrpdmid := fmt.Sprintf(`SELECT RPDMID FROM DH_DP_RPDM_MAP WHERE REPOSITORY='%s' AND DATAITEM='%s' AND STATUS='A';`, reponame, itemname) row, err := g_ds.QueryRow(sqlrpdmid) if err != nil { l := log.Error("select rpdmid from DH_DP_RPDM_MAP error:", err) logq.LogPutqueue(l) return err } var rpdmId int row.Scan(&rpdmId) if rpdmId == 0 { log.Debug(reponame, itemname, "not exist.") return nil } sqldeltag := fmt.Sprintf(`UPDATE DH_RPDM_TAG_MAP SET STATUS='N' WHERE RPDMID=%d`, rpdmId) _, err = g_ds.Update(sqldeltag) log.Info("sqldeltag", sqldeltag) if err != nil { l := log.Error("delete tag error:", err) logq.LogPutqueue(l) return err } return nil }
func saveDaemonID(id string) { log.Println("TODO save daemonid to db when srv returns code 0.") count := `SELECT COUNT(*) FROM DH_DAEMON;` row, err := g_ds.QueryRow(count) if err != nil { l := log.Error(count, "error.", err) logq.LogPutqueue(l) } var c int row.Scan(&c) if c > 0 { Update := fmt.Sprintf(`UPDATE DH_DAEMON SET DAEMONID='%s';`, id) log.Debug(Update) if _, e := g_ds.Update(Update); e != nil { l := log.Error(Update, "error.", e) logq.LogPutqueue(l) } } else { Insert := fmt.Sprintf(`INSERT INTO DH_DAEMON (DAEMONID) VALUES ('%s');`, id) log.Debug(c, Insert) if _, e := g_ds.Insert(Insert); e != nil { l := log.Error(Insert, "error.", e) logq.LogPutqueue(l) } } }
func repoItemHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { log.Println(r.URL.Path, "(repo/item)") reqBody, _ := ioutil.ReadAll(r.Body) commToServer("get", r.URL.Path+"?size=-1", reqBody, w) return }
func stopHttp(rw http.ResponseWriter, req *http.Request) { rw.WriteHeader(http.StatusOK) //fmt.Fprintf(rw, "Hello HTTP!\n") sl.Close() p2psl.Close() log.Println("connect close") }
func saveEntryPoint(ep string) { log.Println("TODO save ep to db") count := `SELECT COUNT(*) FROM DH_DAEMON;` row, err := g_ds.QueryRow(count) if err != nil { l := log.Error(count, "error.", err) logq.LogPutqueue(l) } var c int row.Scan(&c) if c > 0 { Update := fmt.Sprintf(`UPDATE DH_DAEMON SET ENTRYPOINT='%s';`, ep) log.Debug(Update) if _, e := g_ds.Update(Update); e != nil { l := log.Error(Update, "error.", e) logq.LogPutqueue(l) } } else { Insert := fmt.Sprintf(`INSERT INTO DH_DAEMON (ENTRYPOINT) VALUES ('%s');`, ep) log.Debug(c, Insert) if _, e := g_ds.Insert(Insert); e != nil { l := log.Error(Insert, "error.", e) logq.LogPutqueue(l) } } }
func delTag(reponame, itemname, tagname string) (int, error) { log.Println("TODO delete tag from db") sqlrpdmid := fmt.Sprintf(`SELECT RPDMID FROM DH_DP_RPDM_MAP WHERE REPOSITORY='%s' AND DATAITEM='%s' AND STATUS='A';`, reponame, itemname) var rpdmId int row, err := g_ds.QueryRow(sqlrpdmid) if err != nil { l := log.Error("select rpdmid from DH_DP_RPDM_MAP error:", err) logq.LogPutqueue(l) return 0, err } row.Scan(&rpdmId) sql := fmt.Sprintf(`SELECT TAGID FROM DH_RPDM_TAG_MAP WHERE STATUS='A' AND TAGNAME='%s' AND RPDMID=%d`, tagname, rpdmId) var tagid int row, err = g_ds.QueryRow(sql) if err != nil { l := log.Error("select tagid from DH_DP_RPDM_MAP error:", err) logq.LogPutqueue(l) return 0, err } row.Scan(&tagid) sql = fmt.Sprintf(`UPDATE DH_RPDM_TAG_MAP SET STATUS='N' WHERE TAGNAME='%s' AND RPDMID=%d`, tagname, rpdmId) _, err = g_ds.Update(sql) if err != nil { l := log.Error("delete tag from DH_RPDM_TAG_MAP error:", err) logq.LogPutqueue(l) return 0, err } return tagid, nil }
func delEntryPoint() { log.Println("TODO remove ep from db.") d := `UPDATE DH_DAEMON SET ENTRYPOINT = '';` if _, e := g_ds.Update(d); e != nil { l := log.Error(d, "error.", e) logq.LogPutqueue(l) } }
func InsertPubTagToDb(repo, item, tag, fileName, comment string) (err error) { log.Println("Insert pub tag to db.") rpdmid := GetRepoItemId(repo, item) if rpdmid == 0 { return errors.New("Dataitem is not found which need to be published before publishing tag. ") } sqlInsertTag := fmt.Sprintf("INSERT INTO DH_RPDM_TAG_MAP (TAGID, TAGNAME, RPDMID, DETAIL, CREATE_TIME, STATUS, COMMENT) VALUES (null, '%s', %d, '%s', datetime('now'), 'A', '%s');", tag, rpdmid, fileName, comment) log.Println(sqlInsertTag) _, err = g_ds.Insert(sqlInsertTag) if err != nil { l := log.Error("Insert tag into db error:", err) logq.LogPutqueue(l) return err } return }
func userStatusHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { log.Println(r.URL.Path, "(heartbeat/status)") reqBody, _ := ioutil.ReadAll(r.Body) commToServer("get", r.URL.Path, reqBody, w) return }
func isFileExists(file string) bool { fi, err := os.Stat(file) if err == nil { log.Println("exist", file) return !fi.IsDir() } return os.IsExist(err) }
func subsHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { log.Println(r.URL.RequestURI(), "(subs)") reqBody, _ := ioutil.ReadAll(r.Body) commToServer("get", r.URL.RequestURI(), reqBody, w) return }
func (fs *fsdriver) CheckItemLocation(datapoolname, dpconn, itemlocation string) error { log.Println(dpconn + "/" + itemlocation) err := os.MkdirAll(dpconn+"/"+itemlocation, 0777) if err != nil { log.Error(err) } return err }
func logoutHandler(w http.ResponseWriter, r *http.Request) { log.Println("logout.") if loginAuthStr == "" { w.WriteHeader(http.StatusUnauthorized) } else { loginAuthStr = "" w.WriteHeader(http.StatusOK) } }
func GetDpnameDpconnItemdesc(repo, item string) (dpname, dpconn, dptype, itemDesc string) { _, dpid, itemDesc := GetRpdmidDpidItemdesc(repo, item) if dpid == 0 { log.Println(" dpid==0") return } dpname, dpconn, dptype = GetDpnameDpconnDptypeByDpid(dpid) return }
func GetRpdmidDpidItemdesc(repo, item string) (rpdmid, dpid int, Itemdesc string) { sqlGetRpdmidDpidItemdesc := fmt.Sprintf("SELECT RPDMID, DPID, ITEMDESC FROM DH_DP_RPDM_MAP WHERE REPOSITORY='%s' AND DATAITEM='%s' AND STATUS='A'", repo, item) row, err := g_ds.QueryRow(sqlGetRpdmidDpidItemdesc) if err != nil { fmt.Println(err.Error()) return } row.Scan(&rpdmid, &dpid, &Itemdesc) status := GetDataPoolStatusByID(dpid) if rpdmid == 0 || dpid == 0 || len(Itemdesc) == 0 { log.Println("rpdmid, dpid, Itemdesc :", rpdmid, dpid, Itemdesc) log.Println("datapool status:", status) } if status != "A" { return 0, 0, "" } return }
func PullOneTagAutomatic(p ds.DsPull, c chan int) { var ret string var w *httptest.ResponseRecorder = httptest.NewRecorder() uri := "/transaction/" + p.Repository + "/" + p.Dataitem + "/" + p.Tag token, entrypoint, err := getAccessToken(uri, w) if err != nil { log.Println(err) ret = err.Error() return } else { uri = "/pull/" + p.Repository + "/" + p.Dataitem + "/" + p.Tag + "?token=" + token + "&username=" + url.QueryEscape(gstrUsername) go dl(uri, entrypoint, p, w, c) } log.Println(ret) }
func delItem(reponame, itemname string) (err error) { log.Println("Begin to remove item from db") sql := fmt.Sprintf(`UPDATE DH_DP_RPDM_MAP SET STATUS = 'N' WHERE REPOSITORY='%s' AND DATAITEM='%s';`, reponame, itemname) if _, err := g_ds.Update(sql); err != nil { l := log.Error("delete item error:", err) logq.LogPutqueue(l) return err } return nil }
func rollbackDelItem(reponame, itemname string) error { log.Println("TODO rollback delete item from db") sql := fmt.Sprintf(`UPDATE DH_DP_RPDM_MAP SET STATUS='A' WHERE REPOSITORY='%s' AND DATAITEM='%s';`, reponame, itemname) if _, err := g_ds.Update(sql); err != nil { l := log.Error("rollback delete item error:", err) logq.LogPutqueue(l) return err } return nil }