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

Core components as units #603

Merged
merged 3 commits into from
Nov 20, 2024
Merged
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
44 changes: 43 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ set(GGL_LOG_LEVEL CACHE STRING "GGL log level")

option(GGL_IPC_AUTH_DISABLE "Put IPC authentication in debug mode")

option(GGL_SYSTEMD_SERVICE_BUILD "Install GGL as a set of systemd services"
TRUE)
set(GGL_SYSTEMD_SYSTEM_DIR
"lib/systemd/system"
CACHE STRING "Install directory for systemd unit files")
set(GGL_SYSTEMD_SYSTEM_USER
"ggcore"
CACHE STRING "Core service user")
set(GGL_SYSTEMD_SYSTEM_GROUP
"ggcore"
CACHE STRING "Core service group")

#
# Misc
#
Expand Down Expand Up @@ -269,10 +281,14 @@ check_c_source_compiles(

# Common setup for a GGL module
function(ggl_init_module name)
cmake_parse_arguments(PARSE_ARGV 1 COMP_ARG "" "SRCDIR" "INCDIRS;LIBS")
cmake_parse_arguments(PARSE_ARGV 1 COMP_ARG "" "SRCDIR;SOCKETDIR"
"INCDIRS;LIBS;SOCKETS")
if("${COMP_ARG_SRCDIR}" STREQUAL "")
set(COMP_ARG_SRCDIR src)
endif()
if("${COMP_ARG_SOCKETDIR}" STREQUAL "")
set(COMP_ARG_SOCKETDIR /run/greengrass)
endif()
if("${COMP_ARG_INCDIRS}" STREQUAL "")
set(COMP_ARG_INCDIRS include)
endif()
Expand All @@ -298,6 +314,22 @@ function(ggl_init_module name)
target_link_libraries(${name} PRIVATE ${COMP_ARG_LIBS})
endif()

if(GGL_SYSTEMD_SERVICE_BUILD)
foreach(socket ${COMP_ARG_SOCKETS})
configure_file(${CMAKE_SOURCE_DIR}/misc/systemd/template.socket.in
${CMAKE_CURRENT_BINARY_DIR}/ggl.${socket}.socket @ONLY)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/ggl.${socket}.socket
DESTINATION ${GGL_SYSTEMD_SYSTEM_DIR})
endforeach()

if(EXISTS ${CMAKE_CURRENT_LIST_DIR}/unit/template.service.in)
configure_file(${CMAKE_CURRENT_LIST_DIR}/unit/template.service.in
${CMAKE_CURRENT_BINARY_DIR}/ggl.core.${name}.service @ONLY)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/ggl.core.${name}.service
DESTINATION ${GGL_SYSTEMD_SYSTEM_DIR})
endif()
endif()

file(GLOB BINS CONFIGURE_DEPENDS "bin/*.c")
foreach(bin ${BINS})
get_filename_component(bin_name ${bin} NAME_WE)
Expand Down Expand Up @@ -357,6 +389,16 @@ add_subdirectory(ggl-constants)
add_subdirectory(cloud-logger)
add_subdirectory(hello-world-component)

if(GGL_SYSTEMD_SERVICE_BUILD)
add_custom_target(
greengrass-lite.target ALL
COMMAND cp "${CMAKE_SOURCE_DIR}/misc/systemd/greengrass-lite.target"
"${CMAKE_BINARY_DIR}/misc/greengrass-lite.target"
COMMENT "Nucleus systemd target")
install(FILES "${CMAKE_BINARY_DIR}/misc/greengrass-lite.target"
DESTINATION ${GGL_SYSTEMD_SYSTEM_DIR})
endif()

