Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Simplified Agent Installer #1352

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/tacticalrmm/agents/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@
path("update/", views.update_agents),
path("installer/", views.install_agent),
path("bulkrecovery/", views.bulk_agent_recovery),
path("installer/linux", views.install_agent_linux),
]
79 changes: 56 additions & 23 deletions api/tacticalrmm/agents/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,60 @@
token: str,
api: str,
download_url: str,
download_only: bool = False,
) -> FileResponse:

text = Path(settings.LINUX_AGENT_SCRIPT).read_text()

# replace contents
if not download_only:
match arch:
case "amd64":
arch_id = MeshAgentIdent.LINUX64
case "386":
arch_id = MeshAgentIdent.LINUX32
case "arm64":
arch_id = MeshAgentIdent.LINUX_ARM_64
case "arm":
arch_id = MeshAgentIdent.LINUX_ARM_HF
case _:
arch_id = "not_found"

Check warning on line 53 in api/tacticalrmm/agents/utils.py

View check run for this annotation

Codecov / codecov/patch

api/tacticalrmm/agents/utils.py#L46-L53

Added lines #L46 - L53 were not covered by tests

core = get_core_settings()

uri = get_mesh_ws_url()
mesh_id = asyncio.run(get_mesh_device_id(uri, core.mesh_device_group))
mesh_dl = f"{core.mesh_site}/meshagents?id={mesh_id}&installflags=2&meshinstall={arch_id}"

replace = {
"agentDLChange": download_url,
"meshDLChange": mesh_dl,
"clientIDChange": client,
"siteIDChange": site,
"agentTypeChange": agent_type,
"tokenChange": token,
"apiURLChange": api,
}

for i, j in replace.items():
text = text.replace(i, j)

with tempfile.NamedTemporaryFile() as fp:
with open(fp.name, "w") as f:
f.write(text)
f.write("\n")

return FileResponse(
open(fp.name, "rb"), as_attachment=True, filename="linux_agent_install.sh"
)


def generate_linux_install_command(
install_flags: list,
arch: str,
curl_url: str,
download_url: str,
):
match arch:
case "amd64":
arch_id = MeshAgentIdent.LINUX64
Expand All @@ -55,26 +107,7 @@
f"{core.mesh_site}/meshagents?id={mesh_id}&installflags=2&meshinstall={arch_id}"
)

text = Path(settings.LINUX_AGENT_SCRIPT).read_text()

replace = {
"agentDLChange": download_url,
"meshDLChange": mesh_dl,
"clientIDChange": client,
"siteIDChange": site,
"agentTypeChange": agent_type,
"tokenChange": token,
"apiURLChange": api,
}

for i, j in replace.items():
text = text.replace(i, j)

with tempfile.NamedTemporaryFile() as fp:
with open(fp.name, "w") as f:
f.write(text)
f.write("\n")

return FileResponse(
open(fp.name, "rb"), as_attachment=True, filename="linux_agent_install.sh"
)
install_flags.extend(["--meshdl", f"'{mesh_dl}'", "--agentdl", f"'{download_url}'"])
return f"curl -FL '{curl_url}' | sudo bash -s -- " + " ".join(

Check warning on line 111 in api/tacticalrmm/agents/utils.py

View check run for this annotation

Codecov / codecov/patch

api/tacticalrmm/agents/utils.py#L110-L111

Added lines #L110 - L111 were not covered by tests
str(i) for i in install_flags
)
29 changes: 27 additions & 2 deletions api/tacticalrmm/agents/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from django.utils import timezone as djangotime
from django.urls import reverse
from agents.utils import generate_linux_install, generate_linux_install_command
from meshctrl.utils import get_login_token
from packaging import version as pyver
from rest_framework import serializers
from rest_framework.decorators import api_view, permission_classes
from rest_framework.exceptions import PermissionDenied
from rest_framework.permissions import IsAuthenticated
from rest_framework.permissions import IsAuthenticated, AllowAny
from rest_framework.response import Response
from rest_framework.views import APIView

Expand Down Expand Up @@ -609,7 +611,7 @@
download_url=download_url,
)

elif request.data["installMethod"] in {"manual", "mac"}:
elif request.data["installMethod"] in {"manual", "mac", "bash-manual"}:
resp = {}
if request.data["installMethod"] == "manual":
cmd = [
Expand All @@ -633,6 +635,14 @@
cmd.append("--power")

resp["cmd"] = " ".join(str(i) for i in cmd)
elif request.data["installMethod"] == "bash-manual":
curl_url = request.build_absolute_uri(reverse(install_agent_linux))
cmd = install_flags.copy()
cmd.remove("-m")
cmd.remove("install")
resp["cmd"] = generate_linux_install_command(

Check warning on line 643 in api/tacticalrmm/agents/views.py

View check run for this annotation

Codecov / codecov/patch

api/tacticalrmm/agents/views.py#L638-L643

Added lines #L638 - L643 were not covered by tests
cmd, goarch, curl_url, download_url
)
else:
install_flags.insert(0, f"sudo ./{inno}")
cmd = install_flags.copy()
Expand Down Expand Up @@ -688,6 +698,21 @@
return response


@api_view(["GET"])
@permission_classes([AllowAny])
def install_agent_linux(request):
return generate_linux_install(

Check warning on line 704 in api/tacticalrmm/agents/views.py

View check run for this annotation

Codecov / codecov/patch

api/tacticalrmm/agents/views.py#L704

Added line #L704 was not covered by tests
download_only=True,
client="",
site="",
agent_type="",
arch="",
token="",
api="",
download_url="",
)


@api_view(["POST"])
@permission_classes([IsAuthenticated, RecoverAgentPerms])
def recover(request, agent_id: str) -> Response:
Expand Down
16 changes: 16 additions & 0 deletions api/tacticalrmm/core/agent_linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ meshSystemBin="${meshDir}/meshagent"
meshSvcName='meshagent.service'
meshSysD="/lib/systemd/system/${meshSvcName}"

# Simple Bash Argument Handler
while [[ "$#" -gt 0 ]]; do
case $1 in
-t|--token|--auth) token="$2"; shift ;;
-c|--client-id) clientID="$2"; shift ;;
-s|--site-id) siteID="$2"; shift ;;
-a|--agent-type) agentType="$2"; shift;;
--proxy) proxy="$2"; shift;;
--api) apiURL="$2"; shift;;
--agentdl) agentDL="$2"; shift;;
--meshdl) meshDL="$2"; shift;;
*) echo "Unknown parameter passed: $1"; exit 1 ;;
esac
shift
done

deb=(ubuntu debian raspbian kali linuxmint)
rhe=(fedora rocky centos rhel amzn arch opensuse)

Expand Down