def produce_add(request, template_name='oms_config/produce_add.html'): departments = Department.objects.all() zones = Zone.objects.all() config_files = Upload.objects.all() generate_object = Path.objects.filter(path_key__contains=u'配置生成目录') repo_object = Repository.objects.all() generate_file_list = [] replacements = {} form = ProduceForm(request.POST or None) username = request.session['username'] if form.is_valid(): project_name = form.cleaned_data['project_name'].repo_tag zone_list = form.cleaned_data['zones'] config_file = str(form.cleaned_data['config_file'].doc_file) for item in zone_list: # print item.name generate_path = form.cleaned_data['generate_path'].path_value if project_name == 'war_data': generate_abs_path = os.path.join(generate_path, item.name, project_name, project_name, 'WEB-INF') elif project_name == 'pxqb_login': generate_abs_path = os.path.join( generate_path, item.name, project_name + '_' + item.name) else: generate_abs_path = os.path.join(generate_path, item.name, project_name, 'config') basename = os.path.basename(config_file) if not os.path.exists(generate_abs_path): makedir_p(generate_abs_path) generate_file = os.path.join(generate_abs_path, basename) generate_file_list.append(generate_file) # replacements['{{ZONES}}'] = zone_name info = Information.objects.filter(zones=item) for x in info: replacements['{{%s}}' % x.key] = x.value # print replacements # print generate_file_list for abs_file in generate_file_list: execute_replace(replacements, config_file, abs_file) new_form = form.save(commit=False) new_form.generate_path = generate_file_list new_form.save() form.save() return redirect('produce_list') return render( request, template_name, { 'form': form, 'username': username, 'var4': 'active', 'highlight5': 'active', 'departments': departments, 'zones': zones, 'config_files': config_files, 'generate_object': generate_object, 'repo_object': repo_object, })
def upload_action(request): upload_path = '/data/deploy/OMS/media/Upload' files = request.FILES.getlist('doc_file') for item in files: if not os.path.exists(upload_path): makedir_p(upload_path) def handle_uploaded_file(f): with open(upload_path + '/' + f.name, 'wb+') as destination: for chunk in f.chunks(): destination.write(chunk) handle_uploaded_file(item) uploads = Upload( doc_file=os.path.join(upload_path, item.name), s_id=request.POST['s'], title=request.POST['title'] ) uploads.save() return HttpResponse("File(s) uploaded!")
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 p2p_scp_process(request, template_name='saltstack/code_release_form.html'): global status status = False form = P2PForm(request.POST or None) if form.is_valid(): operate = request.session['username'] # 项目名 project = form.cleaned_data['repository_name'].repo_tag zones = form.cleaned_data['zones'] # 归档路径 archive_path = request.POST['archive_path'] # 如果路径不存在,则创建 if os.path.exists(archive_path): makedir_p(archive_path) # versions = form.cleaned_data['versions'] get_deploy_path = form.cleaned_data['deploy_path'].path_value release_path = form.cleaned_data['release_path'].path_value # 部署路径 发布路径拼接 if form.cleaned_data['use_zone']: deploy_path = os.path.join(get_deploy_path, (project + '_' + zones.name)) release_abs_path = release_path + '_' + zones.name else: deploy_path = os.path.join(get_deploy_path, project) release_abs_path = release_path # 获取仓库地址 repo_name = form.cleaned_data['repository_name'] result = Repository.objects.filter(repo_name__contains=repo_name)[0] repository_url = get_repo_url(result) compress_file = os.path.join(archive_path, '%s.zip' % project) # 获取最新版并压缩 git_checkout(archive_path, project, repository_url) archive_compress(archive_path, project, compress_file) # 获取md5码 command = 'md5sum %s' % compress_file md5sum = get_md5sum(command) servers = form.cleaned_data['tgt'] get_destination_list(servers) SOURCE.append( Assets.objects.filter( networks__private_address__icontains=MASTER_IP)) source_server = SOURCE[0][0] do_copy_control(source_server, archive_path, compress_file, deploy_path, release_abs_path, md5sum) # print return_result for item in return_result: for key in item: if regex_match_error(item[key]) is True: status = True else: status = False # print status # store to mysql # Version.objects.filter(Q(vernier=u'1') | Q(vernier=u'2') | Q(vernier=u'3')).update(vernier=u'0') # if status: # Version.objects.filter(id=versions.id).update(vernier=1) new_form = form.save(commit=False) new_form.fun = 'P2P_SCP' new_form.deploy_path = deploy_path new_form.release_path = release_abs_path new_form.operate = operate new_form.status = status new_form.context = json.dumps(return_result) new_form.save() form.save() return redirect('code_release_list') return render(request, template_name, { 'form': form, 'highlight2': 'active', 'var7': 'active', })