python 批量獲取和更新DC/OS Marathon Applications

1 批量獲取DC/OS 租戶下的applications

調用http://<dcos.master>/service/<tenant>/v2/groups並解析。

def getMarathonApplications(cls, jsonInfos, tenant, path):
    '''
    :param jsonInfos: /service/<tenant>/v2/groups 接口返回的不包含group信息的json數據
    :param tenant: DC/OS租戶
    :param path: json文件存儲路徑
    :return:
    :description: 將Marathon Applications保存爲本地json文件
    '''
    apps = jsonInfos['apps']
    popKeys = ['gpus', 'executor', 'version', 'versionInfo', 'upgradeStrategy', 'requirePorts', 'backoffSeconds', 'backoffFactor', 'maxLaunchDelaySeconds',
               'unreachableStrategy', 'killSelection']
    for app in apps:
        for popKey in popKeys:
            if popKey in app:
                app.pop(popKey)
        if 'env' in app and not app['env']:
            app.pop('env')
        if 'healthChecks' in app and not app['healthChecks']:
            app.pop('healthChecks')
        if 'labels' in app and not app['labels']:
            app.pop('labels')
        filename = path + '/' + tenant + app['id'] + '.json'
        FileUtils.writeJsons(filename=filename, data=[app])
def getMarathonGroups(cls, jsonInfos, tenant, path):
    '''
    :param jsonInfos: /service/<tenant>/v2/groups 接口返回的第一層爲group信息的json數據
    :param tenant: DC/OS租戶
    :param path: json文件存儲路徑
    :return:
    :description: 遞歸調用,將Marathon Applications保存爲本地json文件
    '''
    groups = jsonInfos['groups']
    for group in groups:
        if 'apps' in group:
            getMarathonApplications(jsonInfos=group, tenant=tenant, path=path)
        if 'groups' in group:
            getMarathonGroups(jsonInfos=group, tenant=tenant, path=path)
def getMarathonApplicationsForTenant(master, tenant, path):
    '''
    :param master: DC/OS master address
    :param tenant: DC/OS租戶
    :param path: json文件存儲路徑
    :return:
    :description: 將tenant租戶下的Marathon Applications保存爲本地json文件
    '''
    url = master + '/service/' + tenant + '/v2/groups'
    print(url)
    cookies = CookiesInfo.getCookies(master=master)
    headers = CookiesInfo.getHeaders()
    response = requests.request('GET', url, headers=headers, cookies=cookies)
    if response.status_code == 200:
        jsonInfos = response.json()
        if 'apps' in jsonInfos:
            getMarathonApplications(jsonInfos=jsonInfos, tenant=tenant, path=path)
        if 'groups' in jsonInfos:
            getMarathonGroups(jsonInfos=jsonInfos, tenant=tenant, path=path)
    else:
        print(str(response.status_code) + ' Bad Requests')
def getAllMarathonApplications(master, path):
    '''
    :param master: DC/OS master address
    :param path: json文件存儲路徑
    :return:
    :description: 將整個集羣的Marathon Applications保存爲本地json文件
    '''
    tenants = UsersUtils.getTenant(master=master)
    for tenant in tenants:
        getMarathonApplicationsForTenant(master=master, tenant=tenant, path=path)       

2 批量更新 Marathon Applications 到 DC/OS 租戶

調用http://<dcos.master>/service/<tenant>/v2/apps?force=true

def sendMarathonApplicationsToDcos(master, tenant, filePath):
    '''
    :param master: DC/OS master address
    :param tenant: DC/OS租戶
    :param filePath: 本地json文件存儲路徑
    :return:
    :description: 將本地json文件更新到DC/OS 租戶下
    '''
    fileList = FileUtils.getFileListByType(filePath=filePath, fileType='json')
    for file in fileList:
        print(file)
        url = master + '/service/' + tenant + '/v2/apps?force=true'
        print(url)
        jsonData = json.dumps(FileUtils.readJson(filename=file))
        cookies = CookiesInfo.getCookies(master=master)
        headers = CookiesInfo.getHeaders()
        response = requests.request('PUT', url, headers=headers, cookies=cookies, data=jsonData)
        print(response.status_code)
        if response.status_code == 200:
            print('\033[32m\033[1m ' + file + ' send to ' + tenant + ' successfully!' + '\033[0m')
        else:
            print('\033[31m\033[1m ' + file + ' send to ' + tenant + ' failed!' + '\033[0m')
        time.sleep(2)
def replaceJsonByString(filePath, oldString, newString):
    '''
    :param filePath: 本地json文件存儲路徑
    :param oldString: 被替換的字符串
    :param newString: 替換的字符串
    :return:
    :description: 按字符串批量替換json文件
    '''
    fileList = FileUtils.getFileListByType(filePath=filePath, fileType='json')
    for file in fileList:
        jsonData = json.loads(json.dumps(FileUtils.readJson(filename=file)).replace(oldString, newString))
        FileUtils.writeJsons(file, jsonData)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章