Exemplo n.º 1
0
def createRelease(request, num):
    App = apps.get_model('apps', 'App')
    try:
        release_app = App.objects.get(id=num)
    except:
        context = {
            'message': "Requested App does not Exist!",
            'go_back_to_url': "/app/" + release_app.name,
            'go_back_to_title': "App Page",
        }
        return render(request, 'message.html', context)
    editors = release_app.editors.all()
    if request.user in editors or request.user.is_staff:
        if request.method == 'GET':
            form = ReleaseForm()
        elif request.method == 'POST':
            form = ReleaseForm(request.POST, request.FILES)
            if form.is_valid():
                new_release = form.save(commit=False)
                new_release.app = release_app
                new_release.save()
                context = {
                    'message': "Release added Successfully!",
                    'go_back_to_url': "/app/" + release_app.name,
                    'go_back_to_title': "App Page",
                }
                return render(request, 'message.html', context)
    else:
        context = {
            'message': "You are not authorized to view this page!",
            'go_back_to_url': "/app/" + release_app.name,
            'go_back_to_title': "App Page",
        }
        return render(request, 'message.html', context)
    return render(request, 'create_release.html', {'form': form})
Exemplo n.º 2
0
def editRelease(request, num):
    Release = apps.get_model('apps', 'Release')
    App = apps.get_model('apps', 'App')
    try:
        edit_release = Release.objects.get(id=num)
    except:
        context = {
            'message': "Requested App does not Exist!",
            'go_back_to_url': "/app/" + edit_release.app.name,
            'go_back_to_title': "App Page",
        }
        return render(request, 'message.html', context)
    editors = edit_release.app.editors.all()
    if request.user in editors or request.user.is_staff:
        if request.method == 'GET':
            form = ReleaseForm(instance=edit_release)
        elif request.method == 'POST':
            form = ReleaseForm(request.POST,
                               request.FILES,
                               instance=edit_release)
            if form.is_valid():
                edited_release = form.save()
                edited_release.save()
                context = {
                    'message': "Release edited Successfully!",
                    'go_back_to_url': "/app/" + edit_release.app.name,
                    'go_back_to_title': "App Page",
                }
                return render(request, 'message.html', context)
    else:
        context = {
            'message': "You are not authorized to view this page!",
            'go_back_to_url': "/app/" + edit_release.app.name,
            'go_back_to_title': "App Page",
        }
        return render(request, 'message.html', context)
    return render(request, 'edit_release.html', {'form': form})
Exemplo n.º 3
0
def code_release(request, template_name='saltstack/code_release_form.html'):
    form = ReleaseForm(request.POST or None)
    servers = Assets.objects.all()
    departments = Department.objects.all()
    zones = Zone.objects.all()
    repo = Repository.objects.all()
    deploy_path = Path.objects.all()
    global status

    if form.is_valid():
        repo_name = form.cleaned_data['repository_name']
        result = Repository.objects.filter(repo_name__contains=repo_name)[0]
        if result.repo_protocol == '3':
            repository_url = 'https://%s:%s@%s' % (result.repo_user, result.repo_pass, result.repo_address)
        elif result.repo_protocol == '2':
            repository_url = 'http://%s:%s@%s' % (result.repo_user, result.repo_pass, result.repo_address)
        else:
            repository_url = '%s@%s' % (result.repo_user, result.repo_address)

        project_name = form.cleaned_data['repository_name'].repo_tag
        zone_name = form.cleaned_data['zones'].name
        deploy_path = form.cleaned_data['deploy_path'].path_value
        release_path = form.cleaned_data['release_path'].path_value
        # versions = form.cleaned_data['versions']
        # version = versions.version
        # version_id = versions.id
        print project_name, zone_name, release_path

        if form.cleaned_data['use_zone']:
            deploy_abs_path = os.path.join(deploy_path, (project_name + '_' + zone_name))
            release_abs_path = release_path + '_' + zone_name
        else:
            deploy_abs_path = os.path.join(deploy_path, project_name)
            release_abs_path = release_path

        fun_value = dict(RELEASE_FUN_CHOICES).get(form.cleaned_data['fun'])

        data = {
            'expr_form': 'list',
            'client': form.cleaned_data['client'],
            'tgt': [item.host_name for item in form.cleaned_data['tgt']],
            'fun': fun_value,
            'arg': [deploy_abs_path, repository_url],
        }
        print data
        salt = SaltApi()
        salt.get_token()
        header = salt.get_header()
        context = process(header, **data)
        print context

        if form.cleaned_data['fun'] == u'1':
            shell = 'rm -rf %s && ln -s %s %s' % (release_abs_path, deploy_abs_path, release_abs_path)
            data = {
                'expr_form': 'list',
                'client': form.cleaned_data['client'],
                'tgt': [item.host_name for item in form.cleaned_data['tgt']],
                'fun': 'cmd.run',
                'arg': shell,
            }
            # print data
            release_context = process(header, **data)
        else:
            release_context = "symlink not change."

        for item in form.cleaned_data['tgt']:
            result = yaml.load(context)['return'][0][item.host_name]
            if result is True or result == "Already up-to-date." and regex_match_error(release_context) is True:
                status = True
            else:
                status = False
        new_form = form.save(commit=False)
        new_form.context = context
        new_form.deploy_path = deploy_abs_path
        new_form.release_path = release_abs_path
        new_form.operate = request.session['username']
        new_form.status = status
        new_form.save()
        form.save()
        return redirect('code_release_list')

    return render(request, template_name, {'highlight2': 'active',
                                           'username': request.session['username'],
                                           'form': form,
                                           'servers': servers,
                                           'departments': departments,
                                           'zones': zones,
                                           'deploy_path': deploy_path,
                                           'repo': repo,
                                           'var7': 'active',
                                           })