def clean_port_runner(server_id: int, port: Port, update_traffic: bool = True): with db_session() as db: if db_forward_rule := get_forward_rule(db, server_id, port.id): db.delete(db_forward_rule) db.commit() server = get_server_with_ports_usage(db, server_id) if db_forward_rule := get_forward_rule(db, server_id, port.id): db.delete(db_forward_rule) db.commit()
async def forward_rule_runner_get( response: Response, server_id: int, port_id: int, db=Depends(get_db), user=Depends(get_current_active_user), ): """ Get port forward rule """ forward_rule = get_forward_rule(db, server_id, port_id, user) if not forward_rule: raise HTTPException( status_code=404, detail="Port forward rule not found" ) if not user.is_admin(): if not any( user.id == u.user_id for u in forward_rule.port.allowed_users ): raise HTTPException( status_code=404, detail="Port forward rule not found" ) artifacts = PortForwardRuleArtifacts() if forward_rule.config.get('runner'): try: with open(f"ansible/priv_data_dirs/{server_id}/artifacts/{forward_rule.config.get('runner')}/stdout", 'r') as f: artifacts.stdout = f.read() except: artifacts.stdout = "No stdout found!" return artifacts
def update_rule_error(server_id: int, port_id: int, facts: t.Dict): with db_session() as db: db_rule = get_forward_rule(db, server_id, port_id) db_rule.config["error"] = "\n".join([facts.get("error", "")] + [ re.search(r"\w+\[[0-9]+\]: (.*)$", line).group(1) for line in facts.get("systemd_error", "").split("\n") if re.search(r"\w+\[[0-9]+\]: (.*)$", line) ]).strip() db.add(db_rule) db.commit()
async def forward_rule_get( response: Response, server_id: int, port_id: int, db=Depends(get_db), user=Depends(get_current_active_user), ): """ Get port forward rule """ forward_rule = get_forward_rule(db, server_id, port_id, user) if not forward_rule: raise HTTPException(status_code=404, detail="Port forward rule not found") if not user.is_admin(): if not any(user.id == u.user_id for u in forward_rule.port.allowed_users): raise HTTPException(status_code=404, detail="Port forward rule not found") return forward_rule