Пример #1
0
def add_gotemplate():  ###added an info for gotemplate table
    username = request.json.get('username')
    password = request.json.get('password')
    svn_username = request.json.get('svn_username')
    svn_password = request.json.get('svn_password')
    svn_repo = request.json.get('svn_repo')
    svn_root_path = request.json.get('svn_root_path')
    project = request.json.get('project')
    hostname = request.json.get('hostname')
    env = request.json.get('env')

    if svn_username and svn_password and svn_repo and svn_root_path and project and hostname:
        try:
            hostname = minion.objects.get(saltname=hostname)
            project = gogroup.objects.get(name=project)
            if GOTemplate.objects.filter(hostname=hostname).filter(
                    project=project).filter(env=env):
                connection.close()
                return jsonify(
                    {'result': 'The %s project is existing!!' % project})
            else:
                obj = GOTemplate(username=svn_username,
                                 password=svn_password,
                                 repo=svn_repo,
                                 localpath=svn_root_path,
                                 env=env,
                                 hostname=hostname,
                                 project=project)
                obj.save()
                connection.close()
                return jsonify({
                    'result':
                    'added %s gotemplate is successful!!' % project
                })
        except Exception, e:
            connection.close()
            print e
            return jsonify({'result': 'wrong hostname or project name!!'})
Пример #2
0
def _update_record(name, host):
    _svc = goservices.objects.filter(name=name).first()
    minion_host = minion.objects.get(saltname=host)
    project = _svc.group

    # -------------------------gotemplate-----------------------------------
    if GOTemplate.objects.filter(hostname=minion_host).filter(
            project=project).filter(env=1):
        print 'The %s gotemplate project is existing!!' % project.name
    else:
        obj = GOTemplate(username=svn_username,
                         password=svn_password,
                         repo=svn_gotemplate_repo + project.name,
                         localpath=svn_gotemplate_local_path + project.name,
                         env=1,
                         hostname=minion_host,
                         project=project)
        obj.save()

    # -------------------------goservices-----------------------------------
    if goservices.objects.filter(saltminion=minion_host).filter(
            group=project).filter(name=name).filter(env=1):
        print 'The %s goservice is existing!!' % name
    else:
        obj = goservices(ip=minion_host.ip,
                         name=name,
                         env=1,
                         group=project,
                         saltminion=minion_host,
                         owner=_svc.owner,
                         has_statsd=_svc.has_statsd,
                         has_sentry=_svc.has_sentry,
                         comment=_svc.comment,
                         ports=_svc.ports,
                         level=_svc.level)
        obj.save()
