Exemple #1
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
        return HttpResponse(json.dumps({'code':0,'msg':u'模块执行完成','cmd_results':cmd_results}),content_type="application/json")
    except Exception,e:
        return HttpResponse(json.dumps({'code':1,'msg':u'模块执行失败'}),content_type="application/json")
Exemple #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')
        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")
Exemple #3
0
def search_server_list(request):
    for k,v in CENTER_SERVER.items():
        if not check_center_server_up(v[0],v[1]):
            return HttpResponse(json.dumps({'code':1,'msg':u'无法连接到%s' % k}),content_type="application/json")



    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)

    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()
    p = Pool()
    p.spawn(gevent_run_all,CENTER_SERVER,client_send_data,server_list,p)
    p.join()
    return HttpResponse(json.dumps({'code':0,'msg':u'获取完成'}),content_type="application/json")
Exemple #4
0
def upload_upload(request):
    flag = request.POST.get('flag')
    file_name = request.POST.get('file_name')
    if int(flag) == 0:
        #开始传输
        # orm = upload_files.objects.get(file_name=file_name)
        rsync_dest = request.POST.get('rsync_dest')
        rsync_state = request.POST.get('rsync_state')
        for k,v in CENTER_SERVER.items():
            if not check_center_server_up(v[0],v[1]):
                return HttpResponse(json.dumps({'code':1,'msg':u'无法连接到%s' % k}),content_type="application/json")
        if rsync_dest:
            rsync_ip = CENTER_SERVER[rsync_dest][0]
            if rsync_state:
                master_dir = commands.getoutput('''ssh %s "grep -A2 '^file_roots' /etc/salt/master |grep 'base:' -A1|grep '-'|cut -d'-' -f2"''' % CENTER_SERVER[rsync_dest][0])
                rsync_dir = os.path.join(master_dir,rsync_state)
            else:
                rsync_dir = CENTER_SERVER[rsync_dest][2]
            cmdLine = []
            cmdLine.append('rsync')
            cmdLine.append('--progress')
            cmdLine.append(BASE_DIR + '/uploads/%s' % file_name)
            cmdLine.append('%s:%s' % (rsync_ip,rsync_dir))
            tmpFile = BASE_DIR + "/tmp/upload.tmp"  #临时生成一个文件
            fpWrite = open(tmpFile,'w')
            with open(BASE_DIR + '/tmp/rsync_status_file.tmp','w') as f:
                pass
            process = subprocess.Popen(cmdLine,stdout = fpWrite,stderr = subprocess.PIPE);
            while True:
                fpRead = open(tmpFile,'r')  #这里又重新创建了一个文件读取对象,不知为何,用上面的就是读不出来,改w+也不>行
                lines = fpRead.readlines()
                for line in lines:
                    a = re.search(r'\d+%',line)
                    if a:
                        with open(BASE_DIR + '/tmp/percent.tmp','a') as f:
                                f.write(a.group())
                if  process.poll() == 0:
                    break;
                elif process.poll() == None:
                    pass
                else:
                    break
                fpWrite.truncate()  #此处清空文件,等待记录下一次输出的进度
                fpRead.close()
                time.sleep(0.7)
            fpWrite.close()
            os.remove(BASE_DIR + '/tmp/rsync_status_file.tmp')
        #    error = process.stderr.read()
        #    if not error == None:
        #        print 'error info:%s' % error
            os.remove(tmpFile) #删除临时文件
            os.remove(BASE_DIR + '/tmp/percent.tmp')
            return HttpResponse(json.dumps({'code':0,'msg':u'文件传输成功'}),content_type="application/json")
    elif int(flag) == 1:
        #获取百分比
        if os.path.exists(BASE_DIR + '/tmp/rsync_status_file.tmp'):
            process = 1
        else:
            process = 0
        if os.path.exists(BASE_DIR + '/tmp/percent.tmp'):
            with open(BASE_DIR + '/tmp/percent.tmp') as f:
                data = f.readline()
                if data:
                    last_percent = re.search(r'\d{1,2}%$',data)
                    if last_percent:
                        return HttpResponse(json.dumps({'code':0,'percent':last_percent.group(),'process':process}),content_type="application/json")
        else:
            return HttpResponse(json.dumps({'code':0,'percent':0,'process':process}),content_type="application/json")

    else:
        pass
        return HttpResponse(json.dumps({'code':1,'msg':u'文件传输失败'}),content_type="application/json")
Exemple #5
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")