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;
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)
{
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)
{
"\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"
"\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"
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 = {}
class Container(_lxc.Container):
- def __init__(self, name):
+ def __init__(self, name, config_path=None):
"""
Creates a new Container instance.
"""
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):
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