[REV] optimize grctl install

This commit is contained in:
bay1ts 2017-12-08 23:31:47 +08:00
parent 428bef93ed
commit db6808eca0
2 changed files with 44 additions and 25 deletions

View File

@ -43,7 +43,7 @@ type Node struct {
Node *model.HostNode `json:"node"`
}
type TaskInterface interface {
Get(name string) (*Task)
Get(name string) (*Task,error)
Add(task *model.Task) (error)
AddGroup(group *model.TaskGroup) (error)
Exec(nodes []string ) error
@ -176,13 +176,13 @@ func (t *Node)List() []*model.HostNode {
}
return nodes
}
func (t *Task)Get(id string) (*Task) {
func (t *Task)Get(id string) (*Task,error) {
t.TaskID=id
url:="/tasks/"+id
resp,code,err:=nodeServer.Request(url,"GET",nil)
if err!=nil {
logrus.Errorf("error request url %s,details %s",url,err.Error())
return nil
return nil,err
}
if code != 200 {
fmt.Println("executing failed:"+string(resp))
@ -190,22 +190,22 @@ func (t *Task)Get(id string) (*Task) {
jsonTop,err:=simplejson.NewJson(resp)
if err!=nil {
logrus.Errorf("error get json from url %s",err.Error())
return nil
return nil,err
}
var task model.Task
beanJ:=jsonTop.Get("bean")
taskB,err:=json.Marshal(beanJ)
if err!=nil {
logrus.Errorf("error marshal task %s",err.Error())
return nil
return nil,err
}
err=json.Unmarshal(taskB,&task)
if err!=nil {
logrus.Errorf("error unmarshal task %s",err.Error())
return nil
return nil,err
}
t.Task=&task
return t
return t,nil
}
func (t *Task)List() ([]*model.Task,error) {
url:="/tasks"

View File

@ -194,35 +194,45 @@ func NewCmdInstall() cli.Command {
//}
func Status(task string,nodes []string) {
taskE:=clients.NodeClient.Tasks().Get(task)
lastState:=""
checkFail:=0
lastState:=""
set := make(map[string]bool)
for _, v := range nodes {
set[v] = true
}
for checkFail<3 {
time.Sleep(3*time.Second)
status,err:=taskE.Status()
if err != nil||status==nil {
logrus.Warnf("error get task status,retry")
taskE,err:=clients.NodeClient.Tasks().Get(task)
if err!=nil {
logrus.Warnf("error get task %s,retry",task)
checkFail+=1
if err!=nil {
logrus.Errorf("error get task status ,details %s",err.Error())
}
continue
}
set := make(map[string]bool)
for _, v := range nodes {
set[v] = true
status,err:=taskE.Status()
if err != nil||status==nil {
logrus.Warnf("error get task %s status,retry",task)
checkFail+=1
continue
}
for k,v:=range status.Status{
//不是当前任务需要检测的status
if !set[k] {
continue
}
if strings.Contains(v.Status, "error")||strings.Contains(v.CompleStatus,"Failure")||strings.Contains(v.CompleStatus,"Unknow") {
checkFail+=1
fmt.Errorf("error executing task %s",task)
taskE:=clients.NodeClient.Tasks().Get(task)
//好像不需要更新task因为一直在循环
//taskE,err:=clients.NodeClient.Tasks().Get(task)
//if err != nil||status==nil {
// logrus.Warnf("error get task %s status,update task status failed,retry",task)
// checkFail+=1
// continue
//}
for _,v:=range taskE.Task.OutPut{
fmt.Printf("on %s :\n %s",v.NodeID,v.Body)
if set[v.NodeID]{
fmt.Printf("on %s :\n %s",v.NodeID,v.Body)
}
}
os.Exit(1)
}
@ -236,9 +246,18 @@ func Status(task string,nodes []string) {
}else {
fmt.Printf("task %s is %s %s\n",task,v.Status,v.CompleStatus)
lastState=v.Status
taskFinished:=clients.NodeClient.Tasks().Get(task)
taskFinished:=taskE
//taskFinished,err:=clients.NodeClient.Tasks().Get(task)
//if err != nil||status==nil {
// logrus.Warnf("error get task %s ,retry",task)
// checkFail+=1
// continue
//}
var nextTasks []string
for _,v:=range taskFinished.Task.OutPut{
if !set[v.NodeID] {
continue
}
for _,sv:=range v.Status{
if sv.NextTask == nil ||len(sv.NextTask)==0{
continue
@ -266,12 +285,12 @@ func Task(c *cli.Context,task string,status bool) error {
nodes:=c.StringSlice("nodes")
logrus.Infof("task %s will execute in nodes: %v",task,nodes)
taskEntity:=clients.NodeClient.Tasks().Get(task)
if taskEntity==nil {
taskEntity,err:=clients.NodeClient.Tasks().Get(task)
if taskEntity==nil||err!=nil {
logrus.Errorf("error get task entity from server,please check server api")
return nil
}
err:=taskEntity.Exec(nodes)
err=taskEntity.Exec(nodes)
if err != nil {
logrus.Errorf("error exec task:%s,details %s",task,err.Error())
return err