Exemplo n.º 1
0
def salt_pillar_del(request):
    try:
        _id = request.POST.get("id")
        orm = saltstack_pillar.objects.get(id=_id)
        for i in orm.center_server.split(","):
            master_dir = commands.getoutput(
                '''ssh %s "grep -A2 '^pillar_roots' /etc/salt/master |grep 'base:' -A1|grep '-'|cut -d'-' -f2"'''
                % CENTER_SERVER[i][0]
            )
            os.system('''ssh %s "rm -r %s/%s\.sls"''' % (CENTER_SERVER[i][0], master_dir, orm.name))
        orm.delete()
        for i in CENTER_SERVER.keys():
            master_dir = commands.getoutput(
                '''ssh %s "grep -A2 '^pillar_roots' /etc/salt/master |grep 'base:' -A1|grep '-'|cut -d'-' -f2"'''
                % CENTER_SERVER[i][0]
            )
            content_top = """base:\n  '*':\n"""
            for j in saltstack_pillar.objects.all():
                if i in j.center_server.split(","):
                    content_top += """    - %s\n""" % j.name
            content_top += "EOF"
            if len(re.findall(r"\n", content_top)) < 3:
                content_top = ""
            os.system('''ssh %s "cat > %s/top.sls << EOF\n%s"''' % (CENTER_SERVER[i][0], master_dir, content_top))
        return HttpResponse(json.dumps({"code": 0, "msg": u"删除成功"}), content_type="application/json")
    except Exception, e:
        logger.error(e)
        return HttpResponse(json.dumps({"code": 1, "msg": u"删除失败"}), content_type="application/json")
Exemplo n.º 2
0
def search_server_list(request):
    try:
        for i in CENTER_SERVER.keys():
            recv_data = client_send_data("{'salt':1,'act':'test.ping','hosts':'*','argv':[]}",CENTER_SERVER[i][0],CENTER_SERVER[i][1])
            dict_data = eval(recv_data)
            for k,v in dict_data.items():
                uniq_test = server_list.objects.filter(server_name=k)
                if v == True and not uniq_test:
                    ip = client_send_data("{'salt':1,'act':'grains.item','hosts':'%s','argv':['ipv4']}" % k,CENTER_SERVER[i][0],CENTER_SERVER[i][1])
                    ip = eval(ip)
                    ip[k]['ipv4'].pop(0)
                    os = client_send_data("{'salt':1,'act':'grains.item','hosts':'%s','argv':['os']}" % k,CENTER_SERVER[i][0],CENTER_SERVER[i][1])
                    os = eval(os)
                    belong_to = i
                    server_list.objects.create(server_name=k,ip=ip[k]['ipv4'],os=os[k]['os'],belong_to=belong_to,status=1)
                elif uniq_test:
                    orm_server = server_list.objects.get(server_name=k)
                    orm_server.status = 1
                    orm_server.save()
            for i in server_list.objects.all():
                if not i.server_name in dict_data.keys():
                    i.status = 0
                    i.save()
        return HttpResponse(simplejson.dumps({'code':0,'msg':u'获取完成'}),content_type="application/json")
    except Exception,e:
        logger.error(e)
        return HttpResponse(simplejson.dumps({'code':1,'msg':u'获取失败'}),content_type="application/json")
Exemplo n.º 3
0
def rsync_dest_dropdown(request):
    result = {}
    count = 0
    result['rsync_dest_dropdown_list'] = []
    for k in CENTER_SERVER.keys():
        result['rsync_dest_dropdown_list'].append({'text':k, 'id': count})
        count += 1
    return HttpResponse(json.dumps(result),content_type="application/json")
Exemplo n.º 4
0
def salt_state_dropdown(request):
    center_server = request.POST.get("center_server")
    result = {}
    result["list"] = []
    result["edit"] = []
    if center_server:
        for i in center_server.split(","):
            result["edit"].append({"text": i, "id": CENTER_SERVER[i][3]})
    for i in CENTER_SERVER.keys():
        result["list"].append({"text": i, "id": CENTER_SERVER[i][3]})
    return HttpResponse(json.dumps(result), content_type="application/json")
