From 479998d76f805c02e041adb209857d4ca6b3f87f Mon Sep 17 00:00:00 2001 From: OpenGnSys Support Team Date: Thu, 31 Oct 2024 00:27:57 +0100 Subject: src: add last client command context and use it for image creation Add last client command context that is used in the request to client. This allows to remove the stub image entry in the database in case that the image creation fails. --- src/client.c | 37 ++++++++++++------------------------- src/core.c | 2 +- src/rest.c | 33 +++++++++++++++++++-------------- src/rest.h | 11 ++++++++++- 4 files changed, 42 insertions(+), 41 deletions(-) (limited to 'src') diff --git a/src/client.c b/src/client.c index cdc7b6b..2480768 100644 --- a/src/client.c +++ b/src/client.c @@ -1290,11 +1290,9 @@ static int og_agent_http_response_code(const char *buf) return -1; } -static int og_clear_image_placeholder() +static int og_resp_image_create_error(struct og_client *cli) { - uint32_t image_id = 0; struct og_dbi *dbi; - dbi_result result; dbi = og_dbi_open(&ogconfig.db); if (!dbi) { @@ -1303,26 +1301,9 @@ static int og_clear_image_placeholder() return -1; } - result = dbi_conn_queryf(dbi->conn, - "SELECT idimagen FROM imagenes WHERE fechacreacion IS NULL"); - if (!result) { - syslog(LOG_ERR, "failed to query database\n"); - return -1; - } - - if (!dbi_result_next_row(result)) { - dbi_result_free(result); - og_dbi_close(dbi); - return 0; - } - - image_id = dbi_result_get_uint(result, "idimagen"); - dbi_result_free(result); - - syslog(LOG_INFO, "Trying to delete uninitialized image with id %d\n", image_id); - - if (og_dbi_delete_image(dbi, image_id) < 0) { - og_dbi_close(dbi); + if (og_dbi_delete_image(dbi, cli->last_cmd.ctx.image.id) < 0) { + syslog(LOG_WARNING, "Cannot delete image stub with id %d\n", + cli->last_cmd.ctx.image.id); return -1; } @@ -1334,6 +1315,7 @@ static void og_client_reset_cmd(struct og_client *cli) { cli->last_cmd.id = 0; cli->last_cmd.type = OG_CMD_UNSPEC; + memset(&cli->last_cmd.ctx, 0, sizeof(cli->last_cmd.ctx)); } int og_agent_state_process_response(struct og_client *cli) @@ -1389,11 +1371,16 @@ int og_agent_state_process_response(struct og_client *cli) else cli->last_cmd.result = OG_FAILURE; - if (!success && cmd_type == OG_CMD_IMAGE_CREATE) - og_clear_image_placeholder(); + if (!success && cmd_type == OG_CMD_IMAGE_CREATE) { + syslog(LOG_ERR, "Client %s:%hu reports failure when creating image with id %d\n", + inet_ntoa(cli->addr.sin_addr), ntohs(cli->addr.sin_port), + cli->last_cmd.ctx.image.id); + og_resp_image_create_error(cli); + } if (code != 200 && code != 103) { cli->last_cmd.id = 0; + memset(&cli->last_cmd.ctx, 0, sizeof(cli->last_cmd.ctx)); return ret; } diff --git a/src/core.c b/src/core.c index 759e923..02d4280 100644 --- a/src/core.c +++ b/src/core.c @@ -281,7 +281,7 @@ static void og_agent_send_refresh(struct og_client *cli) params.ips_array[0] = inet_ntoa(cli->addr.sin_addr); params.ips_array_len = 1; - og_send_request(OG_METHOD_GET, OG_CMD_REFRESH, ¶ms, NULL); + og_send_request(OG_METHOD_GET, OG_CMD_REFRESH, ¶ms, NULL, NULL); } /* Shut down connection if there is no complete message after 10 seconds. */ diff --git a/src/rest.c b/src/rest.c index 2a8b642..27af4d1 100644 --- a/src/rest.c +++ b/src/rest.c @@ -254,7 +254,7 @@ static bool og_client_is_busy(const struct og_client *cli, int og_send_request(enum og_rest_method method, enum og_cmd_type type, const struct og_msg_params *params, - const json_t *data) + const json_t *data, const struct og_cmd_ctx *ctx) { const char *content_type = "Content-Type: application/json"; char content [OG_MSG_REQUEST_MAXLEN - 700] = {}; @@ -320,6 +320,8 @@ int og_send_request(enum og_rest_method method, enum og_cmd_type type, continue; cli->last_cmd.type = type; + if (ctx) + cli->last_cmd.ctx = *ctx; } json_decref((json_t *)data); @@ -691,7 +693,7 @@ static int og_cmd_run_post(json_t *element, struct og_msg_params *params) clients = json_copy(element); json_object_del(clients, "clients"); - return og_send_request(OG_METHOD_POST, OG_CMD_SHELL_RUN, params, clients); + return og_send_request(OG_METHOD_POST, OG_CMD_SHELL_RUN, params, clients, NULL); } static int og_cmd_run_get(json_t *element, struct og_msg_params *params, @@ -911,7 +913,7 @@ static int og_cmd_session(json_t *element, struct og_msg_params *params) clients = json_copy(element); json_object_del(clients, "clients"); - return og_send_request(OG_METHOD_POST, OG_CMD_SESSION, params, clients); + return og_send_request(OG_METHOD_POST, OG_CMD_SESSION, params, clients, NULL); } static int og_json_os_array_get(struct og_dbi *dbi, json_t *array, const char *ip) @@ -1065,7 +1067,7 @@ static int og_cmd_poweroff(json_t *element, struct og_msg_params *params) if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR)) return -1; - return og_send_request(OG_METHOD_POST, OG_CMD_POWEROFF, params, NULL); + return og_send_request(OG_METHOD_POST, OG_CMD_POWEROFF, params, NULL, NULL); } static int og_cmd_refresh(json_t *element, struct og_msg_params *params) @@ -1088,7 +1090,7 @@ static int og_cmd_refresh(json_t *element, struct og_msg_params *params) if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR)) return -1; - return og_send_request(OG_METHOD_GET, OG_CMD_REFRESH, params, NULL); + return og_send_request(OG_METHOD_GET, OG_CMD_REFRESH, params, NULL, NULL); } static int og_cmd_reboot(json_t *element, struct og_msg_params *params) @@ -1111,7 +1113,7 @@ static int og_cmd_reboot(json_t *element, struct og_msg_params *params) if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR)) return -1; - return og_send_request(OG_METHOD_POST, OG_CMD_REBOOT, params, NULL); + return og_send_request(OG_METHOD_POST, OG_CMD_REBOOT, params, NULL, NULL); } #define OG_TFTP_TMPL_PATH_UEFI "/opt/opengnsys/tftpboot/grub/templates" @@ -3146,7 +3148,7 @@ static int og_cmd_stop(json_t *element, struct og_msg_params *params) if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR)) return -1; - return og_send_request(OG_METHOD_POST, OG_CMD_STOP, params, NULL); + return og_send_request(OG_METHOD_POST, OG_CMD_STOP, params, NULL, NULL); } static int og_cmd_hardware(json_t *element, struct og_msg_params *params) @@ -3169,7 +3171,7 @@ static int og_cmd_hardware(json_t *element, struct og_msg_params *params) if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR)) return -1; - return og_send_request(OG_METHOD_GET, OG_CMD_HARDWARE, params, NULL); + return og_send_request(OG_METHOD_GET, OG_CMD_HARDWARE, params, NULL, NULL); } static int og_cmd_get_hardware(json_t *element, struct og_msg_params *params, @@ -3300,7 +3302,7 @@ static int og_cmd_software(json_t *element, struct og_msg_params *params) clients = json_copy(element); json_object_del(clients, "clients"); - return og_send_request(OG_METHOD_GET, OG_CMD_SOFTWARE, params, clients); + return og_send_request(OG_METHOD_GET, OG_CMD_SOFTWARE, params, clients, NULL); } static int og_cmd_get_software(json_t *element, struct og_msg_params *params, @@ -3682,6 +3684,7 @@ static int og_cmd_add_image(json_t *element, struct og_msg_params *params, { char repository_ip[OG_DB_IP_MAXLEN + 1]; char new_image_id[OG_DB_INT_MAXLEN + 1]; + struct og_cmd_ctx ctx = {}; struct og_dbi *dbi; json_t *body; int err = 0; @@ -3750,8 +3753,10 @@ static int og_cmd_add_image(json_t *element, struct og_msg_params *params, json_object_set_new(body, "repository", json_string(repository_ip)); json_object_set_new(body, "backup", json_boolean(params->backup)); + ctx.image.id = params->image.id; + return og_send_request(OG_METHOD_POST, OG_CMD_IMAGE_CREATE, params, - body); + body, &ctx); } static int og_cmd_create_image(json_t *element, struct og_msg_params *params) @@ -3846,7 +3851,7 @@ static int og_cmd_restore_image(json_t *element, struct og_msg_params *params) json_object_set_new(body, "repository", json_string(repository_ip)); return og_send_request(OG_METHOD_POST, OG_CMD_IMAGE_RESTORE, params, - body); + body, NULL); } static int og_cmd_delete_image(json_t *element, struct og_msg_params *params) @@ -4443,7 +4448,7 @@ static int og_cmd_cache_delete(json_t *element, struct og_msg_params *params, body = json_copy(element); json_object_del(body, "clients"); - return og_send_request(OG_METHOD_POST, OG_CMD_CACHE_DELETE, params, body); + return og_send_request(OG_METHOD_POST, OG_CMD_CACHE_DELETE, params, body, NULL); } static int og_cmd_cache_fetch(json_t *element, struct og_msg_params *params, @@ -4497,7 +4502,7 @@ static int og_cmd_cache_fetch(json_t *element, struct og_msg_params *params, json_object_del(body, "clients"); json_object_set_new(body, "repository", json_string(repository_ip)); - return og_send_request(OG_METHOD_POST, OG_CMD_CACHE_FETCH, params, body); + return og_send_request(OG_METHOD_POST, OG_CMD_CACHE_FETCH, params, body, NULL); } static int og_cmd_setup(json_t *element, struct og_msg_params *params) @@ -4546,7 +4551,7 @@ static int og_cmd_setup(json_t *element, struct og_msg_params *params) clients = json_copy(element); json_object_del(clients, "clients"); - return og_send_request(OG_METHOD_POST, OG_CMD_SETUP, params, clients); + return og_send_request(OG_METHOD_POST, OG_CMD_SETUP, params, clients, NULL); } static int og_cmd_scope_get(json_t *element, struct og_msg_params *params, diff --git a/src/rest.h b/src/rest.h index ff1486b..be5b17a 100644 --- a/src/rest.h +++ b/src/rest.h @@ -51,6 +51,14 @@ enum og_cmd_result { OG_SUCCESS = 2, }; +struct og_cmd_ctx { + union { + struct { + uint32_t id; + } image; + }; +}; + #define OG_MSG_REQUEST_MAXLEN 131072 struct og_client { @@ -70,6 +78,7 @@ struct og_client { enum og_cmd_type type; unsigned int id; enum og_cmd_result result; + struct og_cmd_ctx ctx; } last_cmd; uint32_t speed; uint32_t seq; @@ -159,7 +168,7 @@ enum og_rest_method { int og_send_request(enum og_rest_method method, enum og_cmd_type type, const struct og_msg_params *params, - const json_t *data); + const json_t *data, const struct og_cmd_ctx *ctx); int og_dbi_scope_get(struct og_dbi *dbi, json_t *array); -- cgit v1.2.3-18-g5258