Exemple #1
0
def php_result(request, ID):
    try:
        branch = request.GET["branch"]
    except:
        branch = ''
    test_release = Test.objects.all().get(id=str(ID))
    host_list, repo_type, repo_url, repo_last_branch, test_auth, server_path, stdout, before_cmd, after_cmd = \
        list(eval(test_release.host_list)), test_release.repo_type, test_release.repo_url.strip(), \
        test_release.last_branch, test_release.auth, os.path.abspath(
                test_release.server_path), '', test_release.before_cmd, test_release.after_cmd
    the_auth = Auth.objects.get(id=test_auth)
    username = the_auth.username if the_auth.username else ''
    password = the_auth.password if the_auth.password else ''
    key = force_text(the_auth.key) if the_auth.key else ''
    local_path = os.path.join('/ops/test', server_path.lstrip('/'), str(ID))
    repo_branch = repo_last_branch if not branch else branch
    if repo_type == 'git':
        git_co(repo_url, repo_branch, key, local_path)
        if before_cmd:
            os.chdir(local_path)
            os.system(before_cmd)
        for i in host_list:
            stdout = rrsync(local_path, i, server_path, ['.svn*', '.git*'])
            result = stdout.replace('\n', '</br>')
        test_release.last_branch = repo_branch
        test_release.save()
    else:
        svn_co(repo_url, local_path, '', username, password)
        for i in host_list:
            stdout = rrsync(local_path, i, server_path, ['.svn*', '.git*'])
            result = stdout.replace('\n', '</br>')
    return render_to_response('release/php_result.html', locals(), RequestContext(request))
Exemple #2
0
def test_release(request, ID):
    the_release = Test.objects.get(id=str(ID))
    try:
        branch = request.GET["branch"]
    except:
        branch = ''
    if PreRecord.objects.all().filter(project=the_release.project):
        return http_error(request, u'发布失败,请查看预发布记录')
    else:
        host_list, server_path, stdout, before_cmd, after_cmd = list(
            eval(the_release.host_list)), os.path.abspath(
                the_release.server_path
            ), '', the_release.before_cmd, the_release.after_cmd
        the_project = Project.objects.get(name=the_release.project)
        the_auth = Auth.objects.get(id=the_project.auth)
        key = force_text(the_auth.key) if the_auth.key else ''
        local_path = os.path.join('/ops/%s' % the_project.name,
                                  server_path.lstrip('/'), str(ID))
        repo_branch = the_release.last_branch if not branch else branch
        git_co(the_project.url, repo_branch, key, local_path)
        if before_cmd:
            os.chdir(local_path)
            os.system(before_cmd)
        for i in host_list:
            stdout = rrsync(local_path, i, server_path, ['.git*'])
            result = stdout.replace('\n', '</br>')
        the_release.last_branch = repo_branch
        the_release.last_hash = git_hash(local_path)
        the_release.save()
        return render_to_response('release/release_result.html', locals(),
                                  RequestContext(request))
Exemple #3
0
def svn_pro_result(request, ID):
    the_svn = SvnControl.objects.get(id=str(ID))
    the_project = Project.objects.get(name=the_svn.project)
    the_auth = Auth.objects.get(id=the_project.auth)
    username = the_auth.username if the_auth.username else ''
    password = the_auth.password if the_auth.password else ''
    pro_host_list = list(eval(the_project.pro_host_list))
    pro_url = the_project.url.strip()
    pro_no_version = the_svn.no_version
    pro_server_path = os.path.abspath(the_project.server_path)
    pro_local_path = os.path.join('/ops/pro', pro_server_path.lstrip('/'), str(ID))
    svn_co(pro_url, pro_local_path, pro_no_version, username, password)
    for i in pro_host_list:
        stdout = rrsync(pro_local_path, i, pro_server_path, ['.svn*', '.git*'])
    pro = ReleaseRecord(project=the_project.name, environment=u'正式', no_version=pro_no_version, release_user=request.user)
    pro.save()
    loger = set_log(level='debug', filename='product_release_%s.log' % datetime.date.today().strftime('%Y-%m-%d'))
    loger.debug('product released by %s, detail : %s' % (request.user, stdout))
    result = stdout.replace('\n', '</br>')
    if request.user.email:
        mail_task.delay(subject=u'项目 %s 正式发布结果 版本号:%s' % (the_project.name, pro_no_version),
                        message=stdout,
                        from_email=EMAIL_HOST_USER,
                        recipient_list=[request.user.email],
                        fail_silently=False)
    return render_to_response('release/svn_pro_result.html', locals(), RequestContext(request))
