Ejemplo n.º 1
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")
Ejemplo n.º 2
0
def run_cmd(request):
    try:
        server_names = request.POST.get('server_names')
        belong_tos = request.POST.get('belong_tos')
        server_names = server_names.split(',')
        belong_tos = belong_tos.split(',')
        cmd = request.POST.get('cmd')
        time_now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        servers = {}
        cmd_results = ''
        for i in  zip(server_names,belong_tos):
            if not servers.has_key(i[1]):
                servers[i[1]] = []
            servers[i[1]].append(i[0])
        for k,v in servers.items():
            v = ','.join(v)
            cmd_result = client_send_data("{'salt':1,'act':'cmd.run','hosts':'%s','argv':%s}" % (v,cmd.split(',')),CENTER_SERVER[k][0],CENTER_SERVER[k][1])
            cmd_result = convert_str_to_html(cmd_result)
            if cmd_results:
                cmd_results = cmd_result
            else:
                cmd_results = cmd_results + '<br><br><br><br>' + cmd_result
        for i in server_names:
            log.objects.create(source_ip=i,username=request.user.username,command=cmd,time=time_now)
        return HttpResponse(simplejson.dumps({'code':0,'msg':u'完成执行完成','cmd_results':cmd_results}),content_type="application/json")
    except Exception,e:
        logger.error(e)
        return HttpResponse(simplejson.dumps({'code':1,'msg':u'完成执行失败'}),content_type="application/json")
Ejemplo n.º 3
0
def run_cmd(request):
    try:
        server_names = request.POST.get('server_names')
        belong_tos = request.POST.get('belong_tos')
        server_names = server_names.split(',')
        belong_tos = belong_tos.split(',')
        cmd = request.POST.get('cmd')
        cmd_template = request.POST.get('cmd_template')

        for center_server in belong_tos:
            if not check_center_server_up(CENTER_SERVER[center_server][0],CENTER_SERVER[center_server][1]):
                return HttpResponse(json.dumps({'code':1,'msg':u'无法连接到%s' % center_server}),content_type="application/json")

        time_now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        servers = {}
        cmd_results = ''
        for i in  zip(server_names,belong_tos):
            if not servers.has_key(i[1]):
                servers[i[1]] = []
            servers[i[1]].append(i[0])
        for k,v in servers.items():
            v = ','.join(v)
            if cmd:
                cmd_result = client_send_data(json.dumps({'salt':1,'act':'cmd.run','hosts':v,'argv':cmd.split(',,')}),CENTER_SERVER[k][0],CENTER_SERVER[k][1])
                cmd_result = convert_str_to_html(cmd_result)
                if not cmd_results:
                    cmd_results = cmd_result
                else:
                    cmd_results = cmd_results + '<br><br><br><br>' + cmd_result
            if cmd_template:
                orm = command_template.objects.get(description=cmd_template)
                command = '''echo '%s' > /tmp/tempfile && if awk 'FNR==1' /tmp/tempfile|grep python > /dev/null 2>&1;then python /tmp/tempfile;else bash /tmp/tempfile;fi;rm -rf /tmp/tempfile''' % re.sub(r"'",'''\'"'"\'''',orm.cmd)
                cmd_result = client_send_data(json.dumps({'salt':1,'act':'cmd.run','hosts':v,'argv':command.split(',,')}),CENTER_SERVER[k][0],CENTER_SERVER[k][1])
                cmd_result = convert_str_to_html(cmd_result)
                if cmd_results:
                    cmd_results = cmd_result
                else:
                    cmd_results = cmd_results + '<br><br><br><br>' + cmd_result
                cmd = u'模板 < %s >' % cmd_template
        for i in server_names:
            log.objects.create(source_ip=i,username=request.user.username,command=cmd,time=time_now)
        return HttpResponse(json.dumps({'code':0,'msg':u'完成执行完成','cmd_results':cmd_results}),content_type="application/json")
    except Exception,e:
        logger.error(e)
        return HttpResponse(json.dumps({'code':1,'msg':u'完成执行失败'}),content_type="application/json")
