1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab ft=cpp
#include <lua.hpp>
#include "services/svc_zone.h"
#include "rgw_lua_utils.h"
#include "rgw_sal_rados.h"
#include "rgw_lua.h"
#ifdef WITH_RADOSGW_LUA_PACKAGES
#include <filesystem>
#include <boost/process.hpp>
#endif
#define dout_subsys ceph_subsys_rgw
namespace rgw::lua {
context to_context(const std::string& s)
{
if (strcasecmp(s.c_str(), "prerequest") == 0) {
return context::preRequest;
}
if (strcasecmp(s.c_str(), "postrequest") == 0) {
return context::postRequest;
}
if (strcasecmp(s.c_str(), "background") == 0) {
return context::background;
}
if (strcasecmp(s.c_str(), "getdata") == 0) {
return context::getData;
}
if (strcasecmp(s.c_str(), "putdata") == 0) {
return context::putData;
}
return context::none;
}
std::string to_string(context ctx)
{
switch (ctx) {
case context::preRequest:
return "prerequest";
case context::postRequest:
return "postrequest";
case context::background:
return "background";
case context::getData:
return "getdata";
case context::putData:
return "putdata";
case context::none:
break;
}
return "none";
}
bool verify(const std::string& script, std::string& err_msg)
{
lua_state_guard lguard(0, nullptr); // no memory limit, sice we don't execute the script
auto L = lguard.get();
try {
open_standard_libs(L);
if (luaL_loadstring(L, script.c_str()) != LUA_OK) {
err_msg.assign(lua_tostring(L, -1));
return false;
}
} catch (const std::runtime_error& e) {
err_msg = e.what();
return false;
}
err_msg = "";
return true;
}
std::string script_oid(context ctx, const std::string& tenant) {
static const std::string SCRIPT_OID_PREFIX("script.");
return SCRIPT_OID_PREFIX + to_string(ctx) + "." + tenant;
}
int read_script(const DoutPrefixProvider *dpp, sal::LuaManager* manager, const std::string& tenant, optional_yield y, context ctx, std::string& script)
{
return manager ? manager->get_script(dpp, y, script_oid(ctx, tenant), script) : -ENOENT;
}
int write_script(const DoutPrefixProvider *dpp, sal::LuaManager* manager, const std::string& tenant, optional_yield y, context ctx, const std::string& script)
{
return manager ? manager->put_script(dpp, y, script_oid(ctx, tenant), script) : -ENOENT;
}
int delete_script(const DoutPrefixProvider *dpp, sal::LuaManager* manager, const std::string& tenant, optional_yield y, context ctx)
{
return manager ? manager->del_script(dpp, y, script_oid(ctx, tenant)) : -ENOENT;
}
#ifdef WITH_RADOSGW_LUA_PACKAGES
namespace bp = boost::process;
int add_package(const DoutPrefixProvider* dpp, rgw::sal::Driver* driver, optional_yield y, const std::string& package_name, bool allow_compilation)
{
// verify that luarocks can load this package
const auto p = bp::search_path("luarocks");
if (p.empty()) {
return -ECHILD;
}
bp::ipstream is;
const auto cmd = p.string() + " search --porcelain" + (allow_compilation ? " " : " --binary ") + package_name;
bp::child c(cmd,
bp::std_in.close(),
bp::std_err > bp::null,
bp::std_out > is);
std::string line;
bool package_found = false;
while (c.running() && std::getline(is, line) && !line.empty()) {
package_found = true;
}
c.wait();
auto ret = c.exit_code();
if (ret) {
return -ret;
}
if (!package_found) {
return -EINVAL;
}
//replace previous versions of the package
const std::string package_name_no_version = package_name.substr(0, package_name.find(" "));
ret = remove_package(dpp, driver, y, package_name_no_version);
if (ret < 0) {
return ret;
}
return driver->get_lua_manager("")->add_package(dpp, y, package_name);
}
int remove_package(const DoutPrefixProvider *dpp, rgw::sal::Driver* driver, optional_yield y, const std::string& package_name)
{
return driver->get_lua_manager("")->remove_package(dpp, y, package_name);
}
namespace bp = boost::process;
int list_packages(const DoutPrefixProvider *dpp, rgw::sal::Driver* driver, optional_yield y, packages_t& packages)
{
return driver->get_lua_manager("")->list_packages(dpp, y, packages);
}
namespace fs = std::filesystem;
// similar to to the "mkdir -p" command
int create_directory_p(const DoutPrefixProvider *dpp, const fs::path& p) {
std::error_code ec;
fs::path total_path;
for (const auto& pp : p) {
total_path /= pp;
auto should_create = !fs::exists(total_path, ec);
if (ec) {
ldpp_dout(dpp, 1) << "cannot check if " << total_path <<
" directory exists. error: " << ec.message() << dendl;
return -ec.value();
}
if (should_create) {
if (!create_directory(total_path, ec)) {
ldpp_dout(dpp, 1) << "failed to create " << total_path <<
" directory. error: " << ec.message() << dendl;
return -ec.value();
}
}
}
return 0;
}
void get_luarocks_config(const bp::filesystem::path& process,
const std::string& luarocks_path,
const bp::environment& env, std::string& output) {
bp::ipstream is;
auto cmd = process.string();
cmd.append(" config");
output.append("Lua CMD: ");
output.append(cmd);
try {
bp::child c(cmd, env, bp::std_in.close(), (bp::std_err & bp::std_out) > is, bp::start_dir(luarocks_path));
std::string line;
do {
if (!line.empty()) {
output.append("\n\t").append(line);
}
} while (c.running() && std::getline(is, line));
c.wait();
output.append("\n\t").append("exit code: ").append(std::to_string(c.exit_code()));
} catch (const std::runtime_error& err) {
output.append("\n\t").append(err.what());
}
}
int install_packages(const DoutPrefixProvider *dpp, rgw::sal::Driver* driver,
optional_yield y, const std::string& luarocks_path,
packages_t& failed_packages, std::string& install_dir) {
packages_t packages;
auto ret = list_packages(dpp, driver, y, packages);
if (ret == -ENOENT) {
// allowlist is empty
return 0;
}
if (ret < 0) {
ldpp_dout(dpp, 1) << "Lua ERROR: failed to get package list. error: " << ret << dendl;
return ret;
}
// verify that luarocks exists
const auto p = bp::search_path("luarocks");
if (p.empty()) {
ldpp_dout(dpp, 1) << "Lua ERROR: failed to find luarocks" << dendl;
return -ECHILD;
}
// create the luarocks parent directory
auto rc = create_directory_p(dpp, luarocks_path);
if (rc < 0) {
ldpp_dout(dpp, 1) << "Lua ERROR: failed to recreate luarocks directory: " <<
luarocks_path << ". error: " << rc << dendl;
return rc;
}
// create a temporary sub-directory to install all luarocks packages
std::string tmp_path_template = luarocks_path;// fs::temp_directory_path();
tmp_path_template.append("/XXXXXX");
const auto tmp_luarocks_path = mkdtemp(tmp_path_template.data());
if (!tmp_luarocks_path) {
const auto rc = -errno;
ldpp_dout(dpp, 1) << "Lua ERROR: failed to create temporary directory from template: " <<
tmp_path_template << ". error: " << rc << dendl;
return rc;
}
install_dir.assign(tmp_luarocks_path);
// get a handle to the current environment
auto env = boost::this_process::environment();
bp::environment _env = env;
_env["HOME"] = luarocks_path;
if (dpp->get_cct()->_conf->subsys.should_gather<ceph_subsys_rgw, 20>()) {
std::string output;
get_luarocks_config(p, luarocks_path, _env, output);
ldpp_dout(dpp, 20) << output << dendl;
}
// the lua rocks install dir will be created by luarocks the first time it is called
for (const auto& package : packages) {
bp::ipstream is;
auto cmd = p.string();
cmd.append(" install --no-doc --lua-version ").
append(CEPH_LUA_VERSION).
append(" --tree ").
append(install_dir).
append(" --deps-mode one ").
append(package);
bp::child c(cmd, _env, bp::std_in.close(), (bp::std_err & bp::std_out) > is, bp::start_dir(luarocks_path));
if (dpp->get_cct()->_conf->subsys.should_gather<ceph_subsys_rgw, 20>()) {
// TODO: yield when reading output
std::string lines = std::string("Lua CMD: ");
lines.append(cmd);
std::string line;
do {
if (!line.empty()) {
lines.append("\n\t").append(line);
}
} while (c.running() && std::getline(is, line));
ldpp_dout(dpp, 20) << lines << dendl;
}
c.wait();
if (c.exit_code()) {
failed_packages.insert(package);
}
}
return 0;
}
int reload_packages(const DoutPrefixProvider *dpp, rgw::sal::Driver* driver, optional_yield y)
{
return driver->get_lua_manager("")->reload_packages(dpp, y);
}
#endif // WITH_RADOSGW_LUA_PACKAGES
}
|