Rainbond/pkg/api/region/node.go

402 lines
12 KiB
Go
Raw Normal View History

package region
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/Sirupsen/logrus"
"github.com/bitly/go-simplejson"
"github.com/goodrain/rainbond/pkg/node/api/model"
"github.com/pquerna/ffjson/ffjson"
//"github.com/goodrain/rainbond/pkg/grctl/cmd"
"errors"
"github.com/goodrain/rainbond/pkg/api/util"
2017-12-22 18:16:16 +08:00
utilhttp "github.com/goodrain/rainbond/pkg/util/http"
)
var nodeclient *RNodeClient
//NewNode new node client
func NewNode(nodeAPI string) {
if nodeclient == nil {
nodeclient = &RNodeClient{
NodeAPI: nodeAPI,
}
}
}
func GetNode() *RNodeClient {
return nodeclient
}
type RNodeClient struct {
2017-11-23 16:13:45 +08:00
NodeAPI string
}
func (r *RNodeClient) Tasks() TaskInterface {
2017-12-22 18:16:16 +08:00
return &task{client: r, prefix: "/tasks"}
}
func (r *RNodeClient) Nodes() NodeInterface {
2017-12-22 18:16:16 +08:00
return &node{client: r, prefix: "/nodes"}
}
func (r *RNodeClient) Configs() ConfigsInterface {
2017-12-22 18:16:16 +08:00
return &configs{client: r, prefix: "/configs"}
}
2017-12-21 16:44:31 +08:00
type task struct {
client *RNodeClient
prefix string
}
2017-12-21 16:44:31 +08:00
type node struct {
client *RNodeClient
prefix string
}
type TaskInterface interface {
2017-12-21 16:44:31 +08:00
Get(name string) (*model.Task, *util.APIHandleError)
Status(name string) (*TaskStatus, error)
2017-12-22 18:16:16 +08:00
HandleTaskStatus(task string) (*map[string]*model.TaskStatus, *util.APIHandleError)
2017-12-21 16:44:31 +08:00
Add(task *model.Task) *util.APIHandleError
AddGroup(group *model.TaskGroup) *util.APIHandleError
Exec(name string, nodes []string) *util.APIHandleError
List() ([]*model.Task, *util.APIHandleError)
Refresh() *util.APIHandleError
}
type NodeInterface interface {
2017-12-22 18:16:16 +08:00
Rule(rule string) ([]*model.HostNode, *util.APIHandleError)
Get(node string) (*model.HostNode, *util.APIHandleError)
List() ([]*model.HostNode, *util.APIHandleError)
Add(node *model.APIHostNode) *util.APIHandleError
2017-12-21 16:44:31 +08:00
Up(nid string) *util.APIHandleError
Down(nid string) *util.APIHandleError
UnSchedulable(nid string) *util.APIHandleError
ReSchedulable(nid string) *util.APIHandleError
Delete(nid string) *util.APIHandleError
2017-12-22 18:16:16 +08:00
Label(nid string, label map[string]string) *util.APIHandleError
}
//ConfigsInterface 数据中心配置API
type ConfigsInterface interface {
2017-12-21 16:44:31 +08:00
Get() (*model.GlobalConfig, *util.APIHandleError)
Put(*model.GlobalConfig) *util.APIHandleError
}
type configs struct {
client *RNodeClient
2017-12-21 16:44:31 +08:00
prefix string
}
2017-12-21 16:44:31 +08:00
func (c *configs) Get() (*model.GlobalConfig, *util.APIHandleError) {
body, code, err := c.client.Request(c.prefix+"/datacenter", "GET", nil)
if err != nil {
2017-12-22 18:16:16 +08:00
return nil, util.CreateAPIHandleError(code, err)
}
if code != 200 {
2017-12-22 18:16:16 +08:00
return nil, util.CreateAPIHandleError(code, fmt.Errorf("Get database center configs code %d", code))
}
var res utilhttp.ResponseBody
2017-12-22 18:16:16 +08:00
var gc = model.GlobalConfig{
Configs: make(map[string]*model.ConfigUnit),
}
res.Bean = &gc
if err := ffjson.Unmarshal(body, &res); err != nil {
2017-12-22 18:16:16 +08:00
return nil, util.CreateAPIHandleError(code, err)
}
if gc, ok := res.Bean.(*model.GlobalConfig); ok {
return gc, nil
}
return nil, nil
}
2017-12-21 16:44:31 +08:00
func (c *configs) Put(gc *model.GlobalConfig) *util.APIHandleError {
rebody, err := ffjson.Marshal(gc)
if err != nil {
2017-12-22 18:16:16 +08:00
return util.CreateAPIHandleError(400, err)
}
2017-12-21 16:44:31 +08:00
_, code, err := c.client.Request(c.prefix+"/datacenter", "PUT", rebody)
if err != nil {
2017-12-22 18:16:16 +08:00
return util.CreateAPIHandleError(code, err)
}
if code != 200 {
2017-12-22 18:16:16 +08:00
return util.CreateAPIHandleError(code, fmt.Errorf("Put database center configs code %d", code))
}
return nil
}
2017-12-22 18:16:16 +08:00
func (n *node) Get(node string) (*model.HostNode, *util.APIHandleError) {
2017-12-21 16:44:31 +08:00
body, code, err := n.client.Request(n.prefix+"/"+node, "GET", nil)
if err != nil {
2017-12-22 18:16:16 +08:00
return nil, util.CreateAPIHandleError(code, err)
}
2017-12-21 16:44:31 +08:00
if code != 200 {
2017-12-22 18:16:16 +08:00
return nil, util.CreateAPIHandleError(code, fmt.Errorf("Get database center configs code %d", code))
2017-11-27 12:06:28 +08:00
}
2017-12-21 16:44:31 +08:00
var res utilhttp.ResponseBody
var gc model.HostNode
res.Bean = &gc
2017-12-22 18:16:16 +08:00
logrus.Infof("res is %v", res)
2017-12-21 16:44:31 +08:00
if err := ffjson.Unmarshal(body, &res); err != nil {
2017-12-22 18:16:16 +08:00
return nil, util.CreateAPIHandleError(code, err)
}
2017-12-22 18:16:16 +08:00
logrus.Infof("res is %v", res)
2017-12-21 16:44:31 +08:00
if gc, ok := res.Bean.(*model.HostNode); ok {
return gc, nil
}
return nil, nil
}
2017-12-22 18:16:16 +08:00
func (n *node) Rule(rule string) ([]*model.HostNode, *util.APIHandleError) {
2017-12-21 16:44:31 +08:00
body, code, err := n.client.Request(n.prefix+"/rule/"+rule, "GET", nil)
2017-11-22 18:46:17 +08:00
if err != nil {
2017-12-22 18:16:16 +08:00
return nil, util.CreateAPIHandleError(code, err)
2017-11-22 18:46:17 +08:00
}
2017-12-21 16:44:31 +08:00
if code != 200 {
2017-12-22 18:16:16 +08:00
return nil, util.CreateAPIHandleError(code, fmt.Errorf("Get database center configs code %d", code))
2017-11-22 18:46:17 +08:00
}
2017-12-21 16:44:31 +08:00
var res utilhttp.ResponseBody
var gc []*model.HostNode
res.List = &gc
2017-12-21 16:44:31 +08:00
if err := ffjson.Unmarshal(body, &res); err != nil {
2017-12-22 18:16:16 +08:00
return nil, util.CreateAPIHandleError(code, err)
2017-11-23 16:13:45 +08:00
}
if gc, ok := res.List.(*[]*model.HostNode); ok {
return *gc, nil
2017-11-23 16:13:45 +08:00
}
2017-12-21 16:44:31 +08:00
return nil, nil
2017-11-22 18:46:17 +08:00
}
2017-12-22 18:16:16 +08:00
func (n *node) List() ([]*model.HostNode, *util.APIHandleError) {
2017-12-21 16:44:31 +08:00
body, code, err := n.client.Request(n.prefix, "GET", nil)
if err != nil {
2017-12-22 18:16:16 +08:00
return nil, util.CreateAPIHandleError(code, err)
}
2017-12-21 16:44:31 +08:00
if code != 200 {
2017-12-22 18:16:16 +08:00
return nil, util.CreateAPIHandleError(code, fmt.Errorf("Get database center configs code %d", code))
}
2017-12-21 16:44:31 +08:00
var res utilhttp.ResponseBody
var gc []*model.HostNode
res.List = &gc
2017-12-21 16:44:31 +08:00
if err := ffjson.Unmarshal(body, &res); err != nil {
2017-12-22 18:16:16 +08:00
return nil, util.CreateAPIHandleError(code, err)
}
if gc, ok := res.List.(*[]*model.HostNode); ok {
return *gc, nil
}
2017-12-21 16:44:31 +08:00
return nil, nil
}
2017-12-21 16:44:31 +08:00
2017-12-22 18:16:16 +08:00
// put/post 类
2017-12-21 16:44:31 +08:00
2017-12-22 18:16:16 +08:00
func (n *node) Add(node *model.APIHostNode) *util.APIHandleError {
2017-12-21 16:44:31 +08:00
body, err := json.Marshal(node)
2017-11-22 18:46:17 +08:00
if err != nil {
2017-12-22 18:16:16 +08:00
return util.CreateAPIHandleError(400, err)
2017-11-22 18:46:17 +08:00
}
2017-12-23 20:00:56 +08:00
_, code, err := n.client.Request(n.prefix, "POST", body)
2017-12-22 18:16:16 +08:00
return n.client.handleErrAndCode(err, code)
2017-12-21 16:44:31 +08:00
}
2017-12-22 18:16:16 +08:00
func (n *node) Label(nid string, label map[string]string) *util.APIHandleError {
2017-12-21 16:44:31 +08:00
body, err := json.Marshal(label)
2017-11-22 18:46:17 +08:00
if err != nil {
2017-12-22 18:16:16 +08:00
return util.CreateAPIHandleError(400, err)
2017-11-22 18:46:17 +08:00
}
2017-12-21 16:44:31 +08:00
_, code, err := n.client.Request(n.prefix+"/"+nid+"/labels", "PUT", body)
2017-12-22 18:16:16 +08:00
return n.client.handleErrAndCode(err, code)
2017-11-22 18:46:17 +08:00
}
2017-12-21 16:44:31 +08:00
2017-12-22 18:16:16 +08:00
func (n *node) Delete(nid string) *util.APIHandleError {
2017-12-21 16:44:31 +08:00
_, code, err := n.client.Request(n.prefix+"/"+nid, "DELETE", nil)
2017-12-22 18:16:16 +08:00
return n.client.handleErrAndCode(err, code)
2017-12-21 16:44:31 +08:00
}
func (n *node) Up(nid string) *util.APIHandleError {
2017-12-22 18:16:16 +08:00
_, code, err := n.client.Request(n.prefix+"/"+nid+"/up", "POST", nil)
return n.client.handleErrAndCode(err, code)
2017-12-21 16:44:31 +08:00
}
func (n *node) Down(nid string) *util.APIHandleError {
2017-12-22 18:16:16 +08:00
_, code, err := n.client.Request(n.prefix+"/"+nid+"/down", "POST", nil)
return n.client.handleErrAndCode(err, code)
2017-12-21 16:44:31 +08:00
}
2017-12-22 18:16:16 +08:00
func (n *node) UnSchedulable(nid string) *util.APIHandleError {
_, code, err := n.client.Request(n.prefix+"/"+nid+"/unschedulable", "PUT", nil)
return n.client.handleErrAndCode(err, code)
2017-12-21 16:44:31 +08:00
}
2017-12-22 18:16:16 +08:00
func (n *node) ReSchedulable(nid string) *util.APIHandleError {
_, code, err := n.client.Request(n.prefix+"/"+nid+"/reschedulable", "PUT", nil)
return n.client.handleErrAndCode(err, code)
2017-12-21 16:44:31 +08:00
}
func (t *task) Get(id string) (*model.Task, *util.APIHandleError) {
2017-12-22 18:16:16 +08:00
url := t.prefix + "/" + id
2017-12-21 16:44:31 +08:00
body, code, err := nodeclient.Request(url, "GET", nil)
if err != nil {
2017-12-22 18:16:16 +08:00
return nil, util.CreateAPIHandleError(code, err)
}
if code != 200 {
2017-12-22 18:16:16 +08:00
return nil, util.CreateAPIHandleError(code, fmt.Errorf("get task with code %d", code))
}
2017-12-21 16:44:31 +08:00
var res utilhttp.ResponseBody
var gc model.Task
res.Bean = &gc
if err := ffjson.Unmarshal(body, &res); err != nil {
2017-12-22 18:16:16 +08:00
return nil, util.CreateAPIHandleError(code, err)
}
2017-12-21 16:44:31 +08:00
if gc, ok := res.Bean.(*model.Task); ok {
return gc, nil
}
2017-12-21 16:44:31 +08:00
return nil, nil
}
//List list all task
2017-12-21 16:44:31 +08:00
func (t *task) List() ([]*model.Task, *util.APIHandleError) {
url := t.prefix
body, code, err := nodeclient.Request(url, "GET", nil)
if err != nil {
2017-12-22 18:16:16 +08:00
return nil, util.CreateAPIHandleError(code, err)
}
2017-12-21 16:44:31 +08:00
if code != 200 {
2017-12-22 18:16:16 +08:00
return nil, util.CreateAPIHandleError(code, fmt.Errorf("get task with code %d", code))
}
2017-12-21 16:44:31 +08:00
var res utilhttp.ResponseBody
var gc []*model.Task
res.List = &gc
2017-12-21 16:44:31 +08:00
if err := ffjson.Unmarshal(body, &res); err != nil {
2017-12-22 18:16:16 +08:00
return nil, util.CreateAPIHandleError(code, err)
}
if gc, ok := res.List.(*[]*model.Task); ok {
return *gc, nil
}
2017-12-21 16:44:31 +08:00
return nil, nil
}
//Exec 执行任务
2017-12-21 16:44:31 +08:00
func (t *task) Exec(taskID string, nodes []string) *util.APIHandleError {
var nodesBody struct {
Nodes []string `json:"nodes"`
}
nodesBody.Nodes = nodes
2017-12-21 16:44:31 +08:00
body, err := json.Marshal(nodesBody)
if err != nil {
2017-12-22 18:16:16 +08:00
return util.CreateAPIHandleError(400, err)
}
2017-12-22 18:16:16 +08:00
url := t.prefix + "/" + taskID + "/exec"
2017-12-21 16:44:31 +08:00
_, code, err := nodeclient.Request(url, "POST", body)
2017-12-22 18:16:16 +08:00
return t.client.handleErrAndCode(err, code)
}
2017-12-21 16:44:31 +08:00
func (t *task) Add(task *model.Task) *util.APIHandleError {
body, _ := json.Marshal(task)
2017-12-21 16:44:31 +08:00
url := t.prefix
_, code, err := nodeclient.Request(url, "POST", body)
2017-12-22 18:16:16 +08:00
return t.client.handleErrAndCode(err, code)
}
2017-12-21 16:44:31 +08:00
func (t *task) AddGroup(group *model.TaskGroup) *util.APIHandleError {
body, _ := json.Marshal(group)
url := "/taskgroups"
2017-12-21 16:44:31 +08:00
_, code, err := nodeclient.Request(url, "POST", body)
2017-12-22 18:16:16 +08:00
return t.client.handleErrAndCode(err, code)
}
//Refresh 刷新静态配置
2017-12-21 16:44:31 +08:00
func (t *task) Refresh() *util.APIHandleError {
2017-12-22 18:16:16 +08:00
url := t.prefix + "/taskreload"
_, code, err := nodeclient.Request(url, "PUT", nil)
2017-12-22 18:16:16 +08:00
return t.client.handleErrAndCode(err, code)
}
2017-12-22 18:16:16 +08:00
type TaskStatus struct {
Status map[string]model.TaskStatus `json:"status,omitempty"`
}
func (t *task) Status(name string) (*TaskStatus, error) {
2017-12-21 16:44:31 +08:00
taskId := name
return HandleTaskStatus(taskId)
2017-11-28 11:22:04 +08:00
}
2017-12-22 18:16:16 +08:00
func (t *task) HandleTaskStatus(task string) (*map[string]*model.TaskStatus, *util.APIHandleError) {
body, code, err := nodeclient.Request("/tasks/"+task+"/status", "GET", nil)
if err != nil {
2017-12-22 18:16:16 +08:00
return nil, util.CreateAPIHandleError(code, err)
}
if code != 200 {
2017-12-22 18:16:16 +08:00
return nil, util.CreateAPIHandleError(code, fmt.Errorf("get task with code %d", code))
}
var res utilhttp.ResponseBody
var gc map[string]*model.TaskStatus
res.Bean = &gc
if err := ffjson.Unmarshal(body, &res); err != nil {
2017-12-22 18:16:16 +08:00
return nil, util.CreateAPIHandleError(code, err)
}
if gc, ok := res.Bean.(*map[string]*model.TaskStatus); ok {
return gc, nil
}
return nil, nil
}
func HandleTaskStatus(task string) (*TaskStatus, error) {
resp, code, err := nodeclient.Request("/tasks/"+task+"/status", "GET", nil)
if err != nil {
logrus.Errorf("error execute status Request,details %s", err.Error())
return nil, err
}
if code == 200 {
j, _ := simplejson.NewJson(resp)
bean := j.Get("bean")
beanB, _ := json.Marshal(bean)
var status TaskStatus
statusMap := make(map[string]model.TaskStatus)
2017-11-28 11:22:04 +08:00
json, _ := simplejson.NewJson(beanB)
second := json.Interface()
if second == nil {
return nil, errors.New("get status failed")
}
m := second.(map[string]interface{})
for k, _ := range m {
var taskStatus model.TaskStatus
taskStatus.CompleStatus = m[k].(map[string]interface{})["comple_status"].(string)
taskStatus.Status = m[k].(map[string]interface{})["status"].(string)
taskStatus.JobID = k
statusMap[k] = taskStatus
}
status.Status = statusMap
return &status, nil
2017-12-21 16:44:31 +08:00
}
return nil, errors.New(fmt.Sprintf("response status is %s", code))
}
2017-12-20 10:23:55 +08:00
//Request Request
func (r *RNodeClient) Request(url, method string, body []byte) ([]byte, int, error) {
//logrus.Infof("requesting url: %s by method :%s,and body is %s",r.NodeAPI+url,method,string(body))
request, err := http.NewRequest(method, "http://127.0.0.1:6100/v2"+url, bytes.NewBuffer(body))
if err != nil {
return nil, 500, err
}
request.Header.Set("Content-Type", "application/json")
res, err := http.DefaultClient.Do(request)
if err != nil {
return nil, 500, err
}
data, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, 500, err
}
defer res.Body.Close()
//logrus.Infof("response is %s,response code is %d",string(data),res.StatusCode)
return data, res.StatusCode, nil
}
2017-12-22 18:16:16 +08:00
func (r *RNodeClient) handleErrAndCode(err error, code int) *util.APIHandleError {
2017-12-21 16:44:31 +08:00
if err != nil {
2017-12-22 18:16:16 +08:00
return util.CreateAPIHandleError(code, err)
2017-12-21 16:44:31 +08:00
}
if code != 200 {
return util.CreateAPIHandleError(code, fmt.Errorf("error with code %d", code))
2017-12-21 16:44:31 +08:00
}
return nil
2017-12-22 18:16:16 +08:00
}