Exemple #4
0
def test_release(request, ID):
    the_release = Test.objects.get(id=str(ID))
    try:
        branch = request.GET["branch"]
    except:
        branch = ''
    if PreRecord.objects.all().filter(project=the_release.project):
        return http_error(request, u'发布失败,请查看预发布记录')
    else:
        host_list, server_path, stdout, before_cmd, after_cmd = list(eval(the_release.host_list)), os.path.abspath(
                the_release.server_path), '', the_release.before_cmd, the_release.after_cmd
        the_project = Project.objects.get(name=the_release.project)
        the_auth = Auth.objects.get(id=the_project.auth)
        key = force_text(the_auth.key) if the_auth.key else ''
        local_path = os.path.join('/ops/%s' % the_project.name, server_path.lstrip('/'), str(ID))
        repo_branch = the_release.last_branch if not branch else branch
        git_co(the_project.url, repo_branch, key, local_path)
        if before_cmd:
            os.chdir(local_path)
            os.system(before_cmd)
        for i in host_list:
            stdout = rrsync(local_path, i, server_path, ['.git*'])
            result = stdout.replace('\n', '</br>')
        the_release.last_branch = repo_branch
        the_release.last_hash = git_hash(local_path)
        the_release.save()
        return render_to_response('release/release_result.html', locals(), RequestContext(request))
Exemple #5
0
def pro_release(request, ID):
    try:
        the_pre_record = PreRecord.objects.get(id=ID)
        the_release = Test.objects.all().get(id=the_pre_record.test_id)
        the_project = Project.objects.get(name=the_release.project)
        pro_host_list = list(eval(the_project.pro_host_list))
        ln_path = os.path.abspath(the_project.server_path)
        test_server_path = os.path.abspath(the_release.server_path)
        pro_server_path = os.path.abspath(
            the_project.server_path) + "_%s" % the_release.last_hash
        local_path = os.path.join('/ops/%s' % the_project.name,
                                  test_server_path.lstrip('/'),
                                  str(the_pre_record.test_id))
        host_list_str = ','.join(pro_host_list)
        salt = SaltApi(SALTAPI_URL, SALTAPI_USER, SALTAPI_PASSWORD)
        salt.login()
        for i in pro_host_list:
            stdout = rrsync(local_path, i, pro_server_path, ['.git*'])
            result = stdout.replace('\n', '</br>')

        #import os
        cmd_rm = 'rm -rf %s' % ln_path
        cmd_ln = 'ln -s %s %s' % (pro_server_path, ln_path)
        cmd_ansible_rm = "ansible php1 -i /etc/ansible/hosts -mshell -a '%s'" % cmd_rm
        cmd_ansible_ln = "ansible php1 -i /etc/ansible/hosts -mshell -a '%s' " % cmd_ln
        #print cmd_ansible_ln
        #cmd="echo %s > /tmp/1.log"%cmd_ansible_rm
        #os.system(cmd)
        os.system(cmd_ansible_rm)
        os.system(cmd_ansible_ln)

        salt.cmd(host_list_str, 'rm -rf %s' % ln_path)
        salt.cmd(host_list_str, 'ln -s %s %s' % (pro_server_path, ln_path))
        print 'ln -s %s %s' % (pro_server_path, ln_path)

        salt.logout()

        try:
            the_rollback = RollBack.objects.get(in_use=True)
            the_rollback.in_use = False
            the_rollback.save()
        except:
            pass
        i_rollback = RollBack(project=the_project.name,
                              branch=the_release.last_branch,
                              hash=the_release.last_hash,
                              in_use=True)
        i_rollback.save()
        pro = ReleaseRecord(project=the_project.name,
                            branch=the_release.last_branch,
                            hash=the_release.last_hash,
                            release_user=request.user)
        pro.save()
        return render_to_response('release/release_result.html', locals(),
                                  RequestContext(request))
    except:
        return http_error(request, u'发布出现错误,请检查预发布记录')
