def deploy_game_wizard(request, template_name='saltstack/deploy_game_wizard.html'): departments = Department.objects.all() war_servers = Assets.objects.filter(tag__name='War') db_servers = Databases.objects.all() domains = Domain.objects.all() zones = Zone.objects.all() web_servers = Assets.objects.filter(tag__name='Web') chat_servers = Assets.objects.filter(tag__name='Chat') path_object = Path.objects.filter(path_key__contains=u'部署路径') services = Services.objects.all() if request.method == 'POST': start_date = '2018-06-01' start_time = '09-00-00' sql_file = 'war.sql' upload_files = Upload.objects.all() config_files = [] for item in upload_files: config_files.append(item.doc_file.name) information = {} replacements = {} config_path = {} context = {} zone_object = Zone.objects.filter(id=request.POST.get('zones')) for item in zone_object: information['zone'] = item.name information['ZONE_NAME'] = item.content domain_object = Domain.objects.filter(id=request.POST.get('domain')) for item in domain_object: information['domain'] = item.name war_server_object = Assets.objects.filter( id=request.POST.get('war_servers')) for item in war_server_object: information['WAR_DATA_ADDR'] = item.networks.private_address information['REDIS_ADDR'] = item.networks.private_address information['WAR_DATA_HOST'] = item.host_name information['WAR_TAGS'] = [tag.name for tag in item.tag.all()] chat_server_object = Assets.objects.filter( id=request.POST.get('chat_servers')) for item in chat_server_object: information['CHAT_HOST_NAME'] = item.alias_name information['CHAT_PRIVATE_ADDR'] = item.networks.private_address information['CHAT_PUBLIC_ADDR'] = item.networks.public_address information['CHAT_TAGS'] = [tag.name for tag in item.tag.all()] db_server_object = Databases.objects.filter( id=request.POST.get('db_server')) for item in db_server_object: information['MYSQL_ADDR'] = item.ip_address information['MYSQL_USER'] = item.schema_user information['MYSQL_PASS'] = item.schema_pass information['SERVER_ID'] = request.POST.get('server_id') information['TX_SID'] = request.POST.get('tx_id') web_server_object = Assets.objects.filter( id=request.POST.get('web_servers')) for item in web_server_object: information['WEB_SERVER_HOST'] = item.host_name information['WEB_TAGS'] = [tag.name for tag in item.tag.all()] deploy_path_object = Path.objects.filter( id=request.POST.get('deploy_path')) for item in deploy_path_object: information['deploy_path'] = item.path_value fun_value = dict(RELEASE_FUN_CHOICES).get(request.POST.get('fun')) # 存入键值到Information库 store_dict = copy.copy(information) remove_list = [ 'zone', 'domain', 'deploy_path', 'WEB_SERVER_HOST', 'WAR_DATA_HOST', 'MYSQL_USER', 'CHAT_HOST_NAME', 'WAR_TAGS', 'CHAT_TAGS', 'WEB_TAGS', 'ZONE_NAME' ] for key in remove_list: store_dict.pop(key) info_queryset = Information.objects.filter( zones_id=request.POST.get('zones')) if not info_queryset: for key in store_dict: info = Information(zones_id=request.POST.get('zones'), key=key, value=information[key]) info.save() print "insert success!" else: for item in info_queryset: for key in store_dict: queryset = Information.objects.filter( zones_id=request.POST.get('zones'), id=item.id, key=key) if queryset: if queryset.update(value=information[key]): print "update success!" # 定义配置生成目录 config_path[ 'war_config_path'] = '/srv/salt/app_config/%s/war_data/war_data/WEB-INF' % information[ 'zone'] config_path[ 'web_base_path'] = '/srv/salt/app_config/%s/pxqb_base/config' % information[ 'zone'] config_path[ 'web_login_path'] = '/srv/salt/app_config/%s/pxqb_login_%s' % ( information['zone'], information['zone']) config_path[ 'nginx_config_path'] = '/srv/salt/service_config/%s' % information[ 'zone'] for key in config_path: if not os.path.exists(config_path[key]): makedir_p(config_path[key]) for key in information: replacements['{{%s}}' % key] = information[key] # 导入sql context['import_sql'] = do_import_sql(start_date, start_time, sql_file, **information) # 记录进数据库 dbs = Databases.objects.filter(ip_address=information['MYSQL_ADDR']) for item in dbs: item.zones.add(request.POST.get('zones')) if not request.POST.get('merge'): # 调用配置生成函数 do_replace_config_file(config_path, config_files, replacements) # 部署war_data代码 war_data_deploy_path = os.path.join(information['deploy_path'], 'war_data') # 备份清理旧的代码 for hostname in [ information['WAR_DATA_HOST'], information['WEB_SERVER_HOST'] ]: context['%s_do_backup_and_clear' % hostname] = do_backup_clear_old_package(hostname) if 'War' in information['WAR_TAGS']: context[ 'deploy_war_data'] = do_deploy_project_to_remote_machine( information['WAR_DATA_HOST'], fun_value, [war_data_deploy_path, war_data_git_url]) # 创建软链 release_path = '/data/wardata' shell = 'if test -L %s;then echo "already release!";' \ 'else ln -s %s %s && echo "make symlink success!";fi' \ % (release_path, war_data_deploy_path, release_path) context[ 'war_data_make_symlink'] = do_deploy_project_to_remote_machine( information['WAR_DATA_HOST'], 'cmd.run', shell) # 部署base和login if 'Web' in information['WEB_TAGS']: object_list = [ 'pxqb_base', 'pxqb_login_%s' % information['zone'] ] for project in object_list: print project web_deploy_path = os.path.join(information['deploy_path'], project) web_release_path = os.path.join('/data/wwwroot', project) if project == 'pxqb_base': git_url = web_base_git_url else: git_url = web_login_git_url context['deploy_%s' % project] = do_deploy_project_to_remote_machine( information['WEB_SERVER_HOST'], fun_value, [web_deploy_path, git_url]) # make symlink shell = 'if test -L %s;then echo "already make symlink!";' \ 'else mkdir -p /data/wwwroot && ln -s %s %s && echo "make symlink success!";fi' % \ (web_release_path, web_deploy_path, web_release_path) context['%s_make_symlink' % project] = do_deploy_project_to_remote_machine( information['WEB_SERVER_HOST'], 'cmd.run', shell) # 同步base和login配置文件 object_config_salt_url = "salt://app_config/%s/%s" % ( information['zone'], project) print object_config_salt_url context['%s_config_sync' % project] = do_deploy_project_to_remote_machine( information['WEB_SERVER_HOST'], 'cp.get_dir', [ object_config_salt_url, information['deploy_path'] ]) # 同步web服务配置 context['%s_sync_web_conf' % information['zone']] = do_sync_web_conf(information) # 重启服务 context['%s_start_redis' % information['WEB_SERVER_HOST']] = \ do_deploy_project_to_remote_machine(information['WEB_SERVER_HOST'], 'service.start', 'redis') service_list = ['nginx', 'php-fpm', 'supervisord'] for service in service_list: context['%s_start_%s' % (information['WEB_SERVER_HOST'], service)] = \ do_deploy_project_to_remote_machine(information['WEB_SERVER_HOST'], 'service.start', service) # 初始化分区数据 else: # 生成配置文件并进行文件对比合并 zone2_object = Zone.objects.filter( id=request.POST.get('previous_zone')) for item in zone2_object: information['previous_zone'] = item.name do_replace_config_file(config_path, config_files, replacements) # 配置文件比对,合并 source_root_path = '/srv/salt/app_config' # 原配置根目录 shared_root_path = '/srv/salt/shared_war_server' # 共用war配置根目录 merge_name = '%s_%s' % ( information['previous_zone'], information['zone']) # 合并目录名 zone_shell_array = '%s %s' % (information['previous_zone'], information['zone']) # 游戏分区数组 shell用 project_name = 'war_data' command = "sh /data/deploy/OMS/saltstack/scripts/shell/merge_config_file.sh '%s' '%s' '%s' '%s' '%s'" \ % (source_root_path, shared_root_path, zone_shell_array, merge_name, project_name) # 执行合并脚本 running_command(command) # 第一次已经同步war_data代码,所以这里只同步war_data配置文件 war_config_salt_url = "salt://shared_war_server/%s_%s/war_data" % \ (information['previous_zone'], information['zone']) context[ 'war_data_config_sync'] = do_deploy_project_to_remote_machine( information['WAR_DATA_HOST'], 'cp.get_dir', [war_config_salt_url, information['deploy_path']]) # 备份清理旧的包 context['%s_do_backup_and_clear' % information['WEB_SERVER_HOST']] = \ do_backup_clear_old_package(information['WEB_SERVER_HOST']) object_list = ['pxqb_base', 'pxqb_login_%s' % information['zone']] for project in object_list: web_deploy_path = os.path.join(information['deploy_path'], project) web_release_path = os.path.join('/data/wwwroot', project) if project == 'pxqb_base': git_url = web_base_git_url else: git_url = web_login_git_url context['deploy_%s' % project] = do_deploy_project_to_remote_machine( information['WEB_SERVER_HOST'], fun_value, [web_deploy_path, git_url]) # make symlink shell = 'if test -L %s;then echo "already make symlink!";' \ 'else mkdir -p /data/wwwroot && ln -s %s %s && echo "make symlink success!";fi' % \ (web_release_path, web_deploy_path, web_release_path) context['%s_make_symlink' % project] = do_deploy_project_to_remote_machine( information['WEB_SERVER_HOST'], 'cmd.run', shell) # 同步配置 object_config_salt_url = "salt://app_config/%s/%s" % ( information['zone'], project) context['%s_config_sync' % project] = do_deploy_project_to_remote_machine( information['WEB_SERVER_HOST'], 'cp.get_dir', [ object_config_salt_url, information['deploy_path'] ]) # 同步web服务配置 context['%s_sync_nginx_conf' % information['zone']] = do_sync_web_conf(information) # 重启服务 context['%s_start_tomcat' % information['WAR_DATA_HOST']] = \ do_deploy_project_to_remote_machine(information['WAR_DATA_HOST'], 'service.start', 'tomcat') context['%s_start_redis' % information['WEB_SERVER_HOST']] = \ do_deploy_project_to_remote_machine(information['WEB_SERVER_HOST'], 'service.start', 'redis') service_list = ['nginx', 'php-fpm', 'supervisord'] for service in service_list: context['%s_start_%s' % (information['WEB_SERVER_HOST'], service)] = \ do_deploy_project_to_remote_machine(information['WEB_SERVER_HOST'], 'service.start', service) # 初始化分区数据 print json.dumps(context) if regex_match_error(json.dumps(context)): status = True else: status = False deploy_wizard_object = DeployWizard( departments_id=u'1', zones_id=int(request.POST.get('zones')), operate=request.session['username'], data_server=information['WAR_DATA_HOST'], chat_server=information['CHAT_HOST_NAME'], web_server=information['WEB_SERVER_HOST'], db_server=information['MYSQL_ADDR'], deploy_path=information['deploy_path'], status=status, context=json.dumps(context, sort_keys=True)) deploy_wizard_object.save() return redirect('deploy_game_wizard_list') return render( request, template_name, { 'departments': departments, 'username': request.session['username'], 'var7': "active", 'highlight15': "active", 'war_servers': war_servers, 'db_servers': db_servers, 'web_servers': web_servers, 'chat_servers': chat_servers, 'domains': domains, 'zones': zones, 'path_object': path_object, 'services': services })
def push_process(request, template_name='saltstack/file_push_form.html'): form = PushForm(request.POST or None) servers = Assets.objects.all().order_by('alias_name') departments = Department.objects.all() zones = Zone.objects.all() files = Produce.objects.all() if form.is_valid(): # department = form.cleaned_data['departments'].name zone = form.cleaned_data['zones'].name domain = form.cleaned_data['domains'].name service = form.cleaned_data['service_name'] service_name = dict(form.fields['service_name'].choices)[service] file_name = form.cleaned_data['file_name'] source_path = form.cleaned_data['source_path'] target_path = form.cleaned_data['target_path'].path_value generate_file = zone + '_' + file_name conf_templates_path = os.path.join('/srv/salt/service_config/templates', service_name) conf_generate_path = os.path.join('/srv/salt/service_config/', zone, service_name) if not os.path.exists(conf_generate_path): makedir_p(conf_generate_path) commands = "sh /data/deploy/OMS/saltstack/scripts/shell/replace_service_conf.sh %s %s %s %s %s %s" % \ (file_name, generate_file, domain, zone, conf_templates_path, conf_generate_path) # 生成新配置文件 running_command(commands) salt_file_storage = os.path.join(source_path, zone, service_name, generate_file) print salt_file_storage target_abs_path = os.path.join(target_path, generate_file) print target_abs_path template = 'jinja' env = 'base' makedirs = False data = { 'expr_form': 'list', 'client': form.cleaned_data['client'], 'fun': 'cp.get_template', 'tgt': [item.host_name for item in form.cleaned_data['tgt'].all()], 'arg': [salt_file_storage, target_abs_path, template, env, makedirs], } print data salt = SaltApi() salt.get_token() header = salt.get_header() context = process(header, **data) print context if regex_match_error(context): status = True else: status = False print status new_form = form.save(commit=False) new_form.context = context new_form.source_path = salt_file_storage new_form.file_name = target_abs_path new_form.status = status new_form.operate = request.session['username'] new_form.save() form.save() return redirect('file_push_list') return render(request, template_name, {'highlight3': 'active', 'username': request.session['username'], 'form': form, 'servers': servers, 'departments': departments, 'zones': zones, 'files': files, 'var7': 'active', })
def merge_server_process(request, template_name='saltstack/merge_server_form.html'): departments = Department.objects.all() zones = Zone.objects.all() # repo_object = Repository.objects.filter(repo_tag=u'war_data') repo_object = Repository.objects.all() assets = Assets.objects.all() war_object = Assets.objects.filter(tag__name='War') chat_object = Assets.objects.filter(tag__name='Chat') web_object = Assets.objects.filter(tag__name='Web') zone_list = [] form = MergeServersForm(request.POST or None) if form.is_valid(): information = {} context = {} war_server_id = form.cleaned_data['data_server'] chat_server_id = form.cleaned_data['chat_server'] war_servers = Assets.objects.filter(id=war_server_id) for item in war_servers: information['{{WAR_DATA_HOST}}'] = item.alias_name information['{{WAR_DATA_ADDR}}'] = item.networks.private_address information['{{REDIS_ADDR}}'] = item.networks.private_address chat_servers = Assets.objects.filter(id=chat_server_id) for item in chat_servers: information['{{CHAT_PUBLIC_ADDR}}'] = item.networks.public_address information[ '{{CHAT_PRIVATE_ADDR}}'] = item.networks.private_address information['{{CHAT_HOST}}'] = item.alias_name project_list = ['war_data', 'pxqb_base', 'pxqb_login'] merge_path = form.cleaned_data['merge_path'] # print merge_path if not os.path.exists(merge_path): makedir_p(merge_path) merge_zones = form.cleaned_data['merge_zones'] for zone in merge_zones: zone_list.append(zone.name) zone_shell_array = ' '.join(zone_list) # print zone_shell_array merge_name = '_'.join(zone_list) # print merge_name deploy_path = '/data/deploy' source_path = '/srv/salt/app_config' # 合并、复制、生成新配置 for project_name in project_list: if project_name == 'war_data': script = "sh /data/deploy/OMS/saltstack/scripts/shell/merge_config_file.sh '%s' '%s' '%s' '%s' '%s'" \ % (source_path, merge_path, zone_shell_array, merge_name, project_name) # print script running_command(script) if project_name == 'pxqb_base': project_abs_path = os.path.join(merge_path, merge_name, project_name, 'config') if not os.path.exists(project_abs_path): makedir_p(project_abs_path) # 合并conf.allserver.php文件 file_list = [] for zone in zone_list: file_list.append('%s/%s/%s/config/conf.allserver.php' % (source_path, zone, project_name)) # print file_list one_file = file_list[0] do_copy_new_file(one_file, merge_name) file_list.pop(0) # print file_list merge_all_server(file_list, merge_name) # 生成新dataserver.php文件 dataserver_file = '/data/deploy/OMS/media/Upload/conf.dataserver.php' merge_target_file = '/srv/salt/merge_config/%s/pxqb_base/config/conf.dataserver.php' % merge_name execute_replace(information, dataserver_file, merge_target_file) if project_name == 'pxqb_login': for zone in zone_list: full_project_name = project_name + '_' + zone copy_target_path = os.path.join(merge_path, merge_name) if not os.path.exists(copy_target_path): makedir_p(copy_target_path) script = "sh /data/deploy/OMS/saltstack/scripts/shell/copy_pxqb_login_config.sh %s %s %s" \ % (full_project_name, copy_target_path, zone) # print script running_command(script) # 同步配置 project_tmp = project_list for project_name in project_list: if project_name == 'pxqb_login': for zone in zone_list: project_tmp.append(project_name + '_' + zone) project_tmp.remove('pxqb_login') for project_name in project_tmp: salt_abs_url = 'salt://merge_config/%s/%s' % (merge_name, project_name) data = { 'expr_form': 'list', 'client': 'local', 'fun': 'cp.get_dir', 'tgt': [item.host_name for item in form.cleaned_data['tgt'].all()], 'arg': [salt_abs_url, deploy_path], } # print data salt = SaltApi() salt.get_token() header = salt.get_header() context['%s' % project_name] = process(header, **data) print context['%s' % project_name] print context if regex_match_error(json.dumps(context)): status = True else: status = False new_form = form.save(commit=False) new_form.operate = request.session['username'] new_form.status = status new_form.chat_server = information['{{CHAT_HOST}}'] new_form.data_server = information['{{WAR_DATA_HOST}}'] new_form.context = json.dumps(context) new_form.save() form.save() return redirect('merge_server_list') return render( request, template_name, { 'highlight13': 'active', 'username': request.session['username'], 'form': form, 'var7': 'active', 'repo_object': repo_object, 'assets': assets, 'zones': zones, 'departments': departments, 'war_object': war_object, 'web_object': web_object, 'chat_object': chat_object, })