2010-02-09 22:48:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 10gen Inc.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2009-01-27 21:16:09 +01:00
|
|
|
// Tool.cpp
|
|
|
|
|
2009-09-24 17:02:00 +02:00
|
|
|
#include "tool.h"
|
2009-01-27 21:16:09 +01:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include <boost/filesystem/operations.hpp>
|
2009-10-09 20:55:29 +02:00
|
|
|
#include <pcrecpp.h>
|
2009-01-27 21:16:09 +01:00
|
|
|
|
2009-05-20 18:15:26 +02:00
|
|
|
#include "util/file_allocator.h"
|
|
|
|
|
2009-01-27 21:16:09 +01:00
|
|
|
using namespace std;
|
|
|
|
using namespace mongo;
|
|
|
|
|
|
|
|
namespace po = boost::program_options;
|
|
|
|
|
2009-06-25 22:11:38 +02:00
|
|
|
mongo::Tool::Tool( string name , string defaultDB , string defaultCollection ) :
|
2009-08-12 22:55:18 +02:00
|
|
|
_name( name ) , _db( defaultDB ) , _coll( defaultCollection ) , _conn(0), _paired(false) {
|
2009-06-25 22:11:38 +02:00
|
|
|
|
2009-09-01 22:27:08 +02:00
|
|
|
_options = new po::options_description( "options" );
|
2009-01-27 21:16:09 +01:00
|
|
|
_options->add_options()
|
|
|
|
("help","produce help message")
|
2010-01-27 19:43:35 +01:00
|
|
|
("host,h",po::value<string>(), "mongo host to connect to (\"left,right\" for pairs)" )
|
2009-01-27 21:16:09 +01:00
|
|
|
("db,d",po::value<string>(), "database to use" )
|
|
|
|
("collection,c",po::value<string>(), "collection to use (some commands)" )
|
2009-08-12 22:31:22 +02:00
|
|
|
("username,u",po::value<string>(), "username" )
|
|
|
|
("password,p",po::value<string>(), "password" )
|
2009-05-20 17:39:12 +02:00
|
|
|
("dbpath",po::value<string>(), "directly access mongod data files in this path, instead of connecting to a mongod instance" )
|
2010-01-26 23:40:06 +01:00
|
|
|
("directoryperdb", "if dbpath specified, each db is in a separate directory" )
|
2009-07-13 20:09:43 +02:00
|
|
|
("verbose,v", "be more verbose (include multiple times for more verbosity e.g. -vvvvv)")
|
2009-01-27 21:16:09 +01:00
|
|
|
;
|
|
|
|
|
2009-09-01 22:27:08 +02:00
|
|
|
_hidden_options = new po::options_description( name + " hidden options" );
|
2009-09-01 22:52:50 +02:00
|
|
|
|
|
|
|
/* support for -vv -vvvv etc. */
|
|
|
|
for (string s = "vv"; s.length() <= 10; s.append("v")) {
|
|
|
|
_hidden_options->add_options()(s.c_str(), "verbose");
|
|
|
|
}
|
2009-01-27 21:16:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
mongo::Tool::~Tool(){
|
|
|
|
delete( _options );
|
2009-09-01 22:27:08 +02:00
|
|
|
delete( _hidden_options );
|
2009-08-11 19:35:59 +02:00
|
|
|
if ( _conn )
|
|
|
|
delete _conn;
|
2009-01-27 21:16:09 +01:00
|
|
|
}
|
|
|
|
|
2009-02-03 04:16:46 +01:00
|
|
|
void mongo::Tool::printExtraHelp( ostream & out ){
|
|
|
|
}
|
|
|
|
|
2009-06-25 22:15:15 +02:00
|
|
|
void mongo::Tool::printHelp(ostream &out) {
|
|
|
|
printExtraHelp(out);
|
2009-09-01 22:27:08 +02:00
|
|
|
_options->print(out);
|
2009-06-25 22:15:15 +02:00
|
|
|
}
|
|
|
|
|
2009-01-27 21:16:09 +01:00
|
|
|
int mongo::Tool::main( int argc , char ** argv ){
|
2010-01-25 21:11:21 +01:00
|
|
|
cmdLine.prealloc = false;
|
|
|
|
|
2009-01-27 21:16:09 +01:00
|
|
|
boost::filesystem::path::default_name_check( boost::filesystem::no_check );
|
2009-06-25 22:11:38 +02:00
|
|
|
|
2009-09-01 22:27:08 +02:00
|
|
|
_name = argv[0];
|
|
|
|
|
2009-09-01 22:52:50 +02:00
|
|
|
/* using the same style as db.cpp */
|
|
|
|
int command_line_style = (((po::command_line_style::unix_style ^
|
|
|
|
po::command_line_style::allow_guessing) |
|
|
|
|
po::command_line_style::allow_long_disguise) ^
|
|
|
|
po::command_line_style::allow_sticky);
|
2009-09-01 21:51:17 +02:00
|
|
|
try {
|
2009-09-01 22:27:08 +02:00
|
|
|
po::options_description all_options("all options");
|
|
|
|
all_options.add(*_options).add(*_hidden_options);
|
|
|
|
|
2009-09-01 21:51:17 +02:00
|
|
|
po::store( po::command_line_parser( argc , argv ).
|
2009-09-01 22:27:08 +02:00
|
|
|
options(all_options).
|
2009-09-01 22:52:50 +02:00
|
|
|
positional( _positonalOptions ).
|
|
|
|
style(command_line_style).run() , _params );
|
2009-09-01 21:51:17 +02:00
|
|
|
|
|
|
|
po::notify( _params );
|
|
|
|
} catch (po::error &e) {
|
2010-01-22 17:53:14 +01:00
|
|
|
cerr << "ERROR: " << e.what() << endl << endl;
|
|
|
|
printHelp(cerr);
|
2009-09-01 21:51:17 +02:00
|
|
|
return EXIT_BADOPTIONS;
|
|
|
|
}
|
2009-01-27 21:16:09 +01:00
|
|
|
|
|
|
|
if ( _params.count( "help" ) ){
|
2009-06-25 22:15:15 +02:00
|
|
|
printHelp(cerr);
|
2009-01-27 21:16:09 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2009-05-20 17:39:12 +02:00
|
|
|
|
2009-09-01 22:52:50 +02:00
|
|
|
if ( _params.count( "verbose" ) ) {
|
2009-07-13 20:09:43 +02:00
|
|
|
logLevel = 1;
|
2009-09-01 22:52:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (string s = "vv"; s.length() <= 10; s.append("v")) {
|
|
|
|
if (_params.count(s)) {
|
|
|
|
logLevel = s.length();
|
|
|
|
}
|
|
|
|
}
|
2009-07-13 20:09:43 +02:00
|
|
|
|
2010-02-18 03:08:10 +01:00
|
|
|
bool useDirectClient = hasParam( "dbpath" );
|
|
|
|
|
|
|
|
if ( ! useDirectClient ) {
|
2009-08-11 19:35:59 +02:00
|
|
|
_host = "127.0.0.1";
|
2009-05-20 17:39:12 +02:00
|
|
|
if ( _params.count( "host" ) )
|
2009-08-11 19:35:59 +02:00
|
|
|
_host = _params["host"].as<string>();
|
2009-09-01 21:43:23 +02:00
|
|
|
|
2009-08-11 19:35:59 +02:00
|
|
|
if ( _host.find( "," ) == string::npos ){
|
|
|
|
DBClientConnection * c = new DBClientConnection();
|
|
|
|
_conn = c;
|
2009-09-01 21:43:23 +02:00
|
|
|
|
2009-08-11 19:35:59 +02:00
|
|
|
string errmsg;
|
|
|
|
if ( ! c->connect( _host , errmsg ) ){
|
|
|
|
cerr << "couldn't connect to [" << _host << "] " << errmsg << endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2009-12-02 22:56:49 +01:00
|
|
|
log(1) << "using pairing" << endl;
|
2009-08-11 19:35:59 +02:00
|
|
|
DBClientPaired * c = new DBClientPaired();
|
2009-08-12 22:55:18 +02:00
|
|
|
_paired = true;
|
2009-08-11 19:35:59 +02:00
|
|
|
_conn = c;
|
2009-09-01 21:43:23 +02:00
|
|
|
|
2009-08-11 19:35:59 +02:00
|
|
|
if ( ! c->connect( _host ) ){
|
|
|
|
cerr << "couldn't connect to paired server: " << _host << endl;
|
|
|
|
return -1;
|
|
|
|
}
|
2009-05-20 17:39:12 +02:00
|
|
|
}
|
2009-06-25 22:11:38 +02:00
|
|
|
|
2009-08-11 19:35:59 +02:00
|
|
|
cerr << "connected to: " << _host << endl;
|
2009-09-01 21:43:23 +02:00
|
|
|
}
|
2009-08-11 19:35:59 +02:00
|
|
|
else {
|
2010-01-26 23:40:06 +01:00
|
|
|
if ( _params.count( "directoryperdb" ) ) {
|
|
|
|
directoryperdb = true;
|
|
|
|
}
|
2009-10-16 21:36:34 +02:00
|
|
|
Client::initThread("tools");
|
2009-08-11 19:35:59 +02:00
|
|
|
_conn = new DBDirectClient();
|
|
|
|
_host = "DIRECT";
|
2009-05-20 17:39:12 +02:00
|
|
|
static string myDbpath = getParam( "dbpath" );
|
|
|
|
mongo::dbpath = myDbpath.c_str();
|
|
|
|
mongo::acquirePathLock();
|
2009-05-20 18:15:26 +02:00
|
|
|
theFileAllocator().start();
|
2009-01-27 21:16:09 +01:00
|
|
|
}
|
2009-06-25 22:11:38 +02:00
|
|
|
|
2009-01-27 21:16:09 +01:00
|
|
|
if ( _params.count( "db" ) )
|
|
|
|
_db = _params["db"].as<string>();
|
2009-06-25 22:11:38 +02:00
|
|
|
|
2009-01-27 21:16:09 +01:00
|
|
|
if ( _params.count( "collection" ) )
|
|
|
|
_coll = _params["collection"].as<string>();
|
2009-09-01 21:43:23 +02:00
|
|
|
|
2009-08-12 22:31:22 +02:00
|
|
|
if ( _params.count( "username" ) )
|
|
|
|
_username = _params["username"].as<string>();
|
|
|
|
|
|
|
|
if ( _params.count( "password" ) )
|
|
|
|
_password = _params["password"].as<string>();
|
2009-09-01 21:43:23 +02:00
|
|
|
|
2009-10-27 16:44:22 +01:00
|
|
|
int ret = -1;
|
2009-07-10 16:59:30 +02:00
|
|
|
try {
|
2009-10-27 16:44:22 +01:00
|
|
|
ret = run();
|
2009-07-10 16:59:30 +02:00
|
|
|
}
|
|
|
|
catch ( DBException& e ){
|
|
|
|
cerr << "assertion: " << e.toString() << endl;
|
2009-10-27 16:44:22 +01:00
|
|
|
ret = -1;
|
2009-07-10 16:59:30 +02:00
|
|
|
}
|
2009-10-27 16:44:22 +01:00
|
|
|
|
|
|
|
if ( currentClient.get() )
|
|
|
|
currentClient->shutdown();
|
2010-02-02 03:35:54 +01:00
|
|
|
|
2010-02-18 03:08:10 +01:00
|
|
|
if ( useDirectClient )
|
|
|
|
dbexit( EXIT_CLEAN );
|
2009-10-27 16:44:22 +01:00
|
|
|
return ret;
|
2009-01-27 21:16:09 +01:00
|
|
|
}
|
2009-08-12 22:31:22 +02:00
|
|
|
|
2009-08-12 22:55:18 +02:00
|
|
|
mongo::DBClientBase& mongo::Tool::conn( bool slaveIfPaired ){
|
|
|
|
if ( _paired && slaveIfPaired )
|
|
|
|
return ((DBClientPaired*)_conn)->slaveConn();
|
|
|
|
return *_conn;
|
|
|
|
}
|
|
|
|
|
2009-10-12 21:05:42 +02:00
|
|
|
void mongo::Tool::addFieldOptions(){
|
|
|
|
add_options()
|
|
|
|
("fields,f" , po::value<string>() , "comma seperated list of field names e.g. -f name,age" )
|
|
|
|
("fieldFile" , po::value<string>() , "file with fields names - 1 per line" )
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2009-10-09 20:55:29 +02:00
|
|
|
void mongo::Tool::needFields(){
|
2009-10-12 21:05:42 +02:00
|
|
|
|
|
|
|
if ( hasParam( "fields" ) ){
|
|
|
|
BSONObjBuilder b;
|
|
|
|
|
|
|
|
string fields_arg = getParam("fields");
|
|
|
|
pcrecpp::StringPiece input(fields_arg);
|
|
|
|
|
|
|
|
string f;
|
|
|
|
pcrecpp::RE re("([\\w\\.]+),?" );
|
|
|
|
while ( re.Consume( &input, &f ) ){
|
|
|
|
_fields.push_back( f );
|
|
|
|
b.append( f.c_str() , 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
_fieldsObj = b.obj();
|
|
|
|
return;
|
2009-10-09 20:55:29 +02:00
|
|
|
}
|
2009-10-12 21:05:42 +02:00
|
|
|
|
|
|
|
if ( hasParam( "fieldFile" ) ){
|
|
|
|
string fn = getParam( "fieldFile" );
|
|
|
|
if ( ! exists( fn ) )
|
2009-12-28 23:06:07 +01:00
|
|
|
throw UserException( 9999 , ((string)"file: " + fn ) + " doesn't exist" );
|
2009-10-12 21:05:42 +02:00
|
|
|
|
|
|
|
const int BUF_SIZE = 1024;
|
|
|
|
char line[ 1024 + 128];
|
|
|
|
ifstream file( fn.c_str() );
|
|
|
|
|
|
|
|
BSONObjBuilder b;
|
|
|
|
while ( file.rdstate() == ios_base::goodbit ){
|
|
|
|
file.getline( line , BUF_SIZE );
|
|
|
|
const char * cur = line;
|
|
|
|
while ( isspace( cur[0] ) ) cur++;
|
|
|
|
if ( strlen( cur ) == 0 )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
_fields.push_back( cur );
|
|
|
|
b.append( cur , 1 );
|
|
|
|
}
|
|
|
|
_fieldsObj = b.obj();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-12-28 23:06:07 +01:00
|
|
|
throw UserException( 9998 , "you need to specify fields" );
|
2009-10-09 20:55:29 +02:00
|
|
|
}
|
|
|
|
|
2009-08-12 22:31:22 +02:00
|
|
|
void mongo::Tool::auth( string dbname ){
|
|
|
|
if ( ! dbname.size() )
|
|
|
|
dbname = _db;
|
|
|
|
|
|
|
|
if ( ! ( _username.size() || _password.size() ) )
|
|
|
|
return;
|
|
|
|
|
|
|
|
string errmsg;
|
|
|
|
if ( _conn->auth( dbname , _username , _password , errmsg ) )
|
|
|
|
return;
|
2009-09-01 21:43:23 +02:00
|
|
|
|
2009-08-12 22:31:22 +02:00
|
|
|
// try against the admin db
|
|
|
|
string err2;
|
|
|
|
if ( _conn->auth( "admin" , _username , _password , errmsg ) )
|
|
|
|
return;
|
2009-09-01 21:43:23 +02:00
|
|
|
|
2009-12-28 23:06:07 +01:00
|
|
|
throw mongo::UserException( 9997 , (string)"auth failed: " + errmsg );
|
2009-08-12 22:31:22 +02:00
|
|
|
}
|