def tomcatdev_push(file, pclist, puthdir): ipdict = {} log = [] confname, path = process().tomcat_conf(id=file) for i in pclist: item = models.device_config.objects.get(id=i) ipdict[item.ipaddress] = item.password obj = models.task(task_name='tomcat推送', config_name=confname, task_Operated=','.join(ipdict.keys()), task_result=3) obj.save() obj_id = obj.id if len(puthdir.strip()) == 0: module_args = 'src=%s dest=/tmp/server.xml' % path else: module_args = 'src=%s dest=/%s/server.xml' % ( path, puthdir.strip().strip('/')) try: for k, y in ipdict.items(): date = ansible_api.MyRunner().cmdrun( pattern=k, module_name='copy', module_args=module_args)['contacted'] log.append(k + ':') log.append(str(date[k]['changed']) + '\n') models.task.objects.filter(id=obj_id).update(task_result=1, task_log='\n'.join(log)) except Exception, e: models.task.objects.filter(id=obj_id).update( task_result=2, task_log='被控制机没有安装libselinux-python,或网络不可达!')
def nginxdev_push(file, pclist, puthdir): iplist = [] log = [] confname, path = process().nginx_conf(id=file) for i in pclist: item = models.device_config.objects.get(id=i) iplist.append(item.ipaddress) obj = models.task(task_name='nginx推送', config_name=confname, task_Operated=','.join(iplist), task_result=3) obj.save() obj_id = obj.id if len(puthdir.strip()) == 0: module_args = 'src=%s dest=/tmp/nginx.conf' % path else: module_args = 'src=%s dest=/%s/nginx.conf' % ( path, puthdir.strip().strip('/')) date = ansible_api.MyRunner().cmdrun(pattern=','.join(iplist), module_name='copy', module_args=module_args)['contacted'] task_result = 1 for i in iplist: try: log.append(i + ':') log.append(str(date[i]['changed']) + '\n') except Exception, e: log.append(str(date[i]['msg']) + '\n') task_result = 2 continue
def nginxgroup_push(group, file): log = [] item = models.group_config.objects.get(id=group) confname, path = process().nginx_conf(id=file) obj = models.task(task_name='nginx组推送', config_name=confname, task_Operated=item.group_name, task_result=3) obj.save() obj_id = obj.id if len(item.nginx_puth.strip()) == 0: module_args = 'src=%s dest=/tmp/nginx.conf' % path else: module_args = 'src=%s dest=/%s/nginx.conf' % ( path, item.nginx_puth.strip().strip('/')) try: for i in models.device_config.objects.filter(group=group): date = ansible_api.MyRunner().cmdrun( pattern=i.ipaddress, module_name='copy', module_args=module_args)['contacted'] log.append(i.ipaddress + ':') log.append(str(date[i.ipaddress]['changed']) + '\n') models.task.objects.filter(id=obj_id).update(task_result=1, task_log='\n'.join(log)) except Exception, e: models.task.objects.filter(id=obj_id).update( task_result=2, task_log='被控制机没有安装libselinux-python,或网络不可达!')
def pcmanage_post(request): try: if models.device_config.objects.filter( ipaddress=request.POST.get('ipAddress')): return (False, 'IP地址重复!请检查!') else: mark = ansible_api.MyRunner().deploy_key( server=request.POST.get('ipAddress'), username='******', password=request.POST.get('password')) if mark[0]: dev = models.device_config( description=request.POST.get('description'), ipaddress=request.POST.get('ipAddress'), memo=request.POST.get('Configuration'), ) dev.save() for i in request.POST.getlist('group'): grou = models.group_config.objects.get(group_name=i) dev.group.add(grou) dev.save() return (True, '添加成功') else: return mark except Exception, e: print e
def playbook(p_id, p_name, inventory=None): basedir = models.Playbook.objects.get(id=p_id) path = os.path.join(basedir.basedir.split('.')[0], p_name) obj = models.task(task_name='playbook执行', config_name='不涉及', task_Operated='不涉及', task_result=3) obj.save() obj_id = obj.id try: if inventory: date = ansible_api.MyRunner().roles_execute( play=path, inventory=os.path.join( basedir.basedir.split('.')[0], 'inventory')) else: date = ansible_api.MyRunner().roles_execute(play=path) models.task.objects.filter(id=obj_id).update(task_result=1, task_log=date) except Exception, e: models.task.objects.filter(id=obj_id).update( task_result=2, task_log='被控制机没有安装libselinux-python,或网络不可达!')
def tinstallgroup_push(group_id, id): log = [] item = models.group_config.objects.get(id=group_id) confname, path = process().tomcatinstall_conf(id=id) obj = models.task(task_name='tomcat组安装', config_name=confname, task_Operated=item.group_name, task_result=3) obj.save() obj_id = obj.id try: for i in models.device_config.objects.filter(group=group_id): date = ansible_api.MyRunner().PlayBook_execute( play=path, params='{"host": "%s"}' % i.ipaddress) log.append(i.ipaddress + ':') log.append(str(date[i.ipaddress]['failures']) + '\n') models.task.objects.filter(id=obj_id).update(task_result=1, task_log='\n'.join(log)) except Exception, e: models.task.objects.filter(id=obj_id).update( task_result=2, task_log='被控制机没有安装libselinux-python,或网络不可达!')
def tinstall_push(pclist, id): ipdict = {} log = [] confname, path = process().tomcatinstall_conf(id=id) for i in pclist: item = models.device_config.objects.get(id=i) ipdict[item.ipaddress] = item.password obj = models.task(task_name='tomcat安装', config_name=confname, task_Operated=','.join(ipdict.keys()), task_result=3) obj.save() obj_id = obj.id try: for k, y in ipdict.items(): date = ansible_api.MyRunner().PlayBook_execute( play=path, params='{"host": "%s"}' % k) log.append(k + ':') log.append(str(date[k]['failures']) + '\n') models.task.objects.filter(id=obj_id).update(task_result=1, task_log='\n'.join(log)) except Exception, e: models.task.objects.filter(id=obj_id).update( task_result=2, task_log='被控制机没有安装libselinux-python,或网络不可达!')
data = { 'status': 1, 'msg': '请求失败', 'data': '执行命令中存在shutdown,init 0,init 6,reboot等敏感命令或执行命令未填写,不允许执行!!' } return data else: pass if request.POST.get('mark') == '1': iplist = [] for i in request.POST.getlist('pclist'): item = models.device_config.objects.get(id=i) iplist.append(item.ipaddress) date = ansible_api.MyRunner().cmdrun( pattern=item.ipaddress, module_args=request.POST.get('cmd'))['contacted'] log.append(item.ipaddress + ':') log.append(date[item.ipaddress]['stdout'] + '\n') data = {'status': 0, 'msg': '请求成功', 'data': '\n'.join(log)} return data elif request.POST.get('mark') == '2': for i in models.device_config.objects.filter( group=request.POST.get('group')): date = ansible_api.MyRunner().cmdrun( pattern=i.ipaddress, module_args=request.POST.get('gcmd'))['contacted'] log.append(i.ipaddress + ':') log.append(date[i.ipaddress]['stdout'] + '\n') data = {'status': 0, 'msg': '请求成功', 'data': '\n'.join(log)} return data