Annotate

tools/migration/prosody-migrator.lua @ 11728:826d57c16d1c

migrator: Customise startup sequence to fix #1673 (Thanks acidsys) Diverge from util.startup.prosodyctl() in order to skip unneeded behavior, such as loading the *Prosody* config file, which we do not need here, based on the `--config` flag which should point at the migrator config file instead. Notably removed: * read_config() since this loads the Prosody config * check_unwriteable() which checks logfiles specified in the Prosody config, so not relevant * make_dummy_hosts() but the migrator sets up its own hosts during migration
author Kim Alvefur <zash@zash.se>
date Thu, 29 Jul 2021 13:47:26 +0200
parent 10004:e057e8318130
child 11729:f37cafeb75d6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4216
ff80a8471e86 tools/migration/*: Numerous changes and restructuring, and the addition of a Makefile
Matthew Wild <mwild1@gmail.com>
parents: 4211
diff changeset
1 #!/usr/bin/env lua
ff80a8471e86 tools/migration/*: Numerous changes and restructuring, and the addition of a Makefile
Matthew Wild <mwild1@gmail.com>
parents: 4211
diff changeset
2
10003
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
3 CFG_SOURCEDIR=CFG_SOURCEDIR or os.getenv("PROSODY_SRCDIR");
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
4 CFG_CONFIGDIR=CFG_CONFIGDIR or os.getenv("PROSODY_CFGDIR");
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
5 CFG_PLUGINDIR=CFG_PLUGINDIR or os.getenv("PROSODY_PLUGINDIR");
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
6 CFG_DATADIR=CFG_DATADIR or os.getenv("PROSODY_DATADIR");
4216
ff80a8471e86 tools/migration/*: Numerous changes and restructuring, and the addition of a Makefile
Matthew Wild <mwild1@gmail.com>
parents: 4211
diff changeset
7
10003
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
8 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
9
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
10 local function is_relative(path)
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
11 local path_sep = package.config:sub(1,1);
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
12 return ((path_sep == "/" and path:sub(1,1) ~= "/")
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
13 or (path_sep == "\\" and (path:sub(1,1) ~= "/" and path:sub(2,3) ~= ":\\")))
4239
69fe5fd861e7 tools/migration: Support for ~/ in paths
Matthew Wild <mwild1@gmail.com>
parents: 4235
diff changeset
14 end
69fe5fd861e7 tools/migration: Support for ~/ in paths
Matthew Wild <mwild1@gmail.com>
parents: 4235
diff changeset
15
10003
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
16 -- Tell Lua where to find our libraries
4239
69fe5fd861e7 tools/migration: Support for ~/ in paths
Matthew Wild <mwild1@gmail.com>
parents: 4235
diff changeset
17 if CFG_SOURCEDIR then
10003
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
18 local function filter_relative_paths(path)
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
19 if is_relative(path) then return ""; end
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
20 end
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
21 local function sanitise_paths(paths)
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
22 return (paths:gsub("[^;]+;?", filter_relative_paths):gsub(";;+", ";"));
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
23 end
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
24 package.path = sanitise_paths(CFG_SOURCEDIR.."/?.lua;"..package.path);
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
25 package.cpath = sanitise_paths(CFG_SOURCEDIR.."/?.so;"..package.cpath);
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
26 end
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
27
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
28 -- Substitute ~ with path to home directory in data path
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
29 if CFG_DATADIR then
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
30 if os.getenv("HOME") then
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
31 CFG_DATADIR = CFG_DATADIR:gsub("^~", os.getenv("HOME"));
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
32 end
4239
69fe5fd861e7 tools/migration: Support for ~/ in paths
Matthew Wild <mwild1@gmail.com>
parents: 4235
diff changeset
33 end
69fe5fd861e7 tools/migration: Support for ~/ in paths
Matthew Wild <mwild1@gmail.com>
parents: 4235
diff changeset
34
4216
ff80a8471e86 tools/migration/*: Numerous changes and restructuring, and the addition of a Makefile
Matthew Wild <mwild1@gmail.com>
parents: 4211
diff changeset
35 local default_config = (CFG_CONFIGDIR or ".").."/migrator.cfg.lua";
4211
9a12fc2baa37 tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4210
diff changeset
36
10003
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
37 local startup = require "util.startup";
11728
826d57c16d1c migrator: Customise startup sequence to fix #1673 (Thanks acidsys)
Kim Alvefur <zash@zash.se>
parents: 10004
diff changeset
38 do
826d57c16d1c migrator: Customise startup sequence to fix #1673 (Thanks acidsys)
Kim Alvefur <zash@zash.se>
parents: 10004
diff changeset
39 startup.parse_args();
826d57c16d1c migrator: Customise startup sequence to fix #1673 (Thanks acidsys)
Kim Alvefur <zash@zash.se>
parents: 10004
diff changeset
40 startup.init_global_state();
826d57c16d1c migrator: Customise startup sequence to fix #1673 (Thanks acidsys)
Kim Alvefur <zash@zash.se>
parents: 10004
diff changeset
41 prosody.process_type = "migrator";
826d57c16d1c migrator: Customise startup sequence to fix #1673 (Thanks acidsys)
Kim Alvefur <zash@zash.se>
parents: 10004
diff changeset
42 startup.force_console_logging();
826d57c16d1c migrator: Customise startup sequence to fix #1673 (Thanks acidsys)
Kim Alvefur <zash@zash.se>
parents: 10004
diff changeset
43 startup.init_logging();
826d57c16d1c migrator: Customise startup sequence to fix #1673 (Thanks acidsys)
Kim Alvefur <zash@zash.se>
parents: 10004
diff changeset
44 startup.init_gc();
826d57c16d1c migrator: Customise startup sequence to fix #1673 (Thanks acidsys)
Kim Alvefur <zash@zash.se>
parents: 10004
diff changeset
45 startup.init_errors();
826d57c16d1c migrator: Customise startup sequence to fix #1673 (Thanks acidsys)
Kim Alvefur <zash@zash.se>
parents: 10004
diff changeset
46 startup.setup_plugindir();
826d57c16d1c migrator: Customise startup sequence to fix #1673 (Thanks acidsys)
Kim Alvefur <zash@zash.se>
parents: 10004
diff changeset
47 startup.setup_plugin_install_path();
826d57c16d1c migrator: Customise startup sequence to fix #1673 (Thanks acidsys)
Kim Alvefur <zash@zash.se>
parents: 10004
diff changeset
48 startup.setup_datadir();
826d57c16d1c migrator: Customise startup sequence to fix #1673 (Thanks acidsys)
Kim Alvefur <zash@zash.se>
parents: 10004
diff changeset
49 startup.chdir();
826d57c16d1c migrator: Customise startup sequence to fix #1673 (Thanks acidsys)
Kim Alvefur <zash@zash.se>
parents: 10004
diff changeset
50 startup.read_version();
826d57c16d1c migrator: Customise startup sequence to fix #1673 (Thanks acidsys)
Kim Alvefur <zash@zash.se>
parents: 10004
diff changeset
51 startup.switch_user();
826d57c16d1c migrator: Customise startup sequence to fix #1673 (Thanks acidsys)
Kim Alvefur <zash@zash.se>
parents: 10004
diff changeset
52 startup.check_dependencies();
826d57c16d1c migrator: Customise startup sequence to fix #1673 (Thanks acidsys)
Kim Alvefur <zash@zash.se>
parents: 10004
diff changeset
53 startup.log_startup_warnings();
826d57c16d1c migrator: Customise startup sequence to fix #1673 (Thanks acidsys)
Kim Alvefur <zash@zash.se>
parents: 10004
diff changeset
54 startup.load_libraries();
826d57c16d1c migrator: Customise startup sequence to fix #1673 (Thanks acidsys)
Kim Alvefur <zash@zash.se>
parents: 10004
diff changeset
55 startup.init_http_client();
826d57c16d1c migrator: Customise startup sequence to fix #1673 (Thanks acidsys)
Kim Alvefur <zash@zash.se>
parents: 10004
diff changeset
56 end
10003
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
57
4210
4583473dcce4 tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4166
diff changeset
58 -- Command-line parsing
4583473dcce4 tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4166
diff changeset
59 local options = {};
7894
217412da818f migrator: Fix argument parsing
Kim Alvefur <zash@zash.se>
parents: 7893
diff changeset
60 local i = 1;
217412da818f migrator: Fix argument parsing
Kim Alvefur <zash@zash.se>
parents: 7893
diff changeset
61 while arg[i] do
4210
4583473dcce4 tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4166
diff changeset
62 if arg[i]:sub(1,2) == "--" then
4583473dcce4 tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4166
diff changeset
63 local opt, val = arg[i]:match("([%w-]+)=?(.*)");
4583473dcce4 tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4166
diff changeset
64 if opt then
4583473dcce4 tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4166
diff changeset
65 options[(opt:sub(3):gsub("%-", "_"))] = #val > 0 and val or true;
4583473dcce4 tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4166
diff changeset
66 end
7894
217412da818f migrator: Fix argument parsing
Kim Alvefur <zash@zash.se>
parents: 7893
diff changeset
67 table.remove(arg, i);
4210
4583473dcce4 tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4166
diff changeset
68 else
7894
217412da818f migrator: Fix argument parsing
Kim Alvefur <zash@zash.se>
parents: 7893
diff changeset
69 i = i + 1;
4210
4583473dcce4 tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4166
diff changeset
70 end
4583473dcce4 tools/migration/main.lua: Add command-line parsing, including --config=CONFIG_FILE, and the ability to specify to/from stores to migrate
Matthew Wild <mwild1@gmail.com>
parents: 4166
diff changeset
71 end
4166
3ac90743039b tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 4162
diff changeset
72
5021
85b2689dbcfe Eliminate direct setfenv usage
Florian Zeitz <florob@babelmonkeys.de>
parents: 4240
diff changeset
73
85b2689dbcfe Eliminate direct setfenv usage
Florian Zeitz <florob@babelmonkeys.de>
parents: 4240
diff changeset
74 local envloadfile = require "util.envload".envloadfile;
85b2689dbcfe Eliminate direct setfenv usage
Florian Zeitz <florob@babelmonkeys.de>
parents: 4240
diff changeset
75
4211
9a12fc2baa37 tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4210
diff changeset
76 local config_file = options.config or default_config;
9a12fc2baa37 tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4210
diff changeset
77 local from_store = arg[1] or "input";
9a12fc2baa37 tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4210
diff changeset
78 local to_store = arg[2] or "output";
9a12fc2baa37 tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4210
diff changeset
79
4166
3ac90743039b tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 4162
diff changeset
80 config = {};
3ac90743039b tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 4162
diff changeset
81 local config_env = setmetatable({}, { __index = function(t, k) return function(tbl) config[k] = tbl; end; end });
7880
1d998891c967 migrator: Remove wrapper around envloadfile since envloadfile does the right thing in a compatible way
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
82 local config_chunk, err = envloadfile(config_file, config_env);
4211
9a12fc2baa37 tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4210
diff changeset
83 if not config_chunk then
7895
1e1c18012048 migrator: Fix missing word
Kim Alvefur <zash@zash.se>
parents: 7894
diff changeset
84 print("There was an error loading the config file, check that the file exists");
4211
9a12fc2baa37 tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4210
diff changeset
85 print("and that the syntax is correct:");
9a12fc2baa37 tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4210
diff changeset
86 print("", err);
9a12fc2baa37 tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4210
diff changeset
87 os.exit(1);
9a12fc2baa37 tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4210
diff changeset
88 end
9a12fc2baa37 tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4210
diff changeset
89
9a12fc2baa37 tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4210
diff changeset
90 config_chunk();
4166
3ac90743039b tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 4162
diff changeset
91
4211
9a12fc2baa37 tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4210
diff changeset
92 local have_err;
9a12fc2baa37 tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4210
diff changeset
93 if #arg > 0 and #arg ~= 2 then
9a12fc2baa37 tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4210
diff changeset
94 have_err = true;
9a12fc2baa37 tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4210
diff changeset
95 print("Error: Incorrect number of parameters supplied.");
9a12fc2baa37 tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4210
diff changeset
96 end
9a12fc2baa37 tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4210
diff changeset
97 if not config[from_store] then
9a12fc2baa37 tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4210
diff changeset
98 have_err = true;
9a12fc2baa37 tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4210
diff changeset
99 print("Error: Input store '"..from_store.."' not found in the config file.");
9a12fc2baa37 tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4210
diff changeset
100 end
9a12fc2baa37 tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4210
diff changeset
101 if not config[to_store] then
9a12fc2baa37 tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4210
diff changeset
102 have_err = true;
9a12fc2baa37 tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4210
diff changeset
103 print("Error: Output store '"..to_store.."' not found in the config file.");
9a12fc2baa37 tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4210
diff changeset
104 end
4235
899ffc1674b5 tools/migration/prosody-migrator.lua: Refactor store handler loading to report errors they throw
Matthew Wild <mwild1@gmail.com>
parents: 4229
diff changeset
105
10003
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
106 for store, conf in pairs(config) do -- COMPAT
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
107 if conf.type == "prosody_files" then
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
108 conf.type = "internal";
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
109 elseif conf.type == "prosody_sql" then
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
110 conf.type = "sql";
4235
899ffc1674b5 tools/migration/prosody-migrator.lua: Refactor store handler loading to report errors they throw
Matthew Wild <mwild1@gmail.com>
parents: 4229
diff changeset
111 end
4211
9a12fc2baa37 tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4210
diff changeset
112 end
4235
899ffc1674b5 tools/migration/prosody-migrator.lua: Refactor store handler loading to report errors they throw
Matthew Wild <mwild1@gmail.com>
parents: 4229
diff changeset
113
4211
9a12fc2baa37 tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4210
diff changeset
114 if have_err then
9a12fc2baa37 tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4210
diff changeset
115 print("");
9a12fc2baa37 tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4210
diff changeset
116 print("Usage: "..arg[0].." FROM_STORE TO_STORE");
9a12fc2baa37 tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4210
diff changeset
117 print("If no stores are specified, 'input' and 'output' are used.");
9a12fc2baa37 tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4210
diff changeset
118 print("");
9a12fc2baa37 tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4210
diff changeset
119 print("The available stores in your migrator config are:");
9a12fc2baa37 tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4210
diff changeset
120 print("");
9a12fc2baa37 tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4210
diff changeset
121 for store in pairs(config) do
9a12fc2baa37 tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4210
diff changeset
122 print("", store);
9a12fc2baa37 tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4210
diff changeset
123 end
4216
ff80a8471e86 tools/migration/*: Numerous changes and restructuring, and the addition of a Makefile
Matthew Wild <mwild1@gmail.com>
parents: 4211
diff changeset
124 print("");
4211
9a12fc2baa37 tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4210
diff changeset
125 os.exit(1);
9a12fc2baa37 tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
Matthew Wild <mwild1@gmail.com>
parents: 4210
diff changeset
126 end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5021
diff changeset
127
10003
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
128 local async = require "util.async";
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
129 local server = require "net.server";
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
130 local watchers = {
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
131 error = function (_, err)
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
132 error(err);
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
133 end;
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
134 waiting = function ()
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
135 server.loop();
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
136 end;
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
137 };
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
138
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
139 local cm = require "core.configmanager";
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
140 local hm = require "core.hostmanager";
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
141 local sm = require "core.storagemanager";
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
142 local um = require "core.usermanager";
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
143
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
144 local function users(store, host)
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
145 if store.users then
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
146 return store:users();
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
147 else
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
148 return um.users(host);
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
149 end
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
150 end
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
151
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
152 local function prepare_config(host, conf)
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
153 if conf.type == "internal" then
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
154 sm.olddm.set_data_path(conf.path or prosody.paths.data);
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
155 elseif conf.type == "sql" then
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
156 cm.set(host, "sql", conf);
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
157 end
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
158 end
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
159
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
160 local function get_driver(host, conf)
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
161 prepare_config(host, conf);
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
162 return assert(sm.load_driver(host, conf.type));
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
163 end
4166
3ac90743039b tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 4162
diff changeset
164
10003
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
165 local migration_runner = async.runner(function (job)
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
166 for host, stores in pairs(job.input.hosts) do
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
167 prosody.hosts[host] = startup.make_host(host);
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
168 sm.initialize_host(host);
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
169 um.initialize_host(host);
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
170
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
171 local input_driver = get_driver(host, job.input);
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
172
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
173 local output_driver = get_driver(host, job.output);
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
174
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
175 for _, store in ipairs(stores) do
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
176 local p, typ = store:match("()%-(%w+)$");
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
177 if typ then store = store:sub(1, p-1); else typ = "keyval"; end
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
178 log("info", "Migrating host %s store %s (%s)", host, store, typ);
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
179
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
180 local origin = assert(input_driver:open(store, typ));
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
181 local destination = assert(output_driver:open(store, typ));
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
182
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
183 if typ == "keyval" then -- host data
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
184 local data, err = origin:get(nil);
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
185 assert(not err, err);
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
186 assert(destination:set(nil, data));
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
187 end
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
188
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
189 for user in users(origin, host) do
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
190 if typ == "keyval" then
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
191 local data, err = origin:get(user);
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
192 assert(not err, err);
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
193 assert(destination:set(user, data));
10004
e057e8318130 migrator: Add support for archives (fixes #651)
Kim Alvefur <zash@zash.se>
parents: 10003
diff changeset
194 elseif typ == "archive" then
e057e8318130 migrator: Add support for archives (fixes #651)
Kim Alvefur <zash@zash.se>
parents: 10003
diff changeset
195 local iter, err = origin:find(user);
e057e8318130 migrator: Add support for archives (fixes #651)
Kim Alvefur <zash@zash.se>
parents: 10003
diff changeset
196 assert(iter, err);
e057e8318130 migrator: Add support for archives (fixes #651)
Kim Alvefur <zash@zash.se>
parents: 10003
diff changeset
197 for id, item, when, with in iter do
e057e8318130 migrator: Add support for archives (fixes #651)
Kim Alvefur <zash@zash.se>
parents: 10003
diff changeset
198 assert(destination:append(user, id, item, when, with));
e057e8318130 migrator: Add support for archives (fixes #651)
Kim Alvefur <zash@zash.se>
parents: 10003
diff changeset
199 end
10003
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
200 else
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
201 error("Don't know how to migrate data of type '"..typ.."'.");
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
202 end
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
203 end
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
204 end
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
205 end
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
206 end, watchers);
4166
3ac90743039b tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents: 4162
diff changeset
207
4240
b3d9063aad4d tools/migration/prosody-migrator.lua: Add messages to show when migration is in progress
Matthew Wild <mwild1@gmail.com>
parents: 4239
diff changeset
208 io.stderr:write("Migrating...\n");
10003
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
209
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
210 migration_runner:run({ input = config[from_store], output = config[to_store] });
4d702f0c6273 migrator: Rewrite to use storage modules
Kim Alvefur <zash@zash.se>
parents: 8062
diff changeset
211
4240
b3d9063aad4d tools/migration/prosody-migrator.lua: Add messages to show when migration is in progress
Matthew Wild <mwild1@gmail.com>
parents: 4239
diff changeset
212 io.stderr:write("Done!\n");