ceph/src/monmaptool.cc

167 lines
4.1 KiB
C++
Raw Normal View History

2008-02-01 21:45:55 +00:00
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>
#include <iostream>
#include <string>
using namespace std;
#include "common/config.h"
#include "common/ceph_argparse.h"
#include "common/common_init.h"
2008-02-01 21:45:55 +00:00
#include "mon/MonMap.h"
void usage()
2008-02-01 21:45:55 +00:00
{
2010-06-11 16:57:13 +00:00
cout << " usage: [--print] [--create [--clobber]] [--add name 1.2.3.4:567] [--rm name] <mapfilename>" << std::endl;
2008-02-01 21:45:55 +00:00
exit(1);
}
int main(int argc, const char **argv)
{
vector<const char*> args;
argv_to_vec(argc, argv, args);
DEFINE_CONF_VARS(usage);
2008-02-01 21:45:55 +00:00
const char *me = argv[0];
const char *fn = 0;
2008-02-01 21:45:55 +00:00
bool print = false;
bool create = false;
bool clobber = false;
bool modified = false;
map<string,entity_addr_t> add;
list<string> rm;
2008-02-01 21:45:55 +00:00
common_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY,
CINIT_FLAG_NO_DEFAULT_CONFIG_FILE);
common_init_finish(&g_ceph_context);
FOR_EACH_ARG(args) {
if (CEPH_ARGPARSE_EQ("print", '\0')) {
CEPH_ARGPARSE_SET_ARG_VAL(&print, OPT_BOOL);
} else if (CEPH_ARGPARSE_EQ("create", '\0')) {
CEPH_ARGPARSE_SET_ARG_VAL(&create, OPT_BOOL);
} else if (CEPH_ARGPARSE_EQ("clobber", '\0')) {
CEPH_ARGPARSE_SET_ARG_VAL(&clobber, OPT_BOOL);
} else if (CEPH_ARGPARSE_EQ("add", '\0')) {
if (++i >= args.size())
usage();
string name = args[i];
2008-11-05 22:54:13 +00:00
if (++i >= args.size())
usage();
2008-02-01 21:45:55 +00:00
entity_addr_t addr;
2010-06-02 05:07:15 +00:00
if (!addr.parse(args[i])) {
2008-02-01 21:45:55 +00:00
cerr << me << ": invalid ip:port '" << args[i] << "'" << std::endl;
return -1;
}
add[name] = addr;
modified = true;
} else if (CEPH_ARGPARSE_EQ("rm", '\0')) {
if (++i >= args.size())
usage();
string name = args[i];
rm.push_back(name);
2008-02-01 21:45:55 +00:00
modified = true;
} else if (!fn)
fn = args[i];
2008-11-05 22:54:13 +00:00
else {
cout << "invalid argument: '" << args[i] << "'" << std::endl;
usage();
2008-11-05 22:54:13 +00:00
}
2008-02-01 21:45:55 +00:00
}
if (!fn)
usage();
2008-02-01 21:45:55 +00:00
MonMap monmap;
cout << me << ": monmap file " << fn << std::endl;
int r = 0;
if (!(create && clobber)) {
try {
r = monmap.read(fn);
} catch (...) {
cerr << me << ": unable to read monmap file" << std::endl;
return -1;
}
}
2008-02-01 21:45:55 +00:00
2009-09-18 23:17:04 +00:00
char buf[80];
2008-02-01 21:45:55 +00:00
if (!create && r < 0) {
cerr << me << ": couldn't open " << fn << ": " << strerror_r(-r, buf, sizeof(buf)) << std::endl;
2008-02-01 21:45:55 +00:00
return -1;
}
else if (create && !clobber && r == 0) {
cerr << me << ": " << fn << " exists, --clobber to overwrite" << std::endl;
return -1;
}
if (create) {
2008-04-09 18:09:49 +00:00
srand(getpid() + time(0));
monmap.generate_fsid();
cout << me << ": generated fsid " << monmap.fsid << std::endl;
modified++;
}
for (map<string,entity_addr_t>::iterator p = add.begin(); p != add.end(); p++) {
if (monmap.contains(p->first)) {
cerr << me << ": map already contains mon." << p->first << std::endl;
usage();
}
if (monmap.contains(p->second)) {
cerr << me << ": map already contains " << p->second << std::endl;
usage();
}
monmap.add(p->first, p->second);
}
for (list<string>::iterator p = rm.begin(); p != rm.end(); p++) {
2008-02-01 21:45:55 +00:00
cout << me << ": removing " << *p << std::endl;
if (!monmap.contains(*p)) {
2008-02-01 21:45:55 +00:00
cerr << me << ": map does not contain " << *p << std::endl;
usage();
2008-02-01 21:45:55 +00:00
}
monmap.remove(*p);
2008-02-01 21:45:55 +00:00
}
if (!print && !modified)
usage();
2008-02-01 21:45:55 +00:00
if (modified)
monmap.epoch++;
2008-02-01 21:45:55 +00:00
if (print)
monmap.print(cout);
2008-02-01 21:45:55 +00:00
if (modified) {
// write it out
cout << me << ": writing epoch " << monmap.epoch
<< " to " << fn
<< " (" << monmap.size() << " monitors)"
<< std::endl;
int r = monmap.write(fn);
2009-03-10 16:49:06 +00:00
if (r < 0) {
2009-09-18 23:17:04 +00:00
cerr << "monmaptool: error writing to '" << fn << "': " << strerror_r(-r, buf, sizeof(buf)) << std::endl;
2009-03-10 16:49:06 +00:00
return 1;
}
2008-02-01 21:45:55 +00:00
}
return 0;
}