Exemple #1
0
def add_sub_project():
    hostname = request.json.get('hostname')
    project = request.json.get('project')
    sub_project_name = request.json.get('sub_project_name')
    username = request.json.get('username')
    password = request.json.get('password')
    env = request.json.get('env')
    owner = request.json.get('owner')
    has_statsd = request.json.get('has_statsd')
    has_sentry = request.json.get('has_sentry')
    comment = request.json.get('comment')

    if project and hostname and sub_project_name and owner and has_sentry and has_statsd and comment:
        try:
            saltminion = minion.objects.get(saltname=hostname)
            project = gogroup.objects.get(name=project)
            ip = saltminion.ip
            if goservices.objects.filter(saltminion=saltminion).filter(
                    group=project).filter(name=sub_project_name).filter(
                        env=env):
                connection.close()
                return jsonify({
                    'result':
                    'The %s subproject is existing!!' % sub_project_name
                })
        except Exception, e:
            connection.close()
            print e
            return jsonify({'result': 'wrong hostname or project name!!'})

        try:
            obj = goservices(ip=ip,
                             name=sub_project_name,
                             env=env,
                             group=project,
                             saltminion=saltminion,
                             owner=owner,
                             has_statsd=has_statsd,
                             has_sentry=has_sentry,
                             comment=comment)
            obj.save()
            connection.close()
            return jsonify({
                'result':
                'added %s subproject is successful.!!' % sub_project_name
            })
        except Exception, e:
            connection.close()
            print e
            return jsonify({
                'result':
                'added %s subproject was failed.!!' % sub_project_name
            })
Exemple #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()
Exemple #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!'}]