如何用python批量刷新kylin失败的job

2019-01-18  本文已影响2人  起个什么呢称呢
#coding=utf-8
import json
import time
import os
import sys
import datetime

import time
from configobj import ConfigObj
config = ConfigObj("./conf.ini",encoding='UTF8')
host=config['host']['host']
port=config['port']['port']
kylin_home=config['path']['kylin_home']
job_time=config['sleep']['job_time']
request_time=config['sleep']['resquest_time']
user=config['user']['user']
passwd=config['password']['pass']
count=config['count']['count']
AT=config['Naenvvar']['AUTO']
reload(sys)
sys.setdefaultencoding('utf-8')

shellres="curl -u  "+ user +":" + passwd + " -X GET http://"+ host +":"+ port + "/kylin/api/jobs" + " >> response.json"

os.popen(shellres)

print ("正在获取失败jobid")
time.sleep(int(request_time))
file_path=kylin_home + "response.json"
f=open(file_path,'r')
text=f.read()
items=[]
data=json.loads(text)
for itema in data:
    items.append(itema)

list_err=[]
list=[]
def decide_code():
    global list
    if ("code" in items and int(data["code"]) == 000):
        list = data["data"]["jobs"]
        append_error()
    elif ("code" in items and int(data["code"]) != 000):
        print("返回错误的json,请检查请求RestApi")
    else:
        list = data
        append_error()

def append_error():
    for item in list:
        # timestamp=item["last_modified"]
        # timestamp /=1000.0
        # date=time.localtime(timestamp)
        # new_date=time.strftime("%Y-%m-%d",date)
        # d1=datetime.datetime.strptime(str(new_date),'%Y-%m-%d')
        # d2=datetime.datetime.strptime(str(current_time),'%Y-%m-%d')
        # delta=d2-d1
        # days=delta.days
        if(item["job_status"]=="ERROR" ):
            list_err.append(item["uuid"])

def draw():
    path = os.getenv(AT)
    for i in range(0,int(count)):
        print(list_err)
        if (path == "true" ):
            for jobid in list_err:
                print("=========================开始恢复"+ jobid +"==========================")
                draw_shell = "curl -u  " + user + ":" + passwd + " -X PUT http://" + host + ":" + port + "/kylin/api/jobs/" + jobid + "/resume"
                os.popen(draw_shell)
                time.sleep(int(job_time))
                print(jobid + "已恢复成功")
        print("\n")
        else:
            print("不允许批量自动刷新失败任务,如需要,请修改环境变量")

def decide_draw():
    decide_code()
    if(list_err.__len__() == 0 ):
        print("没有失败的job")
    else:
        print("已获取到" + str(list_err.__len__())+ "个失败的jobid")
        print("开始恢复失败的job")
        draw()


if __name__ == '__main__':
    decide_draw()
    os.remove(file_path)

以上就是批量刷新失败job脚本。自己替换相应的即可。

上一篇下一篇

猜你喜欢

热点阅读