Browse Source

rpcd-mod-lxc: add lxc-create ubus bindings

Example call:

$ ubus call lxc create '{ "name": "bar", "template": "download", "args": [ "--no-validate", "--dist", "debian", "--release", "sid", "--arch", "i386" ] }'

Signed-off-by: Luka Perkov <luka@openwrt.org>
Luka Perkov 10 years ago
parent
commit
ceceff5b33
1 changed files with 77 additions and 0 deletions
  1. 77
    0
      utils/rpcd-mod-lxc/files/lxc.c

+ 77
- 0
utils/rpcd-mod-lxc/files/lxc.c View File

@@ -53,6 +53,15 @@ enum {
53 53
 	__RPC_LXC_RENAME_MAX,
54 54
 };
55 55
 
56
+enum {
57
+	RPC_LXC_CREATE_NAME,
58
+	RPC_LXC_CREATE_CONFIG,
59
+	RPC_LXC_CREATE_TEMPLATE,
60
+	RPC_LXC_CREATE_FLAGS,
61
+	RPC_LXC_CREATE_ARGS,
62
+	__RPC_LXC_CREATE_MAX,
63
+};
64
+
56 65
 static const struct blobmsg_policy rpc_lxc_min_policy[__RPC_LXC_MAX] = {
57 66
 	[RPC_LXC_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
58 67
 	[RPC_LXC_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
@@ -70,6 +79,14 @@ static const struct blobmsg_policy rpc_lxc_rename_policy[__RPC_LXC_RENAME_MAX] =
70 79
 	[RPC_LXC_RENAME_NEWNAME] = { .name = "newname", .type = BLOBMSG_TYPE_STRING },
71 80
 };
72 81
 
82
+static const struct blobmsg_policy rpc_lxc_create_policy[__RPC_LXC_CREATE_MAX] = {
83
+	[RPC_LXC_CREATE_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
84
+	[RPC_LXC_CREATE_CONFIG] = { .name = "config", .type = BLOBMSG_TYPE_STRING },
85
+	[RPC_LXC_CREATE_TEMPLATE] = { .name = "template", .type = BLOBMSG_TYPE_STRING },
86
+	[RPC_LXC_CREATE_FLAGS] = { .name = "flags", .type = BLOBMSG_TYPE_INT32 },
87
+	[RPC_LXC_CREATE_ARGS] = { .name = "args", .type = BLOBMSG_TYPE_ARRAY },
88
+};
89
+
73 90
 static struct rpc_lxc *
74 91
 rpc_lxc_init(struct blob_attr *tb[__RPC_LXC_MAX])
75 92
 {
@@ -335,6 +352,65 @@ out:
335 352
 	return rc;
336 353
 }
337 354
 
355
+static int
356
+rpc_lxc_create(struct ubus_context *ctx, struct ubus_object *obj,
357
+		struct ubus_request_data *req, const char *method,
358
+		struct blob_attr *msg)
359
+{
360
+	struct blob_attr *tb[__RPC_LXC_CREATE_MAX];
361
+	struct rpc_lxc *l = NULL;
362
+	int rc;
363
+
364
+	char *template = "none";
365
+	int flags = 0;
366
+	char **args = NULL;
367
+
368
+	blobmsg_parse(rpc_lxc_create_policy, __RPC_LXC_CREATE_MAX, tb, blob_data(msg), blob_len(msg));
369
+
370
+	l = rpc_lxc_init(tb);
371
+	if (!l) return UBUS_STATUS_INVALID_ARGUMENT;
372
+
373
+	if (tb[RPC_LXC_CREATE_TEMPLATE])
374
+		template = blobmsg_data(tb[RPC_LXC_CREATE_TEMPLATE]);
375
+
376
+	if (tb[RPC_LXC_CREATE_FLAGS])
377
+		flags = blobmsg_get_u32(tb[RPC_LXC_CREATE_FLAGS]);
378
+
379
+	if (tb[RPC_LXC_CREATE_ARGS]) {
380
+		struct blob_attr *cur;
381
+		int num, rem;
382
+
383
+		num = blobmsg_check_array(tb[RPC_LXC_CREATE_ARGS], BLOBMSG_TYPE_STRING);
384
+
385
+		// trailing NULL is needed
386
+		args = calloc(num + 1, sizeof(char *));
387
+		if (!args) {
388
+			rc = UBUS_STATUS_UNKNOWN_ERROR;
389
+			goto out;
390
+		}
391
+
392
+		num = 0;
393
+		blobmsg_for_each_attr(cur, tb[RPC_LXC_CREATE_ARGS], rem)
394
+		{
395
+			if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
396
+				continue;
397
+
398
+			args[num++] = (char *) blobmsg_data(cur);
399
+		}
400
+	}
401
+
402
+	if (!l->container->create(l->container, template, NULL, NULL, flags, args)) {
403
+		rc = UBUS_STATUS_INVALID_ARGUMENT;
404
+		goto out;
405
+	}
406
+
407
+	rc = UBUS_STATUS_OK;
408
+out:
409
+	rpc_lxc_done(l);
410
+	free(args);
411
+	return rc;
412
+}
413
+
338 414
 static int
339 415
 rpc_lxc_destroy(struct ubus_context *ctx, struct ubus_object *obj,
340 416
 		struct ubus_request_data *req, const char *method,
@@ -405,6 +481,7 @@ rpc_lxc_api_init(const struct rpc_daemon_ops *o, struct ubus_context *ctx)
405 481
 		UBUS_METHOD("freeze", rpc_lxc_freeze, rpc_lxc_min_policy),
406 482
 		UBUS_METHOD("unfreeze", rpc_lxc_unfreeze, rpc_lxc_min_policy),
407 483
 		UBUS_METHOD("rename", rpc_lxc_rename, rpc_lxc_rename_policy),
484
+		UBUS_METHOD("create", rpc_lxc_create, rpc_lxc_create_policy),
408 485
 		UBUS_METHOD("destroy", rpc_lxc_destroy, rpc_lxc_min_policy),
409 486
 		UBUS_METHOD_NOARG("list", rpc_lxc_list),
410 487
 	};