Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

61
app/admin/main/cache/dao/BUILD vendored Normal file
View File

@ -0,0 +1,61 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"dao_test.go",
"opscache_test.go",
"overlord_test.go",
"tree_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/admin/main/cache/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
"//vendor/gopkg.in/h2non/gock.v1:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"opscache.go",
"overlord.go",
"tree.go",
],
importpath = "go-common/app/admin/main/cache/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/admin/main/cache/conf:go_default_library",
"//app/admin/main/cache/model:go_default_library",
"//library/database/orm:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//vendor/github.com/jinzhu/gorm:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

38
app/admin/main/cache/dao/dao.go vendored Normal file
View File

@ -0,0 +1,38 @@
package dao
import (
"context"
"go-common/app/admin/main/cache/conf"
"go-common/library/database/orm"
bm "go-common/library/net/http/blademaster"
"github.com/jinzhu/gorm"
)
// Dao dao.
type Dao struct {
c *conf.Config
DB *gorm.DB
client *bm.Client
}
// New new a dao and return.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
DB: orm.NewMySQL(c.MySQL),
client: bm.NewClient(c.HTTPClient),
}
return
}
// Ping check connection of db , mc.
func (d *Dao) Ping(c context.Context) (err error) {
return
}
// Close close connection of db , mc.
func (d *Dao) Close() {
}

45
app/admin/main/cache/dao/dao_test.go vendored Normal file
View File

@ -0,0 +1,45 @@
package dao
import (
"flag"
"os"
"strings"
"testing"
"go-common/app/admin/main/cache/conf"
gock "gopkg.in/h2non/gock.v1"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.common-arch.cache-admin")
flag.Set("conf_token", "02ee6e25ae6d3a2b3bb0e7311362d890")
flag.Set("tree_id", "42048")
flag.Set("conf_version", "docker-1")
flag.Set("deploy_env", "uat")
flag.Set("conf_host", "config.bilibili.co")
flag.Set("conf_path", "/tmp")
flag.Set("region", "sh")
flag.Set("zone", "sh001")
} else {
flag.Set("conf", "../cmd/test.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
m.Run()
os.Exit(0)
}
func httpMock(method, url string) *gock.Request {
r := gock.New(url)
r.Method = strings.ToUpper(method)
return r
}

39
app/admin/main/cache/dao/opscache.go vendored Normal file
View File

@ -0,0 +1,39 @@
package dao
import (
"context"
"go-common/app/admin/main/cache/model"
"go-common/library/log"
)
var (
opsMcURI = "http://ops-cache.bilibili.co/manager/redisapp/get_all_memcache_json"
opsRedisURI = "http://ops-cache.bilibili.co/manager/redisapp/get_all_redis_json"
)
// OpsMemcaches get all ops mc.
func (d *Dao) OpsMemcaches(c context.Context) (mcs []*model.OpsCacheMemcache, err error) {
var res struct {
Data []*model.OpsCacheMemcache `json:"data"`
}
if err = d.client.Get(c, opsMcURI, "", nil, &res); err != nil {
log.Error("ops memcache url(%s) error(%v)", opsMcURI, err)
return
}
mcs = res.Data
return
}
// OpsRediss get all ops redis.
func (d *Dao) OpsRediss(c context.Context) (mcs []*model.OpsCacheRedis, err error) {
var res struct {
Data []*model.OpsCacheRedis `json:"data"`
}
if err = d.client.Get(c, opsRedisURI, "", nil, &res); err != nil {
log.Error("ops redis url(%s) error(%v)", opsRedisURI, err)
return
}
mcs = res.Data
return
}

View File

@ -0,0 +1,32 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestOpsMemcaches(t *testing.T) {
convey.Convey("get OpsMemcaches", t, func(ctx convey.C) {
ctx.Convey("When http response code != 0", func(ctx convey.C) {
mcs, err := d.OpsMemcaches(context.Background())
ctx.Convey("Then err should not be nil", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(mcs, convey.ShouldNotBeNil)
})
})
})
}
func TestOpsRediss(t *testing.T) {
convey.Convey("get OpsRediss", t, func(ctx convey.C) {
ctx.Convey("When http response code != 0", func(ctx convey.C) {
mcs, err := d.OpsRediss(context.Background())
ctx.Convey("Then err should not be nil", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(mcs, convey.ShouldNotBeNil)
})
})
})
}

65
app/admin/main/cache/dao/overlord.go vendored Normal file
View File

@ -0,0 +1,65 @@
package dao
import (
"context"
"net"
"strconv"
"go-common/app/admin/main/cache/model"
"go-common/library/log"
)
var (
apiserverURI = "http://cache-mng.bilibili.co/api/v1/appids/%s"
)
// OverlordClusters get all overlord clusters.
func (d *Dao) OverlordClusters(c context.Context, zone, appid string) (ocs []*model.OverlordCluster, err error) {
var res struct {
Data []*model.OverlordApiserver `json:"grouped_clusters"`
}
if err = d.client.RESTfulGet(c, apiserverURI, "", nil, &res, appid); err != nil {
log.Error("overlord cluster url(%s) appid(%s) error(%v)", apiserverURI, appid, err)
return
}
GETALL:
for _, oa := range res.Data {
if zone == "" || oa.Group == zone {
for _, oc := range oa.Clusters {
cluster := &model.OverlordCluster{
Name: oc.Name,
Type: oc.Type,
Zone: zone,
HashMethod: "fnv1a_64",
HashDistribution: "ketama",
HashTag: "{}",
ListenProto: "tcp",
ListenAddr: net.JoinHostPort("0.0.0.0", strconv.Itoa(oc.FrontEndPort)),
DailTimeout: 1000,
ReadTimeout: 1000,
WriteTimeout: 1000,
NodeConn: 2,
PingFailLimit: 3,
PingAutoEject: true,
}
for _, oci := range oc.Instances {
if oc.Type == "redis_cluster" && oci.Role != "master" {
continue
}
on := &model.OverlordNode{
Alias: oci.Alias,
Addr: net.JoinHostPort(oci.IP, strconv.Itoa(oci.Port)),
Weight: oci.Weight,
}
cluster.Nodes = append(cluster.Nodes, on)
}
ocs = append(ocs, cluster)
}
}
}
if len(ocs) == 0 && zone != "" {
zone = ""
goto GETALL
}
return
}

View File

@ -0,0 +1,20 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestOverlordClusters(t *testing.T) {
convey.Convey("get OverlordClusters", t, func(ctx convey.C) {
ctx.Convey("When http response code != 0", func(ctx convey.C) {
ocs, err := d.OverlordClusters(context.Background(), "", "main.common-arch.overlord")
ctx.Convey("Then err should not be nil", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(ocs, convey.ShouldNotBeNil)
})
})
})
}

201
app/admin/main/cache/dao/tree.go vendored Normal file
View File

