Ejemplo n.º 1
0
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()
Ejemplo n.º 2
0
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)))
Ejemplo n.º 3
0
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,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 = 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,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})
                        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
Ejemplo n.º 4
0
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}}) 
Ejemplo n.º 5
0
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()
Ejemplo n.º 6
0
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})
Ejemplo n.º 7
0
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':[]})
Ejemplo n.º 8
0
                    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)
            if kw.has_key('hosts'):
                try:
Ejemplo n.º 9
0
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)
                except Exception, ex:
Ejemplo n.º 10
0
def cron_add(request):
    serverList = Server_Assets.objects.all()
    if request.method == "GET": 
        return render_to_response('cron/cron_add.html',{"user":request.user,"serverList":serverList},
                                  context_instance=RequestContext(request))
    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_to_response('cron/cron_add.html',{"user":request.user,
                                                               "serverList":serverList,
                                                               "errorInfo":"主机不存在,请检查是否被删除。"},
                                  context_instance=RequestContext(request)) 
        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_to_response('cron/cron_add.html',{"user":request.user,
                                                               "serverList":serverList,
                                                               "errorInfo":"提交失败,错误信息:"+str(e)},
                                  context_instance=RequestContext(request))    
        
        if  int(cron_status) == 1: 
            try:
                sList = [server.ip]
                if server.keyfile == 1:resource = [{"hostname": server.ip, "port": int(server.port)}] 
                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_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) 
            except Exception,e:
                return render_to_response('cron/cron_add.html',{"user":request.user,
                                                                   "serverList":serverList,
                                                                   "errorInfo":"错误信息:"+str(e)}, 
                                      context_instance=RequestContext(request))     
            if result[0].get('status') == 'failed':
                cron.delete()
                return render_to_response('cron/cron_add.html',{"user":request.user,
                                                                   "serverList":serverList,
                                                                   "errorInfo":"错误信息:"+script[0].get('msg')}, 
                                      context_instance=RequestContext(request)) 
Ejemplo n.º 11
0
                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)
            for sip in sList:
                try:
                    server_assets = Server_Assets.objects.get(ip=sip)
                except Exception, ex:
                    continue
                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})         
            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)
            if kw.has_key('hosts'):
                try:
Ejemplo n.º 12
0
     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 project.project_remote_command:
     DsRedis.OpsDeploy.lpush(
Ejemplo n.º 13
0
     hostList.append(ds.server)
     data = dict()
     if server.keyfile == 0:data["password"] = server.passwd
     data["ip"] = server.ip
     data["port"] = int(server.port)
     data["username"] = server.username                    
     data["sudo_passwd"] = server.sudo_passwd
     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)
     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 project.project_remote_command:
     DsRedis.OpsDeploy.lpush(project.project_uuid, data="[CMD start run command to remote server]") 
     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="[CMD] Execute command to {host} status: {status} msg: {msg}".format(host=ds.get('ip'),
                                                                                                                       status=ds.get('status'),
                                                                                                                       msg=ds.get('msg')))
Ejemplo n.º 14
0
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':[]}) 
Ejemplo n.º 15
0
             "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'),
                                 device_brand=mem.get(
                                     'manufacturer'),
Ejemplo n.º 16
0
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', '')
                    })
Ejemplo n.º 17
0
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)                                  
Ejemplo n.º 18
0
     hostList.append(ds.server)
     data = dict()
     if server.keyfile == 0:data["password"] = server.passwd
     data["ip"] = server.ip
     data["port"] = int(server.port)
     data["username"] = server.username                    
     data["sudo_passwd"] = server.sudo_passwd
     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)
     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 project.project_remote_command:
     DsRedis.OpsDeploy.lpush(project.project_uuid, data="[CMD start run command to remote server]") 
     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="[CMD] Execute command to {host} status: {status} msg: {msg}".format(host=ds.get('ip'),
                                                                                                                       status=ds.get('status'),
                                                                                                                       msg=ds.get('msg')))
Ejemplo n.º 19
0
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':[]}) 
Ejemplo n.º 20
0
     order.filedownload_audit_order.dest_server = json.loads(order.filedownload_audit_order.dest_server)
     return render(request,'filemanage/file_download_run.html',{"user":request.user,"order":order,
                                                              "ans_uuid":uuid.uuid4()}) 
 elif request.method == "POST":
     try:
         order = Order_System.objects.get(id=id,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'))
         dataList = []
         if len(sList) > 0 and order.order_status == 8:
             ANS = ANSRunner(resource)                
             module_args = "paths={dest}".format( dest=order.filedownload_audit_order.dest_path)
             ANS.run_model(host_list=sList,module_name='find',module_args=module_args)
             filesData = json.loads(ANS.get_model_result())
             for k,v in filesData.get('success').items():
                 for ds in v.get('files'):
                     data = {}
                     data["id"] = order.id
                     data['host'] = k
                     data['path'] = ds.get('path')
                     data['size'] = ds.get('size')/1024/1024
                     data['islnk'] = ds.get('islnk')
                     dataList.append(data)
             return JsonResponse({'msg':"操作成功","code":200,'data':dataList})
         else:
             return JsonResponse({'msg':"操作失败,未选择主机或者该分组没有成员","code":500,'data':[]})
     else:
         return JsonResponse({'msg':"操作失败,您没有权限进行操作","code":500,'data':[]})
 else:
Ejemplo n.º 21
0
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)
Ejemplo n.º 22
0
def cron_mod(request, cid):
    try:
        cron = Cron_Config.objects.select_related().get(id=cid)
    except:
        return render_to_response('cron/cron_modf.html', {
            "user": request.user,
            "errorInfo": "任务不存在,可能已经被删除."
        },
                                  context_instance=RequestContext(request))
    if request.method == "GET":
        return render_to_response('cron/cron_modf.html', {
            "user": request.user,
            "cron": cron
        },
                                  context_instance=RequestContext(request))
    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_to_response('cron/cron_modf.html', {
                "user": request.user,
                "errorInfo": "更新失败,错误信息:" + str(e)
            },
                                      context_instance=RequestContext(request))
        try:
            sList = [cron.cron_server.ip]
            resource = [{
                "hostname": 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)
                    result = ANS.handle_model_data(ANS.get_model_result(),
                                                   'copy', file_args)
                if result[0].get('status') != 'failed':
                    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)
        except Exception, e:
            return render_to_response('cron/cron_mod.html', {
                "user": request.user,
                "errorInfo": "错误信息:" + str(e)
            },
                                      context_instance=RequestContext(request))