Commit 8e1b0fa9a5b7e959fbf430cd61a8355b9fbd56eb

Authored by Alexis Koralewski
1 parent 9b3ce1a9
Exists in dev

Add login required of agents view & add get_all_cmds management

1 24-11-2022 (AKo): v0.6.11.1 1 24-11-2022 (AKo): v0.6.11.1
2 - AgentSST change names of agents when launched with simulated computer 2 - AgentSST change names of agents when launched with simulated computer
3 - 3 + - Add login required of agents view & add get_all_cmds management
  4 + - UI changes on agent_detail view
  5 +
4 24-11-2022 (EP): v0.6.11.0 6 24-11-2022 (EP): v0.6.11.0
5 - Agent specific command execution in THREAD => now possible 7 - Agent specific command execution in THREAD => now possible
6 - bugfix do_stop/restart => valeur de retour 8 - bugfix do_stop/restart => valeur de retour
1 -0.6.11 1 +0.6.11.1
2 \ No newline at end of file 2 \ No newline at end of file
src/core/pyros_django/api/views.py
@@ -279,6 +279,7 @@ class AgentCmdViewSet(viewsets.ModelViewSet): @@ -279,6 +279,7 @@ class AgentCmdViewSet(viewsets.ModelViewSet):
279 commands_recivied_by_agent = AgentCmd.get_commands_sent_to_agent(agent_name) 279 commands_recivied_by_agent = AgentCmd.get_commands_sent_to_agent(agent_name)
280 agent_cmds = commands_sent_by_agent | commands_recivied_by_agent 280 agent_cmds = commands_sent_by_agent | commands_recivied_by_agent
281 agent_cmds = agent_cmds.exclude(full_name="get_specific_cmds") 281 agent_cmds = agent_cmds.exclude(full_name="get_specific_cmds")
  282 + agent_cmds = agent_cmds.exclude(full_name="get_all_cmds")
282 agent_cmds = agent_cmds.order_by("-s_deposit_time") 283 agent_cmds = agent_cmds.order_by("-s_deposit_time")
283 if number: 284 if number:
284 number = int(number) 285 number = int(number)
src/core/pyros_django/dashboard/views.py
@@ -187,32 +187,33 @@ def settings(request): @@ -187,32 +187,33 @@ def settings(request):
187 CAN_VIEW_AGENTS_STATE = request.session.get("role") in ("Admin", "Unit-PI", "Operator") 187 CAN_VIEW_AGENTS_STATE = request.session.get("role") in ("Admin", "Unit-PI", "Operator")
188 return(render(request, "dashboard/settings.html", locals())) 188 return(render(request, "dashboard/settings.html", locals()))
189 189
190 -def get_last_specific_cmds(agent_name):  
191 - last_agent_specific_cmds = None 190 +def get_last_all_cmds(agent_name):
  191 + last_agent_all_cmds = None
192 last_do_stop_cmd = None 192 last_do_stop_cmd = None
193 if "AgentSST" not in agent_name: 193 if "AgentSST" not in agent_name:
194 try: 194 try:
195 - last_agent_specific_cmds = AgentCmd.objects.filter(full_name="get_specific_cmds",recipient=agent_name,state__contains="CMD_EXECUTED").latest("s_deposit_time") 195 + last_agent_all_cmds = AgentCmd.objects.filter(full_name="get_all_cmds",recipient=agent_name,state__contains="CMD_EXECUTED").latest("s_deposit_time")
196 last_do_stop_cmd = AgentCmd.objects.filter(full_name__contains="do_stop",recipient=agent_name).latest("s_deposit_time") 196 last_do_stop_cmd = AgentCmd.objects.filter(full_name__contains="do_stop",recipient=agent_name).latest("s_deposit_time")
197 - if last_agent_specific_cmds.s_deposit_time <= last_do_stop_cmd.s_deposit_time:  
198 - last_agent_specific_cmds = AgentCmd.send_cmd_from_to("System",agent_name,"get_specific_cmds")  
199 - while not AgentCmd.objects.get(id=last_agent_specific_cmds.id).is_executed() and not AgentCmd.objects.get(id=last_agent_specific_cmds.id).is_exec_error(): 197 + if last_agent_all_cmds.s_deposit_time <= last_do_stop_cmd.s_deposit_time:
  198 + last_agent_all_cmds = AgentCmd.send_cmd_from_to("System",agent_name,"get_all_cmds")
  199 + while not AgentCmd.objects.get(id=last_agent_all_cmds.id).is_executed() and not AgentCmd.objects.get(id=last_agent_all_cmds.id).is_exec_error():
