]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | /* | |
4 | * Ceph - scalable distributed file system | |
5 | * | |
6 | * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net> | |
7 | * | |
8 | * This is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public | |
10 | * License version 2.1, as published by the Free Software | |
11 | * Foundation. See file COPYING. | |
12 | * | |
13 | */ | |
14 | #include <string> | |
15 | ||
16 | #include "common/ceph_argparse.h" | |
17 | #include "common/errno.h" | |
18 | ||
19 | #include "global/global_init.h" | |
20 | #include "include/str_list.h" | |
21 | #include "mon/MonMap.h" | |
22 | ||
7c673cae FG |
23 | |
24 | void usage() | |
25 | { | |
11fdf7f2 TL |
26 | cout << "usage: monmaptool [--print] [--create [--clobber] [--fsid uuid]]\n" |
27 | << " [--enable-all-features]\n" | |
7c673cae FG |
28 | << " [--generate] [--set-initial-members]\n" |
29 | << " [--add name 1.2.3.4:567] [--rm name]\n" | |
9f95a23c | 30 | << " [--addv name [v2:1.2.4.5:567,v1:1.2.3.4:568]]\n" |
7c673cae FG |
31 | << " [--feature-list [plain|parseable]]\n" |
32 | << " [--feature-set <value> [--optional|--persistent]]\n" | |
11fdf7f2 TL |
33 | << " [--feature-unset <value> [--optional|--persistent]]\n" |
34 | << " [--set-min-mon-release <release-major-number>]\n" | |
35 | << " <mapfilename>" | |
7c673cae | 36 | << std::endl; |
11fdf7f2 TL |
37 | } |
38 | ||
39 | void helpful_exit() | |
40 | { | |
41 | cerr << "monmaptool -h for usage" << std::endl; | |
7c673cae FG |
42 | exit(1); |
43 | } | |
44 | ||
45 | struct feature_op_t { | |
46 | enum type_t { | |
47 | PERSISTENT, | |
48 | OPTIONAL, | |
49 | PLAIN, | |
50 | PARSEABLE, | |
51 | NONE | |
52 | }; | |
53 | ||
54 | enum op_t { | |
55 | OP_SET, | |
56 | OP_UNSET, | |
57 | OP_LIST | |
58 | }; | |
59 | ||
60 | op_t op; | |
61 | type_t type; | |
62 | mon_feature_t feature; | |
63 | ||
64 | feature_op_t() : op(OP_LIST), type(NONE) { } | |
65 | // default to 'persistent' feature if not specified | |
66 | feature_op_t(op_t o) : op(o), type(PERSISTENT) { } | |
67 | feature_op_t(op_t o, type_t t) : op(o), type(t) { } | |
68 | feature_op_t(op_t o, type_t t, mon_feature_t &f) : | |
69 | op(o), type(t), feature(t) { } | |
70 | ||
71 | void set_optional() { | |
72 | type = OPTIONAL; | |
73 | } | |
74 | void set_persistent() { | |
75 | type = PERSISTENT; | |
76 | } | |
77 | bool parse_value(string &s, ostream *errout = NULL) { | |
78 | ||
79 | feature = ceph::features::mon::get_feature_by_name(s); | |
80 | if (feature != ceph::features::mon::FEATURE_NONE) { | |
81 | return true; | |
82 | } | |
83 | ||
84 | // try parsing as numerical value | |
85 | uint64_t feature_val; | |
86 | string interr; | |
87 | feature_val = strict_strtoll(s.c_str(), 10, &interr); | |
88 | if (!interr.empty()) { | |
89 | if (errout) { | |
90 | *errout << "unknown features name '" << s | |
91 | << "' or unable to parse value: " << interr << std::endl; | |
92 | } | |
93 | return false; | |
94 | } | |
95 | feature = mon_feature_t(feature_val); | |
96 | return true; | |
97 | } | |
98 | }; | |
99 | ||
100 | void features_list(feature_op_t &f, MonMap &m) | |
101 | { | |
102 | if (f.type == feature_op_t::type_t::PLAIN) { | |
103 | ||
104 | cout << "MONMAP FEATURES:" << std::endl; | |
105 | cout << " persistent: "; | |
106 | m.persistent_features.print_with_value(cout); | |
107 | cout << std::endl; | |
108 | cout << " optional: "; | |
109 | m.optional_features.print_with_value(cout); | |
110 | cout << std::endl; | |
111 | cout << " required: "; | |
112 | m.get_required_features().print_with_value(cout); | |
113 | cout << std::endl; | |
114 | ||
115 | cout << std::endl; | |
116 | cout << "AVAILABLE FEATURES:" << std::endl; | |
117 | cout << " supported: "; | |
118 | ceph::features::mon::get_supported().print_with_value(cout); | |
119 | cout << std::endl; | |
120 | cout << " persistent: "; | |
121 | ceph::features::mon::get_persistent().print_with_value(cout); | |
122 | cout << std::endl; | |
123 | } else if (f.type == feature_op_t::type_t::PARSEABLE) { | |
124 | ||
125 | cout << "monmap:persistent:"; | |
126 | m.persistent_features.print_with_value(cout); | |
127 | cout << std::endl; | |
128 | cout << "monmap:optional:"; | |
129 | m.optional_features.print_with_value(cout); | |
130 | cout << std::endl; | |
131 | cout << "monmap:required:"; | |
132 | m.get_required_features().print_with_value(cout); | |
133 | cout << std::endl; | |
134 | cout << "available:supported:"; | |
135 | ceph::features::mon::get_supported().print_with_value(cout); | |
136 | cout << std::endl; | |
137 | cout << "available:persistent:"; | |
138 | ceph::features::mon::get_persistent().print_with_value(cout); | |
139 | cout << std::endl; | |
140 | } | |
141 | } | |
142 | ||
143 | bool handle_features(list<feature_op_t>& lst, MonMap &m) | |
144 | { | |
145 | if (lst.empty()) | |
146 | return false; | |
147 | ||
148 | bool modified = false; | |
149 | ||
150 | for (auto &f : lst) { | |
151 | if (f.op == feature_op_t::op_t::OP_LIST) { | |
152 | features_list(f, m); | |
153 | } else if (f.op == feature_op_t::op_t::OP_SET || | |
154 | f.op == feature_op_t::op_t::OP_UNSET) { | |
155 | ||
156 | modified = true; | |
157 | ||
158 | mon_feature_t &target = | |
159 | ( f.type == feature_op_t::type_t::OPTIONAL ? | |
160 | m.optional_features : m.persistent_features ); | |
161 | ||
162 | if (f.op == feature_op_t::op_t::OP_SET) { | |
163 | target.set_feature(f.feature); | |
164 | } else { | |
165 | target.unset_feature(f.feature); | |
166 | } | |
167 | } else { | |
11fdf7f2 | 168 | cerr << "unknown feature operation type '" << f.op << "'" << std::endl; |
7c673cae FG |
169 | } |
170 | } | |
171 | return modified; | |
172 | } | |
173 | ||
174 | int main(int argc, const char **argv) | |
175 | { | |
176 | vector<const char*> args; | |
177 | argv_to_vec(argc, argv, args); | |
11fdf7f2 TL |
178 | if (args.empty()) { |
179 | cerr << argv[0] << ": -h or --help for usage" << std::endl; | |
180 | exit(1); | |
181 | } | |
182 | if (ceph_argparse_need_usage(args)) { | |
183 | usage(); | |
184 | exit(0); | |
185 | } | |
7c673cae FG |
186 | |
187 | const char *me = argv[0]; | |
188 | ||
189 | std::string fn; | |
190 | bool print = false; | |
191 | bool create = false; | |
11fdf7f2 | 192 | bool enable_all_features = false; |
7c673cae FG |
193 | bool clobber = false; |
194 | bool modified = false; | |
195 | bool show_features = false; | |
196 | bool generate = false; | |
197 | bool filter = false; | |
9f95a23c | 198 | ceph_release_t min_mon_release{0}; |
7c673cae | 199 | map<string,entity_addr_t> add; |
11fdf7f2 | 200 | map<string,entity_addrvec_t> addv; |
7c673cae FG |
201 | list<string> rm; |
202 | list<feature_op_t> features; | |
203 | ||
204 | auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT, | |
205 | CODE_ENVIRONMENT_UTILITY, | |
206 | CINIT_FLAG_NO_DEFAULT_CONFIG_FILE); | |
207 | common_init_finish(g_ceph_context); | |
208 | std::string val; | |
209 | for (std::vector<const char*>::iterator i = args.begin(); i != args.end(); ) { | |
210 | if (ceph_argparse_double_dash(args, i)) { | |
211 | break; | |
7c673cae FG |
212 | } else if (ceph_argparse_flag(args, i, "-p", "--print", (char*)NULL)) { |
213 | print = true; | |
214 | } else if (ceph_argparse_flag(args, i, "--create", (char*)NULL)) { | |
215 | create = true; | |
11fdf7f2 TL |
216 | } else if (ceph_argparse_flag(args, i, "--enable-all-features", (char*)NULL)) { |
217 | enable_all_features = true; | |
7c673cae FG |
218 | } else if (ceph_argparse_flag(args, i, "--clobber", (char*)NULL)) { |
219 | clobber = true; | |
220 | } else if (ceph_argparse_flag(args, i, "--generate", (char*)NULL)) { | |
221 | generate = true; | |
222 | } else if (ceph_argparse_flag(args, i, "--set-initial-members", (char*)NULL)) { | |
223 | filter = true; | |
11fdf7f2 TL |
224 | } else if (ceph_argparse_witharg(args, i, &val, "--set-min-mon-release", |
225 | (char*)NULL)) { | |
9f95a23c | 226 | min_mon_release = ceph_release_from_name(val); |
7c673cae FG |
227 | } else if (ceph_argparse_flag(args, i, "--add", (char*)NULL)) { |
228 | string name = *i; | |
229 | i = args.erase(i); | |
230 | if (i == args.end()) | |
11fdf7f2 | 231 | helpful_exit(); |
7c673cae FG |
232 | entity_addr_t addr; |
233 | if (!addr.parse(*i)) { | |
234 | cerr << me << ": invalid ip:port '" << *i << "'" << std::endl; | |
235 | return -1; | |
236 | } | |
7c673cae FG |
237 | add[name] = addr; |
238 | modified = true; | |
239 | i = args.erase(i); | |
11fdf7f2 TL |
240 | } else if (ceph_argparse_flag(args, i, "--addv", (char*)NULL)) { |
241 | string name = *i; | |
242 | i = args.erase(i); | |
243 | if (i == args.end()) | |
244 | helpful_exit(); | |
245 | entity_addrvec_t addrs; | |
246 | if (!addrs.parse(*i)) { | |
247 | cerr << me << ": invalid ip:port '" << *i << "'" << std::endl; | |
248 | return -1; | |
249 | } | |
250 | addv[name] = addrs; | |
251 | modified = true; | |
252 | i = args.erase(i); | |
7c673cae FG |
253 | } else if (ceph_argparse_witharg(args, i, &val, "--rm", (char*)NULL)) { |
254 | rm.push_back(val); | |
255 | modified = true; | |
256 | } else if (ceph_argparse_flag(args, i, "--feature-list", (char*)NULL)) { | |
257 | string format = *i; | |
258 | if (format == "plain" || format == "parseable") { | |
259 | i = args.erase(i); | |
260 | } else { | |
261 | format = "plain"; | |
262 | } | |
263 | ||
264 | feature_op_t f(feature_op_t::op_t::OP_LIST, | |
265 | feature_op_t::type_t::PLAIN); | |
266 | ||
267 | if (format == "parseable") { | |
268 | f.type = feature_op_t::type_t::PARSEABLE; | |
269 | } else if (format != "plain") { | |
270 | cerr << "invalid format type for list: '" << val << "'" << std::endl; | |
11fdf7f2 | 271 | helpful_exit(); |
7c673cae FG |
272 | } |
273 | ||
274 | features.push_back(f); | |
275 | show_features = true; | |
276 | } else if (ceph_argparse_witharg(args, i, &val, | |
277 | "--feature-set", (char*)NULL)) { | |
278 | // parse value | |
279 | feature_op_t f(feature_op_t::op_t::OP_SET); | |
280 | if (!f.parse_value(val, &cerr)) { | |
11fdf7f2 | 281 | helpful_exit(); |
7c673cae FG |
282 | } |
283 | features.push_back(f); | |
284 | ||
285 | } else if (ceph_argparse_witharg(args, i, &val, | |
286 | "--feature-unset", (char*)NULL)) { | |
287 | // parse value | |
288 | feature_op_t f(feature_op_t::op_t::OP_UNSET); | |
289 | if (!f.parse_value(val, &cerr)) { | |
11fdf7f2 | 290 | helpful_exit(); |
7c673cae FG |
291 | } |
292 | features.push_back(f); | |
293 | } else if (ceph_argparse_flag(args, i, "--optional", (char*)NULL)) { | |
294 | if (features.empty()) { | |
11fdf7f2 | 295 | helpful_exit(); |
7c673cae FG |
296 | } |
297 | features.back().set_optional(); | |
298 | } else if (ceph_argparse_flag(args, i, "--persistent", (char*)NULL)) { | |
299 | if (features.empty()) { | |
11fdf7f2 | 300 | helpful_exit(); |
7c673cae FG |
301 | } |
302 | features.back().set_persistent(); | |
303 | } else { | |
304 | ++i; | |
305 | } | |
306 | } | |
307 | if (args.empty()) { | |
308 | cerr << me << ": must specify monmap filename" << std::endl; | |
11fdf7f2 | 309 | helpful_exit(); |
7c673cae FG |
310 | } |
311 | else if (args.size() > 1) { | |
312 | cerr << me << ": too many arguments" << std::endl; | |
11fdf7f2 | 313 | helpful_exit(); |
7c673cae FG |
314 | } |
315 | fn = args[0]; | |
316 | ||
317 | MonMap monmap; | |
318 | ||
319 | cout << me << ": monmap file " << fn << std::endl; | |
320 | ||
321 | int r = 0; | |
322 | if (!(create && clobber)) { | |
323 | try { | |
324 | r = monmap.read(fn.c_str()); | |
325 | } catch (...) { | |
326 | cerr << me << ": unable to read monmap file" << std::endl; | |
327 | return -1; | |
328 | } | |
329 | } | |
330 | ||
331 | if (!create && r < 0) { | |
332 | cerr << me << ": couldn't open " << fn << ": " << cpp_strerror(r) << std::endl; | |
333 | return -1; | |
334 | } | |
335 | else if (create && !clobber && r == 0) { | |
336 | cerr << me << ": " << fn << " exists, --clobber to overwrite" << std::endl; | |
337 | return -1; | |
338 | } | |
339 | ||
340 | if (create) { | |
341 | monmap.epoch = 0; | |
342 | monmap.created = ceph_clock_now(); | |
343 | monmap.last_changed = monmap.created; | |
344 | srand(getpid() + time(0)); | |
11fdf7f2 | 345 | if (g_conf().get_val<uuid_d>("fsid").is_zero()) { |
7c673cae FG |
346 | monmap.generate_fsid(); |
347 | cout << me << ": generated fsid " << monmap.fsid << std::endl; | |
348 | } | |
349 | modified = true; | |
350 | } | |
11fdf7f2 TL |
351 | if (enable_all_features) { |
352 | // populate persistent features, too | |
353 | monmap.persistent_features = ceph::features::mon::get_persistent(); | |
354 | modified = true; | |
355 | } | |
7c673cae FG |
356 | |
357 | if (generate) { | |
11fdf7f2 | 358 | int r = monmap.build_initial(g_ceph_context, true, cerr); |
7c673cae FG |
359 | if (r < 0) |
360 | return r; | |
361 | } | |
362 | ||
9f95a23c | 363 | if (min_mon_release != ceph_release_t::unknown) { |
11fdf7f2 TL |
364 | monmap.min_mon_release = min_mon_release; |
365 | cout << "setting min_mon_release = " << min_mon_release << std::endl; | |
366 | modified = true; | |
367 | } | |
368 | ||
7c673cae FG |
369 | if (filter) { |
370 | // apply initial members | |
371 | list<string> initial_members; | |
11fdf7f2 | 372 | get_str_list(g_conf()->mon_initial_members, initial_members); |
7c673cae FG |
373 | if (!initial_members.empty()) { |
374 | cout << "initial_members " << initial_members << ", filtering seed monmap" << std::endl; | |
11fdf7f2 | 375 | set<entity_addrvec_t> removed; |
7c673cae | 376 | monmap.set_initial_members(g_ceph_context, initial_members, |
11fdf7f2 | 377 | string(), entity_addrvec_t(), |
7c673cae FG |
378 | &removed); |
379 | cout << "removed " << removed << std::endl; | |
380 | } | |
381 | modified = true; | |
382 | } | |
383 | ||
11fdf7f2 TL |
384 | if (!g_conf().get_val<uuid_d>("fsid").is_zero()) { |
385 | monmap.fsid = g_conf().get_val<uuid_d>("fsid"); | |
7c673cae FG |
386 | cout << me << ": set fsid to " << monmap.fsid << std::endl; |
387 | modified = true; | |
388 | } | |
389 | ||
11fdf7f2 TL |
390 | for (auto& p : add) { |
391 | entity_addr_t addr = p.second; | |
392 | entity_addrvec_t addrs; | |
393 | if (monmap.contains(p.first)) { | |
394 | cerr << me << ": map already contains mon." << p.first << std::endl; | |
395 | helpful_exit(); | |
396 | } | |
397 | if (addr.get_port() == 0) { | |
398 | if (monmap.persistent_features.contains_all( | |
399 | ceph::features::mon::FEATURE_NAUTILUS)) { | |
400 | addr.set_type(entity_addr_t::TYPE_MSGR2); | |
401 | addr.set_port(CEPH_MON_PORT_IANA); | |
402 | addrs.v.push_back(addr); | |
403 | addr.set_type(entity_addr_t::TYPE_LEGACY); | |
404 | addr.set_port(CEPH_MON_PORT_LEGACY); | |
405 | addrs.v.push_back(addr); | |
406 | } else { | |
407 | addr.set_type(entity_addr_t::TYPE_LEGACY); | |
408 | addr.set_port(CEPH_MON_PORT_LEGACY); | |
409 | addrs.v.push_back(addr); | |
410 | } | |
411 | } else if (addr.get_port() == CEPH_MON_PORT_LEGACY) { | |
412 | addr.set_type(entity_addr_t::TYPE_LEGACY); | |
413 | addrs.v.push_back(addr); | |
414 | } else { | |
415 | if (monmap.persistent_features.contains_all( | |
416 | ceph::features::mon::FEATURE_NAUTILUS)) { | |
417 | addr.set_type(entity_addr_t::TYPE_MSGR2); | |
418 | } | |
419 | addrs.v.push_back(addr); | |
420 | } | |
421 | if (monmap.contains(addrs)) { | |
422 | cerr << me << ": map already contains " << addrs << std::endl; | |
423 | helpful_exit(); | |
7c673cae | 424 | } |
11fdf7f2 TL |
425 | monmap.add(p.first, addrs); |
426 | } | |
427 | for (auto& p : addv) { | |
428 | if (monmap.contains(p.first)) { | |
429 | cerr << me << ": map already contains mon." << p.first << std::endl; | |
430 | helpful_exit(); | |
431 | } | |
432 | if (monmap.contains(p.second)) { | |
433 | cerr << me << ": map already contains " << p.second << std::endl; | |
434 | helpful_exit(); | |
7c673cae | 435 | } |
11fdf7f2 | 436 | monmap.add(p.first, p.second); |
7c673cae | 437 | } |
11fdf7f2 TL |
438 | for (auto& p : rm) { |
439 | cout << me << ": removing " << p << std::endl; | |
440 | if (!monmap.contains(p)) { | |
441 | cerr << me << ": map does not contain " << p << std::endl; | |
442 | helpful_exit(); | |
7c673cae | 443 | } |
11fdf7f2 | 444 | monmap.remove(p); |
7c673cae FG |
445 | } |
446 | ||
447 | if (handle_features(features, monmap)) { | |
448 | modified = true; | |
449 | } | |
450 | ||
11fdf7f2 TL |
451 | if (!print && !modified && !show_features) { |
452 | cerr << "no action specified" << std::endl; | |
453 | helpful_exit(); | |
454 | } | |
7c673cae FG |
455 | |
456 | if (print) | |
457 | monmap.print(cout); | |
458 | ||
459 | if (modified) { | |
460 | // write it out | |
461 | cout << me << ": writing epoch " << monmap.epoch | |
462 | << " to " << fn | |
463 | << " (" << monmap.size() << " monitors)" | |
464 | << std::endl; | |
465 | int r = monmap.write(fn.c_str()); | |
466 | if (r < 0) { | |
467 | cerr << "monmaptool: error writing to '" << fn << "': " << cpp_strerror(r) << std::endl; | |
468 | return 1; | |
469 | } | |
470 | } | |
471 | ||
472 | ||
473 | return 0; | |
474 | } |