# coding=utf8
import requests
import os
from requests_toolbelt.multipart.encoder import MultipartEncoder
import sys
import time
import json
import urllib
username='admin'
password='Omp123!'
file_path=r'C:UsersAdministratorDesktopUYUN-Platform-smc-V2.0.R16.0.tar.gz'
server_ip='10.1.61.123'
host_ips='10.1.61.123'
host_ips=host_ips.split(',')
port = 7500
headers = {'Content-Type': 'application/json'}
def login(username,password):
global server_ip, port,headers
login_data = {
'username': username,
'passwd': password
}
conn = requests.Session()
login_url = 'http://{}:{}/omp/api/v1/user/login'.format(server_ip, port)
response = conn.post(
url=login_url,
json=login_data,
headers=headers
)
if response.status_code != 200:
print(
'Login failed reason:{}'.format(response.text.decode()))
sys.exit(1)
else:
print 'login success'
return conn
def upload_file(file_path):
global server_ip, port, conn
file_name = file_path.split(os.sep)[-1]
url = 'http://{}:{}/omp/api/v1/pkgs/upload'.format(server_ip, port)
encoded_name = urllib.quote(file_name)
with open(file_path, 'rb') as f:
m = MultipartEncoder(
fields={'file': (encoded_name, f, 'application/gzip')}
)
decoded_m = m.to_string()
response = conn.post(url,
data=decoded_m,
headers={'Content-Type': m.content_type,
'charset': 'UTF-8'},
verify=False)
if response.status_code==200:
print 'upload success'
else:
print 'upload fail', response.text
sys.exit(1)
return file_name
def analysis(file_name):
global server_ip, port, conn
url = 'http://{}:{}/omp/api/v1/pkgs/analysis?fileName={}'.format(server_ip, port, file_name)
headers = {'Content-Type': 'application/json'}
response = conn.get(url,
headers=headers,
verify=False)
if response.status_code==200:
print 'analysis success'
else:
print 'analysis fail:', response.text
sys.exit(1)
def check_install_list(upload_time):
global server_ip, port, conn
products = {}
pkg_ids = {}
j = 1
while 1:
main_url = 'http://{}:{}/omp/api/v1/pkgs/query'.format(server_ip, port)
main_params = {'pageSize': 40, 'pageIndex': j}
j += 1
response = conn.get(main_url, params=main_params)
if response.status_code != 200:
print('query error')
sys.exit(1)
else:
res = json.loads(response.text)
pkg_names = res.get('data')
lens = 0
for i in pkg_names:
pkg_name = i['pkgName']
# print pkg_name,i['pkgs']
for module in i['pkgs']:
# print module
lens += 1
if int(module['addTime']) >= int(upload_time) * 1000:
pkg_id = module['pkgId']
products.update({pkg_name: module['version']})
pkg_ids.update({pkg_name: pkg_id})
if lens < 40:
break
# print 'install products', products, pkg_ids
if not products:
print '没找到要安装的模块包'
sys.exit(1)
else:
print 'check install list success'
return products, pkg_ids
def check_uninstall_list(host_ips,products):
global server_ip, port, conn
uninstall_list = {}
install_products = products.keys()
j = 1
while 1:
main_url = 'http://{}:{}/omp/api/v1/installed_pkgs/query'.format(server_ip, port)
main_params = {'pageSize': 40, 'pageIndex': j}
j += 1
response = conn.get(main_url, params=main_params)
if response.status_code != 200:
print('query error')
sys.exit(1)
else:
res = json.loads(response.text)
pkg_names = res.get('data')
lens = 0
for i in pkg_names:
pkg_name = i['pkgName']
for pkg_module in i['installedDiffInfoVOs']:
lens += 1
if pkg_module['ip'] not in host_ips:
continue
# print pkg_name,pkg_module
pkg_id = pkg_module['pkgId']
host_ip = pkg_module['hostId']
if products.get(pkg_name) and (
pkg_module['version'] <= products.get(pkg_name) or pkg_module['status'] != 1):
# print pkg_name,pkg_id
if not uninstall_list.get(host_ip):
uninstall_list.update({host_ip: []})
uninstall_list[host_ip].append({pkg_name: pkg_id})
elif products.get(pkg_name) and pkg_module['version'] > products.get(pkg_name) and pkg_module[
'status'] == 1:
install_products.remove(pkg_name)
if lens < 40:
break
# print uninstall_list
install_list = []
# print install_products
# print pkg_ids
for k, v in pkg_ids.items():
if k in install_products:
install_list.append(v)
print 'check install list success'
return install_list,uninstall_list
def uninstall(uninstall_list):
global server_ip, port, conn
main_url = 'http://{}:{}/omp/api/v1/pkgs/uninstall'.format(server_ip, port)
for host_id, v in uninstall_list.items():
for pkg in v:
pkg_name = pkg.keys()[0]
pkg_id = pkg.values()[0]
main_params = {'pkgId': pkg_id, 'hostIds': [host_id], 'forceDelete': True}
print main_params
response = conn.post(main_url, json=main_params, headers=headers)
if response.status_code != 200:
print('{} uninstall {} error:'.format(host_id, pkg_name))
print response.text
sys.exit(1)
else:
print ('{} uninstall {} success'.format(host_id, pkg_name))
def get_hosts(host_ips):
global server_ip, port, conn
host_ip=host_ips[0]
url = 'http://{}:{}/omp/api/v1/hosts/query'.format(server_ip, port)
response = conn.get(url)
host_id_infos = []
content = response.content
if response.status_code != 200:
print('Get host info failed:{}'.format(content.decode()))
sys.exit(1)
else:
print content
host_ids = json.loads(content)
for i in host_ids:
if host_ip and i['ip'] == host_ip:
host_id_infos = [i['hostId']]
break
host_id_infos.append(i['hostId'])
print('host_id:', host_id_infos)
return host_id_infos
def check_all(modules,pkg_install_info,host_id_infos,order=0):
try:
global server_ip, port
# print pkg_install_info,order
# print modules
node_url = 'http://{}:{}/omp/api/v1/pkgs/installation_info/nodes_check'.format(server_ip, port)
data = {"pkgId": list(pkg_install_info[order].values())[0], "pkgName": list(pkg_install_info[order].keys())[0],
"hostIds": host_id_infos, "products": [{"modules": modules, "order": order}]}
# print 'check data',data
response = conn.post(node_url, data=json.dumps(data), headers=headers)
if response.status_code != 200:
print('check failed:{}'.format(response.text.decode()))
sys.exit(1)
else:
print(response.text)
return data
except Exception as e:
print e
def install(install_list_temp,host_id_infos):
pkg_install_info = {}
install_list={}
for i in install_list_temp:
install_list.update({i: 0})
for pkg_id, status in install_list.items():
install_ids = []
install_names = []
print '开始安装', pkg_id
check_url = 'http://{}:{}/omp/api/v1/pkgs/verify_install'.format(server_ip, port)
# print check_url,pkg_id
response = conn.get(check_url, params={'pkgId': pkg_id})
if response.status_code != 200:
print('verify_install error:')
print response.text
sys.exit(1)
else:
print 'verify_install', response.text
conf_url = 'http://{}:{}/omp/api/v1/pkgs/dependent_pkgs/query'.format(server_ip, port)
response = conn.get(conf_url, params={'pkgId': pkg_id})
if response.status_code != 200:
print('query dependent pkgs error')
print response.text
sys.exit(1)
else:
print('query dependent pkgs success')
pkgs = json.loads(response.text)
# print 'pkgs',pkgs
for i in pkgs:
# print i
for pkg in i['modules']:
if pkg['pkgId'] == pkg_id:
pkg_tag_name = pkg['name']
pkg_install_info.update({pkg['order']: {pkg['name']: pkg['pkgId']}})
print 'pkg_install_info', pkg_install_info
length = len(pkg_install_info.keys())
# print(length)
modules = []
for i in range(0, length):
data = check_all(modules,pkg_install_info,host_id_infos, order=i)
# print data
modules.append({"order": i, "pkgId": data["pkgId"], "pkgName": data["pkgName"], "hostIds": host_id_infos})
print '模块检查', modules
# 环境检测
check_url = 'http://{}:{}/omp/api/v1/pkgs/installation_env/check'.format(server_ip, port)
install_datas = {}
data = []
for k, v in pkg_install_info.items():
pkg_item_id = list(v.values())[0]
if pkg_item_id in install_list.keys() and install_list[pkg_item_id] == 0:
display = True
install_ids.append(pkg_item_id)
else:
display = False
data.append({"pkgId": pkg_item_id, "hostIds": host_id_infos, "display": display})
# print data
response = conn.post(check_url, data=json.dumps(data), headers=headers)
if response.status_code != 200:
print(response.text)
print('安装包检查报错,请检查')
sys.exit(1)
else:
print('安装包检查成功')
print(response.text)
check_url = 'http://{}:{}/omp/api/v1/pkgs/installation_info/confirm'.format(server_ip, port)
response = conn.post(check_url, data=json.dumps(data), headers=headers)
if response.status_code != 200:
print(response.text)
print('安装时环境确认报错,请检查')
sys.exit(1)
else:
print(response.text)
print('环境确认成功')
insert_url = 'http://{}:{}/omp/api/v1/tasks/insert'.format(server_ip, port)
data = []
for pkg in pkgs[0]['modules']:
# print(pkg)
pkg_install_info.update({pkg['order']: {pkg['name']: pkg['pkgId']}})
for k, v in pkg_install_info.items():
item_id = list(v.values())[0]
if item_id in install_list.keys() and install_list[item_id] == 0:
display = True
else:
display = False
data.append({"pkgId": item_id, "hostIds": host_id_infos, "display": display, "order": k})
# print data
response = conn.post(insert_url, data=json.dumps(data), headers=headers)
if response.status_code != 200:
print(response.text)
print('插入任务错误,请检查')
sys.exit(1)
else:
task_id = json.loads(response.text)['taskId']
print('插入任务成功')
t = time.time()
add_time = int(round(t * 1000))
data = []
for k, v in pkg_install_info.items():
id = list(v.values())[0]
if id in install_list.keys() and install_list[item_id] == 0:
display = True
install_names.append(list(v.keys())[0])
else:
display = False
data.append({"pkgId": id, "hostIds": host_id_infos, "display": display, "order": k, "opts": [],
"taskId": task_id})
# print data
install_url = 'http://{}:{}/omp/api/v1/pkgs/install'.format(server_ip, port)
response = conn.post(install_url, data=json.dumps(data), headers=headers)
if response.status_code != 200:
print(response.text)
print('执行安装任务错误,请检查')
sys.exit(1)
else:
print(response.text)
print('安装任务开始执行')
print 'pkg_install_info', pkg_install_info
length = 0
nums = 1
while length < nums:
time.sleep(60)
query_url = 'http://{}:{}/omp/api/v1/logs/exerecord/abnormal_info/count'.format(server_ip, port)
data = {'taskId': task_id, 'completed': 0}
response = conn.get(
url=query_url,
params=data
)
if response.status_code != 200:
print (
'query warn failed reason:{}'.format(response.content.decode()))
sys.exit(1)
else:
res = json.loads(response.text)
# print res
if res.get('warningCnt') != 0:
print ('出现告警提示')
elif res.get('errorCnt') != 0:
print ('出现报错')
sys.exit(1)
# 检查模块
print '检查模块',pkg_tag_name
query_url = 'http://{}:{}/omp/api/v1/installed_pkgs/query'.format(server_ip, port)
data = {'keyWord': pkg_tag_name, 'status': 1}
response = conn.get(
url=query_url,
params=data
)
if response.status_code != 200:
print (
'query product pkg failed reason:{}'.format(response.content.decode()))
sys.exit(1)
else:
res = json.loads(response.text)
print 'res', res
for i in res['data']:
for j in i['installedDiffInfoVOs']:
if j['pkgId'] == pkg_id and j['status'] != 1:
print ('部分模块安装出错')
sys.exit(1)
elif j['pkgId'] == pkg_id:
length += 1
# print 'length', length
if length == nums:
print '{}安装成功'.format(pkg_tag_name)
break
install_list.update({pkg_id: 1})
conn=login(username,password)
upload_time = int(time.time())
file_name=upload_file(file_path)
analysis(file_name)
products, pkg_ids=check_install_list(upload_time)
install_list,uninstall_list=check_uninstall_list(host_ips,products)
print install_list,uninstall_list
if uninstall_list:
uninstall(uninstall_list)
hosts=get_hosts(host_ips)
install(install_list,hosts)