11#!/usr/bin/env python3
2- # Copyright (C) 2025 Checkmk GmbH - License: GNU General Public License v2
2+ # Copyright (C) 2021 Checkmk GmbH - License: GNU General Public License v2
33# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
44# conditions defined in the file COPYING, which is part of this source code package.
55
6+ from collections .abc import Mapping
67from typing import Annotated , Literal
78
9+ from cmk import fields
810from cmk .gui .http import ContentDispositionType , Response
911from cmk .gui .openapi .framework import (
1012 ApiContext ,
1618 QueryParam ,
1719 VersionedEndpoint ,
1820)
19- from cmk .gui .openapi .restful_objects . constructors import domain_type_action_href
21+ from cmk .gui .openapi .restful_objects import constructors , Endpoint
2022from cmk .gui .openapi .shared_endpoint_families .agent import AGENTS_FAMILY
2123from cmk .gui .openapi .utils import ProblemException
2224from cmk .gui .token_auth import AgentDownloadToken , get_token_store
23- from cmk .gui .utils import agent
25+
26+ from ._utils import (
27+ packed_agent_path_linux_deb ,
28+ packed_agent_path_linux_rpm ,
29+ packed_agent_path_windows_msi ,
30+ )
31+
32+ OS_TYPES_AVAILABLE_IN_RAW = ["linux_rpm" , "linux_deb" , "windows_msi" ]
33+
34+ OS_TYPE_RAW = {
35+ "os_type" : fields .String (
36+ description = (
37+ "The type of the operating system. May be one of "
38+ + ", " .join (OS_TYPES_AVAILABLE_IN_RAW )
39+ ),
40+ enum = sorted (OS_TYPES_AVAILABLE_IN_RAW ),
41+ example = "linux_deb" ,
42+ required = True ,
43+ ),
44+ }
45+
46+
47+ @Endpoint (
48+ constructors .domain_type_action_href ("agent" , "download" ),
49+ "cmk/download" ,
50+ method = "get" ,
51+ content_type = "application/octet-stream" ,
52+ query_params = [OS_TYPE_RAW ],
53+ family_name = AGENTS_FAMILY .name ,
54+ )
55+ def download_agent (params : Mapping [str , object ]) -> Response :
56+ """Download agents shipped with Checkmk"""
57+ os_type = params .get ("os_type" )
58+ response = Response ()
59+
60+ if os_type == "windows_msi" :
61+ agent_path = packed_agent_path_windows_msi ()
62+ response .set_content_type ("application/x-msi" )
63+ elif os_type == "linux_rpm" :
64+ agent_path = packed_agent_path_linux_rpm ()
65+ response .set_content_type ("application/x-rpm" )
66+ elif os_type == "linux_deb" :
67+ agent_path = packed_agent_path_linux_deb ()
68+ response .set_content_type ("application/x-deb" )
69+ else :
70+ # This should never happen. Due to validation `os_type` can only be one
71+ # of the three elements above.
72+ raise AssertionError (f"Agent: os_type '{ os_type } ' not known in raw edition." )
73+
74+ response .set_content_disposition (ContentDispositionType .ATTACHMENT , agent_path .name )
75+
76+ with open (agent_path , mode = "rb" ) as f :
77+ response .data = f .read ()
78+ response .status_code = 200
79+ return response
80+
2481
2582_OS_TYPES_AVAILABLE = ["linux_deb" , "linux_rpm" , "windows_msi" ]
2683
@@ -55,13 +112,13 @@ def download_agent_by_token(
55112
56113 response = Response ()
57114 if os_type == "windows_msi" :
58- agent_path = agent . packed_agent_path_windows_msi ()
115+ agent_path = packed_agent_path_windows_msi ()
59116 response .set_content_type ("application/x-msi" )
60117 elif os_type == "linux_rpm" :
61- agent_path = agent . packed_agent_path_linux_rpm ()
118+ agent_path = packed_agent_path_linux_rpm ()
62119 response .set_content_type ("application/x-rpm" )
63120 elif os_type == "linux_deb" :
64- agent_path = agent . packed_agent_path_linux_deb ()
121+ agent_path = packed_agent_path_linux_deb ()
65122 response .set_content_type ("application/x-deb" )
66123 else :
67124 raise AssertionError (f"Agent: os_type '{ os_type } ' not known in this edition." )
@@ -75,7 +132,7 @@ def download_agent_by_token(
75132
76133ENDPOINT_DOWNLOAD_BY_TOKEN = VersionedEndpoint (
77134 metadata = EndpointMetadata (
78- path = domain_type_action_href ("agent" , "download_by_token" ),
135+ path = constructors . domain_type_action_href ("agent" , "download_by_token" ),
79136 link_relation = "cmk/download_by_token" ,
80137 method = "get" ,
81138 content_type = "application/octet-stream" ,
0 commit comments