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
|
#!/usr/bin/env lua
CFG_SOURCEDIR=CFG_SOURCEDIR or os.getenv("PROSODY_SRCDIR");
CFG_CONFIGDIR=CFG_CONFIGDIR or os.getenv("PROSODY_CFGDIR");
CFG_PLUGINDIR=CFG_PLUGINDIR or os.getenv("PROSODY_PLUGINDIR");
CFG_DATADIR=CFG_DATADIR or os.getenv("PROSODY_DATADIR");
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
local function is_relative(path)
local path_sep = package.config:sub(1,1);
return ((path_sep == "/" and path:sub(1,1) ~= "/")
or (path_sep == "\\" and (path:sub(1,1) ~= "/" and path:sub(2,3) ~= ":\\")))
end
-- Tell Lua where to find our libraries
if CFG_SOURCEDIR then
local function filter_relative_paths(path)
if is_relative(path) then return ""; end
end
local function sanitise_paths(paths)
return (paths:gsub("[^;]+;?", filter_relative_paths):gsub(";;+", ";"));
end
package.path = sanitise_paths(CFG_SOURCEDIR.."/?.lua;"..package.path);
package.cpath = sanitise_paths(CFG_SOURCEDIR.."/?.so;"..package.cpath);
end
-- Substitute ~ with path to home directory in data path
if CFG_DATADIR then
if os.getenv("HOME") then
CFG_DATADIR = CFG_DATADIR:gsub("^~", os.getenv("HOME"));
end
end
local default_config = (CFG_CONFIGDIR or ".").."/migrator.cfg.lua";
local function usage()
print("Usage: " .. arg[0] .. " [OPTIONS] FROM_STORE TO_STORE");
print(" --config FILE Specify config file")
print(" -v, --verbose Incease log-level");
print("");
print("If no stores are specified, 'input' and 'output' are used.");
end
local startup = require "util.startup";
do
startup.parse_args({
short_params = { v = "verbose", h = "help", ["?"] = "help" };
value_params = { config = true };
});
startup.init_global_state();
prosody.process_type = "migrator";
if prosody.opts.help then
usage();
os.exit(0);
end
startup.force_console_logging();
startup.init_logging();
startup.init_gc();
startup.init_errors();
startup.setup_plugindir();
startup.setup_plugin_install_path();
startup.setup_datadir();
startup.chdir();
startup.read_version();
startup.switch_user();
startup.check_dependencies();
startup.log_startup_warnings();
prosody.config_loaded = true;
startup.load_libraries();
startup.init_http_client();
prosody.core_post_stanza = function ()
-- silence assert in core.moduleapi
error("Attempt to send stanzas from inside migrator.", 0);
end
end
-- Command-line parsing
local options = prosody.opts;
local envloadfile = require "util.envload".envloadfile;
local config_file = options.config or default_config;
local from_store = arg[1] or "input";
local to_store = arg[2] or "output";
config = {};
local config_env = setmetatable({}, { __index = function(t, k) return function(tbl) config[k] = tbl; end; end });
local config_chunk, err = envloadfile(config_file, config_env);
if not config_chunk then
print("There was an error loading the config file, check that the file exists");
print("and that the syntax is correct:");
print("", err);
os.exit(1);
end
config_chunk();
local have_err;
if #arg > 0 and #arg ~= 2 then
have_err = true;
print("Error: Incorrect number of parameters supplied.");
end
if not config[from_store] then
have_err = true;
print("Error: Input store '"..from_store.."' not found in the config file.");
end
if not config[to_store] then
have_err = true;
print("Error: Output store '"..to_store.."' not found in the config file.");
end
for store, conf in pairs(config) do -- COMPAT
if conf.type == "prosody_files" then
conf.type = "internal";
elseif conf.type == "prosody_sql" then
conf.type = "sql";
end
end
if have_err then
print("");
usage();
print("");
print("The available stores in your migrator config are:");
print("");
for store in pairs(config) do
print("", store);
end
print("");
os.exit(1);
end
local async = require "util.async";
local server = require "net.server";
local watchers = {
error = function (_, err)
error(err);
end;
waiting = function ()
server.loop();
end;
};
local cm = require "core.configmanager";
local hm = require "core.hostmanager";
local sm = require "core.storagemanager";
local um = require "core.usermanager";
local function users(store, host)
if store.users then
return store:users();
else
return um.users(host);
end
end
local function prepare_config(host, conf)
if conf.type == "internal" then
sm.olddm.set_data_path(conf.path or prosody.paths.data);
elseif conf.type == "sql" then
cm.set(host, "sql", conf);
end
end
local function get_driver(host, conf)
prepare_config(host, conf);
return assert(sm.load_driver(host, conf.type));
end
local migrate_once = {
keyval = function(origin, destination, user)
local data, err = origin:get(user);
assert(not err, err);
assert(destination:set(user, data));
end;
archive = function(origin, destination, user)
local iter, err = origin:find(user);
assert(iter, err);
for id, item, when, with in iter do
assert(destination:append(user, id, item, when, with));
end
end;
}
local migration_runner = async.runner(function (job)
for host, stores in pairs(job.input.hosts) do
prosody.hosts[host] = startup.make_host(host);
sm.initialize_host(host);
um.initialize_host(host);
local input_driver = get_driver(host, job.input);
local output_driver = get_driver(host, job.output);
for _, store in ipairs(stores) do
local p, typ = store:match("()%-(%w+)$");
if typ then store = store:sub(1, p-1); else typ = "keyval"; end
log("info", "Migrating host %s store %s (%s)", host, store, typ);
local origin = assert(input_driver:open(store, typ));
local destination = assert(output_driver:open(store, typ));
local migrate = assert(migrate_once[typ], "Unknown store type: "..typ);
migrate(origin, destination, nil); -- host data
for user in users(origin, host) do
migrate(origin, destination, user);
end
end
end
end, watchers);
io.stderr:write("Migrating...\n");
migration_runner:run({ input = config[from_store], output = config[to_store] });
io.stderr:write("Done!\n");
|