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_docker.c | 65 ++++++++++++++++++++++
.../lxcdocker2xmldata-command.json | 47 ++++++++++++++++
.../lxcdocker2xmldata-command.xml | 17 ++++++
tests/lxcdocker2xmltest.c | 1 +
4 files changed, 130 insertions(+)
create mode 100644 tests/lxcdocker2xmldata/lxcdocker2xmldata-command.json
create mode 100644 tests/lxcdocker2xmldata/lxcdocker2xmldata-command.xml
diff --git a/src/lxc/lxc_docker.c b/src/lxc/lxc_docker.c
index dbb2a81..b3e25f7 100644
--- a/src/lxc/lxc_docker.c
+++ b/src/lxc/lxc_docker.c
@@ -70,12 +70,70 @@ static int virLXCDockerParseMem(virDomainDefPtr dom,
return 0;
}
+struct virLXCDockerCmdArgsIteratorArgs {
+ virDomainDefPtr vmdef;
+ size_t ninitargs;
+};
+
+static int virLXCDockerCmdArgsIterator(size_t pos ATTRIBUTE_UNUSED,
+ virJSONValuePtr item,
+ void *opaque)
+{
+ struct virLXCDockerCmdArgsIteratorArgs *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 virLXCDockerBuildInitCmd(virDomainDefPtr vmdef,
+ virJSONValuePtr config)
+{
+ virJSONValuePtr entry_point = virJSONValueObjectGetArray(config,
"Entrypoint");
+ virJSONValuePtr command = virJSONValueObjectGetArray(config, "Cmd");
+ struct virLXCDockerCmdArgsIteratorArgs iterator_args = { vmdef, 0 };
+
+ if (entry_point && virJSONValueArrayForeachSteal(entry_point,
+ &virLXCDockerCmdArgsIterator,
+ &iterator_args) < 0)
+ goto error;
+
+ if (command && virJSONValueArrayForeachSteal(command,
+ &virLXCDockerCmdArgsIterator,
+ &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 virLXCDockerParseJSONConfig(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;
@@ -101,6 +159,13 @@ virDomainDefPtr virLXCDockerParseJSONConfig(virCapsPtr caps
ATTRIBUTE_UNUSED,
}
}
+ if ((docker_config = virJSONValueObjectGetObject(json_obj, "Config")) !=
NULL) {
+ if (virLXCDockerBuildInitCmd(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/lxcdocker2xmldata/lxcdocker2xmldata-command.json
b/tests/lxcdocker2xmldata/lxcdocker2xmldata-command.json
new file mode 100644
index 0000000..14e4d92
--- /dev/null
+++ b/tests/lxcdocker2xmldata/lxcdocker2xmldata-command.json
@@ -0,0 +1,47 @@
+{
+ "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": "",
+ "Cmd": [
+ "/bin/ping",
+ "localhost"
+ ],
+ "WorkingDir": "",
+ "Entrypoint": null,
+ "OnBuild": null,
+ "Labels": {}
+ }
+
+}
diff --git a/tests/lxcdocker2xmldata/lxcdocker2xmldata-command.xml
b/tests/lxcdocker2xmldata/lxcdocker2xmldata-command.xml
new file mode 100644
index 0000000..52155d1
--- /dev/null
+++ b/tests/lxcdocker2xmldata/lxcdocker2xmldata-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/lxcdocker2xmltest.c b/tests/lxcdocker2xmltest.c
index ccac4c4..dc7778a 100644
--- a/tests/lxcdocker2xmltest.c
+++ b/tests/lxcdocker2xmltest.c
@@ -129,6 +129,7 @@ mymain(void)
} while (0)
DO_TEST("simple", true);
+ DO_TEST("command", true);
virObjectUnref(xmlopt);
virObjectUnref(caps);
--
2.7.4