[lxc-devel] [PATCH] python-lxc: Update for new calls
Serge Hallyn
serge.hallyn at ubuntu.com
Mon Feb 11 21:26:02 UTC 2013
Quoting Stéphane Graber (stgraber at ubuntu.com):
> Add the two new calls to the API and add the new container_path
> parameter to the constructor (optional).
>
> This also extends list_containers to support the config_path parameter.
> At this point none of the actual tools are changed to make use of those
> as we'll probably want to make sure all the tools get the extra option
> at once.
>
> Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
Tested-by: Serge E. Hallyn <serge.hallyn at ubuntu.com>
Acked-by: Serge E. Hallyn <serge.hallyn at ubuntu.com>
> ---
> src/python-lxc/lxc.c | 42 +++++++++++++++++++++++++++++++++++----
> src/python-lxc/lxc/__init__.py.in | 20 ++++++++++++++-----
> 2 files changed, 53 insertions(+), 9 deletions(-)
>
> diff --git a/src/python-lxc/lxc.c b/src/python-lxc/lxc.c
> index 16356f9..2abdc4c 100644
> --- a/src/python-lxc/lxc.c
> +++ b/src/python-lxc/lxc.c
> @@ -78,14 +78,15 @@ Container_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
> static int
> Container_init(Container *self, PyObject *args, PyObject *kwds)
> {
> - static char *kwlist[] = {"name", NULL};
> + static char *kwlist[] = {"name", "config_path", NULL};
> char *name = NULL;
> + char *config_path = NULL;
>
> - if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|", kwlist,
> - &name))
> + if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|s", kwlist,
> + &name, &config_path))
> return -1;
>
> - self->container = lxc_container_new(name, NULL);
> + self->container = lxc_container_new(name, config_path);
> if (!self->container) {
> fprintf(stderr, "%d: error creating lxc_container %s\n", __LINE__, name);
> return -1;
> @@ -254,6 +255,12 @@ Container_get_config_item(Container *self, PyObject *args, PyObject *kwds)
> }
>
> static PyObject *
> +Container_get_config_path(Container *self, PyObject *args, PyObject *kwds)
> +{
> + return PyUnicode_FromString(self->container->get_config_path(self->container));
> +}
> +
> +static PyObject *
> Container_get_keys(Container *self, PyObject *args, PyObject *kwds)
> {
> static char *kwlist[] = {"key", NULL};
> @@ -349,6 +356,23 @@ Container_set_config_item(Container *self, PyObject *args, PyObject *kwds)
> }
>
> static PyObject *
> +Container_set_config_path(Container *self, PyObject *args, PyObject *kwds)
> +{
> + static char *kwlist[] = {"path", NULL};
> + char *path = NULL;
> +
> + if (! PyArg_ParseTupleAndKeywords(args, kwds, "s|", kwlist,
> + &path))
> + Py_RETURN_FALSE;
> +
> + if (self->container->set_config_path(self->container, path)) {
> + Py_RETURN_TRUE;
> + }
> +
> + Py_RETURN_FALSE;
> +}
> +
> +static PyObject *
> Container_shutdown(Container *self, PyObject *args, PyObject *kwds)
> {
> static char *kwlist[] = {"timeout", NULL};
> @@ -494,6 +518,11 @@ static PyMethodDef Container_methods[] = {
> "\n"
> "Get the current value of a config key."
> },
> + {"get_config_path", (PyCFunction)Container_get_config_path, METH_NOARGS,
> + "get_config_path() -> string\n"
> + "\n"
> + "Return the LXC config path (where the containers are stored)."
> + },
> {"get_keys", (PyCFunction)Container_get_keys, METH_VARARGS | METH_KEYWORDS,
> "get_keys(key) -> string\n"
> "\n"
> @@ -521,6 +550,11 @@ static PyMethodDef Container_methods[] = {
> "\n"
> "Set a config key to the provided value."
> },
> + {"set_config_path", (PyCFunction)Container_set_config_path, METH_VARARGS | METH_KEYWORDS,
> + "set_config_path(path) -> boolean\n"
> + "\n"
> + "Set the LXC config path (where the containers are stored)."
> + },
> {"shutdown", (PyCFunction)Container_shutdown, METH_VARARGS | METH_KEYWORDS,
> "shutdown(timeout = -1) -> boolean\n"
> "\n"
> diff --git a/src/python-lxc/lxc/__init__.py.in b/src/python-lxc/lxc/__init__.py.in
> index 07c956b..151a505 100644
> --- a/src/python-lxc/lxc/__init__.py.in
> +++ b/src/python-lxc/lxc/__init__.py.in
> @@ -33,6 +33,8 @@ import warnings
> warnings.warn("The python-lxc API isn't yet stable "
> "and may change at any point in the future.", Warning, 2)
>
> +default_config_path = "@LXCPATH@"
> +
>
> class ContainerNetwork():
> props = {}
> @@ -143,7 +145,7 @@ class ContainerNetworkList():
>
>
> class Container(_lxc.Container):
> - def __init__(self, name):
> + def __init__(self, name, config_path=None):
> """
> Creates a new Container instance.
> """
> @@ -151,7 +153,11 @@ class Container(_lxc.Container):
> if os.geteuid() != 0:
> raise Exception("Running as non-root.")
>
> - _lxc.Container.__init__(self, name)
> + if config_path:
> + _lxc.Container.__init__(self, name, config_path)
> + else:
> + _lxc.Container.__init__(self, name)
> +
> self.network = ContainerNetworkList(self)
>
> def add_device_node(self, path, destpath=None):
> @@ -455,14 +461,18 @@ class Container(_lxc.Container):
> return _lxc.Container.wait(self, state, timeout)
>
>
> -def list_containers(as_object=False):
> +def list_containers(as_object=False, config_path=None):
> """
> List the containers on the system.
> """
> +
> + if not config_path:
> + config_path = default_config_path
> +
> containers = []
> - for entry in glob.glob("@LXCPATH@/*/config"):
> + for entry in glob.glob("%s/*/config" % config_path):
> if as_object:
> - containers.append(Container(entry.split("/")[-2]))
> + containers.append(Container(entry.split("/")[-2], config_path))
> else:
> containers.append(entry.split("/")[-2])
> return containers
> --
> 1.8.1.2
>
>
> ------------------------------------------------------------------------------
> Free Next-Gen Firewall Hardware Offer
> Buy your Sophos next-gen firewall before the end March 2013
> and get the hardware for free! Learn more.
> http://p.sf.net/sfu/sophos-d2d-feb
> _______________________________________________
> Lxc-devel mailing list
> Lxc-devel at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/lxc-devel
More information about the lxc-devel
mailing list