Пример #1
0
def adminMenu(request):
    if request.method == u'GET' and request.GET.__contains__('reset') and request.GET['reset'] == 'true':
        snaptype = request.GET['type']
        branch_id = request.GET['branch_id']
        branch = Branch.objects.get(id=branch_id)
        if snaptype == 'config':
            branch.run_status = 'd'
        else:
            branch.code_run_status = 'd'
        branch.save()
        return redirect("/admin/?success=true")

    if request.method == u'GET' and request.GET.__contains__('snapshot') and request.GET['snapshot'] == 'true':
        branch_id = request.GET['branch_id']
        snaptype = request.GET['type']
        branch = Branch.objects.get(id=branch_id)
        if snaptype == 'config' and branch.run_status != 'r':
            repo_name = branch.repo.name
            branch_name = branch.name
            pr = subprocess.Popen(os.path.join(settings.BASE_DIR,
                                  'config_cronjob.sh') + ' ' + repo_name + ' ' + branch_name + ' >/var/sftmp/ssRun.out 2>&1 &',
                                  shell=True)
            logger.debug('Started With pid ' + str(pr.pid))
            pr.wait()
            if pr.returncode == 0:
                brlog = BranchLog()
                try:
                    brlog = BranchLog.objects.get(branch=branch, logtype=snaptype)
                except ObjectDoesNotExist:
                    brlog.branch = branch
                    brlog.logtype = snaptype
                brlog.last_log = 'Started'
                brlog.save()
                branch.run_status = 'r'
                branch.save()
                return redirect("/admin/?success=true")
            return redirect("/admin/?failed=true")
        if snaptype == 'code' and branch.code_run_status != 'r':
            repo_name = branch.repo.name
            branch_name = branch.name
            pr = subprocess.Popen(os.path.join(settings.BASE_DIR,
                                    'code_cronjob.sh') + ' ' + repo_name + ' ' + branch_name + ' >/var/sftmp/ssRun.out 2>&1 &',
                                    shell=True)
            logger.debug('Started With pid ' + str(pr.pid))
            pr.wait()
            if pr.returncode == 0:
                brlog = BranchLog()
                try:
                    brlog = BranchLog.objects.get(branch=branch, logtype=snaptype)
                except ObjectDoesNotExist:
                    brlog.branch = branch
                    brlog.logtype = snaptype
                brlog.last_log = 'Started'
                brlog.save()
                branch.code_run_status = 'r'
                branch.save()
                return redirect("/admin/?success=true")
            return redirect("/admin/?failed=true")

    repos = Repo.objects.all()
    branches = Branch.objects.all()
    ctab = CronTab()
    cronlist = []
    for item in [entry.render() for entry in ctab]:
        if item.find(CRON_COMMENT) != -1:
            cronlist.append(item)

    return render(request, 'admin_menu.html', {'repos': repos, 'branches': branches, 'crontab': cronlist})