Commit 8e1b0fa9a5b7e959fbf430cd61a8355b9fbd56eb

Authored by Alexis Koralewski
1 parent 9b3ce1a9
Exists in dev

Add login required of agents view & add get_all_cmds management

CHANGELOG
1 1 24-11-2022 (AKo): v0.6.11.1
2 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 6 24-11-2022 (EP): v0.6.11.0
5 7 - Agent specific command execution in THREAD => now possible
6 8 - bugfix do_stop/restart => valeur de retour
... ...
VERSION
1   -0.6.11
  1 +0.6.11.1
2 2 \ No newline at end of file
... ...
src/core/pyros_django/api/views.py
... ... @@ -279,6 +279,7 @@ class AgentCmdViewSet(viewsets.ModelViewSet):
279 279 commands_recivied_by_agent = AgentCmd.get_commands_sent_to_agent(agent_name)
280 280 agent_cmds = commands_sent_by_agent | commands_recivied_by_agent
281 281 agent_cmds = agent_cmds.exclude(full_name="get_specific_cmds")
  282 + agent_cmds = agent_cmds.exclude(full_name="get_all_cmds")
282 283 agent_cmds = agent_cmds.order_by("-s_deposit_time")
283 284 if number:
284 285 number = int(number)
... ...
src/core/pyros_django/dashboard/views.py
... ... @@ -187,32 +187,33 @@ def settings(request):
187 187 CAN_VIEW_AGENTS_STATE = request.session.get("role") in ("Admin", "Unit-PI", "Operator")
188 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 192 last_do_stop_cmd = None
193 193 if "AgentSST" not in agent_name:
194 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 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 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 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 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 209 else:
210 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 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 217 def agent_detail(request, agent_name):
217 218 agent_log_path = os.path.join(os.environ.get("PROJECT_ROOT_PATH"),f"logs/{agent_name}/",f"{agent_name}.log")
218 219 try:
... ... @@ -228,11 +229,14 @@ def agent_detail(request, agent_name):
228 229 except FileNotFoundError:
229 230 error_message = f"Cannot find agent logs. (Agent log path tried : {agent_log_path})"
230 231 specific_cmd_with_args = None
  232 + cmd_with_choices = []
231 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 236 if "get_specific_cmds" in agent_general_commands:
233 237 agent_general_commands.remove("get_specific_cmds")
234 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 240 wait_time = 0
237 241 max_wait_time = 20
238 242 unimplemented_command = None
... ... @@ -254,24 +258,36 @@ def agent_detail(request, agent_name):
254 258 # get text between paretheses
255 259 args = re.findall(pattern="\(([^)]+)\)",string=specific_cmd)
256 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 276 if request.GET.get("cmd"):
262 277 if request.GET.get("cmd") != "all":
263 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 280 else:
266 281 return JsonResponse(None,safe=False)
267 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 284 if request.GET.get("cmd"):
270 285 return JsonResponse(None,safe=False)
271 286 commands_sent_by_agent = AgentCmd.get_commands_sent_by_agent(agent_name)
272 287 commands_recivied_by_agent = AgentCmd.get_commands_sent_to_agent(agent_name)
273 288 agent_cmds = commands_sent_by_agent | commands_recivied_by_agent
274 289 agent_cmds = agent_cmds.exclude(full_name="get_specific_cmds")
  290 + agent_cmds = agent_cmds.exclude(full_name="get_all_cmds")
275 291 agent_cmds = agent_cmds.order_by("-s_deposit_time")
276 292 paginator = Paginator(agent_cmds, pyros_settings.NB_ELEMENT_PER_PAGE)
277 293 page_number = request.GET.get("page",1)
... ... @@ -315,7 +331,7 @@ def agent_detail(request, agent_name):
315 331 commands = paginator.page(paginator.num_pages)
316 332 return render(request, "dashboard/agent_detail.html", locals())
317 333  
318   -
  334 +@login_required
319 335 def agent_action(request):
320 336 if request.POST:
321 337 action = request.POST.get("action")
... ... @@ -338,6 +354,7 @@ def agent_action(request):
338 354 messages.add_message(request, messages.INFO, f"Error while creating command, please try again.")
339 355 return redirect(agent_detail,agent_name=agentsst)
340 356  
  357 +@login_required
341 358 def agents_commands(request):
342 359 agents_cmds = AgentCmd.objects.all()
343 360 cmd_status = AgentCmd.CMD_STATUS_CODES
... ... @@ -380,6 +397,7 @@ def agents_commands(request):
380 397 agents_cmds = agents_cmds.filter(state=selected_cmd_status)
381 398 url_filters = urlencode({"start_datetime":start_datetime,"end_datetime":end_datetime,"selected_cmd_status":selected_cmd_status})
382 399 agents_cmds = agents_cmds.exclude(full_name="get_specific_cmds")
  400 + agents_cmds = agents_cmds.exclude(full_name="get_all_cmds")
383 401 agents_cmds = agents_cmds.order_by("-s_deposit_time","-id")
384 402 paginator = Paginator(agents_cmds, pyros_settings.NB_ELEMENT_PER_PAGE)
385 403 page_number = request.GET.get("page",1)
... ... @@ -452,6 +470,7 @@ def soft_mode(request):
452 470 is_auto = soft_mode == Majordome.AUTO_MODE
453 471 return render(request, "dashboard/soft_mode.html", locals())
454 472  
  473 +@login_required
455 474 def agents_state(request):
456 475 agents = AgentSurvey.objects.all()
457 476 datetime_now = datetime.datetime.now(tz=timezone.utc)
... ...