Commit edb09f8d by Stéphane Graber

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: 's avatarStéphane Graber <stgraber@ubuntu.com> Tested-by: 's avatarSerge E. Hallyn <serge.hallyn@ubuntu.com> Acked-by: 's avatarSerge E. Hallyn <serge.hallyn@ubuntu.com>
parent afeecbba
...@@ -78,14 +78,15 @@ Container_new(PyTypeObject *type, PyObject *args, PyObject *kwds) ...@@ -78,14 +78,15 @@ Container_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
static int static int
Container_init(Container *self, PyObject *args, PyObject *kwds) Container_init(Container *self, PyObject *args, PyObject *kwds)
{ {
static char *kwlist[] = {"name", NULL}; static char *kwlist[] = {"name", "config_path", NULL};
char *name = NULL; char *name = NULL;
char *config_path = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|", kwlist, if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|s", kwlist,
&name)) &name, &config_path))
return -1; return -1;
self->container = lxc_container_new(name, NULL); self->container = lxc_container_new(name, config_path);
if (!self->container) { if (!self->container) {
fprintf(stderr, "%d: error creating lxc_container %s\n", __LINE__, name); fprintf(stderr, "%d: error creating lxc_container %s\n", __LINE__, name);
return -1; return -1;
...@@ -254,6 +255,12 @@ Container_get_config_item(Container *self, PyObject *args, PyObject *kwds) ...@@ -254,6 +255,12 @@ Container_get_config_item(Container *self, PyObject *args, PyObject *kwds)
} }
static PyObject * 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) Container_get_keys(Container *self, PyObject *args, PyObject *kwds)
{ {
static char *kwlist[] = {"key", NULL}; static char *kwlist[] = {"key", NULL};
...@@ -349,6 +356,23 @@ Container_set_config_item(Container *self, PyObject *args, PyObject *kwds) ...@@ -349,6 +356,23 @@ Container_set_config_item(Container *self, PyObject *args, PyObject *kwds)
} }
static PyObject * 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) Container_shutdown(Container *self, PyObject *args, PyObject *kwds)
{ {
static char *kwlist[] = {"timeout", NULL}; static char *kwlist[] = {"timeout", NULL};
...@@ -494,6 +518,11 @@ static PyMethodDef Container_methods[] = { ...@@ -494,6 +518,11 @@ static PyMethodDef Container_methods[] = {
"\n" "\n"
"Get the current value of a config key." "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", (PyCFunction)Container_get_keys, METH_VARARGS | METH_KEYWORDS,
"get_keys(key) -> string\n" "get_keys(key) -> string\n"
"\n" "\n"
...@@ -521,6 +550,11 @@ static PyMethodDef Container_methods[] = { ...@@ -521,6 +550,11 @@ static PyMethodDef Container_methods[] = {
"\n" "\n"
"Set a config key to the provided value." "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", (PyCFunction)Container_shutdown, METH_VARARGS | METH_KEYWORDS,
"shutdown(timeout = -1) -> boolean\n" "shutdown(timeout = -1) -> boolean\n"
"\n" "\n"
......
...@@ -33,6 +33,8 @@ import warnings ...@@ -33,6 +33,8 @@ import warnings
warnings.warn("The python-lxc API isn't yet stable " warnings.warn("The python-lxc API isn't yet stable "
"and may change at any point in the future.", Warning, 2) "and may change at any point in the future.", Warning, 2)
default_config_path = "@LXCPATH@"
class ContainerNetwork(): class ContainerNetwork():
props = {} props = {}
...@@ -143,7 +145,7 @@ class ContainerNetworkList(): ...@@ -143,7 +145,7 @@ class ContainerNetworkList():
class Container(_lxc.Container): class Container(_lxc.Container):
def __init__(self, name): def __init__(self, name, config_path=None):
""" """
Creates a new Container instance. Creates a new Container instance.
""" """
...@@ -151,7 +153,11 @@ class Container(_lxc.Container): ...@@ -151,7 +153,11 @@ class Container(_lxc.Container):
if os.geteuid() != 0: if os.geteuid() != 0:
raise Exception("Running as non-root.") raise Exception("Running as non-root.")
if config_path:
_lxc.Container.__init__(self, name, config_path)
else:
_lxc.Container.__init__(self, name) _lxc.Container.__init__(self, name)
self.network = ContainerNetworkList(self) self.network = ContainerNetworkList(self)
def add_device_node(self, path, destpath=None): def add_device_node(self, path, destpath=None):
...@@ -455,14 +461,18 @@ class Container(_lxc.Container): ...@@ -455,14 +461,18 @@ class Container(_lxc.Container):
return _lxc.Container.wait(self, state, timeout) 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. List the containers on the system.
""" """
if not config_path:
config_path = default_config_path
containers = [] containers = []
for entry in glob.glob("@LXCPATH@/*/config"): for entry in glob.glob("%s/*/config" % config_path):
if as_object: if as_object:
containers.append(Container(entry.split("/")[-2])) containers.append(Container(entry.split("/")[-2], config_path))
else: else:
containers.append(entry.split("/")[-2]) containers.append(entry.split("/")[-2])
return containers return containers
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment