def apps_script_online(request): if request.method == "GET": serverList = Server_Assets.objects.all() groupList = Group.objects.all() serviceList = Service_Assets.objects.all() return render(request,'apps/apps_script_online.html',{"user":request.user,"ans_uuid":uuid.uuid4(), "serverList":serverList,"groupList":groupList, "serviceList":serviceList}) elif request.method == "POST" and request.user.has_perm('OpsManage.can_change_ansible_script'): resource = [] sList = [] def saveScript(content,filePath): if os.path.isdir(os.path.dirname(filePath)) is not True:os.makedirs(os.path.dirname(filePath))#判断文件存放的目录是否存在,不存在就创建 with open(filePath, 'w') as f: f.write(content) return filePath if request.POST.get('server_model') in ['service','group','custom']: if request.POST.get('server_model') == 'custom': serverList = request.POST.getlist('ansible_server[]') for server in serverList: server_assets = Server_Assets.objects.get(id=server) sList.append(server_assets.ip) if server_assets.keyfile == 1:resource.append({"hostname": server_assets.ip, "port": int(server_assets.port)}) else:resource.append({"hostname": server_assets.ip, "port": int(server_assets.port),"username": server_assets.username,"password": server_assets.passwd}) elif request.POST.get('server_model') == 'group': serverList = Assets.objects.filter(group=request.POST.get('ansible_group')) for server in serverList: try: sList.append(server.server_assets.ip) except Exception, ex: print ex continue if server.server_assets.keyfile == 1:resource.append({"hostname": server.server_assets.ip, "port": int(server.server_assets.port)}) else:resource.append({"hostname": server.server_assets.ip, "port": int(server.server_assets.port),"username": server.server_assets.username,"password": server.server_assets.passwd}) elif request.POST.get('server_model') == 'service': serverList = Assets.objects.filter(business=request.POST.get('ansible_service')) for server in serverList: try: sList.append(server.server_assets.ip) except Exception, ex: print ex continue if server.server_assets.keyfile == 1:resource.append({"hostname": server.server_assets.ip, "port": int(server.server_assets.port)}) else:resource.append({"hostname": server.server_assets.ip, "port": int(server.server_assets.port),"username": server.server_assets.username,"password": server.server_assets.passwd}) if len(sList) > 0 and request.POST.get('type') == 'run' and request.POST.get('script_file'): filePath = saveScript(content=request.POST.get('script_file'),filePath='/tmp/script-{ram}'.format(ram=uuid.uuid4().hex[0:8])) redisKey = request.POST.get('ans_uuid') logId = AnsibleRecord.Model.insert(user=str(request.user),ans_model='script',ans_server=','.join(sList),ans_args=filePath) DsRedis.OpsAnsibleModel.delete(redisKey) DsRedis.OpsAnsibleModel.lpush(redisKey, "[Start] Ansible Model: {model} Script:{args}".format(model='script',args=filePath)) if request.POST.get('ansible_debug') == 'on':ANS = ANSRunner(resource,redisKey,logId,verbosity=4) else:ANS = ANSRunner(resource,redisKey,logId) ANS.run_model(host_list=sList,module_name='script',module_args=filePath) DsRedis.OpsAnsibleModel.lpush(redisKey, "[Done] Ansible Done.") try: os.remove(filePath) except Exception, ex: print ex return JsonResponse({'msg':"操作成功","code":200,'data':[]})
def AnsibleScripts(**kw): logId = None try: if kw.has_key('scripts_id'): script = Ansible_Script.objects.get(id=kw.get('scripts_id')) filePath = os.getcwd() + str(script.script_file) if kw.has_key('hosts'): try: sList = list(kw.get('hosts')) except Exception, ex: return ex else: try: sList = json.loads(script.script_server) except Exception, ex: return ex if kw.has_key('logs'): logId = AnsibleRecord.Model.insert(user='******', ans_model='script', ans_server=','.join(sList), ans_args=filePath) sList, resource = AssetsSource().queryAssetsByIp(ipList=sList) ANS = ANSRunner(resource, redisKey=None, logId=logId) ANS.run_model(host_list=sList, module_name='script', module_args="{filePath} {args}".format( filePath=filePath, args=script.script_args)) return ANS.get_model_result()
def apps_model(request): if request.method == "GET": serverList = Server_Assets.objects.all() return render_to_response('apps/apps_model.html',{"user":request.user, "serverList":serverList}, context_instance=RequestContext(request)) elif request.method == "POST" and request.user.has_perm('OpsManage.can_change_ansible_playbook'): resource = [] sList = [] serverList = request.POST.getlist('ansible_server') for server in serverList: server_assets = Server_Assets.objects.get(id=server) sList.append(server_assets.ip) if server_assets.keyfile == 1:resource.append({"hostname": server_assets.ip, "port": int(server_assets.port)}) else:resource.append({"hostname": server_assets.ip, "port": int(server_assets.port),"username": server_assets.username,"password": server_assets.passwd}) if len(request.POST.get('custom_model')) > 0:model_name = request.POST.get('custom_model') else:model_name = request.POST.get('ansible_model',None) redisKey = base.makeToken(strs=str(request.user)+"ansible_model") DsRedis.OpsAnsibleModel.delete(redisKey) DsRedis.OpsAnsibleModel.lpush(redisKey, "[Start] Ansible Model: {model} ARGS:{args}".format(model=model_name,args=request.POST.get('ansible_agrs',"None"))) ANS = ANSRunner(resource,redisKey) ANS.run_model(host_list=sList,module_name=model_name,module_args=request.POST.get('ansible_agrs',"")) DsRedis.OpsAnsibleModel.lpush(redisKey, "[Done] Ansible Done.") #操作日志异步记录 recordAnsibleModel.delay(user=str(request.user),ans_model=model_name,ans_server=','.join(sList),ans_args=request.POST.get('ansible_agrs',None)) return JsonResponse({'msg':"操作成功","code":200,'data':[]})
def assets_facts(request,args=None): if request.method == "POST" and request.user.has_perm('OpsManage.change_server_assets'): server_id = request.POST.get('server_id') genre = request.POST.get('type') if genre == 'setup': try: server_assets = Server_Assets.objects.get(id=request.POST.get('server_id')) if server_assets.keyfile == 1:resource = [{"hostname": server_assets.ip, "port": int(server_assets.port),"username": server_assets.username}] else:resource = [{"hostname": server_assets.ip, "port": server_assets.port,"username": server_assets.username, "password": server_assets.passwd}] except Exception,ex: logger.error(msg="更新资产失败: {ex}".format(ex=str(ex))) return JsonResponse({'msg':"数据更新失败-查询不到该主机资料~","code":502}) ANS = ANSRunner(resource) ANS.run_model(host_list=[server_assets.ip],module_name='setup',module_args="") data = ANS.handle_cmdb_data(ANS.get_model_result()) if data: for ds in data: status = ds.get('status') if status == 0: try: assets = Assets.objects.get(id=server_assets.assets_id) Assets.objects.filter(id=server_assets.assets_id).update(sn=ds.get('serial'),model=ds.get('model'), manufacturer=ds.get('manufacturer')) except Exception, ex: logger.error(msg="获取服务器信息失败: {ex}".format(ex=str(ex))) return JsonResponse({'msg':"数据更新失败-查询不到该主机的资产信息","code":403}) try: Server_Assets.objects.filter(id=server_id).update(cpu_number=ds.get('cpu_number'),kernel=ds.get('kernel'), selinux=ds.get('selinux'),hostname=ds.get('hostname'), system=ds.get('system'),cpu=ds.get('cpu'), disk_total=ds.get('disk_total'),cpu_core=ds.get('cpu_core'), swap=ds.get('swap'),ram_total=ds.get('ram_total'), vcpu_number=ds.get('vcpu_number') ) recordAssets.delay(user=str(request.user),content="修改服务器资产:{ip}".format(ip=server_assets.ip),type="server",id=server_assets.id) except Exception, ex: logger.error(msg="更新服务器信息失败: {ex}".format(ex=str(ex))) return JsonResponse({'msg':"数据更新失败-写入数据失败","code":400}) for nk in ds.get('nks'): macaddress = nk.get('macaddress') count = NetworkCard_Assets.objects.filter(assets=assets,macaddress=macaddress).count() if count > 0: try: NetworkCard_Assets.objects.filter(assets=assets,macaddress=macaddress).update(assets=assets,device=nk.get('device'), ip=nk.get('address'),module=nk.get('module'), mtu=nk.get('mtu'),active=nk.get('active')) except Exception, ex: logger.warn(msg="更新服务器网卡信息失败: {ex}".format(ex=str(ex))) else: try: NetworkCard_Assets.objects.create(assets=assets,device=nk.get('device'), macaddress=nk.get('macaddress'), ip=nk.get('address'),module=nk.get('module'), mtu=nk.get('mtu'),active=nk.get('active')) except Exception, ex: logger.warn(msg="写入服务器网卡信息失败: {ex}".format(ex=str(ex)))
def cron_config(request): serverList = Server_Assets.objects.all() if request.method == "GET": return render(request,'cron/cron_config.html',{"user":request.user,"serverList":serverList}, ) elif request.method == "POST": try: server = Server_Assets.objects.get(id=request.POST.get('cron_server')) except: return JsonResponse({'msg':"主机资源不存在","code":500,'data':[]}) try: repeatCron = "" for ds in request.POST.get('cron_data').split('\n'): cron = ds.split('|') cron_name = cron[0] cron_time = cron[1] cron_data = cron_time.split(' ',5) try: cron = Cron_Config.objects.create( cron_minute=cron_data[0], cron_hour=cron_data[1], cron_day=cron_data[2], cron_week=cron_data[3], cron_month=cron_data[4], cron_user=request.POST.get('cron_user'), cron_name=cron_name, cron_desc=cron_name, cron_server=server, cron_command=cron_data[5], cron_script=request.FILES.get('file', None), cron_status=request.POST.get('cron_status',0), ) recordCron.delay(cron_user=str(request.user),cron_id=cron.id,cron_name=cron.cron_name,cron_content="导入计划任务",cron_server=server.ip) if int(cron.cron_status) == 1: sList = [server.ip] if server.keyfile == 1:resource = [{"ip": server.ip, "port": int(server.port),"username": server.username}] else:resource = [{"ip": server.ip, "port": int(server.port),"username": server.username,"password": server.passwd}] ANS = ANSRunner(resource) ANS.run_model(host_list=sList,module_name="cron",module_args="""name={name} minute='{minute}' hour='{hour}' day='{day}' weekday='{weekday}' month='{month}' user='******' job='{job}'""".format(name=cron.cron_name,minute=cron.cron_minute, hour=cron.cron_hour,day=cron.cron_day, weekday=cron.cron_week,month=cron.cron_month, user=cron.cron_user,job=cron.cron_command ) ) except Exception,e: repeatCron = cron_name + "<br>" + repeatCron except: return JsonResponse({'msg':'数据格式不对',"code":500,'data':[]}) if repeatCron:return JsonResponse({'msg':'添加失败,以下是重复内容:<br>' + repeatCron,"code":200,'data':[]}) else:return JsonResponse({'msg':'添加成功',"code":200,'data':[]})
def apps_model(request): if request.method == "GET": projectList = Project_Assets.objects.all() serverList = Server_Assets.objects.all() groupList = Group.objects.all() serviceList = Service_Assets.objects.all() return render(request,'apps/apps_model.html',{"user":request.user,"ans_uuid":uuid.uuid4(), "serverList":serverList,"groupList":groupList, "serviceList":serviceList,"projectList":projectList}) elif request.method == "POST" and request.user.has_perm('OpsManage.can_exec_ansible_model'): resource = [] sList = [] if request.POST.get('server_model') in ['service','group','custom']: if request.POST.get('server_model') == 'custom': serverList = request.POST.getlist('ansible_server') for server in serverList: server_assets = Server_Assets.objects.get(id=server) sList.append(server_assets.ip) if server_assets.keyfile == 1:resource.append({"hostname": server_assets.ip, "port": int(server_assets.port),"username": server_assets.username}) else:resource.append({"hostname": server_assets.ip, "port": int(server_assets.port),"username": server_assets.username,"password": server_assets.passwd}) elif request.POST.get('server_model') == 'group': serverList = Assets.objects.filter(group=request.POST.get('ansible_group'),assets_type__in=["server","vmser"]) for server in serverList: sList.append(server.server_assets.ip) if server.server_assets.keyfile == 1:resource.append({"hostname": server.server_assets.ip, "port": int(server.server_assets.port),"username": server.server_assets.username}) else:resource.append({"hostname": server.server_assets.ip, "port": int(server.server_assets.port),"username": server.server_assets.username,"password": server.server_assets.passwd}) elif request.POST.get('server_model') == 'service': serverList = Assets.objects.filter(business=request.POST.get('ansible_service'),assets_type__in=["server","vmser"]) for server in serverList: sList.append(server.server_assets.ip) if server.server_assets.keyfile == 1:resource.append({"hostname": server.server_assets.ip, "port": int(server.server_assets.port),"username": server.server_assets.username}) else:resource.append({"hostname": server.server_assets.ip, "port": int(server.server_assets.port),"username": server.server_assets.username,"password": server.server_assets.passwd}) if len(request.POST.get('custom_model')) > 0:model_name = request.POST.get('custom_model') else:model_name = request.POST.get('ansible_model',None) if len(sList) > 0: redisKey = request.POST.get('ans_uuid') logId = AnsibleRecord.Model.insert(user=str(request.user),ans_model=model_name,ans_server=','.join(sList),ans_args=request.POST.get('ansible_args',None)) DsRedis.OpsAnsibleModel.delete(redisKey) DsRedis.OpsAnsibleModel.lpush(redisKey, "[Start] Ansible Model: {model} ARGS:{args}".format(model=model_name,args=request.POST.get('ansible_args',"None"))) if request.POST.get('ansible_debug') == 'on':ANS = ANSRunner(resource,redisKey,logId,verbosity=4) else:ANS = ANSRunner(resource,redisKey,logId) ANS.run_model(host_list=sList,module_name=model_name,module_args=request.POST.get('ansible_args',"")) DsRedis.OpsAnsibleModel.lpush(redisKey, "[Done] Ansible Done.") return JsonResponse({'msg':"操作成功","code":200,'data':[]}) else: return JsonResponse({'msg':"操作失败,未选择主机或者该分组没有成员","code":500,'data':[]}) else: return JsonResponse({'msg':"操作失败,不支持的操作类型","code":500,'data':[]})
def assets_batch(request): if request.method == "POST": fList = [] sList = [] resource = [] serList = [] if request.POST.get('model') == 'update': for ast in request.POST.getlist('assetsIds[]'): try: assets = Assets.objects.get(id=int(ast)) except Exception, ex: print ex continue if assets.assets_type in ['vmser','server']: try: server_assets = Server_Assets.objects.get(assets=assets) except Exception, ex: fList.append(assets.management_ip) continue serList.append(server_assets.ip) if server_assets.keyfile == 1:resource.append({"hostname": server_assets.ip, "port": int(server_assets.port),"username": server_assets.username}) else:resource.append({"hostname": server_assets.ip, "port": server_assets.port,"username": server_assets.username, "password": server_assets.passwd}) ANS = ANSRunner(resource) ANS.run_model(host_list=serList,module_name='setup',module_args="") data = ANS.handle_cmdb_data(ANS.get_model_result()) if data: for ds in data: status = ds.get('status') if status == 0: try: Server_Assets.objects.filter(ip=ds.get('ip')).update(cpu_number=ds.get('cpu_number'),kernel=ds.get('kernel'), selinux=ds.get('selinux'),hostname=ds.get('hostname'), system=ds.get('system'),cpu=ds.get('cpu'), disk_total=ds.get('disk_total'),cpu_core=ds.get('cpu_core'), swap=ds.get('swap'),ram_total=ds.get('ram_total'), vcpu_number=ds.get('vcpu_number') ) sList.append(server_assets.ip) except Exception: fList.append(server_assets.ip) else:fList.append(server_assets.ip) if sList: return JsonResponse({'msg':"数据更新成功","code":200,'data':{"success":sList,"failed":fList}}) else:return JsonResponse({'msg':"数据更新失败","code":500,'data':{"success":sList,"failed":fList}})
def apps_model(request): if request.method == "GET": projectList = Project_Assets.objects.all() serverList = AssetsSource().serverList() groupList = Group.objects.all() serviceList = Service_Assets.objects.all() inventoryList = Ansible_Inventory.objects.all() return render(request,'apps/apps_model.html',{"user":request.user,"ans_uuid":uuid.uuid4(), "serverList":serverList,"groupList":groupList, "serviceList":serviceList,"projectList":projectList, "inventoryList":inventoryList}) elif request.method == "POST" and request.user.has_perm('OpsManage.can_exec_ansible_model'): resource = [] sList = [] if request.POST.get('server_model') in ['service','group','custom','inventory']: if request.POST.get('server_model') == 'custom': serverList = request.POST.getlist('ansible_server') sList,resource = AssetsSource().custom(serverList) elif request.POST.get('server_model') == 'group': sList,resource = AssetsSource().group(group=request.POST.get('ansible_group')) elif request.POST.get('server_model') == 'service': sList,resource = AssetsSource().service(business=request.POST.get('ansible_service')) elif request.POST.get('server_model') == 'inventory': sList,resource,groups = AssetsSource().inventory(inventory=request.POST.get('ansible_inventory')) if len(request.POST.get('custom_model')) > 0:model_name = request.POST.get('custom_model') else:model_name = request.POST.get('ansible_model',None) if len(sList) > 0: redisKey = request.POST.get('ans_uuid') logId = AnsibleRecord.Model.insert(user=str(request.user),ans_model=model_name,ans_server=','.join(sList),ans_args=request.POST.get('ansible_args',None)) DsRedis.OpsAnsibleModel.delete(redisKey) DsRedis.OpsAnsibleModel.lpush(redisKey, "[Start] Ansible Model: {model} ARGS:{args}".format(model=model_name,args=request.POST.get('ansible_args',"None"))) if request.POST.get('ansible_debug') == 'on':ANS = ANSRunner(resource,redisKey,logId,verbosity=4) else:ANS = ANSRunner(resource,redisKey,logId) if request.POST.get('server_model') == 'inventory':sList = groups[:-1] ANS.run_model(host_list=sList,module_name=model_name,module_args=request.POST.get('ansible_args',"")) DsRedis.OpsAnsibleModel.lpush(redisKey, "[Done] Ansible Done.") return JsonResponse({'msg':"操作成功","code":200,'data':[]}) else: return JsonResponse({'msg':"操作失败,未选择主机或者该分组没有成员","code":500,'data':[]}) else: return JsonResponse({'msg':"操作失败,不支持的操作类型","code":500,'data':[]})
def assets_facts(request,args=None): if request.method == "POST" and request.user.has_perm('OpsManage.change_server_assets'): server_id = request.POST.get('server_id') genre = request.POST.get('type') if genre == 'setup': try: server_assets = Server_Assets.objects.get(id=request.POST.get('server_id')) if server_assets.keyfile == 1:resource = [{"hostname": server_assets.ip, "port": int(server_assets.port)}] else:resource = [{"hostname": server_assets.ip, "port": server_assets.port,"username": server_assets.username, "password": server_assets.passwd}] except Exception,e: return JsonResponse({'msg':"数据更新失败-查询不到该主机资料~","code":502}) ANS = ANSRunner(resource) ANS.run_model(host_list=[server_assets.ip],module_name='setup',module_args="") data = ANS.handle_cmdb_data(ANS.get_model_result()) if data: for ds in data: status = ds.get('status') if status == 0: try: Assets.objects.filter(id=server_assets.assets_id).update(sn=ds.get('serial'),model=ds.get('model'), manufacturer=ds.get('manufacturer')) except Exception,e: return JsonResponse({'msg':"数据更新失败-查询不到该主机的资产信息","code":403}) try: Server_Assets.objects.filter(id=server_id).update(cpu_number=ds.get('cpu_number'),kernel=ds.get('kernel'), selinux=ds.get('selinux'),hostname=ds.get('hostname'), system=ds.get('system'),cpu=ds.get('cpu'), disk_total=ds.get('disk_total'),cpu_core=ds.get('cpu_core'), swap=ds.get('swap'),ram_total=ds.get('ram_total'), vcpu_number=ds.get('vcpu_number') ) recordAssets.delay(user=str(request.user),content="修改服务器资产:{ip}".format(ip=server_assets.ip),type="server",id=server_assets.id) except Exception,e: print e return JsonResponse({'msg':"数据更新失败-写入数据失败","code":400}) else: return JsonResponse({'msg':"数据更新失败-无法链接主机~","code":502}) return JsonResponse({'msg':"数据更新成功","code":200})
def cron_mod(request,cid): try: cron = Cron_Config.objects.select_related().get(id=cid) except: return render(request,'cron/cron_modf.html',{"user":request.user, "errorInfo":"任务不存在,可能已经被删除."}, ) if request.method == "GET": return render(request,'cron/cron_modf.html', {"user":request.user,"cron":cron}, ) elif request.method == "POST": try: Cron_Config.objects.filter(id=cid).update( cron_minute=request.POST.get('cron_minute'), cron_hour=request.POST.get('cron_hour'), cron_day=request.POST.get('cron_day'), cron_week=request.POST.get('cron_week'), cron_month=request.POST.get('cron_month'), cron_user=request.POST.get('cron_user'), cron_desc=request.POST.get('cron_desc'), cron_command=request.POST.get('cron_command'), cron_script_path=request.POST.get('cron_script_path',None), cron_status=request.POST.get('cron_status'), ) recordCron.delay(cron_user=str(request.user),cron_id=cid,cron_name=cron.cron_name,cron_content="修改计划任务",cron_server=cron.cron_server.ip) except Exception,e: return render(request,'cron/cron_modf.html', {"user":request.user,"errorInfo":"更新失败,错误信息:"+str(e)}, ) try: sList = [cron.cron_server.ip] if cron.cron_server.keyfile == 1:resource = [{"ip": cron.cron_server.ip, "port": int(cron.cron_server.port),"username": cron.cron_server.username}] else:resource = [{"ip": cron.cron_server.ip, "port": int(cron.cron_server.port), "username": cron.cron_server.username,"password": cron.cron_server.passwd}] cron = Cron_Config.objects.get(id=cid) if request.FILES.get('cron_script'): cron.cron_script=request.FILES.get('cron_script') cron.save() ANS = ANSRunner(resource) if cron.cron_status == 0:ANS.run_model(host_list=sList,module_name="cron",module_args="""name={name} state=absent""".format(name=cron.cron_name)) else: if cron.cron_script: src = os.getcwd() + '/' + str(cron.cron_script) file_args = """src={src} dest={dest} owner={user} group={user} mode=755""".format(src=src,dest=cron.cron_script_path,user=cron.cron_user) ANS.run_model(host_list=sList,module_name="copy",module_args=file_args) cron_args = """name={name} minute='{minute}' hour='{hour}' day='{day}' weekday='{weekday}' month='{month}' user='******' job='{job}'""".format(name=cron.cron_name,minute=cron.cron_minute, hour=cron.cron_hour,day=cron.cron_day, weekday=cron.cron_week,month=cron.cron_month, user=cron.cron_user,job=cron.cron_command ) ANS.run_model(host_list=sList,module_name="cron",module_args=cron_args) except Exception,e: return render(request,'cron/cron_modf.html',{"user":request.user,"errorInfo":"错误信息:"+str(e)}, )
def file_downloads(request): if request.method == "POST": try: order = Order_System.objects.get(id=request.POST.get('id',0),order_type=3) except Exception,ex: return JsonResponse({'msg':ex,"code":500,'data':[]}) if request.user.is_superuser or request.user.id in [order.order_executor,order.order_user]: sList,resource = AssetsSource().queryAssetsByIp(ipList=request.POST.getlist('dest_server')) if len(sList) > 0 and order.order_status == 8: ANS = ANSRunner(resource) dest = os.getcwd() + '/upload/file/download/' module_args = "src={src} dest={dest}".format(src=request.POST.get('path'),dest=dest) ANS.run_model(host_list=sList,module_name='fetch',module_args=module_args) filesData = json.loads(ANS.get_model_result()) filePath = filesData.get('success').get(request.POST.get('dest_server')).get('dest') if filePath: response = StreamingHttpResponse(base.file_iterator(filePath)) response['Content-Type'] = 'application/octet-stream' response['Content-Disposition'] = 'attachment; filename="{file_name}'.format(file_name=os.path.basename(filePath)) return response else: return JsonResponse({'msg':"操作失败,未选择主机或者该分组没有成员","code":500,'data':[]}) else: return JsonResponse({'msg':"操作失败,您没有权限进行操作","code":500,'data':[]})
def deploy_run(request,pid): try: project = Project_Config.objects.get(id=pid) serverList = Project_Number.objects.filter(project=project) if project.project_repertory == 'git':version = GitTools() elif project.project_repertory == 'svn':version = SvnTools() except: return render_to_response('deploy/deploy_run.html',{"user":request.user, "errorInfo":"项目不存在,可能已经被删除."}, context_instance=RequestContext(request)) if request.method == "GET": if project.project_model == 'branch':bList = version.branch(path=project.project_repo_dir) elif project.project_model == 'tag':bList = version.tag(path=project.project_repo_dir) #获取最新版本 version.pull(path=project.project_repo_dir) vList = version.log(path=project.project_repo_dir, number=50) return render_to_response('deploy/deploy_run.html',{"user":request.user, "project":project,"serverList":serverList, "bList":bList,"vList":vList}, context_instance=RequestContext(request)) elif request.method == "POST": if request.POST.getlist('project_server',None): serverList = [ Project_Number.objects.get(project=project,server=ds) for ds in request.POST.getlist('project_server') ] allServerList = [ ds.server for ds in Project_Number.objects.filter(project=project) ] #获取项目目标服务器列表与分批部署服务器(post提交)列表的差集 tmpServer = [ i for i in allServerList if i not in request.POST.getlist('project_server') ] else:return JsonResponse({'msg':"项目部署失败:未选择目标服务器","code":500,'data':[]}) if DsRedis.OpsProject.get(redisKey=project.project_uuid+"-locked") is None:#判断该项目是否有人在部署 #给项目部署加上锁 DsRedis.OpsProject.set(redisKey=project.project_uuid+"-locked",value=request.user) DsRedis.OpsDeploy.delete(project.project_uuid) if request.POST.get('project_mode',None) == "rollback": if project.project_model == 'branch': trueDir = project.project_dir+project.project_env+'/'+ request.POST.get('project_version') + '/' DsRedis.OpsDeploy.lpush(project.project_uuid, data="[Start] Start Rollback branch:%s vesion: %s" % (request.POST.get('project_branch'),request.POST.get('project_version'))) elif project.project_model == 'tag': trueDir = project.project_dir+project.project_env+'/'+ request.POST.get('project_branch') + '/' DsRedis.OpsDeploy.lpush(project.project_uuid, data="[Start] Start Rollback tag:%s" % request.POST.get('project_branch')) else: #判断版本上线类型再切换分支到指定的分支/Tag if project.project_model == 'branch': bName = request.POST.get('project_branch') result = version.checkOut(path=project.project_repo_dir, name=bName) DsRedis.OpsDeploy.lpush(project.project_uuid, data="[Start] Switched to branch %s" % bName) #reset到指定版本 result = version.reset(path=project.project_repo_dir, commintId=request.POST.get('project_version')) DsRedis.OpsDeploy.lpush(project.project_uuid, data="[Running] Git reset to {comid} info: {info}".format(comid=request.POST.get('project_version'),info=result[1])) trueDir = project.project_dir+project.project_env+'/'+ request.POST.get('project_version') + '/' elif project.project_model == 'tag': bName = request.POST.get('project_branch') result = version.checkOut(path=project.project_repo_dir, name=bName) DsRedis.OpsDeploy.lpush(project.project_uuid, data="[Start] Switched to tag %s" % bName) trueDir = project.project_dir+project.project_env+'/'+ bName + '/' #创建版本目录 base.mkdir(dirPath=trueDir) DsRedis.OpsDeploy.lpush(project.project_uuid, data="[Running] Mkdir version dir: {dir} ".format(dir=trueDir)) #创建快捷方式 softdir = project.project_dir+project.project_name+'/' result = base.lns(spath=trueDir, dpath=softdir.rstrip('/')) DsRedis.OpsDeploy.lpush(project.project_uuid, data="[Running] Make softlink cmd: ln -s {sdir} {ddir} info: {info}".format(sdir=trueDir,ddir=softdir,info=result[1])) if result[0] > 0:return JsonResponse({'msg':result[1],"code":500,'data':[]}) #执行部署命令 - 编译型语言 if project.project_local_command: result = base.cmds(cmds=project.project_local_command) DsRedis.OpsDeploy.lpush(project.project_uuid, data="[Running] Execute local command: {cmds} info: {info}".format(cmds=project.project_local_command,info=result[1])) if result[0] > 0:return JsonResponse({'msg':result[1],"code":500,'data':[]}) #非编译型语言 else: #获取要排除的文件 exclude = None if project.project_exclude: try: exclude = '' for s in project.project_exclude.split(','): exclude = '--exclude "{file}"'.format(file=s.replace('\r\n','').replace('\n','').strip()) + ' ' + exclude except Exception,e: return JsonResponse({'msg':str(e),"code":500,'data':[]}) #配置rsync同步文件到本地目录 result = base.rsync(sourceDir=project.project_repo_dir, destDir=trueDir,exclude=exclude) DsRedis.OpsDeploy.lpush(project.project_uuid, data="[Running] Rsync {sDir} to {dDir} exclude {exclude}".format(sDir=project.project_repo_dir,dDir=trueDir,exclude=exclude)) if result[0] > 0:return JsonResponse({'msg':result[1],"code":500,'data':[]}) #授权文件 result = base.chown(user=project.project_user, path=trueDir) DsRedis.OpsDeploy.lpush(project.project_uuid, data="[Running] Chown {user} to {path}".format(user=project.project_user,path=trueDir)) if result[0] > 0:return JsonResponse({'msg':result[1],"code":500,'data':[]}) #调用ansible同步代码到远程服务器上 resource = [] hostList = [] for ds in serverList: server = Server_Assets.objects.get(ip=ds.server) hostList.append(ds.server) data = dict() if server.keyfile == 1: data['port'] = int(server.port) data["hostname"] = server.ip else: data["hostname"] = server.ip data["port"] = int(server.port) data["username"] = server.username data["password"] = server.passwd resource.append(data) if resource and hostList: if exclude:args = '''src={srcDir} dest={desDir} links=yes recursive=yes compress=yes delete=yes rsync_opts="{exclude}"'''.format(srcDir=softdir, desDir=ds.dir,exclude=exclude) else:args = '''src={srcDir} dest={desDir} links=yes recursive=yes compress=yes delete=yes'''.format(srcDir=softdir, desDir=ds.dir) ANS = ANSRunner(resource) ANS.run_model(host_list=hostList,module_name='synchronize',module_args=args) #精简返回的结果 dataList = ANS.handle_model_data(ANS.get_model_result() , 'synchronize', module_args=args) for ds in dataList: DsRedis.OpsDeploy.lpush(project.project_uuid, data="[Running] Rsync project to {host} status: {status} msg: {msg}".format(host=ds.get('ip'), status=ds.get('status'), msg=ds.get('msg'))) if ds.get('status') == 'failed':result = (1,ds.get('ip')+ds.get('msg')) #目标服务器执行后续命令 if project.project_remote_command: ANS.run_model(host_list=hostList,module_name='raw',module_args=project.project_remote_command) #精简返回的结果 dataList = ANS.handle_model_data(ANS.get_model_result() , 'raw', module_args=project.project_remote_command) for ds in dataList: DsRedis.OpsDeploy.lpush(project.project_uuid, data="[Running] Execute command to {host} status: {status} msg: {msg}".format(host=ds.get('ip'), status=ds.get('status'), msg=ds.get('msg'))) if ds.get('status') == 'failed':result = (1,"部署错误: " +ds.get('msg')) if result[0] > 0: DsRedis.OpsProject.delete(redisKey=project.project_uuid+"-locked") return JsonResponse({'msg':result[1],"code":500,'data':[]}) DsRedis.OpsDeploy.lpush(project.project_uuid, data="[Done] Deploy Success.") #切换版本之后取消项目部署锁 DsRedis.OpsProject.delete(redisKey=project.project_uuid+"-locked") #异步记入操作日志 if request.POST.get('project_version'):bName = request.POST.get('project_version') recordProject.delay(project_user=str(request.user),project_id=project.id, project_name=project.project_name,project_content="部署项目", project_branch=bName) return JsonResponse({'msg':"项目部署成功","code":200,'data':tmpServer}) else: return JsonResponse({'msg':"项目部署失败:{user}正在部署改项目,请稍后再提交部署。".format(user=DsRedis.OpsProject.get(redisKey=project.project_uuid+"-locked")),"code":500,'data':[]})
continue if server_assets.keyfile == 1: resource.append({ "hostname": server_assets.ip, "port": int(server_assets.port) }) else: resource.append({ "hostname": server_assets.ip, "port": int(server_assets.port), "username": server_assets.username, "password": server_assets.passwd }) ANS = ANSRunner(resource, redisKey=None, logId=logId) ANS.run_model(host_list=sList, module_name='script', module_args=filePath) return ANS.get_model_result() except Exception, e: print e return False @task def AnsiblePlayBook(**kw): logId = None resource = [] try: if kw.has_key('playbook_id'): playbook = Ansible_Playbook.objects.get(id=kw.get('playbook_id')) filePath = os.getcwd() + str(playbook.playbook_file)
resource.append(data) DsRedis.OpsDeploy.lpush( project.project_uuid, data="[RSYNC start rsync project to remote server]") if resource and hostList: if exclude: args = "src={srcDir} dest={desDir} links=yes recursive=yes compress=yes delete=yes rsync_opts='{exclude}'".format( srcDir=softdir, desDir=ds.dir, exclude=exclude.replace(' ', ',')[:-1]) else: args = '''src={srcDir} dest={desDir} links=yes recursive=yes compress=yes delete=yes'''.format( srcDir=softdir, desDir=ds.dir) ANS = ANSRunner(resource) ANS.run_model(host_list=hostList, module_name='synchronize', module_args=args) #精简返回的结果 dataList = ANS.handle_model_data(ANS.get_model_result(), 'synchronize', module_args=args) for ds in dataList: DsRedis.OpsDeploy.lpush( project.project_uuid, data= "[RSYNC] Rsync project to {host} status: {status} msg: {msg}" .format(host=ds.get('ip'), status=ds.get('status'), msg=ds.get('msg'))) if ds.get('status') == 'failed': result = (1, ds.get('ip') + ds.get('msg'))
if cron.cron_server.keyfile == 1: resource = [{ "hostname": cron.cron_server.ip, "port": int(cron.cron_server.port), "username": cron.cron_server.username }] else: resource = [{ "hostname": cron.cron_server.ip, "port": int(cron.cron_server.port), "username": cron.cron_server.username, "password": cron.cron_server.passwd }] ANS = ANSRunner(resource) ANS.run_model(host_list=sList, module_name="cron", module_args="""name={name} state=absent""".format( name=cron.cron_name)) cron.delete() except Exception, e: return JsonResponse({ 'msg': '删除失败:' + str(e), "code": 500, 'data': [] }) return JsonResponse({'msg': '删除成功', "code": 200, 'data': []}) @login_required() @permission_required('OpsManage.can_add_cron_config', login_url='/noperm/') def cron_config(request): serverList = Server_Assets.objects.all()
if request.user.is_superuser or request.user.id in [order.order_executor,order.order_user]: sList,resource = AssetsSource().queryAssetsByIp(ipList=request.POST.getlist('dest_server')) if len(sList) > 0 and order.order_status == 8: redisKey = request.POST.get('ans_uuid') DsRedis.OpsAnsibleModel.delete(redisKey) ANS = ANSRunner(resource,redisKey) DsRedis.OpsAnsibleModel.lpush(redisKey, "[Start] file distribution".format(model='copy',args=request.POST.get('ansible_args',"None"))) for files in request.POST.getlist('file_path'): file = UploadFiles.objects.get(id=files) filePath = os.getcwd() + '/upload/' + str(file.file_path) module_args = "src={src} dest={dest} mode={chown_rwx} owner={chown_user} group={chown_user} backup={backup}".format(src=filePath,backup=request.POST.get('backup','yes'), dest=order.fileupload_audit_order.dest_path, chown_user=order.fileupload_audit_order.chown_user, chown_rwx=order.fileupload_audit_order.chown_rwx ) ANS.run_model(host_list=sList,module_name='copy',module_args=module_args) DsRedis.OpsAnsibleModel.lpush(redisKey, "[Done] Ansible Done.") return JsonResponse({'msg':"操作成功","code":200,'data':[]}) else: return JsonResponse({'msg':"操作失败,未选择主机或者该分组没有成员","code":500,'data':[]}) else: return JsonResponse({'msg':"操作失败,您没有权限进行操作","code":500,'data':[]}) else: return JsonResponse({'msg':"操作失败,不支持的操作类型","code":500,'data':[]}) @login_required() @permission_required('filemanage.can_add_fileupload_audit_order',login_url='/noperm/') def file_download_run(request,id): if request.method == "GET": try:
@task() def updateAssets(): sList = [] resource = [] for assets in Assets.objects.all(): if assets.assets_type in ['vmser','server']: try: server_assets = Server_Assets.objects.get(assets=assets) except Exception, ex: print ex continue sList.append(server_assets.ip) if server_assets.keyfile == 1:resource.append({"hostname": server_assets.ip, "port": int(server_assets.port),"username": server_assets.username}) else:resource.append({"hostname": server_assets.ip, "port": server_assets.port,"username": server_assets.username, "password": server_assets.passwd}) ANS = ANSRunner(resource) ANS.run_model(host_list=sList,module_name='setup',module_args="") data = ANS.handle_cmdb_data(ANS.get_model_result()) if data: for ds in data: status = ds.get('status') if status == 0: try: assets = Server_Assets.objects.get(ip=ds.get('ip')).assets Server_Assets.objects.filter(ip=ds.get('ip')).update(cpu_number=ds.get('cpu_number'),kernel=ds.get('kernel'), selinux=ds.get('selinux'),hostname=ds.get('hostname'), system=ds.get('system'),cpu=ds.get('cpu'), disk_total=ds.get('disk_total'),cpu_core=ds.get('cpu_core'), swap=ds.get('swap'),ram_total=ds.get('ram_total'), vcpu_number=ds.get('vcpu_number') ) sList.append(server_assets.ip)
def cron_add(request): serverList = Server_Assets.objects.all() if request.method == "GET": return render( request, 'cron/cron_add.html', { "user": request.user, "serverList": serverList }, ) elif request.method == "POST": cron_status = request.POST.get('cron_status', 0) try: server = Server_Assets.objects.get( id=request.POST.get('cron_server')) except: return render( request, 'cron/cron_add.html', { "user": request.user, "serverList": serverList, "errorInfo": "主机不存在,请检查是否被删除。" }, ) try: cron = Cron_Config.objects.create( cron_minute=request.POST.get('cron_minute'), cron_hour=request.POST.get('cron_hour'), cron_day=request.POST.get('cron_day'), cron_week=request.POST.get('cron_week'), cron_month=request.POST.get('cron_month'), cron_user=request.POST.get('cron_user'), cron_name=request.POST.get('cron_name'), cron_desc=request.POST.get('cron_desc'), cron_server=server, cron_command=request.POST.get('cron_command'), cron_script=request.FILES.get('cron_script', None), cron_script_path=request.POST.get('cron_script_path', None), cron_status=cron_status, ) recordCron.delay(cron_user=str(request.user), cron_id=cron.id, cron_name=cron.cron_name, cron_content="添加计划任务", cron_server=server.ip) except Exception, e: return render( request, 'cron/cron_add.html', { "user": request.user, "serverList": serverList, "errorInfo": "提交失败,错误信息:" + str(e) }, ) if int(cron_status) == 1: try: sList = [server.ip] if server.keyfile == 1: resource = [{ "hostname": server.ip, "port": int(server.port), "username": server.username }] else: resource = [{ "hostname": server.ip, "port": int(server.port), "username": server.username, "password": server.passwd }] ANS = ANSRunner(resource) if cron.cron_script: src = os.getcwd() + '/' + str(cron.cron_script) file_args = """src={src} dest={dest} owner={user} group={user} mode=755""".format( src=src, dest=cron.cron_script_path, user=cron.cron_user) ANS.run_model(host_list=sList, module_name="copy", module_args=file_args) result = ANS.handle_model_data(ANS.get_model_result(), 'copy', file_args) if result[0].get('status') == 'failed': cron.delete() return render( request, 'cron/cron_add.html', { "user": request.user, "serverList": serverList, "errorInfo": "错误信息:" + result[0].get('msg') }, ) # cron_args = """name={name} minute='{minute}' hour='{hour}' day='{day}' weekday='{weekday}' month='{month}' user='******' job='{job}'""".format( name=cron.cron_name, minute=cron.cron_minute, hour=cron.cron_hour, day=cron.cron_day, weekday=cron.cron_week, month=cron.cron_month, user=cron.cron_user, job=cron.cron_command) ANS.run_model(host_list=sList, module_name="cron", module_args=cron_args) result = ANS.handle_model_data(ANS.get_model_result(), 'cron', cron_args) print ANS.get_model_result() except Exception, e: return render( request, 'cron/cron_add.html', { "user": request.user, "serverList": serverList, "errorInfo": "错误信息:" + str(e) }, ) if result[0].get('status') == 'failed': cron.delete() return render( request, 'cron/cron_add.html', { "user": request.user, "serverList": serverList, "errorInfo": "错误信息:" + result[0].get('msg').replace('\n', '') })
"hostname": server_assets.ip, "port": int(server_assets.port), "username": server_assets.username }] else: resource = [{ "hostname": server_assets.ip, "port": server_assets.port, "username": server_assets.username, "password": server_assets.passwd }] except Exception, e: return JsonResponse({'msg': "数据更新失败-查询不到该主机资料~", "code": 502}) ANS = ANSRunner(resource) ANS.run_model(host_list=[server_assets.ip], module_name='crawHw', module_args="") data = ANS.handle_cmdb_crawHw_data(ANS.get_model_result()) if data: for ds in data: if ds.get('mem_info'): for mem in ds.get('mem_info'): if Ram_Assets.objects.filter( assets=assets, device_slot=mem.get('slot')).count() > 0: try: Ram_Assets.objects.filter( assets=assets, device_slot=mem.get('slot')).update( device_slot=mem.get('slot'), device_model=mem.get('serial'),
def assets_batch(request): if request.method == "POST": fList = [] sList = [] resource = [] serList = [] if request.POST.get('model') == 'update': for ast in request.POST.getlist('assetsIds[]'): try: assets = Assets.objects.get(id=int(ast)) except Exception, ex: print ex continue if assets.assets_type in ['vmser', 'server']: try: server_assets = Server_Assets.objects.get( assets=assets) except Exception, ex: fList.append(assets.management_ip) continue serList.append(server_assets.ip) if server_assets.keyfile == 1: resource.append({ "hostname": server_assets.ip, "port": int(server_assets.port), "username": server_assets.username }) else: resource.append({ "hostname": server_assets.ip, "port": server_assets.port, "username": server_assets.username, "password": server_assets.passwd }) ANS = ANSRunner(resource) ANS.run_model(host_list=serList, module_name='setup', module_args="") data = ANS.handle_cmdb_data(ANS.get_model_result()) if data: for ds in data: status = ds.get('status') if status == 0: assets = Server_Assets.objects.get( ip=ds.get('ip')).assets try: Server_Assets.objects.filter( ip=ds.get('ip')).update( cpu_number=ds.get('cpu_number'), kernel=ds.get('kernel'), selinux=ds.get('selinux'), hostname=ds.get('hostname'), system=ds.get('system'), cpu=ds.get('cpu'), disk_total=ds.get('disk_total'), cpu_core=ds.get('cpu_core'), swap=ds.get('swap'), ram_total=ds.get('ram_total'), vcpu_number=ds.get('vcpu_number')) sList.append(server_assets.ip) except Exception: fList.append(server_assets.ip) for nk in ds.get('nks'): macaddress = nk.get('macaddress') count = NetworkCard_Assets.objects.filter( assets=assets, macaddress=macaddress).count() if count > 0: try: NetworkCard_Assets.objects.filter( assets=assets, macaddress=macaddress).update( assets=assets, device=nk.get('device'), ip=nk.get('address'), module=nk.get('module'), mtu=nk.get('mtu'), active=nk.get('active')) except Exception, ex: print ex else: try: NetworkCard_Assets.objects.create( assets=assets, device=nk.get('device'), macaddress=nk.get('macaddress'), ip=nk.get('address'), module=nk.get('module'), mtu=nk.get('mtu'), active=nk.get('active')) except Exception, ex: print ex else: fList.append(server_assets.ip)
def apps_script_online(request): if request.method == "GET": serverList = AssetsSource().serverList() groupList = Group.objects.all() serviceList = Service_Assets.objects.all() projectList = Project_Assets.objects.all() return render(request,'apps/apps_script_online.html',{"user":request.user,"ans_uuid":uuid.uuid4(), "serverList":serverList,"groupList":groupList, "serviceList":serviceList,"projectList":projectList}) elif request.method == "POST" and request.user.has_perm('OpsManage.can_exec_ansible_script'): resource = [] sList = [] def saveScript(content,filePath): if os.path.isdir(os.path.dirname(filePath)) is not True:os.makedirs(os.path.dirname(filePath))#判断文件存放的目录是否存在,不存在就创建 with open(filePath, 'w') as f: f.write(content) return filePath if request.POST.get('server_model') in ['service','group','custom']: if request.POST.get('server_model') == 'custom': serverList = request.POST.getlist('ansible_server[]') sList,resource = AssetsSource().custom(serverList=serverList) elif request.POST.get('server_model') == 'group': sList,resource = AssetsSource().group(group=request.POST.get('ansible_group',0)) elif request.POST.get('server_model') == 'service': sList,resource = AssetsSource().service(business=request.POST.get('ansible_service')) if len(sList) > 0 and request.POST.get('type') == 'run' and request.POST.get('script_file'): filePath = saveScript(content=request.POST.get('script_file'),filePath='/tmp/script-{ram}'.format(ram=uuid.uuid4().hex[0:8])) redisKey = request.POST.get('ans_uuid') logId = AnsibleRecord.Model.insert(user=str(request.user),ans_model='script',ans_server=','.join(sList),ans_args=filePath) DsRedis.OpsAnsibleModel.delete(redisKey) DsRedis.OpsAnsibleModel.lpush(redisKey, "[Start] Ansible Model: {model} Script:{filePath} {args}".format(model='script',filePath=filePath,args=request.POST.get('script_args'))) if request.POST.get('ansible_debug') == 'on':ANS = ANSRunner(resource,redisKey,logId,verbosity=4) else:ANS = ANSRunner(resource,redisKey,logId) ANS.run_model(host_list=sList,module_name='script',module_args="{filePath} {args}".format(filePath=filePath,args=request.POST.get('script_args'))) DsRedis.OpsAnsibleModel.lpush(redisKey, "[Done] Ansible Done.") try: os.remove(filePath) except Exception, ex: logger.warn(msg="删除文件失败: {ex}".format(ex=ex)) return JsonResponse({'msg':"操作成功","code":200,'data':[]}) if request.POST.get('type') == 'save' and request.POST.get('script_file') and \ ( request.user.has_perm('OpsManage.can_add_ansible_script') or request.user.has_perm('OpsManage.can_edit_ansible_script') ): fileName = '/upload/scripts/script-{ram}'.format(ram=uuid.uuid4().hex[0:8]) filePath = os.getcwd() + fileName saveScript(content=request.POST.get('script_file'),filePath=filePath) try: service = int(request.POST.get('ansible_service')) except: service = None try: group = int(request.POST.get('ansible_group')) except: group = None try: Ansible_Script.objects.create( script_name=request.POST.get('script_name'), script_uuid=request.POST.get('ans_uuid'), script_args=request.POST.get('script_args'), script_server=json.dumps(sList), script_group=group, script_file=fileName, script_service=service, script_type=request.POST.get('server_model') ) except Exception,ex: logger.warn(msg="添加ansible脚本失败: {ex}".format(ex=ex)) return JsonResponse({'msg':str(ex),"code":500,'data':[]}) return JsonResponse({'msg':"保存成功","code":200,'data':[]})