From 01e77f4cb263497f543b7dbc7730decd7e6808ca Mon Sep 17 00:00:00 2001 From: Javier Sánchez Parra Date: Mon, 7 Oct 2019 14:00:12 +0200 Subject: #915 Add POST "image/create/basic" command to REST API in ogAdmServer This patch implements the command "image/create/basic" that provides partitioning and formatting functionality. Request: POST /image/create/basic { "clients":[ "192.168.56.11" ], "disk":"1", "partition":"1", "code":"131", "id":"8", "name":"debianbasica", "repository":"192.168.56.10", "sync_params":{ "sync":"1", "diff":"0", "remove":"1", "compress":"0", "cleanup":"0", "cache":"0", "cleanup_cache":"0", "remove_dst":"0" } } Reply: 200 OK --- sources/ogAdmServer.cpp | 108 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) (limited to 'sources') diff --git a/sources/ogAdmServer.cpp b/sources/ogAdmServer.cpp index da33988..386a466 100644 --- a/sources/ogAdmServer.cpp +++ b/sources/ogAdmServer.cpp @@ -3250,6 +3250,17 @@ struct og_partition { const char *format; }; +struct og_sync_params { + const char *sync; + const char *diff; + const char *remove; + const char *compress; + const char *cleanup; + const char *cache; + const char *cleanup_cache; + const char *remove_dst; +}; + struct og_msg_params { const char *ips_array[OG_CLIENTS_MAX]; const char *mac_array[OG_CLIENTS_MAX]; @@ -3267,6 +3278,7 @@ struct og_msg_params { const char *cache; const char *cache_size; struct og_partition partition_setup[OG_PARTITION_MAX]; + struct og_sync_params sync_setup; }; static int og_json_parse_clients(json_t *element, struct og_msg_params *params) @@ -3297,6 +3309,36 @@ static int og_json_parse_string(json_t *element, const char **str) return 0; } +static int og_json_parse_sync_params(json_t *element, og_sync_params *params) +{ + const char *key; + json_t *value; + int err = 0; + + json_object_foreach(element, key, value) { + if (!strcmp(key, "sync")) + err = og_json_parse_string(value, ¶ms->sync); + else if (!strcmp(key, "diff")) + err = og_json_parse_string(value, ¶ms->diff); + else if (!strcmp(key, "remove")) + err = og_json_parse_string(value, ¶ms->remove); + else if (!strcmp(key, "compress")) + err = og_json_parse_string(value, ¶ms->compress); + else if (!strcmp(key, "cleanup")) + err = og_json_parse_string(value, ¶ms->cleanup); + else if (!strcmp(key, "cache")) + err = og_json_parse_string(value, ¶ms->cache); + else if (!strcmp(key, "cleanup_cache")) + err = og_json_parse_string(value, ¶ms->cleanup_cache); + else if (!strcmp(key, "remove_dst")) + err = og_json_parse_string(value, ¶ms->remove_dst); + + if (err != 0) + return err; + } + return err; +} + static int og_json_parse_partition(json_t *element, og_partition *part) { const char *key; @@ -4010,6 +4052,62 @@ static int og_cmd_setup_image(json_t *element, struct og_msg_params *params) return 0; } +static int og_cmd_create_basic_image(json_t *element, struct og_msg_params *params) +{ + char buf[4096] = {}; + int err = 0, len; + const char *key; + json_t *value; + TRAMA *msg; + + if (json_typeof(element) != JSON_OBJECT) + return -1; + + json_object_foreach(element, key, value) { + if (!strcmp(key, "clients")) + err = og_json_parse_clients(value, params); + else if (!strcmp(key, "disk")) + err = og_json_parse_string(value, ¶ms->disk); + else if (!strcmp(key, "partition")) + err = og_json_parse_string(value, ¶ms->partition); + else if (!strcmp(key, "code")) + err = og_json_parse_string(value, ¶ms->code); + else if (!strcmp(key, "id")) + err = og_json_parse_string(value, ¶ms->id); + else if (!strcmp(key, "name")) + err = og_json_parse_string(value, ¶ms->name); + else if (!strcmp(key, "repository")) + err = og_json_parse_string(value, ¶ms->repository); + else if (!strcmp(key, "sync_params")) + err = og_json_parse_sync_params(value, &(params->sync_setup)); + + if (err < 0) + break; + } + + len = snprintf(buf, sizeof(buf), + "nfn=CrearImagenBasica\rdsk=%s\rpar=%s\rcpt=%s\ridi=%s\r" + "nci=%s\ripr=%s\rrti=\rmsy=%s\rwhl=%s\reli=%s\rcmp=%s\rbpi=%s\r" + "cpc=%s\rbpc=%s\rnba=%s\r", + params->disk, params->partition, params->code, params->id, + params->name, params->repository, params->sync_setup.sync, + params->sync_setup.diff, params->sync_setup.remove, + params->sync_setup.compress, params->sync_setup.cleanup, + params->sync_setup.cache, params->sync_setup.cleanup_cache, + params->sync_setup.remove_dst); + + msg = og_msg_alloc(buf, len); + if (!msg) + return -1; + + og_send_cmd((char **)params->ips_array, params->ips_array_len, + CLIENTE_OCUPADO, msg); + + og_msg_free(msg); + + return 0; +} + static int og_client_method_not_found(struct og_client *cli) { /* To meet RFC 7231, this function MUST generate an Allow header field @@ -4232,6 +4330,16 @@ static int og_client_state_process_payload_rest(struct og_client *cli) return og_client_bad_request(cli); } err = og_cmd_software(root, ¶ms); + } else if (!strncmp(cmd, "image/create/basic", + strlen("image/create/basic"))) { + if (method != OG_METHOD_POST) + return og_client_method_not_found(cli); + + if (!root) { + syslog(LOG_ERR, "command create with no payload\n"); + return og_client_bad_request(cli); + } + err = og_cmd_create_basic_image(root, ¶ms); } else if (!strncmp(cmd, "image/create", strlen("image/create"))) { if (method != OG_METHOD_POST) return og_client_method_not_found(cli); -- cgit v1.2.3-18-g5258