Пример #1
0
def exec_rollback(request, ID):
    the_rollback = RollBack.objects.get(id=ID)
    if the_rollback.in_use:
        return http_error(request, u'正式环境为此版本')
    else:
        old_rollback = RollBack.objects.get(in_use=True)
        old_rollback.in_use = False
        old_rollback.save()
        the_project = Project.objects.get(name=the_rollback.project)
        ln_path = os.path.abspath(the_project.server_path)
        pro_server_path = os.path.abspath(
            the_project.server_path) + "_%s" % the_rollback.hash
        pro_host_list = list(eval(the_project.pro_host_list))
        host_list_str = ','.join(pro_host_list)
        salt = SaltApi(SALTAPI_URL, SALTAPI_USER, SALTAPI_PASSWORD)
        salt.login()
        salt.cmd(host_list_str, 'rm -f %s' % ln_path)
        salt.cmd(host_list_str, 'ln -s %s %s' % (pro_server_path, ln_path))
        salt.logout()
        the_rollback.in_use = True
        the_rollback.save()
        pro = ReleaseRecord(project=the_project.name,
                            branch=the_rollback.branch,
                            hash=the_rollback.hash,
                            release_user=request.user)
        pro.save()
        return http_success(request, u'版本回退成功')
Пример #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))
Пример #3
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))
Пример #4
0
def exec_rollback(request, ID):
    the_rollback = RollBack.objects.get(id=ID)
    if the_rollback.in_use:
        return http_error(request, u'正式环境为此版本')
    else:
        old_rollback = RollBack.objects.get(in_use=True)
        old_rollback.in_use = False
        old_rollback.save()
        the_project = Project.objects.get(name=the_rollback.project)
        ln_path = os.path.abspath(the_project.server_path)
        pro_server_path = os.path.abspath(the_project.server_path) + "_%s" % the_rollback.hash
        pro_host_list = list(eval(the_project.pro_host_list))
        host_list_str = ','.join(pro_host_list)
        salt = SaltApi(SALTAPI_URL, SALTAPI_USER, SALTAPI_PASSWORD)
        salt.login()
        salt.cmd(host_list_str, 'rm -f %s' % ln_path)
        salt.cmd(host_list_str, 'ln -s %s %s' % (pro_server_path, ln_path))
        salt.logout()
        the_rollback.in_use = True
        the_rollback.save()
        pro = ReleaseRecord(project=the_project.name,
                            branch=the_rollback.branch,
                            hash=the_rollback.hash,
                            release_user=request.user)
        pro.save()
        return http_success(request, u'版本回退成功')
Пример #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'发布出现错误,请检查预发布记录')
Пример #6
0
def del_rollback(request, ID):
    the_rollback = RollBack.objects.get(id=ID)
    if the_rollback.in_use:
        return http_error(request, u'不能删除正在使用的版本')
    else:
        the_project = Project.objects.get(name=the_rollback.project)
        pro_server_path = os.path.abspath(the_project.server_path) + "_%s" % the_rollback.hash
        pro_host_list = list(eval(the_project.pro_host_list))
        host_list_str = ','.join(pro_host_list)
        salt = SaltApi(SALTAPI_URL, SALTAPI_USER, SALTAPI_PASSWORD)
        salt.login()
        salt.cmd(host_list_str, 'rm -rf %s' % pro_server_path)
        salt.logout()
        the_rollback.delete()
        return http_success(request, u'版本删除成功')
Пример #7
0
def del_rollback(request, ID):
    the_rollback = RollBack.objects.get(id=ID)
    if the_rollback.in_use:
        return http_error(request, u'不能删除正在使用的版本')
    else:
        the_project = Project.objects.get(name=the_rollback.project)
        pro_server_path = os.path.abspath(
            the_project.server_path) + "_%s" % the_rollback.hash
        pro_host_list = list(eval(the_project.pro_host_list))
        host_list_str = ','.join(pro_host_list)
        salt = SaltApi(SALTAPI_URL, SALTAPI_USER, SALTAPI_PASSWORD)
        salt.login()
        salt.cmd(host_list_str, 'rm -rf %s' % pro_server_path)
        salt.logout()
        the_rollback.delete()
        return http_success(request, u'版本删除成功')
Пример #8
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))
Пример #9
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))
Пример #10
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'发布出现错误,请检查预发布记录')