@ -0,0 +1,201 @@
package dao
import (
"bytes"
"context"
"fmt"
"net/http"
"strings"
"time"
"go-common/app/admin/main/cache/model"
"go-common/library/ecode"
"go-common/library/log"
)
var (
tokenURI = "http://easyst.bilibili.co/v1/token"
dataURI = "http://easyst.bilibili.co/v1/node/apptree"
authURI = "http://easyst.bilibili.co/v1/auth"
nodeURI = "http://easyst.bilibili.co/v1/node/bilibili%s"
appsURI = "http://easyst.bilibili.co/v1/node/role/app"
prefix = []byte("bilibili.")
)
// Token get Token.
func (d *Dao) Token(c context.Context, body string) (msg map[string]interface{}, err error) {
var (
req *http.Request
)
if req, err = http.NewRequest("POST", tokenURI, strings.NewReader(body)); err != nil {
log.Error("Token url(%s) error(%v)", tokenURI, err)
return
}
var res struct {
Code int `json:"code"`
Data map[string]interface{} `json:"data"`
Message string `json:"message"`
Status int `json:"status"`
}
if err = d.client.Do(c, req, &res); err != nil {
log.Error("d.Token url(%s) res(%+v) err(%v)", tokenURI, res, err)
return
}
if res.Code != 90000 {
err = fmt.Errorf("error code :%d", res.Code)
log.Error("Status url(%s) res(%v)", tokenURI, res)
return
}
msg = res.Data
return
}
// Auth get Token.
func (d *Dao) Auth(c context.Context, cookie string) (msg map[string]interface{}, err error) {
var (
req *http.Request
)
if req, err = http.NewRequest("GET", authURI, nil); err != nil {
log.Error("Token url(%s) error(%v)", tokenURI, err)
return
}
req.Header.Set("Cookie", cookie)
var res struct {
Code int `json:"code"`
Data map[string]interface{} `json:"data"`
Message string `json:"message"`
Status int `json:"status"`
}
if err = d.client.Do(c, req, &res); err != nil {
log.Error("d.Token url(%s) res(%s) err(%v)", tokenURI, res, err)
return
}
if res.Code != 90000 {
err = fmt.Errorf("error code :%d", res.Code)
log.Error("Status url(%s) res(%v)", tokenURI, res)
return
}
msg = res.Data
return
}
// Tree get service tree.
func (d *Dao) Tree(c context.Context, token string) (data interface{}, err error) {
var (
req *http.Request
tmp map[string]interface{}
ok bool
)
if req, err = http.NewRequest("GET", dataURI, nil); err != nil {
log.Error("Status url(%s) error(%v)", dataURI, err)
return
}
req.Header.Set("X-Authorization-Token", token)
req.Header.Set("Content-Type", "application/json")
var res struct {
Code int `json:"code"`
Data map[string]map[string]interface{} `json:"data"`
Message string `json:"message"`
Status int `json:"status"`
}
if err = d.client.Do(c, req, &res); err != nil {
log.Error("d.Status url(%s) res($s) err(%v)", dataURI, res, err)
return
}
if res.Code != 90000 {
err = fmt.Errorf("error code :%d", res.Code)
log.Error("Status url(%s) res(%v)", dataURI, res)
return
}
if tmp, ok = res.Data["bilibili"]; ok {
data, ok = tmp["children"]
}
if !ok {
err = ecode.NothingFound
}
return
}
// Role get service tree.
func (d *Dao) Role(c context.Context, token string) (nodes *model.CacheData, err error) {
var (
req *http.Request
)
if req, err = http.NewRequest("GET", appsURI, nil); err != nil {
log.Error("Status url(%s) error(%v)", dataURI, err)
return
}
req.Header.Set("X-Authorization-Token", token)
req.Header.Set("Content-Type", "application/json")
var res struct {
Code int `json:"code"`
Data []*model.RoleNode `json:"data"`
Message string `json:"message"`
Status int `json:"status"`
}
if err = d.client.Do(c, req, &res); err != nil {
log.Error("d.Status url(%s) res($s) err(%v)", dataURI, res, err)
return
}
if res.Code != 90000 {
err = fmt.Errorf("error code :%d", res.Code)
log.Error("Status url(%s) res(%v)", dataURI, res)
return
}
nodes = &model.CacheData{Data: make(map[int64]*model.RoleNode)}
nodes.CTime = time.Now()
for _, node := range res.Data {
if bytes.Equal(prefix, []byte(node.Path)[0:9]) {
node.Path = string([]byte(node.Path)[9:])
}
nodes.Data[node.ID] = node
}
return
}
// NodeTree get service tree.
func (d *Dao) NodeTree(c context.Context, token, bu, team string) (nodes []*model.Node, err error) {
var (
req *http.Request
node string
)
if len(bu) != 0 {
node = "." + bu
}
if len(team) != 0 {
node = "." + team
}
if len(node) == 0 {
nodes = append(nodes, &model.Node{Name: "main", Path: "main"})
nodes = append(nodes, &model.Node{Name: "ai", Path: "ai"})
return
}
if req, err = http.NewRequest("GET", fmt.Sprintf(nodeURI, node), nil); err != nil {
log.Error("Status url(%s) error(%v)", dataURI, err)
return
}
req.Header.Set("X-Authorization-Token", token)
req.Header.Set("Content-Type", "application/json")
var res struct {
Code int `json:"code"`
Data *model.Res `json:"data"`
Message string `json:"message"`
Status int `json:"status"`
}
if err = d.client.Do(c, req, &res); err != nil {
log.Error("d.Status url(%s) res($s) err(%v)", dataURI, res, err)
return
}
if res.Code != 90000 {
err = fmt.Errorf("error code :%d", res.Code)
log.Error("Status url(%s) error(%v)", dataURI, err)
return
}
for _, tnode := range res.Data.Data {
if bytes.Equal(prefix, []byte(tnode.Path)[0:9]) {
tnode.Path = string([]byte(tnode.Path)[9:])
}
nodes = append(nodes, &model.Node{Name: tnode.Name, Path: tnode.Path})
}
return
}

137
app/admin/main/cache/dao/tree_test.go vendored Normal file
View File