Ejemplo n.º 4
0
 def gevent_run(client_send_data,server_list,i,k,v,dict_data):
     uniq_test = server_list.objects.filter(server_name=k)
     if v == True and not uniq_test:
         inner_ip = client_send_data(json.dumps({'salt':1,'act':'grains.item','hosts':k,'argv':['ipv4']}),CENTER_SERVER[i][0],CENTER_SERVER[i][1])
         inner_ip = literal_eval(inner_ip)
         inner_ip[k]['ipv4'].remove('127.0.0.1')
         # cmd = ["curl http://www.whereismyip.com/|grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'"]
         cmd = ['curl http://members.3322.org/dyndns/getip']
         external_ip = client_send_data(json.dumps({'salt':1,'act':'cmd.run','hosts':k,'argv':cmd}),CENTER_SERVER[i][0],CENTER_SERVER[i][1])
         external_ip = literal_eval(external_ip)
         external_ip = external_ip[k].split('\n')[-1]
         os = client_send_data(json.dumps({'salt':1,'act':'grains.item','hosts':k,'argv':['os']}),CENTER_SERVER[i][0],CENTER_SERVER[i][1])
         os = literal_eval(os)
         belong_to = i
         server_list.objects.create(server_name=k,inner_ip=','.join(inner_ip[k]['ipv4']),external_ip=external_ip,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.filter(belong_to=i):
         if not i.server_name in dict_data.keys():
             i.status = 0
             i.save()
Ejemplo n.º 5
0
 def gevent_run(client_send_data,belong_to,j,cmd,CENTER_SERVER):
     client_send_data(json.dumps({'salt':1,'act':'cmd.run','hosts':j,'argv':cmd.split(',,')}),CENTER_SERVER[belong_to][0],CENTER_SERVER[belong_to][1])
Ejemplo n.º 6
0
#coding:utf-8
from libs.socket_send_data import client_send_data
import time,json
cmd = ["curl http://www.whereismyip.com/|grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'"]
start = time.time()
external_ip = client_send_data(json.dumps({'salt':1,'act':'cmd.run','hosts':'*','argv':cmd}),'123.59.107.121',7777)
end = time.time()
print external_ip
print end - start
Ejemplo n.º 7
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)
Ejemplo n.º 8
0
#coding:utf-8
from libs.socket_send_data import client_send_data
import time
cmd = ["curl http://www.whereismyip.com/|grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'"]
start = time.time()
external_ip = client_send_data("{'salt':1,'act':'cmd.run','hosts':'%s','argv':%s}" % ('DB-01',cmd),'123.59.107.121',7777)
end = time.time()
print external_ip
print end - start
Ejemplo n.º 9
0
def salt_top_run(request):
    center_server = request.POST.get("center_server")
    run_target = request.POST.get("run_target")
    state = request.POST.get("state")

    for server in center_server.split("|"):
        if not check_center_server_up(CENTER_SERVER[server][0], CENTER_SERVER[server][1]):
            return HttpResponse(
                json.dumps({"code": 1, "msg": u"无法连接到%s" % server, "cmd_results": ""}), content_type="application/json"
            )

    run_target_dict = {}
    target = []
    cmd_results = ""
    time_now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")

    for i in zip(center_server.split("|"), run_target.split("|"), state.split("|")):
        if not run_target_dict.has_key(i[0]):
            run_target_dict[i[0]] = []
        run_target_dict[i[0]].append((i[1], i[2]))

    for i in run_target_dict.keys():
        master_dir = commands.getoutput(
            '''ssh %s "grep -A2 '^file_roots' /etc/salt/master |grep 'base:' -A1|grep '-'|cut -d'-' -f2"'''
            % CENTER_SERVER[i][0]
        )
        content = "base:\n"
        for j in run_target_dict[i]:
            for n in j[0].split(","):
                if not n in target:
                    target.append(n)
                    cmd = u"state模块 < %s >" % j[1]
                    log.objects.create(source_ip=n, username=request.user.username, command=cmd, time=time_now)
                else:
                    return HttpResponse(
                        json.dumps({"code": 1, "msg": u"目标主机不能重复", "cmd_results": cmd_results}),
                        content_type="application/json",
                    )
                content += "  '%s':\n" % n
                for m in j[1].split(","):
                    content += "    - %s\n" % m
        content += "EOF"
        os.system('''ssh %s "cat > %s/top.sls << EOF\n%s"''' % (CENTER_SERVER[i][0], master_dir, content))

    try:
        # def gevent_run_all(CENTER_SERVER,client_send_data,p,q):
        #     for i in run_target_dict.keys():
        #         for j in run_target_dict[i]:
        #             p.spawn(gevent_run,CENTER_SERVER,client_send_data,i,j[0],q)
        # def gevent_run(CENTER_SERVER,client_send_data,i,j,q):
        #     cmd_result = client_send_data(json.dumps({'salt':1,'act':'state.highstate','hosts':j,'argv':''}),CENTER_SERVER[i][0],CENTER_SERVER[i][1])
        #     cmd_result = convert_str_to_html(cmd_result)
        #     q.put(cmd_result)
        # p = Pool()
        # q = Queue()
        # p.spawn(gevent_run_all,CENTER_SERVER,client_send_data,p,q)
        # p.join()
        # for i in range(q.qsize()):
        #     cmd_result = q.get()
        #     if not cmd_results:
        #         cmd_results = cmd_result
        #     else:
        #         cmd_results = cmd_results + '<br><br><br><br>' + cmd_result

        hosts_list = []
        for i in run_target_dict.keys():
            for j in run_target_dict[i]:
                hosts_list.append(j[0])
            cmd_result = client_send_data(
                json.dumps({"salt": 1, "act": "state.highstate", "hosts": ",".join(hosts_list), "argv": ""}),
                CENTER_SERVER[i][0],
                CENTER_SERVER[i][1],
            )
            cmd_results = convert_str_to_html(cmd_result)
        return HttpResponse(
            json.dumps({"code": 0, "msg": u"模块执行完成", "cmd_results": cmd_results}), content_type="application/json"
        )
    except Exception:
        return HttpResponse(json.dumps({"code": 1, "msg": u"模块执行失败"}), content_type="application/json")
Ejemplo n.º 10
0
 def gevent_run(CENTER_SERVER,client_send_data,i,j,q):
     cmd_result = client_send_data(json.dumps({'salt':1,'act':'state.highstate','hosts':j,'argv':''}),CENTER_SERVER[i][0],CENTER_SERVER[i][1])
     cmd_result = convert_str_to_html(cmd_result)
     q.put(cmd_result)