1 批量获取DC/OS 租户下的applications
调用http://<dcos.master>/service/<tenant>/v2/groups
并解析。
def getMarathonApplications(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(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)