def CheckMinion(request): global username, role, clientip username = request.user.username try: role = request.user.userprofile.role except: role = 'none' clientip = request.META['REMOTE_ADDR'] if request.is_websocket(): for postdata in request.websocket: data = json.loads(postdata) logger.info('%s is requesting. %s 执行参数:%s' % (clientip, request.get_full_path(), data)) result = {} for minion_id in data: commandexe = Command(minion_id, 'test.ping') result['minion_id'] = minion_id result['test_ping'] = commandexe.TestPing()[minion_id] request.websocket.send(json.dumps(result)) ### close websocket ### request.websocket.close() else: if request.method == 'POST': #print request.body #return HttpResponse("%s" %request.body) data = json.loads(request.body) result = {} for tgt in data['tgt']: logger.info('%s is requesting %s. data: %s' % (clientip, request.get_full_path(), data)) #logger.info('%s' %(data['tgt'])) commandexe = Command(tgt, 'test.ping') result[tgt] = commandexe.TestPing()[tgt] logger.info(result) return HttpResponse(json.dumps(result)) elif request.method == 'GET': logger.info('%s is requesting %s.' % (clientip, request.get_full_path())) return HttpResponse('You get nothing!') else: logger.info('%s is requesting %s.' % (clientip, request.get_full_path())) return HttpResponse('nothing!')
def CommandExecute(request): if request.is_websocket(): global username, role, clientip username = request.user.username try: role = request.user.userprofile.role except: role = 'none' clientip = request.META['REMOTE_ADDR'] for postdata in request.websocket: if not postdata: break data = json.loads(postdata) logger.info('%s is requesting. %s 执行参数:%s' % (clientip, request.get_full_path(), data)) #request.websocket.send(json.dumps(data)) ### step one ## info_one = {} info_one['step'] = 'one' request.websocket.send(json.dumps(info_one)) #time.sleep(2) ### final step ### info_final = {} info_final['step'] = 'final' arglist = ["runas=%s" % data['exe_user']] arglist.append(data['arguments']) commandexe = Command( data['target'], data['function'], arglist, data['expr_form'], ) if data['function'] == 'test.ping': info_final['results'] = commandexe.TestPing() elif data['function'] == 'cmd.run': info_final['results'] = commandexe.CmdRun() elif data['function'] == 'state.sls': info_final['results'] = commandexe.StateSls(data['arguments']) #logger.info(json.dumps(info_final)) request.websocket.send(json.dumps(info_final)) ### close websocket ### request.websocket.close()