Пример #3
0
def handle_tickets(request):
    task_id = request.POST['id']
    submit = request.POST['submit']
    reply = request.POST['reply']
    operating_id = TicketTasks.objects.get(tasks_id=task_id)
    content = TicketTasks.objects.get(tasks_id=task_id).content
    content = json.loads(content)
    username = User.objects.get(username=content['handler'])
    phone_number = UserProfile.objects.get(user=username).phone_number  
    handle_result = 0

    if submit == 'reject':
        TicketTasks.objects.filter(tasks_id=task_id).update(state='4')
        TicketOperating.objects.create(operating_id=operating_id,handler=username,content=reply,result='2',submitter=content['owner'])
        
        logs(user=request.user,ip=request.META['REMOTE_ADDR'],action='handle ticket (%s)' % content['title'],result='successful')
        info = 'Your "%s" order be reject,please visit to workflow page.' % content['title']
        owner = User.objects.get(username=content['owner'])
        owner_phone_number = UserProfile.objects.get(user=owner).phone_number
        dingding_robo(phone_number=owner_phone_number,types=2,info=info)
        result = [{'HandleTasks':'The task_id handle to success!'}]
        return render(request,'getdata.html',{'result':result}) 

    if content['ticket_type'] == 'go':
        #'----------------------------ticket_type--------------------'
        for host in content['hosts']:
            data = {
                'client':'local',
                'tgt':host,
                'fun':'state.sls',
                'arg':['goservices.supervisor_submodule','pillar={"goprograme":"%s","svnrepo":"%s","supProgrameName":"%s","goRunCommand":"%s"}' % (content['project'],content['svn_repo'],content['supervisor_name'],content['go_command'])]
            }
            result = salt_api.salt_cmd(data)
            try:
                minion_host = minion.objects.get(saltname=host)    
                supervisor_info = gostatus.objects.get(hostname=minion_host)
                supervisor_obj = xmlrpclib.Server('http://%s:%s@%s:%s/RPC2' % (
                    supervisor_info.supervisor_username, supervisor_info.supervisor_password,
                    supervisor_info.supervisor_host, supervisor_info.supervisor_port))
                if supervisor_obj.supervisor.getProcessInfo(content['supervisor_name']):
                    deploy_result = 1
                    print '-------successful-----'
            except Exception, e:
                print e
                deploy_result = 0
                handle_result = 1
                TicketTasks.objects.filter(tasks_id=task_id).update(state='5')
                TicketOperating.objects.create(operating_id=operating_id,handler=username,content=reply,result='3',submitter=content['owner'])
                logs(user=request.user,ip=request.META['REMOTE_ADDR'],action='handle ticket (%s)' % content['title'],result='failed')
                info = 'The "%s" order is failed,please check in %s host.' % (content['title'],host)
                dingding_robo(phone_number=phone_number,types=2,info=info)
                result = [{'HandleTasks':'The task_id handle to failed!'}]
                print '------failed-------------'           
                return render(request,'getdata.html',{'result':result}) 
            
            #-------------------------new project-----------------------------------
            try:
                if deploy_result == 1:
                    if gogroup.objects.filter(name=content['project']):
                        print 'The %s project is existing!!' % content['project']
                    else:
                        obj = gogroup(name=content['project'])
                        obj.save()

                        project = gogroup.objects.get(name=content['project'])
                        obj = svn(username=svn_username,
                            password=svn_password,
                            repo=content['svn_repo'],
                            localpath=go_local_path + content['project'],
                            movepath=go_move_path + content['project'],
                            revertpath=go_revert_path,
                            executefile=go_local_path + content['project'] + '/' + content['project']
                            ,project=project)
                        obj.save()


                    #-------------------------gotemplate-----------------------------------
                    project = gogroup.objects.get(name=content['project'])
                    ip = minion_host.ip

                    if GOTemplate.objects.filter(hostname=minion_host).filter(project=project).filter(env=1):
                        print 'The %s gotemplate project is existing!!' % content['project']
                    else:
                        obj = GOTemplate(
                            username=svn_username,
                            password=svn_password,
                            repo=svn_gotemplate_repo,
                            localpath=svn_gotemplate_local_path,
                            env=1,
                            hostname=minion_host,
                            project=project)
                        obj.save()

                    #-------------------------goservices-----------------------------------
                    if goservices.objects.filter(saltminion=minion_host).filter(group=project).filter(name=content['supervisor_name']).filter(env=1):
                        print 'The %s goservice is existing!!' % content['supervisor_name']
                    else:
                        obj = goservices(
                            ip=ip,
                            name=content['supervisor_name'] ,
                            env=1,
                            group=project,
                            saltminion=minion_host,
                            owner=content['owner'],
                            has_statsd=content['statsd'],
                            has_sentry=content['sentry'],
                            comment=content['function'])
                        obj.save()                     
            except Exception, e:
                print e
                handle_result = 1
                TicketTasks.objects.filter(tasks_id=task_id).update(state='5')
                TicketOperating.objects.create(operating_id=operating_id,handler=username,content=reply,result='3',submitter=content['owner'])
                logs(user=request.user,ip=request.META['REMOTE_ADDR'],action='handle ticket (%s)' % content['title'],result='failed')
                result = [{'HandleTasks':'The task_id handle to failed!'}]