diff --git a/ggdeploymentd/src/deployment_handler.c b/ggdeploymentd/src/deployment_handler.c index 846b6a63e..4fe79bf5b 100644 --- a/ggdeploymentd/src/deployment_handler.c +++ b/ggdeploymentd/src/deployment_handler.c @@ -1996,7 +1996,7 @@ 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); + GglBufVec updated_comp_name_vec = GGL_BUF_VEC(comp_name_buf); GGL_MAP_FOREACH(pair, resolved_components_kv_vec.map) { int component_artifacts_fd = -1; @@ -2259,7 +2259,7 @@ static void handle_deployment( // TODO: add install file processing logic here. if (component_updated) { - ret = ggl_buf_vec_push(&comp_name_vec, pair->key); + ret = ggl_buf_vec_push(&updated_comp_name_vec, pair->key); if (ret != GGL_ERR_OK) { GGL_LOGE("Failed to add the component name into vector"); return; @@ -2267,49 +2267,141 @@ static void handle_deployment( } } - 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 (updated_comp_name_vec.buf_list.len != 0) { + // process all install files first + for (size_t i = 0; i < updated_comp_name_vec.buf_list.len; i++) { + 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 ); - 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 + 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, + updated_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 ); - } else { // relevant install service file exists + if (ret != GGL_ERR_OK) { + GGL_LOGW( + "Component %.*s does not have the relevant install " + "service file", + (int) updated_comp_name_vec.buf_list.bufs[i].len, + updated_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; + } + // NOLINTBEGIN(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; + } + + system_ret + = system((char *) start_command_vec.buf.data); + // NOLINTEND(concurrency-mt-unsafe) + 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; + } + } + } + } + + // process all run or startup files after install only + for (size_t i = 0; i < updated_comp_name_vec.buf_list.len; i++) { + 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, + updated_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 @@ -2318,9 +2410,7 @@ static void handle_deployment( &link_command_vec, GGL_STR("sudo systemctl link ") ); ggl_byte_vec_chain_append( - &ret, - &link_command_vec, - install_service_file_path_vec.buf + &ret, &link_command_vec, service_file_path_vec.buf ); ggl_byte_vec_chain_push(&ret, &link_command_vec, '\0'); if (ret != GGL_ERR_OK) { @@ -2332,159 +2422,84 @@ static void handle_deployment( int system_ret = system((char *) link_command_vec.buf.data); if (WIFEXITED(system_ret)) { if (WEXITSTATUS(system_ret) != 0) { - GGL_LOGE("systemctl link failed"); + GGL_LOGE("sudo systemctl link command failed"); return; } GGL_LOGI( - "systemctl link exited with child status %d\n", + "sudo systemctl link exited with child status %d\n", WEXITSTATUS(system_ret) ); } else { - GGL_LOGE("systemctl link did not exit normally"); + GGL_LOGE("sudo 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); + // 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, - install_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("sudo systemctl enable failed"); return; } GGL_LOGI( - "systemctl start exited with child status %d\n", + "sudo systemctl enable exited with child status " + "%d\n", WEXITSTATUS(system_ret) ); } else { - GGL_LOGE("systemctl start did not exit normally"); + GGL_LOGE("sudo systemctl enable 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, 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) + // 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( + &reload_command_vec, + GGL_STR("sudo systemctl daemon-reload\0") ); - } else { - GGL_LOGE("systemctl link did not exit normally"); - return; - } - - // 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( - &enable_command_vec, GGL_STR("sudo systemctl enable ") - ); - 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."); - return; - } - - // NOLINTNEXTLINE(concurrency-mt-unsafe) - system_ret = system((char *) enable_command_vec.buf.data); - if (WIFEXITED(system_ret)) { - if (WEXITSTATUS(system_ret) != 0) { - GGL_LOGE("systemctl enable failed"); + if (ret != GGL_ERR_OK) { + GGL_LOGE("Failed to create sudo systemctl " + "daemon-reload command."); return; } - GGL_LOGI( - "systemctl enable exited with child status %d\n", - WEXITSTATUS(system_ret) - ); - } else { - GGL_LOGE("systemctl enable did not exit normally"); - return; - } - - // 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( - &reload_command_vec, - GGL_STR("sudo systemctl daemon-reload\0") - ); - if (ret != GGL_ERR_OK) { - GGL_LOGE("Failed to create systemctl daemon-reload command." - ); - return; - } - // NOLINTNEXTLINE(concurrency-mt-unsafe) - system_ret = system((char *) reload_command_vec.buf.data); - if (WIFEXITED(system_ret)) { - if (WEXITSTATUS(system_ret) != 0) { - GGL_LOGE("systemctl daemon-reload failed"); + // NOLINTNEXTLINE(concurrency-mt-unsafe) + system_ret = system((char *) reload_command_vec.buf.data); + if (WIFEXITED(system_ret)) { + if (WEXITSTATUS(system_ret) != 0) { + GGL_LOGE("sudo systemctl daemon-reload failed"); + return; + } + GGL_LOGI( + "sudo systemctl daemon-reload exited with child " + "status " + "%d\n", + WEXITSTATUS(system_ret) + ); + } else { + GGL_LOGE( + "sudo systemctl daemon-reload did not exit normally" + ); return; } - GGL_LOGI( - "systemctl daemon-reload exited with child status %d\n", - WEXITSTATUS(system_ret) - ); - } else { - GGL_LOGE("systemctl daemon-reload did not exit normally"); - return; } } }