def play_config_update_varibales(self): """ Create variables when updating the configuration file """ conf_path = "{0}/{1}/{2}".format( MEDIA_ROOT, self.data['app_name'], ansible_role_config.PLAY_FRAMEWORK['config']) play_config = "{0}/{1}".format( self.data['app_path'], ansible_role_config.PLAY_FRAMEWORK['config']) self.content['play_bk_dir'] = self.data['app_bk'] self.content['play_config'] = play_config if self.data['config'] == ansible_role_config.PLAY_CONFIG_TYPE['app']: self.content['app_config'] = '{0}/{1}'.format( conf_path, self.data['config']) elif self.data['config'] == ansible_role_config.PLAY_CONFIG_TYPE[ 'api']: self.content['api_config'] = "{0}/{1}".format( conf_path, self.data['config']) elif self.data['config'] == ansible_role_config.PLAY_CONFIG_TYPE[ 'reload']: self.content['reloadable_config'] = "{0}/{1}".format( conf_path, self.data['config']) """create variables""" create_ansible_variables(self.data['vars_file'], self.content)
def play_rollback_config(**kwargs): try: content = {} project_path = ansible_role_config.ANSIBLE_PLAYBOOK_REPOSITORY_PATH[ 'play_path'] role = ansible_role_config.ANSIBLE_PLAY_ROLES['play_rollback_task'] inventory = "{0}/inventory/{1}".format(project_path, kwargs['group']) playbook = "{0}/{1}.yml".format(project_path, kwargs['group']) vars_file = "{0}/group_vars/{1}.yml".format(project_path, kwargs['group']) job_name = "{0}-config-rollback-{1}".format( kwargs['app_name'], datetime.datetime.now().strftime("%Y%m%d%H%M")) """create host""" create_ansible_hosts(inventory, playbook, kwargs['group'], role, kwargs['host_list']) """创建调用变量""" if os.path.exists(vars_file): os.remove(vars_file) content['play_config'] = kwargs['release_path'] content['play_bk_config'] = '{0}/{1}'.format(kwargs['backup_path'], kwargs['backup_file']) content['reload_control'] = kwargs['switch'] content['play_name'] = kwargs['app_name'] content['play_port'] = kwargs['app_port'] content['play_path'] = kwargs['app_path'] content['reload_control'] = kwargs['switch'] create_ansible_variables(vars_file, content) """创建回退task""" task_id, log_file = execute_ansible_task(playbook, inventory) return task_id, log_file, job_name, inventory except: logg = logging.getLogger('opsadmin') logg.error('%s' % traceback.format_exc())
def tomcat_config_update_varibales(self, mode): """ Create variables when updating the configuration file """ self.mode = mode self.content['config_file'] = self.data['config_file'] if self.mode == "add": self.content['add_config'] = self.data['add_config'] elif self.mode == "mod": self.content['mod_config'] = self.data['mod_config'] """create variables""" create_ansible_variables(self.data['vars_file'], self.content)
def tomcat_full_release_variables(self, arg): """ Creating variables for the full release of the Java program, divided into SVN updates and manually uploaded updates """ self.arg = arg if self.arg == "svn": self.content['svn_path'] = self.data['svn_path'] elif self.arg == 'trans': self.content['trans_path'] = self.data['trans_path'] """create variables""" create_ansible_variables(self.data['vars_file'], self.content)
def play_full_release_variables(self): """ Creating variables for the full release of the Java program, divided into SVN updates and manually uploaded updates """ self.content['play_workspace'] = self.data['app_work'] self.content['play_bk_dir'] = self.data['app_bk'] self.content['play_lib_release'] = self.data['lib_path'] self.content['play_plugins_release'] = self.data['plugins_path'] if self.arg == "svn": self.content['svn_path'] = self.data['svn_path'] elif self.arg == 'trans': self.content['trans_path'] = self.data['trans_path'] """create variables""" create_ansible_variables(self.data['vars_file'], self.content)
def tomcat_increment_update_variables(self, mode): """ Create variables when updating the java packages """ self.mode = mode self.content['dst_path'] = self.data['dst_path'] if self.mode == "add": self.content['local_path'] = self.data['local_path'] self.content['add_file'] = self.data['add_file'] elif self.mode == "del": self.content['del_file'] = self.data['del_file'] elif self.mode == "iter": self.content['old_file'] = self.data['old_file'] self.content['new_file'] = self.data['new_file'] self.content['local_path'] = self.data['local_path'] """create variables""" create_ansible_variables(self.data['vars_file'], self.content)
def play_increment_update_variables(self, mode): """ Create variables when updating the java packages """ self.content['play_workspace'] = self.data['app_work'] self.content['play_bk_dir'] = self.data['app_bk'] self.content['jar_path'] = self.data['trans_path'] if mode == "add": if self.data['lib'] and self.data['plugins'] is None: self.content['lib_type'] = self.data['lib'] elif self.data['lib'] is None and self.data['plugins']: self.content['plugins_type'] = self.data['plugins'] elif self.data['lib'] and self.data['plugins']: self.content['lib_type'] = self.data['lib'] self.content['plugins_type'] = self.data['plugins'] elif mode == "iter": self.content['jar_path'] = self.data['trans_path'] self.content['dst_path'] = self.data['dst_path'] self.content['old_file'] = self.data['old_file'] self.content['new_file'] = self.data['new_file'] """create variables""" create_ansible_variables(self.data['vars_file'], self.content)
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 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())