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

Add support for install phase execution during deployment #593

Merged
merged 17 commits into from
Oct 31, 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
3 changes: 2 additions & 1 deletion ggdeploymentd/src/component_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,11 @@ bool resolve_component_version(
// TODO: also check that the component region matches the expected region
// (component store functionality)
GGL_LOGI(
"Found local candidate for %s that satisfies version requirements. "
"Found local candidate for %.*s that satisfies version requirements. "
"Using "
"the local candidate as the resolved version "
"without negotiating with the cloud.",
(int) component_name.len,
(char *) component_name.data
);

Expand Down
200 changes: 160 additions & 40 deletions ggdeploymentd/src/deployment_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

#define MAX_RECIPE_BUF_SIZE 256000
#define MAX_DECODE_BUF_LEN 4096
#define MAX_COMP_NAME_BUF_SIZE 10000

static struct DeploymentConfiguration {
char data_endpoint[128];
Expand Down Expand Up @@ -1973,6 +1974,9 @@ static void handle_deployment(
}
GGL_CLEANUP(ggl_free_digest, digest_context);

static GglBuffer comp_name_buf[MAX_COMP_NAME_BUF_SIZE];
GglBufVec comp_name_vec = GGL_BUF_VEC(comp_name_buf);

GGL_MAP_FOREACH(pair, resolved_components_kv_vec.map) {
int component_artifacts_fd = -1;
open_component_artifacts_dir(
Expand Down Expand Up @@ -2234,32 +2238,147 @@ static void handle_deployment(
// TODO: add install file processing logic here.

if (component_updated) {
static uint8_t service_file_path_buf[PATH_MAX];
GglByteVec service_file_path_vec
= GGL_BYTE_VEC(service_file_path_buf);
ret = ggl_byte_vec_append(
&service_file_path_vec, GGL_STR("ggl.")
);
ggl_byte_vec_chain_append(
&ret, &service_file_path_vec, pair->key
);
ggl_byte_vec_chain_append(
&ret, &service_file_path_vec, GGL_STR(".service")
);
ret = ggl_buf_vec_push(&comp_name_vec, pair->key);
if (ret != GGL_ERR_OK) {
GGL_LOGE("Failed to create service file path.");
GGL_LOGE("Failed to add the component name into vector");
return;
}
}
}

if (comp_name_vec.buf_list.len == 0) {
GGL_LOGE("Failed to retrieve any component name");
return;
}

for (size_t i = 0; i < comp_name_vec.buf_list.len; i++) {
// install
static uint8_t install_service_file_path_buf[PATH_MAX];
GglByteVec install_service_file_path_vec
= GGL_BYTE_VEC(install_service_file_path_buf);
ret = ggl_byte_vec_append(
&install_service_file_path_vec, args->root_path
);
ggl_byte_vec_append(&install_service_file_path_vec, GGL_STR("/"));
ggl_byte_vec_append(
&install_service_file_path_vec, GGL_STR("ggl.")
);
ggl_byte_vec_chain_append(
&ret,
&install_service_file_path_vec,
comp_name_vec.buf_list.bufs[i]
);
ggl_byte_vec_chain_append(
&ret,
&install_service_file_path_vec,
GGL_STR(".install.service")
);
if (ret == GGL_ERR_OK) {
// check if the current component name has relevant install
// service file created
int fd = -1;
ret = ggl_file_open(
install_service_file_path_vec.buf, O_RDONLY, 0, &fd
);
if (ret != GGL_ERR_OK) {
GGL_LOGW(
"Component %.*s does not have the relevant install "
"service file",
(int) comp_name_vec.buf_list.bufs[i].len,
comp_name_vec.buf_list.bufs[i].data
);
} else { // relevant install service file exists

// run link command
static uint8_t link_command_buf[PATH_MAX];
GglByteVec link_command_vec
= GGL_BYTE_VEC(link_command_buf);
ret = ggl_byte_vec_append(
&link_command_vec, GGL_STR("sudo systemctl link ")
);
ggl_byte_vec_chain_append(
&ret,
&link_command_vec,
install_service_file_path_vec.buf
);
ggl_byte_vec_chain_push(&ret, &link_command_vec, '\0');
if (ret != GGL_ERR_OK) {
GGL_LOGE("Failed to create systemctl link command.");
return;
}

// NOLINTNEXTLINE(concurrency-mt-unsafe)
int system_ret = system((char *) link_command_vec.buf.data);
if (WIFEXITED(system_ret)) {
if (WEXITSTATUS(system_ret) != 0) {
GGL_LOGE("systemctl link failed");
return;
}
GGL_LOGI(
"systemctl link exited with child status %d\n",
WEXITSTATUS(system_ret)
);
} else {
GGL_LOGE("systemctl link did not exit normally");
return;
}

// run start command
static uint8_t start_command_buf[PATH_MAX];
GglByteVec start_command_vec
= GGL_BYTE_VEC(start_command_buf);
ret = ggl_byte_vec_append(
&start_command_vec, GGL_STR("sudo systemctl start ")
);
ggl_byte_vec_chain_append(
&ret,
&start_command_vec,
install_service_file_path_vec.buf
);
ggl_byte_vec_chain_push(&ret, &start_command_vec, '\0');
if (ret != GGL_ERR_OK) {
GGL_LOGE("Failed to create systemctl start command.");
return;
}

// NOLINTNEXTLINE(concurrency-mt-unsafe)
system_ret = system((char *) start_command_vec.buf.data);
if (WIFEXITED(system_ret)) {
if (WEXITSTATUS(system_ret) != 0) {
GGL_LOGE("systemctl start failed");
return;
}
GGL_LOGI(
"systemctl start exited with child status %d\n",
WEXITSTATUS(system_ret)
);
} else {
GGL_LOGE("systemctl start did not exit normally");
return;
}
}
}

// run or startup
static uint8_t service_file_path_buf[PATH_MAX];
GglByteVec service_file_path_vec
= GGL_BYTE_VEC(service_file_path_buf);
ret = ggl_byte_vec_append(&service_file_path_vec, args->root_path);
ggl_byte_vec_append(&service_file_path_vec, GGL_STR("/"));
ggl_byte_vec_append(&service_file_path_vec, GGL_STR("ggl."));
ggl_byte_vec_chain_append(
&ret, &service_file_path_vec, comp_name_vec.buf_list.bufs[i]
);
ggl_byte_vec_chain_append(
&ret, &service_file_path_vec, GGL_STR(".service")
);
if (ret == GGL_ERR_OK) {
// run link command
static uint8_t link_command_buf[PATH_MAX];
GglByteVec link_command_vec = GGL_BYTE_VEC(link_command_buf);
ret = ggl_byte_vec_append(
&link_command_vec, GGL_STR("sudo systemctl link ")
);
ggl_byte_vec_chain_append(
&ret, &link_command_vec, args->root_path
);
ggl_byte_vec_chain_push(&ret, &link_command_vec, '/');
ggl_byte_vec_chain_append(
&ret, &link_command_vec, service_file_path_vec.buf
);
Expand All @@ -2285,64 +2404,65 @@ static void handle_deployment(
return;
}

static uint8_t start_command_buf[PATH_MAX];
GglByteVec start_command_vec = GGL_BYTE_VEC(start_command_buf);
// run enable command
static uint8_t enable_command_buf[PATH_MAX];
GglByteVec enable_command_vec
= GGL_BYTE_VEC(enable_command_buf);
ret = ggl_byte_vec_append(
&start_command_vec, GGL_STR("sudo systemctl start ")
&enable_command_vec, GGL_STR("sudo systemctl enable ")
);
ggl_byte_vec_chain_append(
&ret, &start_command_vec, service_file_path_vec.buf
&ret, &enable_command_vec, service_file_path_vec.buf
);
ggl_byte_vec_chain_push(&ret, &start_command_vec, '\0');
ggl_byte_vec_chain_push(&ret, &enable_command_vec, '\0');
if (ret != GGL_ERR_OK) {
GGL_LOGE("Failed to create systemctl start command.");
GGL_LOGE("Failed to create systemctl enable command.");
return;
}

// NOLINTNEXTLINE(concurrency-mt-unsafe)
system_ret = system((char *) start_command_vec.buf.data);
system_ret = system((char *) enable_command_vec.buf.data);
if (WIFEXITED(system_ret)) {
if (WEXITSTATUS(system_ret) != 0) {
GGL_LOGE("systemctl start failed");
GGL_LOGE("systemctl enable failed");
return;
}
GGL_LOGI(
"systemctl start exited with child status %d\n",
"systemctl enable exited with child status %d\n",
WEXITSTATUS(system_ret)
);
} else {
GGL_LOGE("systemctl start did not exit normally");
GGL_LOGE("systemctl enable did not exit normally");
return;
}

static uint8_t enable_command_buf[PATH_MAX];
GglByteVec enable_command_vec
= GGL_BYTE_VEC(enable_command_buf);
// run daemon-reload command
static uint8_t reload_command_buf[PATH_MAX];
GglByteVec reload_command_vec
= GGL_BYTE_VEC(reload_command_buf);
ret = ggl_byte_vec_append(
&enable_command_vec, GGL_STR("sudo systemctl enable ")
&reload_command_vec,
GGL_STR("sudo systemctl daemon-reload\0")
);
ggl_byte_vec_chain_append(
&ret, &enable_command_vec, service_file_path_vec.buf
);
ggl_byte_vec_chain_push(&ret, &enable_command_vec, '\0');
if (ret != GGL_ERR_OK) {
GGL_LOGE("Failed to create systemctl enable command.");
GGL_LOGE("Failed to create systemctl daemon-reload command."
);
return;
}

// NOLINTNEXTLINE(concurrency-mt-unsafe)
system_ret = system((char *) enable_command_vec.buf.data);
system_ret = system((char *) reload_command_vec.buf.data);
if (WIFEXITED(system_ret)) {
if (WEXITSTATUS(system_ret) != 0) {
GGL_LOGE("systemctl enable failed");
GGL_LOGE("systemctl daemon-reload failed");
return;
}
GGL_LOGI(
"systemctl enable exited with child status %d\n",
"systemctl daemon-reload exited with child status %d\n",
WEXITSTATUS(system_ret)
);
} else {
GGL_LOGE("systemctl enable did not exit normally");
GGL_LOGE("systemctl daemon-reload did not exit normally");
return;
}
}
Expand Down
15 changes: 7 additions & 8 deletions ggl-recipe/src/recipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ static GglError manifest_selection(

// Check if the current OS supported first
if ((strncmp((char *) os->buf.data, "linux", os->buf.len) == 0
|| strncmp((char *) os->buf.data, "*", os->buf.len) == 0)) {
|| strncmp((char *) os->buf.data, "*", os->buf.len) == 0)
|| strncmp((char *) os->buf.data, "all", os->buf.len) == 0) {
// Then check if architecture is also supported
if (((architecture_obj == NULL)
|| (architecture_obj->buf.len == 0)
Expand Down Expand Up @@ -202,13 +203,11 @@ GglError select_linux_manifest(
return ret;
}

if (selected_lifecycle_object == NULL) {
GGL_LOGE("No lifecycle was found for linux");
return GGL_ERR_FAILURE;
}
// If a lifecycle is successfully selected then look no futher
if (selected_lifecycle_object->type == GGL_TYPE_MAP) {
break;
if (selected_lifecycle_object != NULL) {
// If a lifecycle is successfully selected then look no futher
if (selected_lifecycle_object->type == GGL_TYPE_MAP) {
break;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion recipe2unit-test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

ggl_init_module(recipe2unit-test LIBS ggl-lib recipe2unit)
ggl_init_module(recipe2unit-test LIBS ggl-file ggl-lib recipe2unit)
18 changes: 17 additions & 1 deletion recipe2unit-test/src/entry.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,36 @@

#include "ggl/recipe2unit.h"
#include "recipe2unit-test.h"
#include <fcntl.h>
#include <ggl/buffer.h>
#include <ggl/bump_alloc.h>
#include <ggl/error.h>
#include <ggl/file.h>
#include <ggl/log.h>
#include <ggl/object.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>

// For the testing purpose, move the sample recipe.yml to /run/packages/recipes
// and rename it to recipe-1.0.0.yml

GglError run_recipe2unit_test(void) {
static Recipe2UnitArgs args = { 0 };
char component_name[] = "recipe.yml";
char component_name[] = "recipe";
char version[] = "1.0.0";
char root_dir[] = ".";
char recipe_runner_path[] = "/home/reciperunner";

int root_path_fd;
GglError ret
= ggl_dir_open(GGL_STR(root_dir), O_PATH, false, &root_path_fd);
if (ret != GGL_ERR_OK) {
GGL_LOGE("Failed to open root dir.");
return ret;
}
args.root_path_fd = root_path_fd;

GglBuffer component_name_buff
= (GglBuffer) { (uint8_t *) component_name, strlen(component_name) };
GglBuffer version_buff
Expand Down
Loading