Commit 83c98d82 by Dwight Engen Committed by Stéphane Graber

Update Lua API

Add [gs]et_config_path from API to Lua binding. Add additional optional parameter to container_new(). Add tests for these new Lua API bindings. Commit 2a59a681 changed the meaning of lxc_path_get() in the binding, causing lua script breakage. Reinstate original behavior of lxc_path_get() and rename it to lxc_default_config_path_get() to make its intent clearer. Signed-off-by: 's avatarDwight Engen <dwight.engen@oracle.com> Acked-by: 's avatarStéphane Graber <stgraber@ubuntu.com>
parent 13f5be62
...@@ -43,9 +43,15 @@ ...@@ -43,9 +43,15 @@
static int container_new(lua_State *L) static int container_new(lua_State *L)
{ {
struct lxc_container *c;
const char *name = luaL_checkstring(L, 1); const char *name = luaL_checkstring(L, 1);
struct lxc_container *c = lxc_container_new(name, NULL); const char *configpath = NULL;
int argc = lua_gettop(L);
if (argc > 1)
configpath = luaL_checkstring(L, 2);
c = lxc_container_new(name, configpath);
if (c) { if (c) {
lua_boxpointer(L, c); lua_boxpointer(L, c);
luaL_getmetatable(L, CONTAINER_TYPENAME); luaL_getmetatable(L, CONTAINER_TYPENAME);
...@@ -238,6 +244,25 @@ static int container_save_config(lua_State *L) ...@@ -238,6 +244,25 @@ static int container_save_config(lua_State *L)
return 1; return 1;
} }
static int container_get_config_path(lua_State *L)
{
struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME);
const char *config_path;
config_path = c->get_config_path(c);
lua_pushstring(L, config_path);
return 1;
}
static int container_set_config_path(lua_State *L)
{
struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME);
const char *config_path = luaL_checkstring(L, 2);
lua_pushboolean(L, !!c->set_config_path(c, config_path));
return 1;
}
static int container_clear_config_item(lua_State *L) static int container_clear_config_item(lua_State *L)
{ {
struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME); struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME);
...@@ -326,6 +351,8 @@ static luaL_Reg lxc_container_methods[] = ...@@ -326,6 +351,8 @@ static luaL_Reg lxc_container_methods[] =
{"config_file_name", container_config_file_name}, {"config_file_name", container_config_file_name},
{"load_config", container_load_config}, {"load_config", container_load_config},
{"save_config", container_save_config}, {"save_config", container_save_config},
{"get_config_path", container_get_config_path},
{"set_config_path", container_set_config_path},
{"get_config_item", container_get_config_item}, {"get_config_item", container_get_config_item},
{"set_config_item", container_set_config_item}, {"set_config_item", container_set_config_item},
{"clear_config_item", container_clear_config_item}, {"clear_config_item", container_clear_config_item},
...@@ -338,18 +365,17 @@ static int lxc_version_get(lua_State *L) { ...@@ -338,18 +365,17 @@ static int lxc_version_get(lua_State *L) {
return 1; return 1;
} }
static int lxc_path_get(lua_State *L) { static int lxc_default_config_path_get(lua_State *L) {
struct lxc_container *c = lua_unboxpointer(L, 1, CONTAINER_TYPENAME); char *lxcpath = lxc_get_default_config_path();
const char *lxcpath;
lxcpath = c->get_config_path(c);
lua_pushstring(L, lxcpath); lua_pushstring(L, lxcpath);
free(lxcpath);
return 1; return 1;
} }
static luaL_Reg lxc_lib_methods[] = { static luaL_Reg lxc_lib_methods[] = {
{"version_get", lxc_version_get}, {"version_get", lxc_version_get},
{"path_get", lxc_path_get}, {"default_config_path_get", lxc_default_config_path_get},
{"container_new", container_new}, {"container_new", container_new},
{NULL, NULL} {NULL, NULL}
}; };
......
...@@ -107,14 +107,18 @@ container = {} ...@@ -107,14 +107,18 @@ container = {}
container_mt = {} container_mt = {}
container_mt.__index = container container_mt.__index = container
function container:new(lname) function container:new(lname, config)
local lcore local lcore
local lnetcfg = {} local lnetcfg = {}
local lstats = {} local lstats = {}
if lname then if lname then
if config then
lcore = core.container_new(lname, config)
else
lcore = core.container_new(lname) lcore = core.container_new(lname)
end end
end
return setmetatable({ctname = lname, core = lcore, netcfg = lnetcfg, stats = lstats}, container_mt) return setmetatable({ctname = lname, core = lcore, netcfg = lnetcfg, stats = lstats}, container_mt)
end end
...@@ -176,6 +180,14 @@ function container:destroy() ...@@ -176,6 +180,14 @@ function container:destroy()
return self.core:destroy() return self.core:destroy()
end end
function container:get_config_path()
return self.core:get_config_path()
end
function container:set_config_path(path)
return self.core:set_config_path(path)
end
function container:append_config_item(key, value) function container:append_config_item(key, value)
return self.core:set_config_item(key, value) return self.core:set_config_item(key, value)
end end
...@@ -408,5 +420,5 @@ function containers_running(names_only) ...@@ -408,5 +420,5 @@ function containers_running(names_only)
return containers return containers
end end
lxc_path = core.path_get() lxc_path = core.default_config_path_get()
cgroup_path = cgroup_path_get() cgroup_path = cgroup_path_get()
...@@ -22,9 +22,10 @@ ...@@ -22,9 +22,10 @@
-- --
local lxc = require("lxc") local lxc = require("lxc")
local lfs = require("lfs")
local getopt = require("alt_getopt") local getopt = require("alt_getopt")
local LXC_PATH = lxc.path_get() local LXC_PATH = lxc.default_config_path_get()
local container local container
local cfg_containers = {} local cfg_containers = {}
...@@ -83,6 +84,28 @@ function test_container_new() ...@@ -83,6 +84,28 @@ function test_container_new()
assert(container:config_file_name() == string.format("%s/%s/config", LXC_PATH, optarg["n"])) assert(container:config_file_name() == string.format("%s/%s/config", LXC_PATH, optarg["n"]))
end end
function test_container_config_path()
local cfgcontainer
local cfgpath = "/tmp/" .. optarg["n"]
local cfgname = cfgpath .. "/config"
log(0, "Test container config path...")
-- create a config file in the new location from container's config
assert(lfs.mkdir(cfgpath))
assert(container:save_config(cfgname))
cfgcontainer = lxc.container:new(optarg["n"], "/tmp")
assert(cfgcontainer ~= nil)
log(0, "cfgname:%s cfgpath:%s", cfgcontainer:config_file_name(), cfgcontainer:get_config_path())
assert(cfgcontainer:config_file_name() == cfgname)
assert(cfgcontainer:get_config_path() == "/tmp")
assert(cfgcontainer:set_config_path(LXC_PATH))
assert(cfgcontainer:get_config_path() == LXC_PATH)
assert(os.remove(cfgname))
assert(lfs.rmdir(cfgpath))
end
function test_container_create() function test_container_create()
if (optarg["c"]) then if (optarg["c"]) then
log(0, "%-20s %s", "Destroy existing container:", optarg["n"]) log(0, "%-20s %s", "Destroy existing container:", optarg["n"])
...@@ -280,6 +303,7 @@ test_container_new() ...@@ -280,6 +303,7 @@ test_container_new()
test_container_create() test_container_create()
test_container_stopped() test_container_stopped()
test_container_in_cfglist(true) test_container_in_cfglist(true)
test_container_config_path()
test_config_items() test_config_items()
test_config_keys() test_config_keys()
......
...@@ -987,6 +987,10 @@ out: ...@@ -987,6 +987,10 @@ out:
return ret; return ret;
} }
char *lxc_get_default_config_path(void)
{
return default_lxc_path();
}
struct lxc_container *lxc_container_new(const char *name, const char *configpath) struct lxc_container *lxc_container_new(const char *name, const char *configpath)
{ {
......
...@@ -86,6 +86,7 @@ struct lxc_container *lxc_container_new(const char *name, const char *configpath ...@@ -86,6 +86,7 @@ struct lxc_container *lxc_container_new(const char *name, const char *configpath
int lxc_container_get(struct lxc_container *c); int lxc_container_get(struct lxc_container *c);
int lxc_container_put(struct lxc_container *c); int lxc_container_put(struct lxc_container *c);
int lxc_get_wait_states(const char **states); int lxc_get_wait_states(const char **states);
char *lxc_get_default_config_path(void);
#if 0 #if 0
char ** lxc_get_valid_keys(); char ** lxc_get_valid_keys();
......
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