def tomcat_rollback_task(request, pid): """ 创建ansible rollback task """ path1, path2, path3 = u'项目变更回退', u'tomcat框架项目', '程序回退' task_record = AnsibleTaskRecord.objects.get(id=pid) username = request.session.get('name') app_type_dict = get_app_type(username) if task_record.app_type not in app_type_dict.keys(): return HttpResponseRedirect(reverse('auth_forbidden')) if request.method == 'POST': try: switch = request.POST.get('switch') backup_file = request.POST.get('backup_file') group = task_record.ansible_group host_list = task_record.hosts_list.split(',') bk_file = '{0}/{1}'.format(task_record.backup_path, backup_file) app_name = task_record.app_name deploy_path = task_record.app_path rollback_path = '{0}/{1}'.format(task_record.app_path, task_record.release_path) update_file_type = task_record.update_file_type if update_file_type == 'pack': task_id, log_file, job_name, inventory = tomcat_rollback_pack(group=group, app_name=app_name, deploy_path=deploy_path, bk_file=bk_file, rollback_path=rollback_path, switch=switch, host_list=host_list) else: task_id, log_file, job_name, inventory = tomcat_rollback_config(group=group, app_name=app_name, deploy_path=deploy_path, bk_file=bk_file, rollback_path=rollback_path, switch=switch, host_list=host_list) AnsibleTaskRecord.objects.filter(id=pid).update(state="rollback") return JsonResponse({"task_id": task_id, 'log_file': log_file, 'job_name':job_name, 'inventory': inventory, 'code': 200}) except Exception as err: logg = logging.getLogger('opsadmin') logg.error('%s' % traceback.format_exc()) return JsonResponse({'error': err, 'code': 500}) return render(request, 'tasks/rollback/tomcat_rollback_task.html',locals())
def ansible_task_create(request): path1, path2 = u'项目任务流程', u'创建发布任务' try: app_list = [] app_type_dict = get_app_type(request.session.get('name')) apps = list( AppInfo.objects.values('app_id', 'app_name', 'app_type', 'app_alias', 'frameworks', 'app_status')) for app_dict in apps: if app_dict['app_status'] == 0 or app_dict[ 'app_type'] == 'private_cloud': pass else: tag_dict = AnsibleTaskRecord.objects.filter( app_name=app_dict['app_name']).values( 'release_tag').order_by("-release_tag").first() if not tag_dict: app_dict['release_tag'] = "" else: app_dict['release_tag'] = tag_dict['release_tag'] for apps_type_key in app_type_dict.keys(): if apps_type_key in app_dict.values(): app_list.append(app_dict) except Exception as error: apps = [{ 'app_id': 0, 'app_name': error, 'app_alias': error, 'frameworks': error }] return render(request, 'tasks/release/create_task_list.html', { 'path1': path1, 'path2': path2, 'app_list': app_list })
def play_rollback_task(request, pid): path1, path2, path3 = u'项目变更回退', u'play框架项目', '程序回退' task_record = AnsibleTaskRecord.objects.get(id=pid) username = request.session.get('name') app_type_dict = get_app_type(username) if task_record.app_type not in app_type_dict.keys(): return HttpResponseRedirect(reverse('auth_forbidden')) if request.method == 'POST': try: content = [] switch = request.POST.get('switch') backup_file = request.POST.get('backup_file') update_file_type = task_record.update_file_type app_data = AppInfo.objects.filter( app_name=task_record.app_name).first() app_port = app_data.run_port """创建调用变量""" if update_file_type == 'pack': task_id, log_file, job_name, inventory = play_rollback_pack( group=task_record.ansible_group, app_name=task_record.app_name, release_path=task_record.release_path, backup_path=task_record.backup_path, backup_file=backup_file, app_path=task_record.app_path, app_port=app_port, switch=switch, host_list=task_record.hosts_list.split(',')) else: task_id, log_file, job_name, inventory = play_rollback_config( group=task_record.ansible_group, app_name=task_record.app_name, release_path=task_record.release_path, backup_path=task_record.backup_path, backup_file=backup_file, app_path=task_record.app_path, app_port=app_port, switch=switch, host_list=task_record.hosts_list.split(',')) """创建回退task""" AnsibleTaskRecord.objects.filter(id=pid).update(state="rollback") return JsonResponse({ "task_id": task_id, 'log_file': log_file, 'job_name': job_name, 'inventory': inventory, 'code': 200 }) except Exception as err: logg = logging.getLogger('opsadmin') logg.error('%s' % traceback.format_exc()) return JsonResponse({'error': err, 'code': 500}) return render(request, 'tasks/rollback/play_rollback_task.html', locals())
def create_workflow_task(request): path1, path2 = u'项目任务流程', u'创建发布任务' username = request.session.get('name') app_type_dict = get_app_type(username) dic = {} if request.method == 'POST': try: task_name = request.POST.get('task_name') dic['temp_name'] = task_name result = TaskTemplate.objects.filter(temp_name=task_name) if not result: TaskTemplate.objects.create(**dic) msg = '任务创建成功,是否立即编排此任务?' return HttpResponse(msg) else: msg = '任务已存在' except Exception as e: return HttpResponse(str(e)) return render(request, 'tasks/jobs/create_workflow_task.html', locals())
def pcloud_restart_process(request): path1, path2, path3 = u'项目平台更新', u'私有云项目', '批量重启服务' username = request.session.get('name') app_type_dict = get_app_type(username) job_name = "private_cloud-restart-{0}".format( datetime.datetime.now().strftime("%Y%m%d%H%M")) app_type = ansible_role_config.PCLOUD_APP_TYPE[0] """"auth check""" if len( set(app_type_dict.keys()).intersection( set(ansible_role_config.PCLOUD_APP_TYPE))) == 0: if username == "管理员": pass else: return render(request, 'error/403.html') if request.method == 'POST': try: content = {} root_list = [] #define root program list service_list = [] #define service program list interface_list = [] """ansible task path""" curr_date = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S") project_path = ansible_role_config.ANSIBLE_PLAYBOOK_REPOSITORY_PATH[ 'pcloud_path'] group = request.POST.get('ab_group', '') root_path = request.POST.getlist('root_path') service_path = request.POST.getlist('service_path') interface_path = request.POST.getlist('interface_path') switch = request.POST.get('switch', '') exe_mode = request.POST.get('exe_mode', '') inventory = "{0}/inventory/{1}".format(project_path, group) playbook = "{0}/{1}.yml".format(project_path, group) vars_file = "{0}/group_vars/{1}.yml".format(project_path, group) """创建主机资源组""" task_role = ansible_role_config.ANSIBLE_PCLOUD_ROLES[ 'pcloud_restart_task'] create_ansible_hosts(inventory, playbook, group, task_role, "pcloud", exe_mode=exe_mode) """创建调用变量""" """delete old variables if exists, for safety""" if os.path.exists(vars_file): os.remove(vars_file) if root_path: for root in root_path: root_list.append(root.encode('utf8')) content['root_path'] = root_list if service_path: for service in service_path: service_list.append(service.encode('utf8')) content['service_path'] = service_list if interface_path: for interface in interface_path: interface_list.append(interface.encode('utf8')) content['interface_path'] = interface_list content['reload_do'] = switch """create variables""" create_ansible_variables(vars_file, content) task_id, log_file = pcloud_execute_ansible_job(playbook, inventory) return JsonResponse({ "task_id": task_id, 'log_file': log_file, 'job_name': job_name, 'inventory': inventory, 'code': 200 }) except Exception as err: logg = logging.getLogger('opsadmin') logg.error('%s' % traceback.format_exc()) return JsonResponse({'error': str(err), 'code': 500}) return render(request, 'pcloud/restart/restart_process.html', locals())
def pcloud_release_config(request): path1, path2, path3 = u'项目平台更新', u'私有云项目', '配置文件更新' username = request.session.get('name') app_type_dict = get_app_type(username) app_info_list = AppInfo.objects.filter(app_type="private_cloud").values( 'backup_path', 'work_path').distinct() job_name = "private_cloud-cfg-update-{0}".format( datetime.datetime.now().strftime("%Y%m%d%H%M")) app_type = ansible_role_config.PCLOUD_APP_TYPE[0] """"auth check""" if len( set(app_type_dict.keys()).intersection( set(ansible_role_config.PCLOUD_APP_TYPE))) == 0: if username == "管理员": pass else: return render(request, 'error/403.html') if request.method == 'POST': try: content = {} root_list = [] #define root program list service_list = [] #define service program list interface_list = [] """ansible task path""" project_path = ansible_role_config.ANSIBLE_PLAYBOOK_REPOSITORY_PATH[ 'pcloud_path'] curr_date = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S") group = request.POST.get('ab_group', '') root_path = request.POST.getlist('root_path') service_path = request.POST.getlist('service_path') interface_path = request.POST.getlist('interface_path') arg = request.POST.get('arg') switch = request.POST.get('switch') exe_mode = request.POST.get('exe_mode', '') """"define ansible task role file""" inventory = "{0}/inventory/{1}".format(project_path, group) playbook = "{0}/{1}.yml".format(project_path, group) vars_file = "{0}/group_vars/{1}.yml".format(project_path, group) """创建调用变量""" """delete old variables if exists, for safety""" if os.path.exists(vars_file): os.remove(vars_file) if arg == "add": #批量添加固定参数 fix_arg = request.POST.get('add_arg').split('\r') content['fix_arg'] = '"%s"' % ''.join(fix_arg) """创建主机资源组""" task_role = ansible_role_config.ANSIBLE_PCLOUD_ROLES[ 'pcloud_config_add_task'] create_ansible_hosts(inventory, playbook, group, task_role, "pcloud", exe_mode=exe_mode) elif arg == 'add_p': #批量添加变量参数 batch_arg = request.POST.get('add_p_arg').split('\r') content['batch_arg'] = '"%s"' % '\n'.join(batch_arg) """创建主机资源组""" task_role = ansible_role_config.ANSIBLE_PCLOUD_ROLES[ 'pcloud_config_add_task'] create_ansible_hosts(inventory, playbook, group, task_role, "pcloud", exe_mode=exe_mode) elif arg == 'del': express_del = request.POST.get('del_arg') content['express_del'] = express_del """创建主机资源组""" task_role = ansible_role_config.ANSIBLE_PCLOUD_ROLES[ 'pcloud_config_delete_task'] create_ansible_hosts(inventory, playbook, group, task_role, "pcloud", exe_mode=exe_mode) else: express_mod = request.POST.get('mod_arg') content['express_mod'] = express_mod """创建主机资源组""" task_role = ansible_role_config.ANSIBLE_PCLOUD_ROLES[ 'pcloud_config_mod_task'] create_ansible_hosts(inventory, playbook, group, task_role, "pcloud", exe_mode=exe_mode) if root_path: for root in root_path: root_list.append(root.encode('utf8')) content['root_path'] = root_list if service_path: for service in service_path: service_list.append(service.encode('utf8')) content['service_path'] = service_list if interface_path: for interface in interface_path: interface_list.append(interface.encode('utf8')) content['interface_path'] = interface_list for app in app_info_list: content['bk_path'] = app['backup_path'] content['curr_dt'] = curr_date content['root_path'] = root_list content['service_path'] = service_list content['config_file'] = '{0}/{1}'.format( ansible_role_config.TOMCAT_FRAMEWORK['config'], ansible_role_config.TOMCAT_CONFIG_TYPE['app']) content['reload_cfg'] = switch """create variables file""" create_ansible_variables(vars_file, content) """"execute ansible job""" task_id, log_file = pcloud_execute_ansible_job(playbook, inventory) return JsonResponse({ "task_id": task_id, 'log_file': log_file, 'job_name': job_name, 'inventory': inventory, 'code': 200 }) except Exception as err: logg = logging.getLogger('opsadmin') logg.error('%s' % traceback.format_exc()) return JsonResponse({'error': str(err), 'code': 500}) return render(request, 'pcloud/update/release_config.html', locals())
def pcloud_release_code(request): path1, path2, path3 = u'项目平台更新', u'私有云项目', '全量发布源码' username = request.session.get('name') app_type_dict = get_app_type(username) app_info_list = AppInfo.objects.filter(app_type="private_cloud").values( 'backup_path', 'work_path').distinct() job_name = "private_cloud-full-release-{0}".format( datetime.datetime.now().strftime("%Y%m%d%H%M")) app_type = ansible_role_config.PCLOUD_APP_TYPE[0] """"auth check""" if len( set(app_type_dict.keys()).intersection( set(ansible_role_config.PCLOUD_APP_TYPE))) == 0: if username == "管理员": pass else: return render(request, 'error/403.html') if request.method == 'POST': try: content = {} root_list = [] #define root program list service_list = [] #define service program list interface_list = [] """ansible task path""" curr_date = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S") project_path = ansible_role_config.ANSIBLE_PLAYBOOK_REPOSITORY_PATH[ 'pcloud_path'] group = request.POST.get('ab_group', '') root_path = request.POST.getlist('root_path') service_path = request.POST.getlist('service_path') interface_path = request.POST.getlist('interface_path') pcloud = request.POST.getlist('pcloud') action = request.POST.get('action') arg = request.POST.get('arg', '') exe_mode = request.POST.get('exe_mode', '') switch = request.POST.get('switch') inventory = "{0}/inventory/{1}".format(project_path, group) playbook = "{0}/{1}.yml".format(project_path, group) vars_file = "{0}/group_vars/{1}.yml".format(project_path, group) content['curr_dt'] = curr_date content['reload_app'] = switch """创建调用变量""" """delete old variables if exists, for safety""" if os.path.exists(vars_file): os.remove(vars_file) if root_path: for root in root_path: root_list.append(root.encode('utf8')) content['root_path'] = root_list if service_path: for service in service_path: service_list.append(service.encode('utf8')) content['service_path'] = service_list if interface_path: for interface in interface_path: interface_list.append(interface.encode('utf8')) content['interface_path'] = interface_list for app in app_info_list: content['code_workspace'] = app['work_path'] content['bk_path'] = app['backup_path'] if arg == "svn": task_role = ansible_role_config.ANSIBLE_PCLOUD_ROLES[ 'pcloud_svn_release_task'] """创建主机资源组""" create_ansible_hosts(inventory, playbook, group, task_role, "pcloud", exe_mode=exe_mode) content['svn_path'] = pcloud[0] if pcloud[1]: content['svn_service'] = pcloud[1] if pcloud[2]: content['svn_root'] = pcloud[2] if pcloud[3]: content['svn_interface'] = pcloud[3] """create variables""" create_ansible_variables(vars_file, content) elif arg == 'trans': task_role = ansible_role_config.ANSIBLE_PCLOUD_ROLES[ 'pcloud_trans_release_task'] """创建主机资源组""" create_ansible_hosts(inventory, playbook, group, task_role, "pcloud", exe_mode=exe_mode) content['trans_path'] = "{0}/{1}".format(MEDIA_ROOT, app_type) """create variables""" create_ansible_variables(vars_file, content) task_id, log_file = pcloud_execute_ansible_job(playbook, inventory) return JsonResponse({ "task_id": task_id, 'log_file': log_file, 'job_name': job_name, 'inventory': inventory, 'code': 200 }) except Exception as err: logg = logging.getLogger('opsadmin') logg.error('%s' % traceback.format_exc()) return JsonResponse({'error': str(err), 'code': 500}) return render(request, 'pcloud/release/release_code.html', locals())
def pcloud_increment_release_code(request): path1, path2, path3 = u'项目平台更新', u'私有云项目', '增量更新程序' username = request.session.get('name') app_type_dict = get_app_type(username) app_info_list = AppInfo.objects.filter(app_type="private_cloud").values( 'backup_path', 'work_path').distinct() job_name = "private_cloud-jar-update-{0}".format( datetime.datetime.now().strftime("%Y%m%d%H%M")) app_type = ansible_role_config.PCLOUD_APP_TYPE[0] """"auth check""" if len( set(app_type_dict.keys()).intersection( set(ansible_role_config.PCLOUD_APP_TYPE))) == 0: if username == "管理员": pass else: return render(request, 'error/403.html') if request.method == 'POST': try: content = {} root_list = [] #define root program list service_list = [] #define service program list interface_list = [] """ansible task path""" curr_date = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S") project_path = ansible_role_config.ANSIBLE_PLAYBOOK_REPOSITORY_PATH[ 'pcloud_path'] group = request.POST.get('ab_group', '') root_path = request.POST.getlist('root_path') service_path = request.POST.getlist('service_path') interface_path = request.POST.getlist('interface_path') dst_path = request.POST.get('dst_path') mode = request.POST.get('mode') exe_mode = request.POST.get('exe_mode', '') switch = request.POST.get('switch') """"define ansible task role file""" inventory = "{0}/inventory/{1}".format(project_path, group) playbook = "{0}/{1}.yml".format(project_path, group) vars_file = "{0}/group_vars/{1}.yml".format(project_path, group) """创建调用变量dict""" content['dst_path'] = ''.join(dst_path.split()) content['curr_dt'] = curr_date content['reload_jar'] = switch """delete old variables if exists, for safety""" if os.path.exists(vars_file): os.remove(vars_file) for app in app_info_list: content['bk_path'] = app['backup_path'] content['code_workspace'] = app['work_path'] if root_path: for root in root_path: root_list.append(root.encode('utf8')) content['root_path'] = root_list if service_path: for service in service_path: service_list.append(service.encode('utf8')) content['service_path'] = service_list if interface_path: for interface in interface_path: interface_list.append(interface.encode('utf8')) content['interface_path'] = interface_list """"define update scene""" if mode == 'add': content['local_path'] = "{0}/{1}".format(MEDIA_ROOT, app_type) content['add_file'] = '"None"' """创建主机资源组""" task_role = ansible_role_config.ANSIBLE_PCLOUD_ROLES[ 'pcloud_jar_add_task'] create_ansible_hosts(inventory, playbook, group, task_role, "pcloud", exe_mode=exe_mode) elif mode == 'del': del_file = request.POST.get('del_file').split('\r') content['del_file'] = '"%s"' % ''.join(del_file) task_role = ansible_role_config.ANSIBLE_PCLOUD_ROLES[ 'pcloud_jar_delete_task'] create_ansible_hosts(inventory, playbook, group, task_role, exe_mode=exe_mode) elif mode == 'iter': old_file = request.POST.get('old_file').strip() new_file = request.POST.get('new_file').strip() content['old_file'] = old_file content['new_file'] = new_file content['local_path'] = "{0}/{1}".format(MEDIA_ROOT, app_type) task_role = ansible_role_config.ANSIBLE_PCLOUD_ROLES[ 'pcloud_jar_add_task'] create_ansible_hosts(inventory, playbook, group, task_role, "pcloud", exe_mode=exe_mode) """create variables file""" create_ansible_variables(vars_file, content) """"execute ansible job""" task_id, log_file = pcloud_execute_ansible_job(playbook, inventory) return JsonResponse({ "task_id": task_id, 'log_file': log_file, 'job_name': job_name, 'inventory': inventory, 'code': 200 }) except Exception as err: return JsonResponse({'error': str(err), 'code': 500}) return render(request, 'pcloud/update/increment_release_code.html', locals())
def create_process_control_task(request, pid): path1, path2 = u'项目任务流程', u'应用重启' apps = AppInfo.objects.get(app_id=pid) username = request.session.get('name') app_type_dict = get_app_type(username) if apps.app_type in app_type_dict.keys(): app_name = apps.app_name app_id = apps.app_id app_type = apps.app_type group = AnsibleGroup.objects.get(app_type=app_type).group hosts = AppInfo.objects.get(app_name=app_name).app_ip.all() """检查项目当前状态""" if apps.app_status == 3: return HttpResponseRedirect(reverse('index')) else: return HttpResponseRedirect(reverse('auth_forbidden')) if request.method == 'POST': try: content = {} """ansible task path""" host_select = request.POST.getlist('host_id_select') switch = request.POST.get('switch', '') exe_mode = request.POST.get('exe_mode', '') job_name = "{0}-{1}".format( app_name, datetime.datetime.now().strftime("%Y%m%d%H%M")) if apps.frameworks == "play": inventory, playbook, vars_file = generate_ansible_roles( 'play_path', group) task_role = ansible_role_config.ANSIBLE_PLAY_ROLES[ 'play_restart_task'] content['play_path'] = apps.main_path content['play_name'] = app_name content['play_port'] = apps.run_port else: inventory, playbook, vars_file = generate_ansible_roles( 'tomcat_path', group) task_role = ansible_role_config.ANSIBLE_TOMCAT_ROLES[ 'tomcat_restart_task'] content['deploy_path'] = apps.main_path content['app_name'] = app_name """创建主机资源组""" create_ansible_hosts(inventory, playbook, group, task_role, host_select, exe_mode=exe_mode) content['reload_control'] = switch """create variables""" create_ansible_variables(vars_file, content) task_id, log_file = execute_ansible_task(playbook, inventory) AppInfo.objects.filter(app_id=pid).update(app_status=3) return JsonResponse({ "task_id": task_id, 'log_file': log_file, 'app_type': app_type, 'job_name': job_name, 'inventory': inventory, 'pid': pid, 'code': 200 }) except Exception as err: logg = logging.getLogger('opsadmin') logg.error('%s' % traceback.format_exc()) return JsonResponse({'error': str(err), 'code': 500}) return render(request, 'tasks/restart/create_restart_task.html', locals())
def create_task_play(request, pid): path1, path2 = u'项目任务流程', u'创建发布任务' apps = AppInfo.objects.get(app_id=pid) username = request.session.get('name') app_type_dict = get_app_type(username) if apps.app_type in app_type_dict.keys(): app_name = apps.app_name app_type = apps.app_type group = AnsibleGroup.objects.get(app_type=app_type).group """检查项目当前状态""" if apps.app_status == 3: return HttpResponseRedirect(reverse('index')) #task_state = AnsibleTaskList.objects.filter(app_name=app_name, task_status=0) hosts = AppInfo.objects.get(app_name=app_name).app_ip.all() app_ip_count = len(hosts) task_count = len( AnsibleTaskList.objects.filter(app_name=app_name, task_status=0)) if task_count < app_ip_count: task_state = False else: task_state = True else: return HttpResponseRedirect(reverse('auth_forbidden')) if request.method == 'POST': host_select = request.POST.getlist('host_id_select') arg = request.POST.get('arg') exe_mode = request.POST.get('exe_mode', '') task_detail = request.POST.get('task_detail', '') app_ver = request.POST.get('app_ver', '') switch = request.POST.get('switch') task_name = "{0}-{1}".format( app_name, datetime.datetime.now().strftime("%Y%m%d%H%M")) inventory, playbook, vars_file = generate_ansible_roles( 'play_path', group) create_date = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S") release_tag = "{0}_{1}".format(app_ver, create_date) action = request.POST.get('action') svn_path = request.POST.get('svn_path') """创建发布任务,不立即执行""" if action == "save": save_task = { 'app_id': pid, 'app_name': app_name, 'app_type': app_type, 'app_version': app_ver, 'creator': username, 'change_logs': task_detail, 'hosts_list': ','.join(host_select), 'release_type': arg, 'svn_path': svn_path, 'ansible_group': group, 'reload_switch': switch, 'task_run_type': exe_mode, 'create_time': create_date, 'get_code': request.POST.get('mode_play') } try: AnsibleTaskList.objects.create(**save_task) return JsonResponse({'code': 200}) except Exception as err: logg = logging.getLogger('opsadmin') logg.error('%s' % traceback.format_exc()) return JsonResponse({'error': str(err), 'code': 500}) else: """创建发布任务,立即执行""" try: """delete old variables if exists, for safety""" if os.path.exists(vars_file): os.remove(vars_file) task_create = PlayCreateTask(app_type, app_name, group, inventory, playbook, vars_file, exe_mode, host_select, create_date) if arg == "full": mode = request.POST.get('mode_play') if mode == "svn": task_create.play_full_release_task(mode, svn_path=svn_path, switch=switch, username=username, app_ver=app_ver) elif mode == "trans": trans_path = "{0}/{1}".format(MEDIA_ROOT, app_name) task_create.play_full_release_task( mode, trans_path=trans_path, switch=switch, username=username, app_ver=app_ver) elif arg == "diff": mode = request.POST.get('mode_play_jar') if mode == "add": lib = request.POST.get('lib_type') plugins = request.POST.get('plugins_type') task_create.play_increment_update_task( mode=mode, lib=lib, plugins=plugins, switch=switch, username=username, app_ver=app_ver) elif mode == "iter": old_file = request.POST.get('old_file', '') new_file = request.POST.get('new_file', '') dst_path = request.POST.get('dst_path', '') task_create.play_increment_update_task( mode=mode, dst_path=dst_path, old_file=old_file, new_file=new_file, switch=switch, username=username, app_ver=app_ver) """执行发布任务""" data = execute_release_task(playbook, inventory, task_name, app_type, task_detail, pid, release_tag) AppInfo.objects.filter(app_id=pid).update(app_status=3) return JsonResponse(data, safe=False) except Exception as err: logg = logging.getLogger('opsadmin') logg.error('%s' % traceback.format_exc()) return JsonResponse({'error': str(err), 'code': 500}) return render( request, 'tasks/release/task_create_play.html', { 'path1': path1, 'path2': path2, 'app_type': app_type, 'app_name': app_name, 'app_id': apps.app_id, 'task_state': task_state, 'hosts': hosts })
def play_release_config(request): path1, path2, path3 = u'项目平台更新', u'play框架项目', '增量发布配置' username = request.session.get('name') file_path = MEDIA_ROOT play_config = ansible_role_config.PLAY_CONFIG_FILE app_type_dict = get_app_type(username) play_app_type = ['pay'] hosts = AppInfo.objects.get(app_type=play_app_type[0]).app_ip.all() """"auth check""" if len(set(app_type_dict.keys()).intersection(set(play_app_type))) == 0: if username == "管理员": pass else: return render(request, 'error/403.html') if request.method == 'POST': try: curr_dt = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S") project_path = ansible_role_config.ANSIBLE_PLAYBOOK_REPOSITORY_PATH[ 'play_path'] group = request.POST.get('ab_group', '') host_select = request.POST.getlist('host_id_select') app_name = request.POST.get('app_name') cfg = request.POST.get('config') switch = request.POST.get('switch') app_type = request.POST.get('app_type', '') inventory = "{0}/inventory/{1}".format(project_path, group) playbook = "{0}/{1}.yml".format(project_path, group) job_name = "{0}-cfg-update-{1}".format( app_name, datetime.datetime.now().strftime("%Y%m%d%H%M")) task_role = ansible_role_config.ANSIBLE_PLAY_ROLES[ 'play_config_release_task'] """创建主机资源组""" create_ansible_hosts(inventory, playbook, group, task_role, host_select) """创建调用变量""" vars_file = "{0}/group_vars/{1}.yml".format(project_path, group) """delete old variables if exists, for safety""" if os.path.exists(vars_file): os.remove(vars_file) app_name, app_port, app_path, app_bk, app_work = get_app_detail( app_type, app_name) data_dict = { 'vars_file': vars_file, 'app_name': app_name, 'app_port': app_port, 'app_path': app_path, 'app_bk': app_bk, 'config': cfg, 'reload_control': switch, 'curr_dt': curr_dt } create_variables = CreatePlayVariables(data_dict, '') create_variables.play_config_update_varibales() """创建发布task""" data = execute_release_task(playbook, inventory, job_name, app_type, "", "", "") AnsibleTaskRecord.objects.create( app_name=app_name, app_type=app_type, ansible_group=group, app_path=app_path, release_path='{0}/{1}'.format( app_path, ansible_role_config.PLAY_FRAMEWORK['config']), update_file_type='config', ansible_user=username, backup_path=app_bk, backup_file="conf_full_{0}".format(curr_dt), state="release", create_time=curr_dt) return JsonResponse(data) except Exception as err: logg = logging.getLogger('opsadmin') logg.error('%s' % traceback.format_exc()) return JsonResponse({'error': str(err), 'code': 500}) return render(request, 'tasks/release/config/release_config.html', locals())
def pcloud_rollback_config(request): """ 创建ansible job """ path1, path2, path3 = u'项目变更回退', u'私有云项目', '配置文件回退' #ansible_group = AnsibleGroup.objects.filter(app_type="private_cloud", group_type="rollback") role = ansible_role_config.ANSIBLE_PCLOUD_ROLES[ 'pcloud_rollback_config_task'] project_path = ansible_role_config.ANSIBLE_PLAYBOOK_REPOSITORY_PATH[ 'pcloud_path'] username = request.session.get('name') app_type_dict = get_app_type(username) """"auth check""" if len( set(app_type_dict.keys()).intersection( set(ansible_role_config.PCLOUD_APP_TYPE))) == 0: if username == "管理员": pass else: return render(request, 'error/403.html') job_name = "private_cloud-cfg-rollback-{0}".format( datetime.datetime.now().strftime("%Y%m%d%H%M")) if request.method == 'POST': try: content = {} bk_config_list = [] group = request.POST.get('ab_group', '') bk_config = request.POST.getlist('bk_config') rollback_path = request.POST.get('rollback_path') switch = request.POST.get('switch') inventory = "{0}/inventory/{1}".format(project_path, group) playbook = "{0}/{1}.yml".format(project_path, group) """create host""" create_ansible_hosts(inventory, playbook, group, role, 'pcloud') vars_file = "{0}/group_vars/{1}.yml".format(project_path, group) """创建调用变量""" """delete old variables if exists, for safety""" if os.path.exists(vars_file): os.remove(vars_file) if bk_config: for bk_cfg in bk_config: bk_config_list.append(bk_cfg.encode('utf8')) content['bk_cfg_file'] = bk_config_list content['rollback_cfg_path'] = rollback_path.strip() content['reload_rb_cfg'] = switch create_ansible_variables(vars_file, content) """创建发布task""" task_id, log_file = pcloud_execute_ansible_job(playbook, inventory) return JsonResponse({ "task_id": task_id, 'log_file': log_file, 'job_name': job_name, 'inventory': inventory, 'code': 200 }) except Exception as err: logg = logging.getLogger('opsadmin') logg.error('%s' % traceback.format_exc()) return JsonResponse({'error': str(err), 'code': 500}) return render(request, 'pcloud/rollback/rollback_config.html', locals())