@ -0,0 +1,137 @@
package dao
import (
"context"
"net/http"
"testing"
"github.com/smartystreets/goconvey/convey"
gock "gopkg.in/h2non/gock.v1"
)
var token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiJtYWluIiwicGxhdGZvcm1faWQiOiIyRjNiOGZEVkdsTW5qOGFDRGxNYVciLCJleHAiOjE1NDA2NDE3MjQsImlzcyI6Im1haW4ifQ.SxKceDl69N1su3kDO70c4P1NuPhXOxp5rXpM3n8Jyig"
func TestToken(t *testing.T) {
d.client.SetTransport(gock.DefaultTransport)
convey.Convey("get token", t, func(ctx convey.C) {
ctx.Convey("When http response code != 0", func(ctx convey.C) {
httpMock("POST", "http://easyst.bilibili.co/v1/token").Reply(200).JSON(`{
"code": 90000,
"data": {
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiJtYWluIiwicGxhdGZvcm1faWQiOiIyRjNiOGZEVkdsTW5qOGFDRGxNYVciLCJleHAiOjE1NDA2NDE3MjQsImlzcyI6Im1haW4ifQ.SxKceDl69N1su3kDO70c4P1NuPhXOxp5rXpM3n8Jyig",
"user_name": "main",
"secret": "",
"expired": 1540641724
},
"message": "success",
"status": 200
}`)
data, err := d.Token(context.Background(), "")
ctx.Convey("Then err should not be nil", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(data, convey.ShouldNotBeNil)
})
})
ctx.Reset(func() {
gock.Off()
d.client.SetTransport(http.DefaultClient.Transport)
})
})
}
func TestAuth(t *testing.T) {
d.client.SetTransport(gock.DefaultTransport)
convey.Convey("get auth", t, func(ctx convey.C) {
ctx.Convey("When http response code != 0", func(ctx convey.C) {
httpMock("GET", "http://easyst.bilibili.co/v1/auth").Reply(200).JSON(`{"code":90000,"message":"success"}`)
_, err := d.Auth(context.Background(), "sven-apm=afab110001a20fa3a1c9b5bd67564ccd71c6253406725184bfa5d88c7ece9d17; Path=/; Domain=bilibili.co; Expires=Tue, 23 Oct 2018 07:25:02 GMT; Max-Age=1800; HttpOnly")
ctx.Convey("Then err should not be nil", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
// ctx.So(data, convey.ShouldNotBeNil)
})
})
ctx.Reset(func() {
gock.Off()
d.client.SetTransport(http.DefaultClient.Transport)
})
})
}
func TestRole(t *testing.T) {
d.client.SetTransport(gock.DefaultTransport)
convey.Convey("TestRole", t, func(ctx convey.C) {
ctx.Convey("When http response code != 0", func(ctx convey.C) {
httpMock("GET", "http://easyst.bilibili.co/v1/node/role/app").Reply(200).JSON(`{"code":90000,"message":"success"}`)
data, err := d.Role(context.Background(), token)
ctx.Convey("Then err should not be nil", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(data, convey.ShouldNotBeNil)
})
})
ctx.Reset(func() {
gock.Off()
d.client.SetTransport(http.DefaultClient.Transport)
})
})
}
func TestNodeTree(t *testing.T) {
d.client.SetTransport(gock.DefaultTransport)
convey.Convey("NodeTree", t, func(ctx convey.C) {
ctx.Convey("When http response code != 0", func(ctx convey.C) {
httpMock("GET", "http://easyst.bilibili.co/v1/node/bilibili.main.common-arch").Reply(200).JSON(`{
"status": 200,
"message": "success",
"code": 90000,
"data": {
"count": 0,
"results": 20,
"page": 1,
"data": null
}
}`)
_, err := d.NodeTree(context.Background(), token, "main.common-arch", "")
ctx.Convey("Then err should not be nil", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
// ctx.So(data, convey.ShouldNotBeNil)
})
})
ctx.Reset(func() {
gock.Off()
d.client.SetTransport(http.DefaultClient.Transport)
})
})
}
func TestTree(t *testing.T) {
d.client.SetTransport(gock.DefaultTransport)
convey.Convey("TestTree", t, func(ctx convey.C) {
ctx.Convey("When http response code != 0", func(ctx convey.C) {
httpMock("GET", "http://easyst.bilibili.co/v1/node/apptree").Reply(200).JSON(`{
"code": 90000,
"data": {
"bilibili": {
"id": 0,
"name": "bilibili",
"alias": "哔哩哔哩",
"type": 1,
"path": "bilibili",
"tags": {},
"children": null
}
},
"message": "success",
"status": 200
}`)
_, err := d.Tree(context.Background(), token)
ctx.Convey("Then err should not be nil", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
// ctx.So(data, convey.ShouldNotBeNil)
})
})
ctx.Reset(func() {
gock.Off()
d.client.SetTransport(http.DefaultClient.Transport)
})
})
}