diff options
Diffstat (limited to 'src/dzl_dockerform.c')
-rwxr-xr-x | src/dzl_dockerform.c | 985 |
1 files changed, 985 insertions, 0 deletions
diff --git a/src/dzl_dockerform.c b/src/dzl_dockerform.c new file mode 100755 index 0000000..080e63c --- /dev/null +++ b/src/dzl_dockerform.c @@ -0,0 +1,985 @@ +/** + * @file DockerForm.c + * @brief Formatting of request/response parameters + + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * This software is the confidential and proprietary information + * of Samsung Electronics, Inc. ("Confidential Information"). You + * shall not disclose such Confidential Information and shall use + * it only in accordance with the terms of the license agreement + * you entered into with Samsung. + */ + +#include <stdio.h> +#include <string.h> +#include <unistd.h> +#include <stdlib.h> +#include <sys/stat.h> +#include <sys/types.h> +#include "dockzen_types.h" +#include "dzl_internal_types.h" +#include "dzl_dockerform.h" +#include "json_util.h" + +int dockerform_generate_url(char *version, char *url, char *newUrl, int urlSize) +{ + if (newUrl == NULL) + return DOCKZEN_ERROR_INVALID_PARAMETER; + + snprintf(newUrl, urlSize, "http://%s%s", version, url); + + return DOCKER_STATUS_NO_ERROR; +} + +int dockerform_genetate_docker_cmd(docker_control_info_s * dockerCtl, char *daemonName, char *cmd, int cmdSize) +{ + char opt_cmd[256] = { 0, }; + + if (!strlen(dockerCtl->path)) + return DOCKZEN_ERROR_INVALID_PARAMETER; + else + snprintf(cmd, cmdSize, "%s/%s", dockerCtl->path, daemonName); + + if (strlen(dockerCtl->opt_cmd)) { + snprintf(opt_cmd, sizeof(opt_cmd), " %s", dockerCtl->opt_cmd); + strncat(cmd, opt_cmd, cmdSize - strlen(cmd) - 1); + } + + strncat(cmd, " &", cmdSize - strlen(cmd) - 1); + + _D("command = [%s]", cmd); + + return DOCKER_STATUS_NO_ERROR; +} + +int dockerform_check_response(char *message) +{ + struct json_object *inputObj, *bodyObj; + const char *data = NULL; + enum json_type type; + int ret = DOCKER_STATUS_NO_ERROR; + + inputObj = json_tokener_parse(message); + type = json_object_get_type(inputObj); + + if (type == json_type_object) { + if (json_object_object_get_ex(inputObj, "message", &bodyObj)) { + // Error : 404 no such swarm or 500 server error or 503 node is not part of a swarm + data = json_object_to_json_string_ext(bodyObj, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY); + _D("bodyObj= %s", data); + ret = 1; + } else { + _D("message obj is not exist"); + } + } + + json_object_put(inputObj); + + return ret; +} + +char *dockerform_json_getStringfromObj(json_object * inputObj, char *key) +{ + const char *buf = NULL; + char *ret_buf = NULL; + json_object *elementObj = NULL; + int i = 0, j = 0; + + if (json_object_object_get_ex(inputObj, key, &elementObj)) { + buf = json_object_to_json_string_ext(elementObj, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY); + ret_buf = malloc(strlen(buf) - 1); + if (ret_buf != NULL) { + memset(ret_buf, 0x00, strlen(buf) - 1); // "<-- exclude + for (i = 1, j = 0; i < strlen(buf) - 1; i++) { // "\ <-- exclude + if (buf[i] == '\\') + continue; + ret_buf[j++] = buf[i]; + } + } else { + return NULL; + } + } else { + return NULL; + } + + _D("ret_buf [%s]", ret_buf); + return ret_buf; +} + +/** + * @fn static int __parse_portinfo(char *info, port_info_s *port) + * @brief This function to parse port info from info string + * @param *info [in] string of port + * @param *port [out] fill port information + * @return int result of function + */ +static int __parse_portinfo(char *info, port_info_s * port) +{ + char *context = NULL; + char *ptr = strtok_r(info, ":", &context); + if (ptr == NULL) + return -1; + + port->targetport = atoi(ptr); + _D("targetport = %d", port->targetport); + + ptr = strtok_r(NULL, "", &context); + if (ptr == NULL) + return -1; + + port->publishedport = atoi(ptr); + _D("publishedport = %d", port->publishedport); + + return 0; +} + +/** + * @fn static int __generate_portinfo(char *info, dockform_port_info_s *portinfo) + * @brief This function to parse port info from info string + * @param *info [in] string of port + * @param *portinfo [out] ports infomation + * @return int result of function + */ +static int __generate_portinfo(char *info, dockform_port_info_s * portinfo) +{ + char *ptr = NULL; + char *ptrNew = NULL; + char *sArr[256] = { 0, }; + char portCopy[256] = { 0, }; + int index = 0; + char *next_ptr; + + if (info == NULL) + return DOCKER_STATUS_UNKNOWN; + + strncpy(portCopy, info, strlen(info) + 1); + + ptr = portCopy; + ptrNew = strtok_r(ptr, ",", &next_ptr); + index = 0; + while (ptrNew != NULL) { + sArr[index] = ptrNew; + index++; + ptrNew = strtok_r(NULL, ",", &next_ptr); + } + + _D("Port Count [%d]", index); + + portinfo->count = index; + portinfo->port_info_h = (port_info_s *) malloc(portinfo->count * sizeof(port_info_s)); + + if (portinfo->port_info_h == NULL) + return DOCKER_STATUS_UNKNOWN; + + for (index = 0; index < portinfo->count; index++) { + if (__parse_portinfo(sArr[index], &portinfo->port_info_h[index])) { + _D("broken"); + free(portinfo->port_info_h); + portinfo->port_info_h = NULL; + return DOCKER_STATUS_UNKNOWN; + } + } + + return DOCKER_STATUS_NO_ERROR; +} + +/** + * @fn static int __parse_volumeinfo(char *info, volume_info_s *volume) + * @brief This function to parse volume info from info string + * @param *info [in] string of volume + * @param *volume [out] fill volume information + * @return int result of function + */ +static int __parse_volumeinfo(char *info, volume_info_s * volume) +{ + char *context = NULL; + char *ptr = strtok_r(info, ":", &context); + if (ptr == NULL) + return -1; + + memcpy(volume->source, ptr, strlen(ptr) + 1); + _D("%s", volume->source); + ptr = strtok_r(NULL, ":", &context); + if (ptr == NULL) + return -1; + + memcpy(volume->target, ptr, strlen(ptr) + 1); + _D("%s", volume->target); + //Target + ptr = strtok_r(NULL, "", &context); + if (ptr == NULL) + return -1; + + if (!strcmp(ptr, "RW") || !strcmp(ptr, "rw")) + volume->read_only = 0; + else + volume->read_only = 1; + + _D("readonly : %d", volume->read_only); + return 0; +} + +/** + * @fn static int __generate_volumeinfo(char *info, dockform_volume_info_s *volumeinfo) + * @brief This function to parse volume info from info string + * @param *info [in] string of volume + * @param *volumeinfo [out] volumes infomation + * @return int result of function + */ +static int __generate_volumeinfo(char *info, dockform_volume_info_s * volumeinfo) +{ + char *ptr = NULL; + char *ptrNew = NULL; + char *sArr[256] = { 0, }; + char volumeCopy[256] = { 0, }; + int index = 0; + char *next_ptr; + + if (info == NULL) + return DOCKER_STATUS_UNKNOWN; + + strncpy(volumeCopy, info, strlen(info) + 1); + + ptr = volumeCopy; + ptrNew = strtok_r(ptr, ",", &next_ptr); + index = 0; + while (ptrNew != NULL) { + sArr[index] = ptrNew; + index++; + ptrNew = strtok_r(NULL, ",", &next_ptr); + } + + _D("Volume Count [%d]", index); + + volumeinfo->count = index; + volumeinfo->volume_info_h = (volume_info_s *) malloc(volumeinfo->count * sizeof(volume_info_s)); + + if (volumeinfo->volume_info_h == NULL) + return DOCKER_STATUS_UNKNOWN; + + for (index = 0; index < volumeinfo->count; index++) { + if (__parse_volumeinfo(sArr[index], &volumeinfo->volume_info_h[index])) { + _D("broken"); + free(volumeinfo->volume_info_h); + volumeinfo->volume_info_h = NULL; + return DOCKER_STATUS_UNKNOWN; + } + } + + return DOCKER_STATUS_NO_ERROR; +} + +/** + * @fn static int __generate_environmentInfo(char *info, dockform_environment_info_s *environmentInfo) + * @brief This function to parse environment info from info string + * @param *info [in] string of environment + * @param *environmentInfo [out] environments infomation + * @return int result of function + */ +static int __generate_environmentInfo(char *info, dockform_environment_info_s * environmentInfo) +{ + char *ptr = NULL; + char *ptrNew = NULL; + char *sArr[256] = { 0, }; + char environmentCopy[256] = { 0, }; + int index = 0; + char *next_ptr; + int env_len = 0; + + if (info == NULL) + return DOCKER_STATUS_UNKNOWN; + + strncpy(environmentCopy, info, strlen(info) + 1); + + ptr = environmentCopy; + ptrNew = strtok_r(ptr, ",", &next_ptr); + index = 0; + while (ptrNew != NULL) { + sArr[index] = ptrNew; + index++; + ptrNew = strtok_r(NULL, ",", &next_ptr); + } + + _D("Environment Count [%d]", index); + + environmentInfo->count = index; + environmentInfo->env = (environment_info_s *) malloc(environmentInfo->count * sizeof(environment_info_s)); + + if (environmentInfo->env == NULL) + return DOCKER_STATUS_UNKNOWN; + + for (index = 0; index < environmentInfo->count; index++) { + env_len = sizeof(environmentInfo->env[index].environment); + + if (!strncpy(&environmentInfo->env[index].environment, sArr[index], env_len - 1)) { + _D("broken"); + free(environmentInfo->env); + environmentInfo->env = NULL; + return DOCKER_STATUS_UNKNOWN; + } + environmentInfo->env[index].environment[env_len - 1] = '\0'; + } + + return DOCKER_STATUS_NO_ERROR; +} + +/** + * @fn static int __create_directory(char *path, mode_t mode) + * @brief This function to create a directory + * @param *source [in] string of directory full path + * @param mode_t [in] mode + * @return int result of function + */ +static int __create_directory(char *path, mode_t mode) +{ + int len = 0; + char *tmp = path; + char tmp_path[128] = { 0, }; + + if (path == NULL) + return -1; + + while ((tmp = strchr(tmp, '/')) != NULL) { + len = tmp - path; + tmp++; + if (len == 0) + continue; + + strncpy(tmp_path, path, len); + tmp_path[len] = 0x00; + + if (access(tmp_path, 0) != 0) { + if (mkdir(tmp_path, mode) == -1) { + _E("mkdir error: %s", tmp_path); + return -1; + } else + _D("created: %s", tmp_path); + } else + _W("already exist: %s", tmp_path); + } + + return mkdir(path, mode); +} + +/** + * @fn static int __check_validation_source_volumeinfo(char *source) + * @brief This function to check source is existing + * @param *source [in] string of source + * @param *mode [in] mode + * @return int result of function + */ +static int __check_validation_source_volumeinfo(char *source, int readonly) +{ + int ret = 0; + mode_t mode; + + ret = access(source, 0); + if (ret != 0) { + _D("no exists: %s", source); + if (readonly == 1) + mode = 0755; + else + mode = 0777; + + if ((ret = __create_directory(source, mode)) != 0) + _D("failed create source directory: %s", source); + else + _D("created source directory: %s", source); + } else + _D("already exist: %s", source); + return ret; +} + +int dockerform_generate_imagecreate_data(container_create_info_s * createInfo, char **ret_buf) +{ + struct json_object *newObj, *nullObj, *replicatedObj, *replicasObj, *taskObj, *containerObj, *endpointspecObj; + struct json_object *argsObj, *mntArrayObj, *mntObj, *resourObj, *updateObj; + const char *buf = NULL; + int buf_len = 0; + int mntArrayCnt = 0; + int i = 0; + dockform_volume_info_s stVolumeInfo = { 0, }; + dockform_port_info_s stPortInfo = { 0, }; + dockform_environment_info_s stEnvironmentInfo = { 0, }; + + newObj = json_object_new_object(); + nullObj = json_object_new_object(); + replicatedObj = json_object_new_object(); + taskObj = json_object_new_object(); + containerObj = json_object_new_object(); + argsObj = json_object_new_array(); + mntArrayObj = json_object_new_array(); + resourObj = json_object_new_object(); + updateObj = json_object_new_object(); + replicasObj = json_object_new_object(); + endpointspecObj = json_object_new_object(); + + json_object_object_add(replicasObj, "Replicas", json_object_new_int(1)); + json_object_object_add(replicatedObj, "Replicated", replicasObj); + + json_object_object_add(newObj, "Mode", replicatedObj); + if (createInfo->name) + json_object_object_add(newObj, "Name", json_object_new_string(createInfo->name)); + + if (createInfo->entry_cmd) + json_object_array_add(argsObj, json_object_new_string(createInfo->entry_cmd)); + + json_object_object_add(containerObj, "Args", argsObj); + json_object_object_add(containerObj, "DNSConfig", nullObj); + if (createInfo->image) + json_object_object_add(containerObj, "Image", json_object_new_string(createInfo->image)); + + /* NetworkMode and Privileged are added manually */ + if (createInfo->net_mode) + json_object_object_add(containerObj, "NetworkMode", json_object_new_string(createInfo->net_mode)); + + if (createInfo->privileged) + json_object_object_add(containerObj, "Privileged", json_object_new_boolean((json_bool) 1)); + else + json_object_object_add(containerObj, "Privileged", json_object_new_boolean((json_bool) 0)); + + memset(&stVolumeInfo, 0x00, sizeof(stVolumeInfo)); + if (createInfo->volume) { + if (!__generate_volumeinfo(createInfo->volume, &stVolumeInfo)) { + mntArrayCnt = stVolumeInfo.count; + _D("volumeinfo: arryacnt[%d]", mntArrayCnt); + for (i = 0; i < mntArrayCnt; i++) { + mntObj = json_object_new_object(); + json_object_object_add(mntObj, "Type", json_object_new_string("bind")); + if (stVolumeInfo.volume_info_h[i].read_only) + json_object_object_add(mntObj, "ReadOnly", json_object_new_boolean((json_bool) 1)); + else + json_object_object_add(mntObj, "ReadOnly", json_object_new_boolean((json_bool) 0)); + + json_object_object_add(mntObj, "Source", json_object_new_string(stVolumeInfo.volume_info_h[i].source)); + json_object_object_add(mntObj, "Target", json_object_new_string(stVolumeInfo.volume_info_h[i].target)); + json_object_array_add(mntArrayObj, mntObj); + + if (__check_validation_source_volumeinfo(stVolumeInfo.volume_info_h[i].source, stVolumeInfo.volume_info_h[i].read_only) != 0) + _D("there is no source, could not be mounted: %s", stVolumeInfo.volume_info_h[i].source); + } + + if (stVolumeInfo.volume_info_h != NULL) + free(stVolumeInfo.volume_info_h); + + } else + _E("Volume Error"); + } + json_object_object_add(containerObj, "Mounts", mntArrayObj); + + mntArrayObj = json_object_new_array(); + memset(&stEnvironmentInfo, 0x00, sizeof(stEnvironmentInfo)); + if (createInfo->environment) { + if (!__generate_environmentInfo(createInfo->environment, &stEnvironmentInfo)) { + mntArrayCnt = stEnvironmentInfo.count; + _D("stEnvironmentInfo: arryacnt[%d]", mntArrayCnt); + for (i = 0; i < mntArrayCnt; i++) { + _D("stEnvironmentInfo.env[%d].environment : %s", i, stEnvironmentInfo.env[i].environment); + json_object_array_add(mntArrayObj, json_object_new_string(stEnvironmentInfo.env[i].environment)); + } + } + } + json_object_object_add(containerObj, "Env", mntArrayObj); + + json_object_object_add(taskObj, "ContainerSpec", containerObj); + + mntArrayObj = json_object_new_array(); + if (createInfo->port) { + memset(&stPortInfo, 0x00, sizeof(stPortInfo)); + if (!__generate_portinfo(createInfo->port, &stPortInfo)) { + mntArrayCnt = stPortInfo.count; + _D("portinfo: arryacnt[%d]", mntArrayCnt); + for (i = 0; i < mntArrayCnt; i++) { + mntObj = json_object_new_object(); + json_object_object_add(mntObj, "Protocol", json_object_new_string("tcp")); + json_object_object_add(mntObj, "PublishMode", json_object_new_string("host")); + json_object_object_add(mntObj, "TargetPort", json_object_new_int(stPortInfo.port_info_h[i].targetport)); + json_object_object_add(mntObj, "PublishedPort", json_object_new_int(stPortInfo.port_info_h[i].publishedport)); + json_object_array_add(mntArrayObj, mntObj); + } + if (stPortInfo.port_info_h != NULL) + free(stPortInfo.port_info_h); + } else + _E("Port Error"); + } + json_object_object_add(endpointspecObj, "Ports", mntArrayObj); + json_object_object_add(newObj, "EndpointSpec", endpointspecObj); + + json_object_object_add(taskObj, "ForceUpdate", json_object_new_int(0)); + json_object_object_add(taskObj, "Placement", nullObj); + + json_object_object_add(resourObj, "Limits", nullObj); + json_object_object_add(resourObj, "Reservations", nullObj); + + json_object_object_add(taskObj, "Resources", resourObj); + json_object_object_add(taskObj, "RestartPolicy", nullObj); + + json_object_object_add(newObj, "TaskTemplate", taskObj); + + json_object_object_add(updateObj, "FailureAction", json_object_new_string("pause")); + json_object_object_add(updateObj, "MaxFailureRatio", json_object_new_int(0)); + json_object_object_add(updateObj, "Parallelism", json_object_new_int(1)); + + json_object_object_add(newObj, "UpdateConfig", updateObj); + + buf = json_object_to_json_string_ext(newObj, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY); + *ret_buf = (char *)malloc(strlen(buf) + 1); + buf_len = strlen(buf); + if (*ret_buf != NULL) { + memset(*ret_buf, 0x00, strlen(buf) + 1); + memcpy(*ret_buf, buf, strlen(buf) + 1); + } + _D("service create json:[%s]", *ret_buf); + + json_object_put(newObj); + + return buf_len; +} + +int dockerform_generate_swarm_init(char **ret_buf) +{ + struct json_object *newObj = NULL; //new json object + struct json_object *specObj = NULL; // spec json object + struct json_object *encryptionObj = NULL; // EncryptionConfig data json object + struct json_object *nullObj = NULL; // null data json object {} + struct json_object *raftObj = NULL; //Raft data json object + struct json_object *taskHistoryObj = NULL; + const char *buf = NULL; + int buf_len = 0; + + specObj = json_object_new_object(); + nullObj = json_object_new_object(); + encryptionObj = json_object_new_object(); + raftObj = json_object_new_object(); + newObj = json_object_new_object(); + taskHistoryObj = json_object_new_object(); + + json_object_object_add(encryptionObj, "AutoLockManagers", json_object_new_boolean((json_bool) 0)); + + json_object_object_add(raftObj, "ElectionTick", json_object_new_int(0)); + json_object_object_add(raftObj, "HeartbeatTick", json_object_new_int(0)); + + json_object_object_add(specObj, "CAConfig", nullObj); + json_object_object_add(specObj, "Dispatcher", nullObj); + json_object_object_add(specObj, "EncryptionConfig", encryptionObj); + json_object_object_add(taskHistoryObj, "TaskHistoryRetentionLimit", json_object_new_int(1)); + json_object_object_add(specObj, "Orchestration", taskHistoryObj); + json_object_object_add(specObj, "Raft", raftObj); + json_object_object_add(specObj, "TaskDefaults", nullObj); + + // setup the 127.0.0.1 to initialize without network connection + json_object_object_add(newObj, "AdvertiseAddr", json_object_new_string("127.0.0.1")); + json_object_object_add(newObj, "AutoLockManagers", json_object_new_boolean((json_bool) 0)); + json_object_object_add(newObj, "ForceNewCluster", json_object_new_boolean((json_bool) 0)); + json_object_object_add(newObj, "ListenAddr", json_object_new_string("0.0.0.0:2377")); + json_object_object_add(newObj, "Spec", specObj); + + _D("newObj = %s", json_object_to_json_string_ext(newObj, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY)); + + buf = json_object_to_json_string_ext(newObj, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY); + + *ret_buf = (char *)malloc(strlen(buf) + 1); + buf_len = strlen(buf); + if (*ret_buf != NULL) { + memset(*ret_buf, 0x00, strlen(buf) + 1); + memcpy(*ret_buf, buf, strlen(buf) + 1); + } + + json_object_put(newObj); + + return buf_len; +} + +int dockerform_generate_networksinfo(char *data, dzl_dockerctl_NetworksInfo * pstNetworkInfo) +{ + struct json_object *networkObj = NULL; + struct json_object *specObj = NULL; + int index = 0; + int network_cnt = 0; + enum json_type type; + char *id = NULL; + char *name = NULL; + char *gateway = NULL; + + _D("dockform_generate_networksinfo call"); + + networkObj = json_tokener_parse(data); + + id = dockerform_json_getStringfromObj(networkObj, "Id"); + if (id != NULL) { + _D("id[%s] len[%d]", id, (unsigned int)strlen(id)); + if (strlen(id) > 0) + memcpy(pstNetworkInfo->id, id, strlen(id) + 1); + free(id); + id = NULL; + } + name = dockerform_json_getStringfromObj(networkObj, "Name"); + if (name != NULL) { + _D("id[%s] len[%d]", name, (unsigned int)strlen(name)); + if (strlen(name) > 0) + memcpy(pstNetworkInfo->name, name, strlen(name) + 1); + free(name); + name = NULL; + } + + if (json_object_object_get_ex(networkObj, "IPAM", &specObj)) { + struct json_object *configtempObj, *configArrayObj; + if (json_object_get_type(specObj) == json_type_object) { + if (json_object_object_get_ex(specObj, "Config", &configtempObj)) { + if (json_object_get_type(configtempObj) == json_type_array) { + configArrayObj = json_object_array_get_idx(configtempObj, 0); + gateway = dockerform_json_getStringfromObj(configArrayObj, "Gateway"); + if (gateway != NULL) { + _D("docker network Geteway !!!!"); + pstNetworkInfo->gatewayflag = 1; + free(gateway); + gateway = NULL; + } + } + } + } + } + + _D("dockerform_generate_networksinfo end"); + return DOCKER_STATUS_NO_ERROR; +} + +int dockerform_generate_servicesinfo(char *data, dzl_dockerctl_ServicesInfo * pstServicesInfo) +{ + struct json_object *serviceObj = NULL, *inputArrayObj = NULL; + int index = 0; + int service_cnt = 0; + enum json_type type; + + _D("dockform_generate_servicesinfo call"); + + serviceObj = json_tokener_parse(data); + type = json_object_get_type(serviceObj); + if (type == json_type_array) { + service_cnt = json_object_array_length(serviceObj); + if (service_cnt == 0) + return DOCKER_STATUS_UNKNOWN; + } + + _D("cnt : [%d]", service_cnt); + + pstServicesInfo->pstServices = (dzl_dockerctl_Service *) malloc(service_cnt * sizeof(dzl_dockerctl_Service)); + if (pstServicesInfo->pstServices == NULL) + return DOCKER_STATUS_UNKNOWN; + memset(pstServicesInfo->pstServices, 0x00, service_cnt * sizeof(dzl_dockerctl_Service)); + + pstServicesInfo->count = service_cnt; + + index = 0; + + while (index < service_cnt) { + struct json_object *specObj; + char *id = NULL; + char *name = NULL; + char *image = NULL; + + inputArrayObj = json_object_array_get_idx(serviceObj, index); + id = dockerform_json_getStringfromObj(inputArrayObj, "ID"); + if (id != NULL) { + _D("id[%s] len[%d]", id, (unsigned int)strlen(id)); + if (strlen(id) > 0) + memcpy(pstServicesInfo->pstServices[index].id, id, strlen(id) + 1); + free(id); + id = NULL; + } + + if (json_object_object_get_ex(inputArrayObj, "Spec", &specObj)) { + struct json_object *tasktempObj, *containerSpecObj; + + if (json_object_get_type(specObj) == json_type_object) { + + name = dockerform_json_getStringfromObj(specObj, "Name"); + if (name != NULL) { + if (strlen(name) > 0) + memcpy(pstServicesInfo->pstServices[index].name, name, strlen(name) + 1); + + free(name); + name = NULL; + } + + if (json_object_object_get_ex(specObj, "TaskTemplate", &tasktempObj)) { + if (json_object_object_get_ex(tasktempObj, "ContainerSpec", &containerSpecObj)) { + image = dockerform_json_getStringfromObj(containerSpecObj, "Image"); + if (image != NULL) { + if (strlen(image) > 0) + memcpy(pstServicesInfo->pstServices[index].imageName, image, strlen(image) + 1); + + free(image); + image = NULL; + } + } + } + } + } + + index++; + } + + _D("dockform_generate_servicesinfo end"); + return DOCKER_STATUS_NO_ERROR; +} + +int dockerform_generate_image_with_repotag_info(char *data, char *repoTag) +{ + int i, j; + int index = 0; + char *buf = NULL; + char *compare_buf = NULL; + int imageCnt; + struct json_object *imageObj = NULL; + struct json_object *imageElementObj = NULL; + struct json_object *imageRepoTagObj = NULL; + + if (!repoTag) + return DOCKER_STATUS_INVALID_PARAM; + + imageObj = json_tokener_parse(data); + imageCnt = json_object_array_length(imageObj); + + _D("Image count [%d]", imageCnt); + + while (index < imageCnt) { + imageElementObj = json_object_array_get_idx(imageObj, index); + if (json_object_object_get_ex(imageElementObj, "RepoTags", &imageRepoTagObj)) { + buf = json_object_to_json_string_ext(imageRepoTagObj, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY); + compare_buf = malloc(strlen(buf) + 1); + if (compare_buf != NULL) { + memset(compare_buf, 0x00, strlen(buf) + 1); + for (i = 0, j = 0; i < strlen(buf); i++) { + if (buf[i] == '\\') + continue; + else + compare_buf[j++] = buf[i]; + } + _D("[%d]RepoTags: %s", index, compare_buf); + + if (strstr(compare_buf, repoTag)) { + free(compare_buf); + return DOCKER_STATUS_NO_ERROR; + } + } + } + index++; + } + free(compare_buf); + return DOCKER_STATUS_UNKNOWN; +} + +int dockerform_generate_tasksinfo(char *data, dzl_dockerctl_TasksInfo * pstTasksInfo) +{ + struct json_object *taskObj = NULL; + struct json_object *inputTaskArrayObj = NULL; + struct json_object *taskServiceIDObj = NULL, *taskStateObj = NULL; + char *id = NULL; + int task_cnt = 0; + int index = 0; + + taskObj = json_tokener_parse(data); + task_cnt = json_object_array_length(taskObj); + + _D("Task count [%d]", task_cnt); + + pstTasksInfo->count = task_cnt; + pstTasksInfo->pstTasks = (dzl_dockerctl_Task *) malloc(task_cnt * sizeof(dzl_dockerctl_Task)); + if (pstTasksInfo->pstTasks == NULL) + return DOCKER_STATUS_UNKNOWN; + + memset(pstTasksInfo->pstTasks, 0x00, task_cnt * sizeof(dzl_dockerctl_Task)); + index = 0; + while (index < task_cnt) { + char *status = NULL; + inputTaskArrayObj = json_object_array_get_idx(taskObj, index); + if (json_object_object_get_ex(inputTaskArrayObj, "ServiceID", &taskServiceIDObj)) { + id = dockerform_json_getStringfromObj(inputTaskArrayObj, "ServiceID"); + if (json_object_object_get_ex(inputTaskArrayObj, "DesiredState", &taskStateObj)) { + status = dockerform_json_getStringfromObj(inputTaskArrayObj, "DesiredState"); + + if (status != NULL && id != NULL) { + if (strlen(id) > 0) + memcpy(pstTasksInfo->pstTasks[index].id, id, strlen(id) + 1); + + if (strlen(status) > 0) + memcpy(pstTasksInfo->pstTasks[index].status, status, strlen(status) + 1); + } + + if (status != NULL) { + free(status); + status = NULL; + } + } + + if (id != NULL) { + free(id); + id = NULL; + } + } + + index++; + } + + return DOCKER_STATUS_NO_ERROR; +} + +int dockerform_generate_service_withid_info(char *data, dzl_dockerctl_serviceinfo_s * info) +{ + char *specConfig = NULL; + char *id = NULL; + char *version_data = NULL; + int version = 0; + + _D("data[%s]", data); + specConfig = json_getObject(data, "Spec"); + id = json_getString(data, "ID"); + + if (id != NULL) { + _D("id[%s]", id); + info->id = (char *)malloc(sizeof(char) * (strlen(id) + 1)); + if (info->id != NULL) { + memset(info->id, 0x00, sizeof(char) * (strlen(id) + 1)); + memcpy(info->id, id, strlen(id) + 1); + } + } + + if (specConfig != NULL) { + _D("specConfig[%s]", specConfig); + info->spec_config = (char *)malloc(sizeof(char) * (strlen(specConfig) + 1)); + if (info->spec_config != NULL) { + memset(info->spec_config, 0x00, sizeof(char) * (strlen(specConfig) + 1)); + memcpy(info->spec_config, specConfig, strlen(specConfig) + 1); + } + } + + version_data = json_getObject(data, "Version"); + if (version_data != NULL) { + _D("version_data[%s]", version_data); + version = json_getNumber(version_data, "Index"); + info->version = version; + } + + if (specConfig != NULL) + free(specConfig); + if (id != NULL) + free(id); + if (version_data != NULL) + free(version_data); + + return DOCKER_STATUS_NO_ERROR; +} + +int dockerform_generate_updateinfo(dzl_dockerctl_serviceinfo_s * service_info, char *imageName, char **ret_buf) +{ + struct json_object *inputObj, *taskObj, *containerObj; + const char *buf = NULL; + int buf_len = 0; + int i, j; + char imageNamebuf[256]; + + inputObj = json_tokener_parse(service_info->spec_config); + + //------------- image name : xx.xxx.xx.xx.xx\/xxxx <-- \ remove + j = 0; + for (i = 0; i < strlen(imageName); i++) { + if (imageName[i] == '\\') + continue; + + imageNamebuf[j++] = imageName[i]; + } + imageNamebuf[j] = '\0'; + + _D("spec_config = %s", service_info->spec_config); + _D("imageNamebuf = %s", imageNamebuf); + + if (json_object_object_get_ex(inputObj, "TaskTemplate", &taskObj)) { + if (json_object_object_get_ex(taskObj, "ContainerSpec", &containerObj)) { + json_object_object_del(containerObj, "Image"); + json_object_object_add(containerObj, "Image", json_object_new_string(imageNamebuf)); + buf = json_object_to_json_string_ext(inputObj, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY); + *ret_buf = (char *)malloc(strlen(buf) + 1); + buf_len = strlen(buf); + if (*ret_buf != NULL) { + memset(*ret_buf, 0x00, strlen(buf) + 1); + memcpy(*ret_buf, buf, strlen(buf) + 1); + } + _D("buf = %s", buf); + } + } + + json_object_put(inputObj); + + return buf_len; +} + +int dockerform_parse_container_info(char *data, dzl_dockerctl_Task * task_info) +{ + struct json_object *eventObj = NULL; + struct json_object *actorObj = NULL; + struct json_object *attributeObj = NULL; + char *name = NULL; + char *status = NULL; + + eventObj = json_tokener_parse(data); + + if (eventObj != NULL) { + status = dockerform_json_getStringfromObj(eventObj, "Action"); + if (status != NULL) { + memcpy(task_info->status, status, strlen(status) + 1); + if (status != NULL) { + free(status); + status = NULL; + } + } + if (json_object_object_get_ex(eventObj, "Actor", &actorObj)) { + if (json_object_object_get_ex(actorObj, "Attributes", &attributeObj)) { + name = dockerform_json_getStringfromObj(attributeObj, "com.docker.swarm.service.name"); + if (name != NULL) { + memcpy(task_info->id, name, strlen(name) + 1); + if (name != NULL) { + free(name); + name = NULL; + } + } + } + } + } else { + _D("eventObj is null"); + return -1; + } + + _D("dockerform_parse_container_info end"); + + return 0; +} + +int dockerform_generate_imageload_file(char *filename, char **ret_buf) +{ + FILE *fd; + off_t filelen = 0; + struct stat st; + int readlen; + + fd = fopen(filename, "rb"); // open the file in binary mode + if (stat(filename, &st) == 0) { + filelen = st.st_size; + _D("filelen(%d)", filelen); + *ret_buf = (char *)malloc(sizeof(char) * (filelen + 1)); + memset(*ret_buf, 0x00, filelen + 1); + readlen = fread(*ret_buf, filelen, 1, fd); // read in the entire file + if (readlen != filelen) { + if (ferror(fd)) + _E("Error reading %s", filename); + else if (feof(fd)) + _D("EOF found %s", filename); + } else + _D("readlen (%d)", readlen); + } else + _E("too large file"); + + fclose(fd); + + return filelen; +} |