Skip to content

Commit 49e2380

Browse files
committed
final small refactor
Signed-off-by: Oleksander Piskun <oleksandr2088@icloud.com>
1 parent fc658f7 commit 49e2380

1 file changed

Lines changed: 52 additions & 50 deletions

File tree

haproxy_agent.py

Lines changed: 52 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,66 +1295,68 @@ async def _install_system_certificates(
12951295
os_info_content: str,
12961296
) -> None:
12971297
target_cert_dir = _get_target_cert_dir(os_info_content)
1298-
if target_cert_dir:
1299-
LOGGER.info("Target system cert directory for container '%s': %s", container_name, target_cert_dir)
1298+
if not target_cert_dir:
1299+
LOGGER.warning(
1300+
"OS in container '%s' not supported for sys cert installation, or bundle empty. Skipping.",
1301+
container_name,
1302+
)
1303+
return
1304+
1305+
LOGGER.info("Target system cert directory for container '%s': %s", container_name, target_cert_dir)
1306+
exit_code, raw_output = await _execute_command_in_container_simplified(
1307+
session, docker_engine_port, container_name, ["mkdir", "-p", target_cert_dir]
1308+
)
1309+
if exit_code != 0:
1310+
LOGGER.error(
1311+
"Failed to create cert dir '%s' in container '%s'. Exit: %s, Raw Output: %s",
1312+
target_cert_dir,
1313+
container_name,
1314+
exit_code,
1315+
raw_output,
1316+
)
1317+
raise web.HTTPInternalServerError(
1318+
text=f"Failed to create cert directory. Exit: {exit_code}. Output: {raw_output[:200]}"
1319+
)
1320+
1321+
certs_to_install = {}
1322+
parsed_certs = _parse_certs_from_bundle(system_certs_bundle)
1323+
for i, cert_content in enumerate(parsed_certs):
1324+
cert_filename = f"custom_ca_cert_{i}.crt"
1325+
certs_to_install[os.path.join(target_cert_dir.lstrip("/"), cert_filename)] = cert_content
1326+
1327+
if not certs_to_install:
1328+
LOGGER.info(
1329+
"No individual certificates parsed from system_certs_bundle for container '%s'.",
1330+
container_name,
1331+
)
1332+
return
1333+
1334+
tar_bytes = _create_tar_archive_in_memory(certs_to_install)
1335+
await _put_archive_to_container(session, docker_engine_port, container_name, "/", tar_bytes)
1336+
LOGGER.info(
1337+
"Installed %d system CA certificates into '%s' in container '%s'.",
1338+
len(parsed_certs),
1339+
target_cert_dir,
1340+
container_name,
1341+
)
1342+
1343+
update_cmd_list = _get_certificate_update_command(os_info_content)
1344+
if update_cmd_list:
1345+
LOGGER.info("Running certificate update command: %s", " ".join(update_cmd_list))
13001346
exit_code, raw_output = await _execute_command_in_container_simplified(
1301-
session, docker_engine_port, container_name, ["mkdir", "-p", target_cert_dir]
1347+
session, docker_engine_port, container_name, update_cmd_list
13021348
)
13031349
if exit_code != 0:
13041350
LOGGER.error(
1305-
"Failed to create cert dir '%s' in container '%s'. Exit: %s, Raw Output: %s",
1306-
target_cert_dir,
1351+
"Certificate update command failed in container '%s'. Exit: %s, Raw Output: %s",
13071352
container_name,
13081353
exit_code,
13091354
raw_output,
13101355
)
1311-
raise web.HTTPInternalServerError(
1312-
text=f"Failed to create cert directory. Exit: {exit_code}. Output: {raw_output[:200]}"
1313-
)
1314-
1315-
certs_to_install = {}
1316-
parsed_certs = _parse_certs_from_bundle(system_certs_bundle)
1317-
for i, cert_content in enumerate(parsed_certs):
1318-
cert_filename = f"custom_ca_cert_{i}.crt"
1319-
certs_to_install[os.path.join(target_cert_dir.lstrip("/"), cert_filename)] = cert_content
1320-
1321-
if certs_to_install:
1322-
tar_bytes = _create_tar_archive_in_memory(certs_to_install)
1323-
await _put_archive_to_container(session, docker_engine_port, container_name, "/", tar_bytes)
1324-
LOGGER.info(
1325-
"Installed %d system CA certificates into '%s' in container '%s'.",
1326-
len(parsed_certs),
1327-
target_cert_dir,
1328-
container_name,
1329-
)
1330-
1331-
update_cmd_list = _get_certificate_update_command(os_info_content)
1332-
if update_cmd_list:
1333-
LOGGER.info("Running certificate update command: %s", " ".join(update_cmd_list))
1334-
exit_code, raw_output = await _execute_command_in_container_simplified(
1335-
session, docker_engine_port, container_name, update_cmd_list
1336-
)
1337-
if exit_code != 0:
1338-
LOGGER.error(
1339-
"Certificate update command failed in container '%s'. Exit: %s, Raw Output: %s",
1340-
container_name,
1341-
exit_code,
1342-
raw_output,
1343-
)
1344-
else:
1345-
LOGGER.info("Certificate update command successful. Raw Output: %s", raw_output.strip())
1346-
else:
1347-
LOGGER.warning("No certificate update command found for OS in container '%s'.", container_name)
13481356
else:
1349-
LOGGER.info(
1350-
"No individual certificates parsed from system_certs_bundle for container '%s'.",
1351-
container_name,
1352-
)
1357+
LOGGER.info("Certificate update command successful. Raw Output: %s", raw_output.strip())
13531358
else:
1354-
LOGGER.warning(
1355-
"OS in container '%s' not supported for sys cert installation, or bundle empty. Skipping.",
1356-
container_name,
1357-
)
1359+
LOGGER.warning("No certificate update command found for OS in container '%s'.", container_name)
13581360

13591361

13601362
async def _install_frp_certificates(

0 commit comments

Comments
 (0)