[PATCH 2/2] lxc: add support for docker-json command conversion

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Docker Json command is split between entrypoint and command properties. Compute a command
out of those to fit the LXC container XML configuration.
---
 src/lxc/lxc_native_docker.c                        | 65 ++++++++++++++++++++++
 .../dockerjson2xmldata-command.json                | 50 +++++++++++++++++
 .../dockerjson2xmldata-command.xml                 | 17 ++++++
 tests/dockerjson2xmltest.c                         |  1 +
 4 files changed, 133 insertions(+)
 create mode 100644 tests/dockerjson2xmldata/dockerjson2xmldata-command.json
 create mode 100644 tests/dockerjson2xmldata/dockerjson2xmldata-command.xml

diff --git a/src/lxc/lxc_native_docker.c b/src/lxc/lxc_native_docker.c
index a278309..618446d 100644
--- a/src/lxc/lxc_native_docker.c
+++ b/src/lxc/lxc_native_docker.c
@@ -66,12 +66,70 @@ static int dockerParseMem(virDomainDefPtr dom,
     return 0;
 }
 
+struct dockerCmdArgsIteratorArgs {
+    virDomainDefPtr vmdef;
+    size_t ninitargs;
+};
+
+static int dockerCmdArgsIterator(size_t pos ATTRIBUTE_UNUSED,
+                                 virJSONValuePtr item,
+                                 void *opaque)
+{
+    struct dockerCmdArgsIteratorArgs *args = opaque;
+    const char *value = virJSONValueGetString(item);
+
+    if (!args->vmdef->os.init) {
+        if (VIR_STRDUP(args->vmdef->os.init, value) < 0)
+            return -1;
+        else
+            return 1;
+    }
+
+    if (VIR_EXPAND_N(args->vmdef->os.initargv, args->ninitargs, 1) < 0)
+        return -1;
+
+    if (VIR_STRDUP(args->vmdef->os.initargv[args->ninitargs - 1], value) < 0)
+        return -1;
+
+    return 1;
+}
+
+
+static int dockerBuildInitCmd(virDomainDefPtr vmdef,
+                              virJSONValuePtr config)
+{
+    virJSONValuePtr entry_point = virJSONValueObjectGetArray(config, "Entrypoint");
+    virJSONValuePtr command = virJSONValueObjectGetArray(config, "Cmd");
+    struct dockerCmdArgsIteratorArgs iterator_args = { vmdef, 0 };
+
+    if (entry_point && virJSONValueArrayForeachSteal(entry_point,
+                                                     &dockerCmdArgsIterator,
+                                                     &iterator_args) < 0)
+        goto error;
+
+    if (command && virJSONValueArrayForeachSteal(command,
+                                                 &dockerCmdArgsIterator,
+                                                 &iterator_args) < 0)
+        goto error;
+
+    /* Append NULL element at the end */
+    if (iterator_args.ninitargs > 0 &&
+        VIR_EXPAND_N(vmdef->os.initargv, iterator_args.ninitargs, 1) < 0)
+        goto error;
+
+    return 0;
+
+ error:
+    return -1;
+}
+
 virDomainDefPtr dockerParseJSONConfig(virCapsPtr caps ATTRIBUTE_UNUSED,
                                       virDomainXMLOptionPtr xmlopt,
                                       const char *config)
 {
     virJSONValuePtr json_obj;
     virJSONValuePtr host_config;
+    virJSONValuePtr docker_config;
 
     if (!(json_obj = virJSONValueFromString(config)))
         return NULL;
@@ -97,6 +155,13 @@ virDomainDefPtr dockerParseJSONConfig(virCapsPtr caps ATTRIBUTE_UNUSED,
         }
     }
 
+    if ((docker_config = virJSONValueObjectGetObject(json_obj, "Config")) != NULL) {
+        if (dockerBuildInitCmd(def, docker_config) < 0) {
+            virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("failed to parse Command"));
+            goto error;
+        }
+    }
+
     def->clock.offset = VIR_DOMAIN_CLOCK_OFFSET_UTC;
     def->onReboot = VIR_DOMAIN_LIFECYCLE_RESTART;
     def->onCrash = VIR_DOMAIN_LIFECYCLE_CRASH_DESTROY;
diff --git a/tests/dockerjson2xmldata/dockerjson2xmldata-command.json b/tests/dockerjson2xmldata/dockerjson2xmldata-command.json
new file mode 100644
index 0000000..d20419d
--- /dev/null
+++ b/tests/dockerjson2xmldata/dockerjson2xmldata-command.json
@@ -0,0 +1,50 @@
+{
+        "Id": "618a2e773b84976ca2736e443a0c98eeeecdcfb2584e3dab1a3cafa313a936da",
+        "Created": "2017-06-07T00:46:07.149971669Z",
+        "Path": "/bin/ping",
+        "Args": [
+            "localhost"
+        ],
+        "HostConfig": {
+            "LogConfig": {
+                "Type": "json-file",
+                "Config": {}
+            },
+            "NetworkMode": "default",
+            "PortBindings": {},
+            "RestartPolicy": {
+                "Name": "no",
+                "MaximumRetryCount": 0
+            },
+            "ShmSize": 67108864,
+            "CpuShares": 0,
+            "Memory": 0,
+            "MemorySwap": 0,
+            "MemorySwappiness": -1,
+            "CpuCount": 0,
+            "CpuPercent": 0,
+            "IOMaximumBandwidth": 0
+        },
+        "GraphDriver": {
+            "Name": "aufs",
+            "Data": null
+        },
+        "Mounts": [],
+        "Config": {
+            "Hostname": "618a2e773b84",
+            "Domainname": "",
+            "User": "",
+            "Env": [
+                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
+            ],
+            "Cmd": [
+                "/bin/ping",
+                "localhost"
+            ],
+            "WorkingDir": "",
+            "Entrypoint": null,
+            "OnBuild": null,
+            "Labels": {}
+        }
+
+}
diff --git a/tests/dockerjson2xmldata/dockerjson2xmldata-command.xml b/tests/dockerjson2xmldata/dockerjson2xmldata-command.xml
new file mode 100644
index 0000000..52155d1
--- /dev/null
+++ b/tests/dockerjson2xmldata/dockerjson2xmldata-command.xml
@@ -0,0 +1,17 @@
+<domain type='lxc'>
+  <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
+  <memory unit='KiB'>0</memory>
+  <currentMemory unit='KiB'>0</currentMemory>
+  <vcpu placement='static'>0</vcpu>
+  <os>
+    <type>exe</type>
+    <init>/bin/ping</init>
+    <initarg>localhost</initarg>
+  </os>
+  <clock offset='utc'/>
+  <on_poweroff>destroy</on_poweroff>
+  <on_reboot>restart</on_reboot>
+  <on_crash>destroy</on_crash>
+  <devices>
+  </devices>
+</domain>
diff --git a/tests/dockerjson2xmltest.c b/tests/dockerjson2xmltest.c
index 41c46a1..de7f77a 100644
--- a/tests/dockerjson2xmltest.c
+++ b/tests/dockerjson2xmltest.c
@@ -107,6 +107,7 @@ mymain(void)
     } while (0)
 
     DO_TEST("simple", true);
+    DO_TEST("command", true);
 
     virObjectUnref(xmlopt);
     virObjectUnref(caps);
-- 
2.7.4

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]
  Powered by Linux