2017-11-22 11:40:14 +08:00
|
|
|
package region
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/goodrain/rainbond/pkg/node/api/model"
|
|
|
|
"bytes"
|
|
|
|
"net/http"
|
|
|
|
"io/ioutil"
|
|
|
|
"encoding/json"
|
|
|
|
"github.com/bitly/go-simplejson"
|
2017-11-22 18:46:17 +08:00
|
|
|
"github.com/Sirupsen/logrus"
|
2017-11-23 15:05:46 +08:00
|
|
|
"fmt"
|
2017-11-22 11:40:14 +08:00
|
|
|
)
|
|
|
|
var nodeServer *RNodeServer
|
|
|
|
|
|
|
|
func NewNode(nodeAPI string) {
|
|
|
|
if nodeServer==nil {
|
|
|
|
nodeServer=&RNodeServer{
|
2017-11-23 16:13:45 +08:00
|
|
|
NodeAPI:nodeAPI,
|
2017-11-22 11:40:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func GetNode() *RNodeServer {
|
|
|
|
return nodeServer
|
|
|
|
}
|
|
|
|
type RNodeServer struct {
|
2017-11-23 16:13:45 +08:00
|
|
|
NodeAPI string
|
2017-11-22 11:40:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RNodeServer)Tasks() TaskInterface {
|
|
|
|
return &Task{}
|
|
|
|
}
|
|
|
|
func (r *RNodeServer)Nodes() NodeInterface {
|
|
|
|
return &Node{}
|
|
|
|
}
|
|
|
|
type Task struct {
|
2017-11-23 16:13:45 +08:00
|
|
|
TaskID string `json:"task_id"`
|
2017-11-22 11:40:14 +08:00
|
|
|
}
|
|
|
|
type Node struct {
|
2017-11-23 16:13:45 +08:00
|
|
|
Id string
|
|
|
|
Node *model.HostNode `json:"node"`
|
2017-11-22 11:40:14 +08:00
|
|
|
}
|
|
|
|
type TaskInterface interface {
|
|
|
|
Get(name string) (*Task)
|
|
|
|
Exec(nodes []string ) error
|
|
|
|
}
|
|
|
|
type NodeInterface interface {
|
|
|
|
Add(node *model.APIHostNode)
|
2017-11-22 18:46:17 +08:00
|
|
|
Get(node string) *Node
|
|
|
|
List() []*model.HostNode
|
|
|
|
Up()
|
|
|
|
Down()
|
|
|
|
UnSchedulable()
|
|
|
|
ReSchedulable()
|
2017-11-22 11:40:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Node)Add(node *model.APIHostNode) {
|
|
|
|
body,_:=json.Marshal(node)
|
2017-11-23 16:13:45 +08:00
|
|
|
nodeServer.Request("/nodes/","POST",body)
|
2017-11-22 11:40:14 +08:00
|
|
|
}
|
2017-11-22 18:46:17 +08:00
|
|
|
func (t *Node)Up() {
|
2017-11-23 16:13:45 +08:00
|
|
|
nodeServer.Request("/nodes/"+t.Id+"/up","POST",nil)
|
2017-11-22 18:46:17 +08:00
|
|
|
}
|
|
|
|
func (t *Node)Down() {
|
2017-11-23 16:13:45 +08:00
|
|
|
nodeServer.Request("/nodes/"+t.Id+"/down","POST",nil)
|
2017-11-22 18:46:17 +08:00
|
|
|
}
|
|
|
|
func (t *Node)UnSchedulable() {
|
2017-11-23 16:13:45 +08:00
|
|
|
nodeServer.Request("/nodes/"+t.Id+"/unschedulable","PUT",nil)
|
2017-11-22 18:46:17 +08:00
|
|
|
}
|
|
|
|
func (t *Node)ReSchedulable() {
|
2017-11-23 16:13:45 +08:00
|
|
|
nodeServer.Request("/nodes/"+t.Id+"/reschedulable","PUT",nil)
|
2017-11-22 18:46:17 +08:00
|
|
|
}
|
|
|
|
func (t *Node)Get(node string) *Node {
|
2017-11-23 16:13:45 +08:00
|
|
|
body,_,err:=nodeServer.Request("/nodes/"+node,"GET",nil)
|
2017-11-22 18:46:17 +08:00
|
|
|
if err != nil {
|
2017-11-23 16:13:45 +08:00
|
|
|
logrus.Errorf("error get node %s,details %s",node,err.Error())
|
2017-11-22 18:46:17 +08:00
|
|
|
return nil
|
|
|
|
}
|
2017-11-23 16:13:45 +08:00
|
|
|
t.Id=node
|
2017-11-22 18:46:17 +08:00
|
|
|
var stored model.HostNode
|
2017-11-23 16:13:45 +08:00
|
|
|
j,err:=simplejson.NewJson(body)
|
2017-11-22 18:46:17 +08:00
|
|
|
if err != nil {
|
2017-11-23 16:13:45 +08:00
|
|
|
logrus.Errorf("error get node %s 's json,details %s",node,err.Error())
|
2017-11-22 18:46:17 +08:00
|
|
|
return nil
|
|
|
|
}
|
2017-11-23 16:13:45 +08:00
|
|
|
bean:=j.Get("bean")
|
|
|
|
n,err:=json.Marshal(bean)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error get bean from response,details %s",err.Error())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err=json.Unmarshal([]byte(n),&stored)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error unmarshal node %s,details %s",node,err.Error())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
t.Node=&stored
|
2017-11-22 18:46:17 +08:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
func (t *Node)List() []*model.HostNode {
|
2017-11-23 16:13:45 +08:00
|
|
|
body,_,err:=nodeServer.Request("/nodes","GET",nil)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error get nodes ,details %s",err.Error())
|
2017-11-24 11:21:51 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
j,err:=simplejson.NewJson(body)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error get json ,details %s",err.Error())
|
|
|
|
return nil
|
2017-11-23 16:13:45 +08:00
|
|
|
}
|
2017-11-22 18:46:17 +08:00
|
|
|
nodeArr,err:=j.Get("list").Array()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Infof("error occurd,details %s",err.Error())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
jsonA, _ := json.Marshal(nodeArr)
|
|
|
|
nodes := []*model.HostNode{}
|
|
|
|
err=json.Unmarshal(jsonA, &nodes)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Infof("error occurd,details %s",err.Error())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return nodes
|
|
|
|
}
|
2017-11-22 11:40:14 +08:00
|
|
|
func (t *Task)Get(id string) (*Task) {
|
|
|
|
return &Task{
|
2017-11-23 16:13:45 +08:00
|
|
|
TaskID:id,
|
2017-11-22 11:40:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
func (t *Task)Exec(nodes []string ) error {
|
2017-11-23 16:13:45 +08:00
|
|
|
taskId:=t.TaskID
|
2017-11-22 11:40:14 +08:00
|
|
|
var nodesBody struct {
|
|
|
|
Nodes []string `json:"nodes"`
|
|
|
|
}
|
|
|
|
nodesBody.Nodes=nodes
|
|
|
|
body,_:=json.Marshal(nodesBody)
|
2017-11-23 15:05:46 +08:00
|
|
|
url:="/tasks/"+taskId+"/exec"
|
2017-11-23 16:13:45 +08:00
|
|
|
resp,code,err:=nodeServer.Request(url,"POST",body)
|
2017-11-23 15:05:46 +08:00
|
|
|
if code != 200 {
|
|
|
|
fmt.Println("executing failed:"+string(resp))
|
|
|
|
}
|
|
|
|
if err!=nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-11-22 11:40:14 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
type TaskStatus struct {
|
|
|
|
Status map[string]model.TaskStatus `json:"status,omitempty"`
|
|
|
|
}
|
|
|
|
func (t *Task)Status() (*TaskStatus,error) {
|
2017-11-23 16:13:45 +08:00
|
|
|
taskId:=t.TaskID
|
|
|
|
resp,code,err:=nodeServer.Request("/tasks/"+taskId+"/status","GET",nil)
|
2017-11-22 11:40:14 +08:00
|
|
|
if err != nil {
|
2017-11-23 16:13:45 +08:00
|
|
|
logrus.Errorf("error execute status Request,details %s",err.Error())
|
2017-11-22 11:40:14 +08:00
|
|
|
return nil,err
|
|
|
|
}
|
2017-11-23 15:05:46 +08:00
|
|
|
if code == 200 {
|
|
|
|
j,_:=simplejson.NewJson(resp)
|
|
|
|
bean,_:=j.Get("bean").Bytes()
|
|
|
|
var status TaskStatus
|
|
|
|
err=json.Unmarshal(bean,&status)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error unmarshal response,details %s",err.Error())
|
|
|
|
return nil,err
|
|
|
|
}
|
|
|
|
return &status,nil
|
|
|
|
}
|
|
|
|
return nil,nil
|
2017-11-22 11:40:14 +08:00
|
|
|
}
|
2017-11-23 16:13:45 +08:00
|
|
|
func (r *RNodeServer)Request(url ,method string, body []byte) ([]byte,int,error) {
|
|
|
|
logrus.Infof("requesting url: %s by method :%s",r.NodeAPI+url,method)
|
|
|
|
request, err := http.NewRequest(method, r.NodeAPI+url, bytes.NewBuffer(body))
|
2017-11-22 11:40:14 +08:00
|
|
|
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)
|
|
|
|
defer res.Body.Close()
|
|
|
|
return data,res.StatusCode,err
|
|
|
|
}
|