200 time.sleep(0.5) 200 time.sleep(0.5)
201 - return AgentCmd.objects.get(id=last_agent_specific_cmds.id) 201 + return AgentCmd.objects.get(id=last_agent_all_cmds.id)
202 except AgentCmd.DoesNotExist: 202 except AgentCmd.DoesNotExist:
203 - if last_do_stop_cmd == None and last_agent_specific_cmds != None:  
204 - return last_agent_specific_cmds  
205 - last_agent_specific_cmds = AgentCmd.send_cmd_from_to("System",agent_name,"get_specific_cmds")  
206 - while not AgentCmd.objects.get(id=last_agent_specific_cmds.id).is_executed() and not AgentCmd.objects.get(id=last_agent_specific_cmds.id).is_exec_error(): 203 + if last_do_stop_cmd == None and last_agent_all_cmds != None:
  204 + return last_agent_all_cmds
  205 + last_agent_all_cmds = AgentCmd.send_cmd_from_to("System",agent_name,"get_all_cmds")
  206 + while not AgentCmd.objects.get(id=last_agent_all_cmds.id).is_executed() and not AgentCmd.objects.get(id=last_agent_all_cmds.id).is_exec_error():
207 time.sleep(0.5) 207 time.sleep(0.5)
208 - return AgentCmd.objects.get(id=last_agent_specific_cmds.id) 208 + return AgentCmd.objects.get(id=last_agent_all_cmds.id)
209 else: 209 else:
210 # AgentSST doesn't have do_stop cmd... (for the moment) 210 # AgentSST doesn't have do_stop cmd... (for the moment)
211 - last_agent_specific_cmds = AgentCmd.send_cmd_from_to("System",agent_name,"get_specific_cmds")  
212 - while not AgentCmd.objects.get(id=last_agent_specific_cmds.id).is_executed() and not AgentCmd.objects.get(id=last_agent_specific_cmds.id).is_exec_error(): 211 + last_agent_all_cmds = AgentCmd.send_cmd_from_to("System",agent_name,"get_all_cmds")
  212 + while not AgentCmd.objects.get(id=last_agent_all_cmds.id).is_executed() and not AgentCmd.objects.get(id=last_agent_all_cmds.id).is_exec_error():
213 time.sleep(0.5) 213 time.sleep(0.5)
214 - return AgentCmd.objects.get(id=last_agent_specific_cmds.id) 214 + return AgentCmd.objects.get(id=last_agent_all_cmds.id)
215 215
  216 +@login_required
216 def agent_detail(request, agent_name): 217 def agent_detail(request, agent_name):
217 agent_log_path = os.path.join(os.environ.get("PROJECT_ROOT_PATH"),f"logs/{agent_name}/",f"{agent_name}.log") 218 agent_log_path = os.path.join(os.environ.get("PROJECT_ROOT_PATH"),f"logs/{agent_name}/",f"{agent_name}.log")
218 try: 219 try:
@@ -228,11 +229,14 @@ def agent_detail(request, agent_name): @@ -228,11 +229,14 @@ def agent_detail(request, agent_name):
228 except FileNotFoundError: 229 except FileNotFoundError:
229 error_message = f"Cannot find agent logs. (Agent log path tried : {agent_log_path})" 230 error_message = f"Cannot find agent logs. (Agent log path tried : {agent_log_path})"
230 specific_cmd_with_args = None 231 specific_cmd_with_args = None
  232 + cmd_with_choices = []
231 agent_general_commands = AgentCmd._AGENT_GENERAL_COMMANDS.copy() 233 agent_general_commands = AgentCmd._AGENT_GENERAL_COMMANDS.copy()
  234 + if "get_all_cmds" in agent_general_commands:
  235 + agent_general_commands.remove("get_all_cmds")
