Add network emulation feature.
[sim/o1-interface.git] / ntsimulator / ntsim-ng / core / docker.c
index dcd8c50..aea7ae8 100644 (file)
@@ -358,6 +358,42 @@ int docker_usage_get(const char **instances_id, int count, docker_usage_t *usage
     return NTS_ERR_OK;
 }
 
+int docker_pull(const char *repo, const char *image, const char *tag) {
+    assert(repo);
+    assert(image);
+    assert(tag);
+
+    char image_full[256];
+    if(tag && (tag[0] != 0)) {
+        sprintf(image_full, "%s/%s:%s", repo, image, tag);
+    }
+    else {
+        sprintf(image_full, "%s/%s:latest", repo, image);    
+    }
+
+    char url[512];
+    sprintf(url, "http:/v%s/images/create?fromImage=%s", framework_environment.settings.docker_engine_version, image_full);
+
+    char *response = 0;
+    int response_code = 0;
+    int rc = http_socket_request(url, DOCKER_SOCK_FNAME, "POST", 0, &response_code, &response);
+
+    if(rc != NTS_ERR_OK) {
+        log_error("http_socket_request failed\n");
+        return NTS_ERR_FAILED;
+    }
+
+    if(response_code != 200) {
+        char *message = docker_parse_json_message(response);
+        log_error("docker_pull failed (%d): %s\n", response_code, message);
+        free(message);
+        free(response);
+        return NTS_ERR_FAILED;
+    }
+    
+    return NTS_ERR_OK;
+}
+
 static char *docker_parse_json_message(const char *json_string) {
     assert(json_string);
 
@@ -410,12 +446,6 @@ static int docker_add_port(cJSON *portBindings, uint16_t docker_port, uint16_t h
         return NTS_ERR_FAILED;
     }
 
-    if(cJSON_AddStringToObject(hostPort, "HostIp", "0.0.0.0") == 0) {   //or, future, bind to container->host_ip
-        log_error("could not create JSON object: HostIpString\n");
-        cJSON_Delete(hostPort);
-        return NTS_ERR_FAILED;
-    }
-
     if(cJSON_AddItemToArray(port, hostPort) == 0) {
         log_error("cJSON_AddItemToArray failed\n");
         cJSON_Delete(hostPort);
@@ -525,9 +555,33 @@ static int docker_container_create(const char *image, docker_container_t *contai
         return NTS_ERR_FAILED;
     }
 
+    cJSON *capAdd = cJSON_CreateArray();
+    if(capAdd == 0) {
+        log_error("could not create JSON array: CapAdd\n");
+        cJSON_Delete(postDataJson);
+        return NTS_ERR_FAILED;
+    }
+    if(cJSON_AddItemToObject(hostConfig, "CapAdd", capAdd) == 0) {
+        log_error("cJSON_AddItemToObject failed\n");
+        cJSON_Delete(postDataJson);
+        return NTS_ERR_FAILED;
+    }
+    cJSON *net_admin = cJSON_CreateString("NET_ADMIN");
+    if(net_admin == 0) {
+        log_error("could not create JSON string\n");
+        cJSON_Delete(postDataJson);
+        return NTS_ERR_FAILED;
+    }
+    if(cJSON_AddItemToArray(capAdd, net_admin) == 0) {
+        log_error("cJSON_AddItemToArray failed\n");
+        cJSON_Delete(postDataJson);
+        return NTS_ERR_FAILED;
+    }
+    
+
     cJSON *portBindings = cJSON_CreateObject();
     if(portBindings == 0) {
-        printf("could not create JSON object: PortBindings");
+        log_error("could not create JSON object: PortBindings\n");
         cJSON_Delete(postDataJson);
         return NTS_ERR_FAILED;
     }