Exemplo n.º 5
0
def salt_state_dropdown(request):
    center_server = request.POST.get('center_server')
    result = {}
    result['list'] = []
    result['edit'] = []
    if center_server:
        for i in center_server.split(','):
            result['edit'].append({'text':i,'id':CENTER_SERVER[i][3]})
    for i in CENTER_SERVER.keys():
        result['list'].append({'text':i,'id':CENTER_SERVER[i][3]})
    return HttpResponse(json.dumps(result),content_type="application/json")
Exemplo n.º 6
0
def salt_top_dropdown(request):
    _id = request.POST.get('id')
    result = {}
    result['target'] = {}
    result['target']['list'] = []
    result['target']['edit'] = []
    result['state'] = {}
    result['state']['list'] = []
    result['state']['edit'] = []
    result['center_server'] = []
    count = 0
    if _id:
        orm = saltstack_top.objects.get(id=_id)
        for i in orm.target.split(','):
            orm_target = server_list.objects.get(server_name=i)
            result['target']['edit'].append({'text':i,'id':orm_target.id})
        for i in orm.state.split(','):
            orm_state = saltstack_state.objects.get(name=i)
            result['state']['edit'].append({'text':i,'id':orm_state.id})

    orm = perm.objects.get(username=request.user.username)
    servers = []
    for i in orm.server_groups.split(','):
        orm_server = server_group_list.objects.get(server_group_name=i)
        servers += orm_server.members_server.split(',')

    for i in CENTER_SERVER.keys():
        result['center_server'].append({'text':i,'id':count})
        count += 1
    for i in server_list.objects.all():
        if i.server_name in servers:
            result['target']['list'].append({'text':i.server_name,'id':i.id})
    for i in saltstack_state.objects.all():
        result['state']['list'].append({'text':i.name,'id':i.id})




    return HttpResponse(json.dumps(result),content_type="application/json")
Exemplo n.º 7
0
def salt_top_dropdown(request):
    _id = request.POST.get("id")
    result = {}
    result["target"] = {}
    result["target"]["list"] = []
    result["target"]["edit"] = []
    result["state"] = {}
    result["state"]["list"] = []
    result["state"]["edit"] = []
    result["center_server"] = []
    count = 0
    if _id:
        orm = saltstack_top.objects.get(id=_id)
        for i in orm.target.split(","):
            orm_target = server_list.objects.get(server_name=i)
            result["target"]["edit"].append({"text": i, "id": orm_target.id})
        for i in orm.state.split(","):
            orm_state = saltstack_state.objects.get(name=i)
            result["state"]["edit"].append({"text": i, "id": orm_state.id})

    orm = perm.objects.get(username=request.user.username)
    servers = []
    for i in orm.server_groups.split(","):
        orm_server = server_group_list.objects.get(server_group_name=i)
        servers += orm_server.members_server.split(",")

    for i in CENTER_SERVER.keys():
        result["center_server"].append({"text": i, "id": count})
        count += 1
    for i in server_list.objects.all():
        if i.server_name in servers:
            result["target"]["list"].append({"text": i.server_name, "id": i.id})
    for i in saltstack_state.objects.all():
        result["state"]["list"].append({"text": i.name, "id": i.id})

    return HttpResponse(json.dumps(result), content_type="application/json")
Exemplo n.º 8
0
 def gevent_run_all(CENTER_SERVER,client_send_data,server_list,p):
     for i in CENTER_SERVER.keys():
         recv_data = client_send_data(json.dumps({'salt':1,'act':'test.ping','hosts':'*','argv':[]}),CENTER_SERVER[i][0],CENTER_SERVER[i][1])
         dict_data = literal_eval(recv_data)
         for k,v in dict_data.items():
             p.spawn(gevent_run,client_send_data,server_list,i,k,v,dict_data)