232 if "get_specific_cmds" in agent_general_commands: 236 if "get_specific_cmds" in agent_general_commands:
233 agent_general_commands.remove("get_specific_cmds") 237 agent_general_commands.remove("get_specific_cmds")
234 if not AgentSurvey.objects.get(name=agent_name).is_stopping(): 238 if not AgentSurvey.objects.get(name=agent_name).is_stopping():
235 - agent_specific_cmds = get_last_specific_cmds(agent_name) 239 + agent_specific_cmds = get_last_all_cmds(agent_name)
236 wait_time = 0 240 wait_time = 0
237 max_wait_time = 20 241 max_wait_time = 20
238 unimplemented_command = None 242 unimplemented_command = None
@@ -254,24 +258,36 @@ def agent_detail(request, agent_name): @@ -254,24 +258,36 @@ def agent_detail(request, agent_name):
254 # get text between paretheses 258 # get text between paretheses
255 args = re.findall(pattern="\(([^)]+)\)",string=specific_cmd) 259 args = re.findall(pattern="\(([^)]+)\)",string=specific_cmd)
256 for arg in args: 260 for arg in args:
257 - arguments = arg.split(",")  
258 - for arg in arguments:  
259 - #arg,arg_type = arg.split(":")  
260 - specific_cmd_with_args[cmd].append(arg.split(":")) 261 + if "typing.Literal" in arg:
  262 + arg_name = arg.split(":")[0]
  263 + values = re.findall(pattern="\[(.*?)\]",string=arg)
  264 + trim_values = []
  265 + for value in values:
  266 + if "'" in value:
  267 + value = value.replace("'","")
  268 + trim_values.append(value)
  269 + specific_cmd_with_args[cmd].append([arg_name,trim_values])
  270 + cmd_with_choices.append(cmd)
  271 + else:
  272 + arguments = arg.split(",")
  273 + for arg in arguments:
  274 + #arg,arg_type = arg.split(":")
  275 + specific_cmd_with_args[cmd].append(arg.split(":"))
261 if request.GET.get("cmd"): 276 if request.GET.get("cmd"):
262 if request.GET.get("cmd") != "all": 277 if request.GET.get("cmd") != "all":
263 if request.GET.get("cmd") in list(specific_cmd_with_args.keys()): 278 if request.GET.get("cmd") in list(specific_cmd_with_args.keys()):
264 - return JsonResponse(specific_cmd_with_args.get(request.GET.get("cmd")),safe=False) 279 + return JsonResponse({"specific_cmd_with_args":specific_cmd_with_args.get(request.GET.get("cmd")),"cmd_with_choices":cmd_with_choices},safe=False)
265 else: 280 else:
266 return JsonResponse(None,safe=False) 281 return JsonResponse(None,safe=False)
267 else: 282 else:
268 - return JsonResponse({"agent_general_commands":agent_general_commands,"specific_cmd_with_args":specific_cmd_with_args,"unimplemented_command":unimplemented_command},safe=False) 283 + return JsonResponse({"agent_general_commands":agent_general_commands,"specific_cmd_with_args":specific_cmd_with_args,"unimplemented_command":unimplemented_command,"cmd_with_choices":cmd_with_choices},safe=False)
269 if request.GET.get("cmd"): 284 if request.GET.get("cmd"):
270 return JsonResponse(None,safe=False) 285 return JsonResponse(None,safe=False)
271 commands_sent_by_agent = AgentCmd.get_commands_sent_by_agent(agent_name) 286 commands_sent_by_agent = AgentCmd.get_commands_sent_by_agent(agent_name)
272 commands_recivied_by_agent = AgentCmd.get_commands_sent_to_agent(agent_name) 287 commands_recivied_by_agent = AgentCmd.get_commands_sent_to_agent(agent_name)
273 agent_cmds = commands_sent_by_agent | commands_recivied_by_agent 288 agent_cmds = commands_sent_by_agent | commands_recivied_by_agent
274 agent_cmds = agent_cmds.exclude(full_name="get_specific_cmds") 289 agent_cmds = agent_cmds.exclude(full_name="get_specific_cmds")
  290 + agent_cmds = agent_cmds.exclude(full_name="get_all_cmds")