Exemple #6
0
def pre_release(request, ID):
    the_release = Test.objects.all().get(id=str(ID))
    if PreRecord.objects.all().filter(project=the_release.project):
        return http_error(request, u'有项目分支正在预发布!')
    else:
        the_project = Project.objects.get(name=the_release.project)
        pre_host_list = list(eval(the_project.pre_host_list))
        test_server_path = os.path.abspath(the_release.server_path)
        pre_server_path = os.path.abspath(the_project.server_path)
        local_path = os.path.join('/ops/%s' % the_project.name, test_server_path.lstrip('/'), str(ID))
        for i in pre_host_list:
            stdout = rrsync(local_path, i, pre_server_path, ['.git*'])
            result = stdout.replace('\n', '</br>')
        i_pre_release = PreRecord(project=the_project.name,
                                  branch=the_release.last_branch,
                                  hash=the_release.last_hash,
                                  test_id=the_release.id)
        i_pre_release.save()
        return render_to_response('release/release_result.html', locals(), RequestContext(request))
Exemple #7
0
def pre_release(request, ID):
    the_release = Test.objects.all().get(id=str(ID))
    if PreRecord.objects.all().filter(project=the_release.project):
        return http_error(request, u'有项目分支正在预发布!')
    else:
        the_project = Project.objects.get(name=the_release.project)
        pre_host_list = list(eval(the_project.pre_host_list))
        test_server_path = os.path.abspath(the_release.server_path)
        pre_server_path = os.path.abspath(the_project.server_path)
        local_path = os.path.join('/ops/%s' % the_project.name,
                                  test_server_path.lstrip('/'), str(ID))
        for i in pre_host_list:
            stdout = rrsync(local_path, i, pre_server_path, ['.git*'])
            result = stdout.replace('\n', '</br>')
        i_pre_release = PreRecord(project=the_project.name,
                                  branch=the_release.last_branch,
                                  hash=the_release.last_hash,
                                  test_id=the_release.id)
        i_pre_release.save()
        return render_to_response('release/release_result.html', locals(),
                                  RequestContext(request))
Exemple #8
0
def pro_release(request, ID):
    try:
        the_pre_record = PreRecord.objects.get(id=ID)
        the_release = Test.objects.all().get(id=the_pre_record.test_id)
        the_project = Project.objects.get(name=the_release.project)
        pro_host_list = list(eval(the_project.pro_host_list))
        ln_path = os.path.abspath(the_project.server_path)
        test_server_path = os.path.abspath(the_release.server_path)
        pro_server_path = os.path.abspath(the_project.server_path) + "_%s" % the_release.last_hash
        local_path = os.path.join('/ops/%s' % the_project.name, test_server_path.lstrip('/'),
                                  str(the_pre_record.test_id))
        host_list_str = ','.join(pro_host_list)
        salt = SaltApi(SALTAPI_URL, SALTAPI_USER, SALTAPI_PASSWORD)
        salt.login()
        for i in pro_host_list:
            stdout = rrsync(local_path, i, pro_server_path, ['.git*'])
            result = stdout.replace('\n', '</br>')
        salt.cmd(host_list_str, 'rm -rf %s' % ln_path)
        salt.cmd(host_list_str, 'ln -s %s %s' % (pro_server_path, ln_path))
        salt.logout()
        try:
            the_rollback = RollBack.objects.get(in_use=True)
            the_rollback.in_use = False
            the_rollback.save()
        except:
            pass
        i_rollback = RollBack(project=the_project.name,
                               branch=the_release.last_branch,
                               hash=the_release.last_hash,
                               in_use=True)
        i_rollback.save()
        pro = ReleaseRecord(project=the_project.name,
                            branch=the_release.last_branch,
                            hash=the_release.last_hash,
                            release_user=request.user)
        pro.save()
        return render_to_response('release/release_result.html', locals(), RequestContext(request))
    except:
        return http_error(request, u'发布出现错误,请检查预发布记录')