From dbcc83daa56b3cba415acff079809b3e77ae069c Mon Sep 17 00:00:00 2001 From: Roberto Hueso Gómez Date: Mon, 7 Oct 2019 10:47:42 +0200 Subject: #915 Add POST "image/setup" command to REST API in ogAdmServer This patch implements the command "image/setup" that provides partitioning and formatting functionality. Request: POST /image/setup { "clients" : [ "192.168.56.11" ], "disk" : "1", "cache" : "1", "cache_size" : "0", "partition_setup": [{"partition": "1", "code": "LINUX", "filesystem": "EMPTY", "size": "498688", "format": "0"}, {"partition": "2", "code": "LINUX-SWAP", "filesystem": "EMPTY", "size": "199987", "format": "0"}, {"partition": "3", "code": "LINUX", "filesystem": "EMPTY", "size": "31053824", "format": "0"}, {"partition": "4", "code": "EMPTY", "filesystem": "EMPTY", "size": "0", "format": "0"}]} Reply: 200 OK --- sources/ogAdmServer.cpp | 100 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/sources/ogAdmServer.cpp b/sources/ogAdmServer.cpp index 0641a32..a72c2c2 100644 --- a/sources/ogAdmServer.cpp +++ b/sources/ogAdmServer.cpp @@ -3240,6 +3240,15 @@ static int og_client_state_process_payload(struct og_client *cli) } #define OG_CLIENTS_MAX 4096 +#define OG_PARTITION_MAX 4 + +struct og_partition { + const char *number; + const char *code; + const char *size; + const char *filesystem; + const char *format; +}; struct og_msg_params { const char *ips_array[OG_CLIENTS_MAX]; @@ -3255,6 +3264,9 @@ struct og_msg_params { const char *code; const char *type; const char *profile; + const char *cache; + const char *cache_size; + struct og_partition partition_setup[OG_PARTITION_MAX]; }; static int og_json_parse_clients(json_t *element, struct og_msg_params *params) @@ -3285,6 +3297,34 @@ static int og_json_parse_string(json_t *element, const char **str) return 0; } +static void og_json_parse_partition(json_t *element, og_partition *part) +{ + part->number = json_string_value(json_object_get(element, "partition")); + part->code = json_string_value(json_object_get(element, "code")); + part->filesystem = json_string_value(json_object_get(element, "filesystem")); + part->size = json_string_value(json_object_get(element, "size")); + part->format = json_string_value(json_object_get(element, "format")); +} + +static int og_json_parse_partition_setup(json_t *element, og_partition *part) +{ + unsigned int i; + json_t *k; + + if (json_typeof(element) != JSON_ARRAY) + return -1; + + for (i = 0; i < json_array_size(element) && i < OG_PARTITION_MAX; ++i) { + k = json_array_get(element, i); + + if (json_typeof(k) != JSON_OBJECT) + return -1; + + og_json_parse_partition(k, part + i); + } + return 0; +} + static int og_cmd_legacy_send(struct og_msg_params *params, const char *cmd, const char *state) { @@ -3903,6 +3943,57 @@ static int og_cmd_restore_image(json_t *element, struct og_msg_params *params) return 0; } +static int og_cmd_setup_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, "cache")) + err = og_json_parse_string(value, ¶ms->cache); + else if (!strcmp(key, "cache_size")) + err = og_json_parse_string(value, ¶ms->cache_size); + else if (!strcmp(key, "partition_setup")) + err = og_json_parse_partition_setup(value, params->partition_setup); + + if (err < 0) + break; + } + + len = snprintf(buf, sizeof(buf), + "nfn=Configurar\rdsk=%s\rcfg=dis=%s*che=%s*tch=%s!", + params->disk, params->disk, params->cache, params->cache_size); + + for (unsigned int i = 0; i < OG_PARTITION_MAX; ++i) { + const og_partition *part = params->partition_setup + i; + + len += snprintf(buf + strlen(buf), sizeof(buf), + "par=%s*cpt=%s*sfi=%s*tam=%s*ope=%s%%", + part->number, part->code, part->filesystem, part->size, part->format); + } + + msg = og_msg_alloc(buf, len + 1); + 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 @@ -4143,6 +4234,15 @@ static int og_client_state_process_payload_rest(struct og_client *cli) return og_client_bad_request(cli); } err = og_cmd_restore_image(root, ¶ms); + } else if (!strncmp(cmd, "image/setup", strlen("image/setup"))) { + 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_setup_image(root, ¶ms); } else { syslog(LOG_ERR, "unknown command: %.32s ...\n", cmd); err = og_client_not_found(cli); -- cgit v1.2.3-18-g5258