if(BUILD_EXAMPLES)
add_subdirectory(samples/echo-server)
add_subdirectory(samples/example-client)
Expand Down
3 changes: 3 additions & 0 deletions core-bus/src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ GglError ggl_call(

GglBuffer recv_buffer = GGL_BUF(ggl_core_bus_client_payload_array);
EventStreamMessage msg = { 0 };
GGL_LOGT(
"Waiting for response from %.*s.", (int) interface.len, interface.data
);
ret = ggl_client_get_response(
ggl_socket_reader(&conn), recv_buffer, error, &msg
);
Expand Down
3 changes: 3 additions & 0 deletions core-bus/src/client_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ GglError ggl_client_send_message(
int *conn_fd
) {
int conn = -1;
GGL_LOGT("Connecting to %.*s.", (int) interface.len, interface.data);
GglError ret = interface_connect(interface, &conn);
if (ret != GGL_ERR_OK) {
return ret;
Expand All @@ -81,6 +82,8 @@ GglError ggl_client_send_message(
return ret;
}

GGL_LOGT("Writing data to %.*s.", (int) interface.len, interface.data);

ret = ggl_socket_write(conn, send_buffer);
if (ret != GGL_ERR_OK) {
return ret;
Expand Down
2 changes: 1 addition & 1 deletion core-bus/src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ GglError ggl_listen(
InterfaceCtx ctx = { .handlers = handlers, .handlers_len = handlers_len };

return ggl_socket_server_listen(
socket_path.buf, 0700, &pool, client_ready, &ctx
&interface, socket_path.buf, 0700, &pool, client_ready, &ctx
);
}

Expand Down
3 changes: 2 additions & 1 deletion core-bus/src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
#define GGL_INTERFACE_NAME_MAX_LEN 50

/// Socket path prefix for core bus sockets.
#define GGL_INTERFACE_SOCKET_PREFIX "/tmp/greengrass/"
#define GGL_INTERFACE_SOCKET_PREFIX "/run/greengrass/"

/// Length of socket path prefix for core bus sockets.
#define GGL_INTERFACE_SOCKET_PREFIX_LEN \
(sizeof(GGL_INTERFACE_SOCKET_PREFIX) - 1)
Expand Down
11 changes: 9 additions & 2 deletions gg-fleet-statusd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,12 @@
# SPDX-License-Identifier: Apache-2.0

ggl_init_module(
gg-fleet-statusd LIBS ggl-lib ggl-json core-bus core-bus-gg-config
core-bus-aws-iot-mqtt ggl-constants)
gg-fleet-statusd
LIBS ggl-lib
ggl-json
core-bus
core-bus-gg-config
core-bus-aws-iot-mqtt
ggl-constants
SOCKETS
gg_fleet_status)
5 changes: 2 additions & 3 deletions gg-fleet-statusd/src/bus_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,8 @@ void gg_fleet_statusd_start_server(void) {
NULL } };
size_t handlers_len = sizeof(handlers) / sizeof(handlers[0]);

GglError ret = ggl_listen(
GGL_STR("/aws/ggl/gg-fleet-statusd"), handlers, handlers_len
);
GglError ret
= ggl_listen(GGL_STR("gg_fleet_status"), handlers, handlers_len);

GGL_LOGE("Exiting with error %u.", (unsigned) ret);
}
2 changes: 1 addition & 1 deletion gg-fleet-statusd/src/fleet_status_service.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ static GglError retrieve_component_health_status(
GglObject result = GGL_OBJ_NULL();
GglError method_error = GGL_ERR_OK;
GglError call_error = ggl_call(
GGL_STR("/aws/ggl/gghealthd"),
GGL_STR("gg_health"),
GGL_STR("get_status"),
GGL_MAP({ GGL_STR("component_name"), GGL_OBJ_BUF(component) }),
&method_error,
Expand Down
27 changes: 27 additions & 0 deletions gg-fleet-statusd/unit/template.service.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[Unit]
StartLimitInterval=20
StartLimitBurst=10
PartOf=greengrass-lite.target

[Install]
WantedBy=greengrass-lite.target

[Service]
Type=idle
ExecStart=@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_BINDIR@/@name@
Restart=always
RestartSec=1
# Disallow from having overly-permissive capabilities
CapabilityBoundingSet=~CAP_SYS_ADMIN ~CAP_SYS_PTRACE
User=@GGL_SYSTEMD_SYSTEM_USER@
Group=@GGL_SYSTEMD_SYSTEM_GROUP@
# Working directory in StateDirectory (/var/lib/greengrass)
StateDirectory=greengrass
WorkingDirectory=%S/greengrass

[Unit]
Description=Core-bus daemon. Sends fleet status updates periodically and when signalled.
After=ggl.core.ggconfigd.service
After=ggl.core.gghealthd.service
After=ggl.core.iotcored.service
After=network-online.target
2 changes: 1 addition & 1 deletion ggconfigd-test/src/entry.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ GglError run_ggconfigd_test(void) {

GglObject result;
ret = ggl_call(
GGL_STR("/aws/ggl/ggdeploymentd"),
GGL_STR("gg_deployment"),
GGL_STR("create_local_deployment"),
args.map,
NULL,
Expand Down
4 changes: 3 additions & 1 deletion ggconfigd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ ggl_init_module(
ggl-yaml
ggl-constants
ggl-json
PkgConfig::sqlite3)
PkgConfig::sqlite3
SOCKETS
gg_config)
target_compile_definitions(ggconfigd
PRIVATE "GGL_COMP_DIR=${CMAKE_CURRENT_LIST_DIR}")
target_compile_options(ggconfigd PRIVATE $<$<COMPILE_LANGUAGE:ASM>:-undef>)
24 changes: 24 additions & 0 deletions ggconfigd/unit/template.service.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[Unit]
StartLimitInterval=20
StartLimitBurst=10
PartOf=greengrass-lite.target

[Install]
WantedBy=greengrass-lite.target

[Service]
Type=exec
ExecStart=@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_BINDIR@/@name@
Restart=always
RestartSec=1
# Disallow from having overly-permissive capabilities
CapabilityBoundingSet=~CAP_SYS_ADMIN ~CAP_SYS_PTRACE
User=@GGL_SYSTEMD_SYSTEM_USER@
Group=@GGL_SYSTEMD_SYSTEM_GROUP@
# Working directory in StateDirectory (/var/lib/greengrass)
StateDirectory=greengrass
WorkingDirectory=%S/greengrass


[Unit]
Description=core-bus config interface daemon
4 changes: 3 additions & 1 deletion ggdeploymentd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ ggl_init_module(
core-bus-aws-iot-mqtt
core-bus-sub-response
recipe2unit
PkgConfig::uuid)
PkgConfig::uuid
SOCKETS
gg_deployment)
3 changes: 1 addition & 2 deletions ggdeploymentd/src/bus_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ void ggdeploymentd_start_server(void) {
NULL } };
size_t handlers_len = sizeof(handlers) / sizeof(handlers[0]);

GglError ret
= ggl_listen(GGL_STR("/aws/ggl/ggdeploymentd"), handlers, handlers_len);
GglError ret = ggl_listen(GGL_STR("gg_deployment"), handlers, handlers_len);

GGL_LOGE("Exiting with error %u.", (unsigned) ret);
}
14 changes: 10 additions & 4 deletions ggdeploymentd/src/deployment_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ static GglError get_tes_credentials(TesCredentials *tes_creds) {
GglObject *aws_session_token = NULL;

static uint8_t credentials_alloc[1500];
static GglBuffer tesd = GGL_STR("/aws/ggl/tesd");
static GglBuffer tesd = GGL_STR("aws_iot_tes");
GglObject result;
GglMap params = { 0 };
GglBumpAlloc credential_alloc
Expand Down Expand Up @@ -1732,7 +1732,7 @@ static GglError add_arn_list_to_config(
}

static GglError send_fss_update(GglBuffer trigger) {
GglBuffer server = GGL_STR("/aws/ggl/gg-fleet-statusd");
GglBuffer server = GGL_STR("gg_fleet_status");
static uint8_t buffer[10 * sizeof(GglObject)] = { 0 };

GglMap args = GGL_MAP({ GGL_STR("trigger"), GGL_OBJ_BUF(trigger) });
Expand Down Expand Up @@ -1853,7 +1853,7 @@ static GglError wait_for_deployment_status(GglMap resolved_components) {
component->key.data
);
GglError ret = ggl_sub_response(
GGL_STR("/aws/ggl/gghealthd"),
GGL_STR("gg_health"),
GGL_STR("subscribe_to_lifecycle_completion"),
GGL_MAP({ GGL_STR("component_name"), GGL_OBJ_BUF(component->key) }),
deployment_status_callback,
Expand Down Expand Up @@ -2456,7 +2456,6 @@ static void handle_deployment(
"daemon-reload command.");
return;
}

// NOLINTNEXTLINE(concurrency-mt-unsafe)
int system_ret = system((char *) reload_command_vec.buf.data);
if (WIFEXITED(system_ret)) {
Expand All @@ -2476,6 +2475,13 @@ static void handle_deployment(
}
}

// NOLINTNEXTLINE(concurrency-mt-unsafe)
int system_ret = system("sudo systemctl reset-failed");
(void) (system_ret);
// NOLINTNEXTLINE(concurrency-mt-unsafe)
system_ret = system("sudo systemctl start greengrass-lite.target");
(void) (system_ret);

ret = wait_for_deployment_status(resolved_components_kv_vec.map);
if (ret != GGL_ERR_OK) {
return;
Expand Down
27 changes: 27 additions & 0 deletions ggdeploymentd/unit/template.service.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[Unit]
StartLimitInterval=20
StartLimitBurst=10
PartOf=greengrass-lite.target

[Install]
WantedBy=greengrass-lite.target

[Service]
Type=exec
ExecStart=@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_BINDIR@/@name@
Restart=always
RestartSec=1
User=@GGL_SYSTEMD_SYSTEM_USER@
Group=@GGL_SYSTEMD_SYSTEM_GROUP@
# Disallow from having overly-permissive capabilities
CapabilityBoundingSet=~CAP_SYS_PTRACE
# Working directory in StateDirectory (/var/lib/greengrass)
StateDirectory=greengrass
WorkingDirectory=%S/greengrass

[Unit]
Description=Greengrass Lite deployment queue and processor
After=ggl.core.ggconfigd.service
After=ggl.core.iotcored.service
After=ggl.core.tesd.service
After=network.target
11 changes: 9 additions & 2 deletions gghealthd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,12 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

ggl_init_module(gghealthd LIBS ggl-lib ggl-file core-bus core-bus-gg-config
PkgConfig::libsystemd)
ggl_init_module(
gghealthd
LIBS ggl-lib
ggl-file
core-bus
core-bus-gg-config
PkgConfig::libsystemd
SOCKETS
gg_health)
2 changes: 1 addition & 1 deletion gghealthd/src/bus_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ GglError run_gghealthd(void) {
NULL } };
static const size_t HANDLERS_LEN = sizeof(handlers) / sizeof(handlers[0]);

ggl_listen(GGL_STR("/aws/ggl/gghealthd"), handlers, HANDLERS_LEN);
ggl_listen(GGL_STR("gg_health"), handlers, HANDLERS_LEN);

return GGL_ERR_OK;
}
28 changes: 28 additions & 0 deletions gghealthd/unit/template.service.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[Unit]
StartLimitInterval=20
StartLimitBurst=10
PartOf=greengrass-lite.target

[Install]
WantedBy=greengrass-lite.target

[Service]
Type=exec
ExecStart=@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_BINDIR@/@name@
Restart=always
RestartSec=1
# Disallow from having overly-permissive capabilities
CapabilityBoundingSet=~CAP_SYS_ADMIN ~CAP_SYS_PTRACE
User=@GGL_SYSTEMD_SYSTEM_USER@
Group=@GGL_SYSTEMD_SYSTEM_GROUP@
# Working directory in StateDirectory (/var/lib/greengrass)
StateDirectory=greengrass
WorkingDirectory=%S/greengrass

[Unit]
Description=core-bus abstract orchestrator interface
After=ggl.core.ggconfigd.service

# Allow gghealthd to change other service's active states
[Service]
NotifyAccess=all
6 changes: 5 additions & 1 deletion ggipcd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ ggl_init_module(
core-bus-gg-config
core-bus-aws-iot-mqtt
ggipc-auth
ggl-rand)
ggl-rand
SOCKETS
gg-ipc.socket
SOCKETDIR
/var/lib/greengrass)
Loading