2017-11-22 11:40:14 +08:00
|
|
|
package region
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2017-11-23 15:05:46 +08:00
|
|
|
"fmt"
|
2017-12-19 18:46:49 +08:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2017-12-21 17:09:17 +08:00
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/bitly/go-simplejson"
|
2017-12-19 18:46:49 +08:00
|
|
|
"github.com/goodrain/rainbond/pkg/node/api/model"
|
|
|
|
"github.com/pquerna/ffjson/ffjson"
|
2017-12-21 17:09:17 +08:00
|
|
|
//"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"
|
2017-11-22 11:40:14 +08:00
|
|
|
)
|
2017-12-19 18:46:49 +08:00
|
|
|
|
2017-12-20 13:48:49 +08:00
|
|
|
var nodeclient *RNodeClient
|
2017-11-22 11:40:14 +08:00
|
|
|
|
2017-12-20 13:48:49 +08:00
|
|
|
//NewNode new node client
|
2017-12-19 18:46:49 +08:00
|
|
|
func NewNode(nodeAPI string) {
|
2017-12-20 13:48:49 +08:00
|
|
|
if nodeclient == nil {
|
|
|
|
nodeclient = &RNodeClient{
|
2017-12-19 18:46:49 +08:00
|
|
|
NodeAPI: nodeAPI,
|
2017-11-22 11:40:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-20 13:48:49 +08:00
|
|
|
func GetNode() *RNodeClient {
|
|
|
|
return nodeclient
|
2017-11-22 11:40:14 +08:00
|
|
|
}
|
2017-12-19 18:46:49 +08:00
|
|
|
|
2017-12-20 13:48:49 +08:00
|
|
|
type RNodeClient struct {
|
2017-11-23 16:13:45 +08:00
|
|
|
NodeAPI string
|
2017-11-22 11:40:14 +08:00
|
|
|
}
|
|
|
|
|
2017-12-20 13:48:49 +08:00
|
|
|
func (r *RNodeClient) Tasks() TaskInterface {
|
2017-12-22 18:16:16 +08:00
|
|
|
return &task{client: r, prefix: "/tasks"}
|
2017-11-22 11:40:14 +08:00
|
|
|
}
|
2017-12-20 13:48:49 +08:00
|
|
|
func (r *RNodeClient) Nodes() NodeInterface {
|
2017-12-22 18:16:16 +08:00
|
|
|
return &node{client: r, prefix: "/nodes"}
|
2017-11-22 11:40:14 +08:00
|
|
|
}
|
2017-12-20 13:48:49 +08:00
|
|
|
func (r *RNodeClient) Configs() ConfigsInterface {
|
2017-12-22 18:16:16 +08:00
|
|
|
return &configs{client: r, prefix: "/configs"}
|
2017-12-20 13:48:49 +08:00
|
|
|
}
|
2017-12-19 18:46:49 +08:00
|
|
|
|
2017-12-21 16:44:31 +08:00
|
|
|
type task struct {
|
|
|
|
client *RNodeClient
|
|
|
|
prefix string
|
2017-11-22 11:40:14 +08:00
|
|
|
}
|
2017-12-21 16:44:31 +08:00
|
|
|
type node struct {
|
|
|
|
client *RNodeClient
|
|
|
|
prefix string
|
2017-11-22 11:40:14 +08:00
|
|
|
}
|
|
|
|
type TaskInterface interface {
|
2017-12-21 16:44:31 +08:00
|
|
|
Get(name string) (*model.Task, *util.APIHandleError)
|
2017-12-21 17:09:17 +08:00
|
|
|
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
|
2017-11-22 11:40:14 +08:00
|
|
|
}
|
|
|
|
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
|
2017-11-27 15:18:05 +08:00
|
|
|
}
|
|
|
|
|
2017-12-20 13:48:49 +08:00
|
|
|
//ConfigsInterface 数据中心配置API
|
|
|
|
type ConfigsInterface interface {
|
2017-12-21 16:44:31 +08:00
|
|
|
Get() (*model.GlobalConfig, *util.APIHandleError)
|
|
|
|
Put(*model.GlobalConfig) *util.APIHandleError
|
2017-12-20 13:48:49 +08:00
|
|
|
}
|
|
|
|
type configs struct {
|
|
|
|
client *RNodeClient
|
2017-12-21 16:44:31 +08:00
|
|
|
prefix string
|
2017-12-20 13:48:49 +08:00
|
|
|
}
|
|
|
|
|
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)
|
2017-12-20 13:48:49 +08:00
|
|
|
if err != nil {
|
2017-12-22 18:16:16 +08:00
|
|
|
return nil, util.CreateAPIHandleError(code, err)
|
2017-12-20 13:48:49 +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-20 13:48:49 +08:00
|
|
|
}
|
|
|
|
var res utilhttp.ResponseBody
|
2017-12-22 18:16:16 +08:00
|
|
|
var gc = model.GlobalConfig{
|
|
|
|
Configs: make(map[string]*model.ConfigUnit),
|
|
|
|
}
|
2017-12-20 13:48:49 +08:00
|
|
|
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-20 13:48:49 +08:00
|
|
|
}
|
|
|
|
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 {
|
2017-12-20 18:22:36 +08:00
|
|
|
rebody, err := ffjson.Marshal(gc)
|
|
|
|
if err != nil {
|
2017-12-22 18:16:16 +08:00
|
|
|
return util.CreateAPIHandleError(400, err)
|
2017-12-20 18:22:36 +08:00
|
|
|
}
|
2017-12-21 16:44:31 +08:00
|
|
|
_, code, err := c.client.Request(c.prefix+"/datacenter", "PUT", rebody)
|
2017-12-20 18:22:36 +08:00
|
|
|
if err != nil {
|
2017-12-22 18:16:16 +08:00
|
|
|
return util.CreateAPIHandleError(code, err)
|
2017-12-20 18:22:36 +08:00
|
|
|
}
|
|
|
|
if code != 200 {
|
2017-12-22 18:16:16 +08:00
|
|
|
return util.CreateAPIHandleError(code, fmt.Errorf("Put database center configs code %d", code))
|
2017-12-20 18:22:36 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-12-20 13:48:49 +08:00
|
|
|
|
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)
|
2017-11-27 15:18:05 +08:00
|
|
|
if err != nil {
|
2017-12-22 18:16:16 +08:00
|
|
|
return nil, util.CreateAPIHandleError(code, err)
|
2017-11-27 15:18:05 +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-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-11-27 13:39:22 +08:00
|
|
|
}
|
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-11-27 13:39:22 +08:00
|
|
|
}
|
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
|
2017-12-22 11:17:13 +08:00
|
|
|
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
|
|
|
}
|
2017-12-22 11:17:13 +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)
|
2017-11-27 15:18:05 +08:00
|
|
|
if err != nil {
|
2017-12-22 18:16:16 +08:00
|
|
|
return nil, util.CreateAPIHandleError(code, err)
|
2017-11-27 15:18:05 +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-27 15:18:05 +08:00
|
|
|
}
|
2017-12-21 16:44:31 +08:00
|
|
|
var res utilhttp.ResponseBody
|
|
|
|
var gc []*model.HostNode
|
2017-12-22 11:17:13 +08:00
|
|
|
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-27 15:18:05 +08:00
|
|
|
}
|
2017-12-22 11:17:13 +08:00
|
|
|
if gc, ok := res.List.(*[]*model.HostNode); ok {
|
|
|
|
return *gc, nil
|
2017-11-27 15:18:05 +08:00
|
|
|
}
|
2017-12-21 16:44:31 +08:00
|
|
|
return nil, nil
|
2017-11-27 15:18:05 +08:00
|
|
|
}
|
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)
|
|
|
|
|
2017-12-19 18:46:49 +08:00
|
|
|
if err != nil {
|
2017-12-22 18:16:16 +08:00
|
|
|
return nil, util.CreateAPIHandleError(code, err)
|
2017-11-28 16:33:11 +08:00
|
|
|
}
|
2017-11-29 09:43:37 +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-11-28 16:33:11 +08:00
|
|
|
}
|
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-11-29 18:44:39 +08:00
|
|
|
}
|
2017-12-21 16:44:31 +08:00
|
|
|
if gc, ok := res.Bean.(*model.Task); ok {
|
|
|
|
return gc, nil
|
2017-11-29 18:44:39 +08:00
|
|
|
}
|
2017-12-21 16:44:31 +08:00
|
|
|
return nil, nil
|
|
|
|
|
2017-12-19 18:46:49 +08:00
|
|
|
}
|
2017-11-29 18:44:39 +08:00
|
|
|
|
2017-12-19 18:46:49 +08:00
|
|
|
//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)
|
|
|
|
|
2017-12-19 18:46:49 +08:00
|
|
|
if err != nil {
|
2017-12-22 18:16:16 +08:00
|
|
|
return nil, util.CreateAPIHandleError(code, err)
|
2017-12-19 18:46:49 +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 task with code %d", code))
|
2017-12-19 18:46:49 +08:00
|
|
|
}
|
2017-12-21 16:44:31 +08:00
|
|
|
var res utilhttp.ResponseBody
|
|
|
|
var gc []*model.Task
|
2017-12-22 11:17:13 +08:00
|
|
|
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-12-19 18:46:49 +08:00
|
|
|
}
|
2017-12-22 11:17:13 +08:00
|
|
|
if gc, ok := res.List.(*[]*model.Task); ok {
|
|
|
|
return *gc, nil
|
2017-12-19 18:46:49 +08:00
|
|
|
}
|
2017-12-21 16:44:31 +08:00
|
|
|
return nil, nil
|
2017-11-29 18:44:39 +08:00
|
|
|
}
|
2017-12-19 18:46:49 +08:00
|
|
|
|
|
|
|
//Exec 执行任务
|
2017-12-21 16:44:31 +08:00
|
|
|
func (t *task) Exec(taskID string, nodes []string) *util.APIHandleError {
|
2017-11-22 11:40:14 +08:00
|
|
|
var nodesBody struct {
|
|
|
|
Nodes []string `json:"nodes"`
|
|
|
|
}
|
2017-12-19 18:46:49 +08:00
|
|
|
nodesBody.Nodes = nodes
|
2017-12-21 16:44:31 +08:00
|
|
|
body, err := json.Marshal(nodesBody)
|
2017-12-19 18:46:49 +08:00
|
|
|
if err != nil {
|
2017-12-22 18:16:16 +08:00
|
|
|
return util.CreateAPIHandleError(400, err)
|
2017-11-23 15:05:46 +08:00
|
|
|
}
|
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-11-22 11:40:14 +08:00
|
|
|
}
|
2017-12-21 16:44:31 +08:00
|
|
|
func (t *task) Add(task *model.Task) *util.APIHandleError {
|
2017-11-30 14:43:24 +08:00
|
|
|
|
2017-12-19 18:46:49 +08:00
|
|
|
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-11-30 15:16:22 +08:00
|
|
|
}
|
2017-12-21 16:44:31 +08:00
|
|
|
func (t *task) AddGroup(group *model.TaskGroup) *util.APIHandleError {
|
2017-12-19 18:46:49 +08:00
|
|
|
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)
|
2017-11-30 14:43:24 +08:00
|
|
|
}
|
2017-12-19 18:46:49 +08:00
|
|
|
|
|
|
|
//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"
|
2017-12-20 13:48:49 +08:00
|
|
|
_, code, err := nodeclient.Request(url, "PUT", nil)
|
2017-12-22 18:16:16 +08:00
|
|
|
return t.client.handleErrAndCode(err, code)
|
2017-12-19 18:46:49 +08:00
|
|
|
}
|
2017-12-22 18:16:16 +08:00
|
|
|
|
2017-11-22 11:40:14 +08:00
|
|
|
type TaskStatus struct {
|
|
|
|
Status map[string]model.TaskStatus `json:"status,omitempty"`
|
|
|
|
}
|
2017-12-19 18:46:49 +08:00
|
|
|
|
2017-12-21 17:09:17 +08:00
|
|
|
func (t *task) Status(name string) (*TaskStatus, error) {
|
2017-12-21 16:44:31 +08:00
|
|
|
taskId := name
|
2017-12-21 17:09:17 +08:00
|
|
|
|
|
|
|
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) {
|
2017-12-21 18:33:41 +08:00
|
|
|
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)
|
2017-12-21 18:33:41 +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 18:33:41 +08:00
|
|
|
}
|
|
|
|
var res utilhttp.ResponseBody
|
2017-12-22 10:14:03 +08:00
|
|
|
var gc map[string]*model.TaskStatus
|
2017-12-21 18:33:41 +08:00
|
|
|
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 18:33:41 +08:00
|
|
|
}
|
2017-12-22 10:14:03 +08:00
|
|
|
if gc, ok := res.Bean.(*map[string]*model.TaskStatus); ok {
|
2017-12-21 18:33:41 +08:00
|
|
|
return gc, nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
2017-12-21 17:09:17 +08:00
|
|
|
func HandleTaskStatus(task string) (*TaskStatus, error) {
|
|
|
|
resp, code, err := nodeclient.Request("/tasks/"+task+"/status", "GET", nil)
|
2017-11-22 11:40:14 +08:00
|
|
|
if err != nil {
|
2017-12-21 17:09:17 +08:00
|
|
|
logrus.Errorf("error execute status Request,details %s", err.Error())
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-12-21 18:33:41 +08:00
|
|
|
|
2017-12-21 17:09:17 +08:00
|
|
|
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
|
|
|
|
2017-12-21 17:09:17 +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
|
|
|
}
|
2017-12-21 17:09:17 +08:00
|
|
|
return nil, errors.New(fmt.Sprintf("response status is %s", code))
|
2017-11-22 11:40:14 +08:00
|
|
|
}
|
2017-12-20 10:23:55 +08:00
|
|
|
|
|
|
|
//Request Request
|
2017-12-20 13:48:49 +08:00
|
|
|
func (r *RNodeClient) Request(url, method string, body []byte) ([]byte, int, error) {
|
2017-12-01 11:38:46 +08:00
|
|
|
//logrus.Infof("requesting url: %s by method :%s,and body is ",r.NodeAPI+url,method,string(body))
|
2017-11-29 09:43:37 +08:00
|
|
|
request, err := http.NewRequest(method, "http://127.0.0.1:6100/v2"+url, bytes.NewBuffer(body))
|
2017-11-22 11:40:14 +08:00
|
|
|
if err != nil {
|
2017-12-19 18:46:49 +08:00
|
|
|
return nil, 500, err
|
2017-11-22 11:40:14 +08:00
|
|
|
}
|
|
|
|
request.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
res, err := http.DefaultClient.Do(request)
|
|
|
|
if err != nil {
|
2017-12-19 18:46:49 +08:00
|
|
|
return nil, 500, err
|
2017-11-22 11:40:14 +08:00
|
|
|
}
|
|
|
|
data, err := ioutil.ReadAll(res.Body)
|
|
|
|
defer res.Body.Close()
|
2017-12-01 11:38:46 +08:00
|
|
|
//logrus.Infof("response is %s,response code is %d",string(data),res.StatusCode)
|
2017-12-19 18:46:49 +08:00
|
|
|
return data, res.StatusCode, err
|
|
|
|
}
|
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 {
|
2017-12-22 18:16:16 +08:00
|
|
|
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
|
|
|
}
|