275 agent_cmds = agent_cmds.order_by("-s_deposit_time") 291 agent_cmds = agent_cmds.order_by("-s_deposit_time")
276 paginator = Paginator(agent_cmds, pyros_settings.NB_ELEMENT_PER_PAGE) 292 paginator = Paginator(agent_cmds, pyros_settings.NB_ELEMENT_PER_PAGE)
277 page_number = request.GET.get("page",1) 293 page_number = request.GET.get("page",1)
@@ -315,7 +331,7 @@ def agent_detail(request, agent_name): @@ -315,7 +331,7 @@ def agent_detail(request, agent_name):
315 commands = paginator.page(paginator.num_pages) 331 commands = paginator.page(paginator.num_pages)
316 return render(request, "dashboard/agent_detail.html", locals()) 332 return render(request, "dashboard/agent_detail.html", locals())
317 333
318 - 334 +@login_required
319 def agent_action(request): 335 def agent_action(request):
320 if request.POST: 336 if request.POST:
321 action = request.POST.get("action") 337 action = request.POST.get("action")
@@ -338,6 +354,7 @@ def agent_action(request): @@ -338,6 +354,7 @@ def agent_action(request):
338 messages.add_message(request, messages.INFO, f"Error while creating command, please try again.") 354 messages.add_message(request, messages.INFO, f"Error while creating command, please try again.")
339 return redirect(agent_detail,agent_name=agentsst) 355 return redirect(agent_detail,agent_name=agentsst)
340 356
  357 +@login_required
341 def agents_commands(request): 358 def agents_commands(request):
342 agents_cmds = AgentCmd.objects.all() 359 agents_cmds = AgentCmd.objects.all()
343 cmd_status = AgentCmd.CMD_STATUS_CODES 360 cmd_status = AgentCmd.CMD_STATUS_CODES
@@ -380,6 +397,7 @@ def agents_commands(request): @@ -380,6 +397,7 @@ def agents_commands(request):
380 agents_cmds = agents_cmds.filter(state=selected_cmd_status) 397 agents_cmds = agents_cmds.filter(state=selected_cmd_status)
381 url_filters = urlencode({"start_datetime":start_datetime,"end_datetime":end_datetime,"selected_cmd_status":selected_cmd_status}) 398 url_filters = urlencode({"start_datetime":start_datetime,"end_datetime":end_datetime,"selected_cmd_status":selected_cmd_status})
382 agents_cmds = agents_cmds.exclude(full_name="get_specific_cmds") 399 agents_cmds = agents_cmds.exclude(full_name="get_specific_cmds")
  400 + agents_cmds = agents_cmds.exclude(full_name="get_all_cmds")
383 agents_cmds = agents_cmds.order_by("-s_deposit_time","-id") 401 agents_cmds = agents_cmds.order_by("-s_deposit_time","-id")
384 paginator = Paginator(agents_cmds, pyros_settings.NB_ELEMENT_PER_PAGE) 402 paginator = Paginator(agents_cmds, pyros_settings.NB_ELEMENT_PER_PAGE)
385 page_number = request.GET.get("page",1) 403 page_number = request.GET.get("page",1)
@@ -452,6 +470,7 @@ def soft_mode(request): @@ -452,6 +470,7 @@ def soft_mode(request):
452 is_auto = soft_mode == Majordome.AUTO_MODE 470 is_auto = soft_mode == Majordome.AUTO_MODE
453 return render(request, "dashboard/soft_mode.html", locals()) 471 return render(request, "dashboard/soft_mode.html", locals())
454 472
  473 +@login_required
455 def agents_state(request): 474 def agents_state(request):
456 agents = AgentSurvey.objects.all() 475 agents = AgentSurvey.objects.all()
457 datetime_now = datetime.datetime.now(tz=timezone.utc) 476 datetime_now = datetime.datetime.now(tz=timezone.utc)