python-lxc: Update for new calls

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@ubuntu.com>
Tested-by: Serge E. Hallyn <serge.hallyn@ubuntu.com>
Acked-by: Serge E. Hallyn <serge.hallyn@ubuntu.com>
This commit is contained in:
Stéphane Graber 2013-02-11 13:45:20 -05:00
parent afeecbba03
commit edb09f8d1b
2 changed files with 53 additions and 9 deletions

View File

@ -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;
@ -253,6 +254,12 @@ Container_get_config_item(Container *self, PyObject *args, PyObject *kwds)
return PyUnicode_FromString(value);
}
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)
{
@ -348,6 +355,23 @@ Container_set_config_item(Container *self, PyObject *args, PyObject *kwds)
Py_RETURN_FALSE;
}
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)
{
@ -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"

View File

@ -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