PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/src/mongo/shell/utils.js

https://github.com/Dazzed/mongo
JavaScript | 1174 lines | 968 code | 143 blank | 63 comment | 294 complexity | 7a02887bd1d51bcd1d94b37deb839f52 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. __quiet = false;
  2. __magicNoPrint = { __magicNoPrint : 1111 }
  3. __callLastError = false;
  4. _verboseShell = false;
  5. chatty = function(s){
  6. if ( ! __quiet )
  7. print( s );
  8. }
  9. function reconnect(db) {
  10. assert.soon(function() {
  11. try {
  12. db.runCommand({ping:1});
  13. return true;
  14. } catch (x) {
  15. return false;
  16. }
  17. });
  18. };
  19. // Please consider using bsonWoCompare instead of this as much as possible.
  20. friendlyEqual = function( a , b ){
  21. if ( a == b )
  22. return true;
  23. a = tojson(a,false,true);
  24. b = tojson(b,false,true);
  25. if ( a == b )
  26. return true;
  27. var clean = function( s ){
  28. s = s.replace( /NumberInt\((\-?\d+)\)/g , "$1" );
  29. return s;
  30. }
  31. a = clean(a);
  32. b = clean(b);
  33. if ( a == b )
  34. return true;
  35. return false;
  36. }
  37. printStackTrace = function(){
  38. try{
  39. throw new Error("Printing Stack Trace");
  40. } catch (e) {
  41. print(e.stack);
  42. }
  43. }
  44. /**
  45. * <p> Set the shell verbosity. If verbose the shell will display more information about command results. </>
  46. * <p> Default is off. <p>
  47. * @param {Bool} verbosity on / off
  48. */
  49. setVerboseShell = function( value ) {
  50. if( value == undefined ) value = true;
  51. _verboseShell = value;
  52. }
  53. argumentsToArray = function( a ){
  54. var arr = [];
  55. for ( var i=0; i<a.length; i++ )
  56. arr[i] = a[i];
  57. return arr;
  58. }
  59. // Formats a simple stacked horizontal histogram bar in the shell.
  60. // @param data array of the form [[ratio, symbol], ...] where ratio is between 0 and 1 and
  61. // symbol is a string of length 1
  62. // @param width width of the bar (excluding the left and right delimiters [ ] )
  63. // e.g. _barFormat([[.3, "="], [.5, '-']], 80) returns
  64. // "[========================---------------------------------------- ]"
  65. _barFormat = function(data, width) {
  66. var remaining = width;
  67. var res = "[";
  68. for (var i = 0; i < data.length; i++) {
  69. for (var x = 0; x < data[i][0] * width; x++) {
  70. if (remaining-- > 0) {
  71. res += data[i][1];
  72. }
  73. }
  74. }
  75. while (remaining-- > 0) {
  76. res += " ";
  77. }
  78. res += "]";
  79. return res;
  80. }
  81. //these two are helpers for Array.sort(func)
  82. compare = function(l, r){ return (l == r ? 0 : (l < r ? -1 : 1)); }
  83. // arr.sort(compareOn('name'))
  84. compareOn = function(field){
  85. return function(l, r) { return compare(l[field], r[field]); }
  86. }
  87. shellPrint = function( x ){
  88. it = x;
  89. if ( x != undefined )
  90. shellPrintHelper( x );
  91. if ( db ){
  92. var e = db.getPrevError();
  93. if ( e.err ) {
  94. if ( e.nPrev <= 1 )
  95. print( "error on last call: " + tojson( e.err ) );
  96. else
  97. print( "an error " + tojson( e.err ) + " occurred " + e.nPrev + " operations back in the command invocation" );
  98. }
  99. db.resetError();
  100. }
  101. }
  102. if ( typeof TestData == "undefined" ){
  103. TestData = undefined
  104. }
  105. jsTestName = function(){
  106. if( TestData ) return TestData.testName
  107. return "__unknown_name__"
  108. }
  109. jsTestFile = function(){
  110. if( TestData ) return TestData.testFile
  111. return "__unknown_file__"
  112. }
  113. jsTestPath = function(){
  114. if( TestData ) return TestData.testPath
  115. return "__unknown_path__"
  116. }
  117. var _jsTestOptions = { enableTestCommands : true }; // Test commands should be enabled by default
  118. jsTestOptions = function(){
  119. if( TestData ) {
  120. return Object.merge(_jsTestOptions,
  121. { setParameters : TestData.setParameters,
  122. setParametersMongos : TestData.setParametersMongos,
  123. noJournal : TestData.noJournal,
  124. noJournalPrealloc : TestData.noJournalPrealloc,
  125. auth : TestData.auth,
  126. keyFile : TestData.keyFile,
  127. authUser : "__system",
  128. authPassword : TestData.keyFileData,
  129. authMechanism : TestData.authMechanism,
  130. adminUser : TestData.adminUser || "admin",
  131. adminPassword : TestData.adminPassword || "password",
  132. useSSL : TestData.useSSL,
  133. useX509 : TestData.useX509});
  134. }
  135. return _jsTestOptions;
  136. }
  137. setJsTestOption = function(name, value) {
  138. _jsTestOptions[name] = value;
  139. }
  140. jsTestLog = function(msg){
  141. print( "\n\n----\n" + msg + "\n----\n\n" )
  142. }
  143. jsTest = {}
  144. jsTest.name = jsTestName
  145. jsTest.file = jsTestFile
  146. jsTest.path = jsTestPath
  147. jsTest.options = jsTestOptions
  148. jsTest.setOption = setJsTestOption
  149. jsTest.log = jsTestLog
  150. jsTest.readOnlyUserRoles = ["read"]
  151. jsTest.basicUserRoles = ["dbOwner"]
  152. jsTest.adminUserRoles = ["root"]
  153. jsTest.dir = function(){
  154. return jsTest.path().replace( /\/[^\/]+$/, "/" )
  155. }
  156. jsTest.randomize = function( seed ) {
  157. if( seed == undefined ) seed = new Date().getTime()
  158. Random.srand( seed )
  159. print( "Random seed for test : " + seed )
  160. }
  161. jsTest.authenticate = function(conn) {
  162. if (!jsTest.options().auth && !jsTest.options().keyFile && !jsTest.options().useX509) {
  163. conn.authenticated = true;
  164. return true;
  165. }
  166. try {
  167. assert.soon(function() {
  168. // Set authenticated to stop an infinite recursion from getDB calling
  169. // back into authenticate.
  170. conn.authenticated = true;
  171. print ("Authenticating as internal " + jsTestOptions().authUser + " user with mechanism " +
  172. DB.prototype._defaultAuthenticationMechanism +
  173. " on connection: " + conn);
  174. conn.authenticated = conn.getDB('admin').auth({
  175. user: jsTestOptions().authUser,
  176. pwd: jsTestOptions().authPassword,
  177. });
  178. return conn.authenticated;
  179. }, "Authenticating connection: " + conn, 5000, 1000);
  180. } catch (e) {
  181. print("Caught exception while authenticating connection: " + tojson(e));
  182. conn.authenticated = false;
  183. }
  184. return conn.authenticated;
  185. }
  186. jsTest.authenticateNodes = function(nodes) {
  187. assert.soon(function() {
  188. for (var i = 0; i < nodes.length; i++) {
  189. // Don't try to authenticate to arbiters
  190. res = nodes[i].getDB("admin").runCommand({replSetGetStatus: 1});
  191. if(res.myState == 7) {
  192. continue;
  193. }
  194. if(jsTest.authenticate(nodes[i]) != 1) {
  195. return false;
  196. }
  197. }
  198. return true;
  199. }, "Authenticate to nodes: " + nodes, 30000);
  200. }
  201. jsTest.isMongos = function(conn) {
  202. return conn.getDB('admin').isMaster().msg=='isdbgrid';
  203. }
  204. defaultPrompt = function() {
  205. var status = db.getMongo().authStatus;
  206. try {
  207. // try to use repl set prompt -- no status or auth detected yet
  208. if (!status || !status.authRequired) {
  209. try {
  210. var prompt = replSetMemberStatePrompt();
  211. // set our status that it was good
  212. db.getMongo().authStatus = {replSetGetStatus:true, isMaster: true};
  213. return prompt;
  214. } catch (e) {
  215. // don't have permission to run that, or requires auth
  216. //print(e);
  217. status = {authRequired: true, replSetGetStatus: false, isMaster: true};
  218. }
  219. }
  220. // auth detected
  221. // try to use replSetGetStatus?
  222. if (status.replSetGetStatus) {
  223. try {
  224. var prompt = replSetMemberStatePrompt();
  225. // set our status that it was good
  226. status.replSetGetStatus = true;
  227. db.getMongo().authStatus = status;
  228. return prompt;
  229. } catch (e) {
  230. // don't have permission to run that, or requires auth
  231. //print(e);
  232. status.authRequired = true;
  233. status.replSetGetStatus = false;
  234. }
  235. }
  236. // try to use isMaster?
  237. if (status.isMaster) {
  238. try {
  239. var prompt = isMasterStatePrompt();
  240. status.isMaster = true;
  241. db.getMongo().authStatus = status;
  242. return prompt;
  243. } catch (e) {
  244. status.authRequired = true;
  245. status.isMaster = false;
  246. }
  247. }
  248. } catch (ex) {
  249. printjson(ex);
  250. // reset status and let it figure it out next time.
  251. status = {isMaster:true};
  252. }
  253. db.getMongo().authStatus = status;
  254. return "> ";
  255. }
  256. replSetMemberStatePrompt = function() {
  257. var state = '';
  258. var stateInfo = db.getSiblingDB( 'admin' ).runCommand( { replSetGetStatus:1, forShell:1 } );
  259. if ( stateInfo.ok ) {
  260. // Report the self member's stateStr if it's present.
  261. stateInfo.members.forEach( function( member ) {
  262. if ( member.self ) {
  263. state = member.stateStr;
  264. }
  265. } );
  266. // Otherwise fall back to reporting the numeric myState field (mongodb 1.6).
  267. if ( !state ) {
  268. state = stateInfo.myState;
  269. }
  270. state = '' + stateInfo.set + ':' + state;
  271. } else {
  272. var info = stateInfo.info;
  273. if ( info && info.length < 20 ) {
  274. state = info; // "mongos", "configsvr"
  275. } else {
  276. throw Error("Failed:" + info);
  277. }
  278. }
  279. return state + '> ';
  280. }
  281. isMasterStatePrompt = function() {
  282. var state = '';
  283. var isMaster = db.runCommand({isMaster:1, forShell:1});
  284. if ( isMaster.ok ) {
  285. var role = "";
  286. if (isMaster.msg == "isdbgrid") {
  287. role = "mongos";
  288. }
  289. if (isMaster.setName) {
  290. if (isMaster.ismaster)
  291. role = "PRIMARY";
  292. else if (isMaster.secondary)
  293. role = "SECONDARY";
  294. else if (isMaster.arbiterOnly)
  295. role = "ARBITER";
  296. else {
  297. role = "OTHER"
  298. }
  299. state = isMaster.setName + ':';
  300. }
  301. state = state + role;
  302. } else {
  303. throw Error("Failed: " + tojson(isMaster));
  304. }
  305. return state + '> ';
  306. }
  307. if (typeof(_useWriteCommandsDefault) == 'undefined') {
  308. // This is for cases when the v8 engine is used other than the mongo shell, like map reduce.
  309. _useWriteCommandsDefault = function() { return false; };
  310. };
  311. if (typeof(_writeMode) == 'undefined') {
  312. // This is for cases when the v8 engine is used other than the mongo shell, like map reduce.
  313. _writeMode = function() { return "commands"; };
  314. };
  315. shellPrintHelper = function (x) {
  316. if (typeof (x) == "undefined") {
  317. // Make sure that we have a db var before we use it
  318. // TODO: This implicit calling of GLE can cause subtle, hard to track issues - remove?
  319. if (__callLastError && typeof( db ) != "undefined" &&
  320. db.getMongo &&
  321. db.getMongo().writeMode() == "legacy") {
  322. __callLastError = false;
  323. // explicit w:1 so that replset getLastErrorDefaults aren't used here which would be bad
  324. var err = db.getLastError(1);
  325. if (err != null) {
  326. print(err);
  327. }
  328. }
  329. return;
  330. }
  331. if (x == __magicNoPrint)
  332. return;
  333. if (x == null) {
  334. print("null");
  335. return;
  336. }
  337. if (typeof x != "object")
  338. return print(x);
  339. var p = x.shellPrint;
  340. if (typeof p == "function")
  341. return x.shellPrint();
  342. var p = x.tojson;
  343. if (typeof p == "function")
  344. print(x.tojson());
  345. else
  346. print(tojson(x));
  347. }
  348. shellAutocomplete = function ( /*prefix*/ ) { // outer scope function called on init. Actual function at end
  349. var universalMethods = "constructor prototype toString valueOf toLocaleString hasOwnProperty propertyIsEnumerable".split( ' ' );
  350. var builtinMethods = {}; // uses constructor objects as keys
  351. builtinMethods[Array] = "length concat join pop push reverse shift slice sort splice unshift indexOf lastIndexOf every filter forEach map some isArray reduce reduceRight".split( ' ' );
  352. builtinMethods[Boolean] = "".split( ' ' ); // nothing more than universal methods
  353. builtinMethods[Date] = "getDate getDay getFullYear getHours getMilliseconds getMinutes getMonth getSeconds getTime getTimezoneOffset getUTCDate getUTCDay getUTCFullYear getUTCHours getUTCMilliseconds getUTCMinutes getUTCMonth getUTCSeconds getYear parse setDate setFullYear setHours setMilliseconds setMinutes setMonth setSeconds setTime setUTCDate setUTCFullYear setUTCHours setUTCMilliseconds setUTCMinutes setUTCMonth setUTCSeconds setYear toDateString toGMTString toISOString toLocaleDateString toLocaleTimeString toTimeString toUTCString UTC now".split( ' ' );
  354. if (typeof JSON != "undefined") { // JSON is new in V8
  355. builtinMethods["[object JSON]"] = "parse stringify".split(' ');
  356. }
  357. builtinMethods[Math] = "E LN2 LN10 LOG2E LOG10E PI SQRT1_2 SQRT2 abs acos asin atan atan2 ceil cos exp floor log max min pow random round sin sqrt tan".split(' ');
  358. builtinMethods[Number] = "MAX_VALUE MIN_VALUE NEGATIVE_INFINITY POSITIVE_INFINITY toExponential toFixed toPrecision".split( ' ' );
  359. builtinMethods[RegExp] = "global ignoreCase lastIndex multiline source compile exec test".split( ' ' );
  360. builtinMethods[String] = "length charAt charCodeAt concat fromCharCode indexOf lastIndexOf match replace search slice split substr substring toLowerCase toUpperCase trim trimLeft trimRight".split(' ');
  361. builtinMethods[Function] = "call apply bind".split( ' ' );
  362. builtinMethods[Object] = "bsonsize create defineProperty defineProperties getPrototypeOf keys seal freeze preventExtensions isSealed isFrozen isExtensible getOwnPropertyDescriptor getOwnPropertyNames".split(' ');
  363. builtinMethods[Mongo] = "find update insert remove".split(' ');
  364. builtinMethods[BinData] = "hex base64 length subtype".split(' ');
  365. var extraGlobals = "Infinity NaN undefined null true false decodeURI decodeURIComponent encodeURI encodeURIComponent escape eval isFinite isNaN parseFloat parseInt unescape Array Boolean Date Math Number RegExp String print load gc MinKey MaxKey Mongo NumberInt NumberLong ObjectId DBPointer UUID BinData HexData MD5 Map Timestamp JSON".split( ' ' );
  366. var isPrivate = function( name ) {
  367. if ( shellAutocomplete.showPrivate ) return false;
  368. if ( name == '_id' ) return false;
  369. if ( name[0] == '_' ) return true;
  370. if ( name[name.length - 1] == '_' ) return true; // some native functions have an extra name_ method
  371. return false;
  372. }
  373. var customComplete = function( obj ) {
  374. try {
  375. if ( obj.__proto__.constructor.autocomplete ) {
  376. var ret = obj.constructor.autocomplete( obj );
  377. if ( ret.constructor != Array ) {
  378. print( "\nautocompleters must return real Arrays" );
  379. return [];
  380. }
  381. return ret;
  382. } else {
  383. return [];
  384. }
  385. } catch ( e ) {
  386. // print( e ); // uncomment if debugging custom completers
  387. return [];
  388. }
  389. }
  390. var worker = function( prefix ) {
  391. var global = ( function() { return this; } ).call(); // trick to get global object
  392. var curObj = global;
  393. var parts = prefix.split( '.' );
  394. for ( var p = 0; p < parts.length - 1; p++ ) { // doesn't include last part
  395. curObj = curObj[parts[p]];
  396. if ( curObj == null )
  397. return [];
  398. }
  399. var lastPrefix = parts[parts.length - 1] || '';
  400. var lastPrefixLowercase = lastPrefix.toLowerCase()
  401. var beginning = parts.slice( 0, parts.length - 1 ).join( '.' );
  402. if ( beginning.length )
  403. beginning += '.';
  404. var possibilities = new Array().concat(
  405. universalMethods,
  406. Object.keySet( curObj ),
  407. Object.keySet( curObj.__proto__ ),
  408. builtinMethods[curObj] || [], // curObj is a builtin constructor
  409. builtinMethods[curObj.__proto__.constructor] || [], // curObj is made from a builtin constructor
  410. curObj == global ? extraGlobals : [],
  411. customComplete( curObj )
  412. );
  413. var noDuplicates = {}; // see http://dreaminginjavascript.wordpress.com/2008/08/22/eliminating-duplicates/
  414. for ( var i = 0; i < possibilities.length; i++ ) {
  415. var p = possibilities[i];
  416. if ( typeof ( curObj[p] ) == "undefined" && curObj != global ) continue; // extraGlobals aren't in the global object
  417. if ( p.length == 0 || p.length < lastPrefix.length ) continue;
  418. if ( lastPrefix[0] != '_' && isPrivate( p ) ) continue;
  419. if ( p.match( /^[0-9]+$/ ) ) continue; // don't array number indexes
  420. if ( p.substr( 0, lastPrefix.length ).toLowerCase() != lastPrefixLowercase ) continue;
  421. var completion = beginning + p;
  422. if ( curObj[p] && curObj[p].constructor == Function && p != 'constructor' )
  423. completion += '(';
  424. noDuplicates[completion] = 0;
  425. }
  426. var ret = [];
  427. for ( var i in noDuplicates )
  428. ret.push( i );
  429. return ret;
  430. }
  431. // this is the actual function that gets assigned to shellAutocomplete
  432. return function( prefix ) {
  433. try {
  434. __autocomplete__ = worker( prefix ).sort();
  435. } catch ( e ) {
  436. print( "exception during autocomplete: " + tojson( e.message ) );
  437. __autocomplete__ = [];
  438. }
  439. }
  440. } ();
  441. shellAutocomplete.showPrivate = false; // toggle to show (useful when working on internals)
  442. shellHelper = function( command , rest , shouldPrint ){
  443. command = command.trim();
  444. var args = rest.trim().replace(/\s*;$/,"").split( "\s+" );
  445. if ( ! shellHelper[command] )
  446. throw Error( "no command [" + command + "]" );
  447. var res = shellHelper[command].apply( null , args );
  448. if ( shouldPrint ){
  449. shellPrintHelper( res );
  450. }
  451. return res;
  452. }
  453. shellHelper.use = function (dbname) {
  454. var s = "" + dbname;
  455. if (s == "") {
  456. print("bad use parameter");
  457. return;
  458. }
  459. db = db.getMongo().getDB(dbname);
  460. print("switched to db " + db.getName());
  461. }
  462. shellHelper.set = function (str) {
  463. if (str == "") {
  464. print("bad use parameter");
  465. return;
  466. }
  467. tokens = str.split(" ");
  468. param = tokens[0];
  469. value = tokens[1];
  470. if ( value == undefined ) value = true;
  471. // value comes in as a string..
  472. if ( value == "true" ) value = true;
  473. if ( value == "false" ) value = false;
  474. if (param == "verbose") {
  475. _verboseShell = value;
  476. }
  477. print("set " + param + " to " + value);
  478. }
  479. shellHelper.it = function(){
  480. if ( typeof( ___it___ ) == "undefined" || ___it___ == null ){
  481. print( "no cursor" );
  482. return;
  483. }
  484. shellPrintHelper( ___it___ );
  485. }
  486. shellHelper.show = function (what) {
  487. assert(typeof what == "string");
  488. var args = what.split( /\s+/ );
  489. what = args[0]
  490. args = args.splice(1)
  491. if (what == "profile") {
  492. if (db.system.profile.count() == 0) {
  493. print("db.system.profile is empty");
  494. print("Use db.setProfilingLevel(2) will enable profiling");
  495. print("Use db.system.profile.find() to show raw profile entries");
  496. }
  497. else {
  498. print();
  499. db.system.profile.find({ millis: { $gt: 0} }).sort({ $natural: -1 }).limit(5).forEach(
  500. function (x) {
  501. print("" + x.op + "\t" + x.ns + " " + x.millis + "ms " + String(x.ts).substring(0, 24));
  502. var l = "";
  503. for ( var z in x ){
  504. if ( z == "op" || z == "ns" || z == "millis" || z == "ts" )
  505. continue;
  506. var val = x[z];
  507. var mytype = typeof(val);
  508. if ( mytype == "string" ||
  509. mytype == "number" )
  510. l += z + ":" + val + " ";
  511. else if ( mytype == "object" )
  512. l += z + ":" + tojson(val ) + " ";
  513. else if ( mytype == "boolean" )
  514. l += z + " ";
  515. else
  516. l += z + ":" + val + " ";
  517. }
  518. print( l );
  519. print("\n");
  520. }
  521. )
  522. }
  523. return "";
  524. }
  525. if (what == "users") {
  526. db.getUsers().forEach(printjson);
  527. return "";
  528. }
  529. if (what == "roles") {
  530. db.getRoles({showBuiltinRoles: true}).forEach(printjson);
  531. return "";
  532. }
  533. if (what == "collections" || what == "tables") {
  534. db.getCollectionNames().forEach(function (x) { print(x) });
  535. return "";
  536. }
  537. if (what == "dbs" || what == "databases") {
  538. var dbs = db.getMongo().getDBs();
  539. var dbinfo = [];
  540. var maxNameLength = 0;
  541. var maxGbDigits = 0;
  542. dbs.databases.forEach(function (x){
  543. var sizeStr = (x.sizeOnDisk / 1024 / 1024 / 1024).toFixed(3);
  544. var nameLength = x.name.length;
  545. var gbDigits = sizeStr.indexOf(".");
  546. if( nameLength > maxNameLength) maxNameLength = nameLength;
  547. if( gbDigits > maxGbDigits ) maxGbDigits = gbDigits;
  548. dbinfo.push({
  549. name: x.name,
  550. size: x.sizeOnDisk,
  551. size_str: sizeStr,
  552. name_size: nameLength,
  553. gb_digits: gbDigits
  554. });
  555. });
  556. dbinfo.sort(compareOn('name'))
  557. dbinfo.forEach(function (db) {
  558. var namePadding = maxNameLength - db.name_size;
  559. var sizePadding = maxGbDigits - db.gb_digits;
  560. var padding = Array(namePadding + sizePadding + 3).join(" ");
  561. if (db.size > 1) {
  562. print(db.name + padding + db.size_str + "GB");
  563. } else {
  564. print(db.name + padding + "(empty)");
  565. }
  566. });
  567. return "";
  568. }
  569. if (what == "log" ) {
  570. var n = "global";
  571. if ( args.length > 0 )
  572. n = args[0]
  573. var res = db.adminCommand( { getLog : n } );
  574. if ( ! res.ok ) {
  575. print("Error while trying to show " + n + " log: " + res.errmsg);
  576. return "";
  577. }
  578. for ( var i=0; i<res.log.length; i++){
  579. print( res.log[i] )
  580. }
  581. return ""
  582. }
  583. if (what == "logs" ) {
  584. var res = db.adminCommand( { getLog : "*" } )
  585. if ( ! res.ok ) {
  586. print("Error while trying to show logs: " + res.errmsg);
  587. return "";
  588. }
  589. for ( var i=0; i<res.names.length; i++){
  590. print( res.names[i] )
  591. }
  592. return ""
  593. }
  594. if (what == "startupWarnings" ) {
  595. var dbDeclared, ex;
  596. try {
  597. // !!db essentially casts db to a boolean
  598. // Will throw a reference exception if db hasn't been declared.
  599. dbDeclared = !!db;
  600. } catch (ex) {
  601. dbDeclared = false;
  602. }
  603. if (dbDeclared) {
  604. var res = db.adminCommand( { getLog : "startupWarnings" } );
  605. if ( res.ok ) {
  606. if (res.log.length == 0) {
  607. return "";
  608. }
  609. print( "Server has startup warnings: " );
  610. for ( var i=0; i<res.log.length; i++){
  611. print( res.log[i] )
  612. }
  613. return "";
  614. } else if (res.errmsg == "no such cmd: getLog" ) {
  615. // Don't print if the command is not available
  616. return "";
  617. } else if (res.code == 13 /*unauthorized*/ ||
  618. res.errmsg == "unauthorized" ||
  619. res.errmsg == "need to login") {
  620. // Don't print if startupWarnings command failed due to auth
  621. return "";
  622. } else {
  623. print("Error while trying to show server startup warnings: " + res.errmsg);
  624. return "";
  625. }
  626. } else {
  627. print("Cannot show startupWarnings, \"db\" is not set");
  628. return "";
  629. }
  630. }
  631. throw Error( "don't know how to show [" + what + "]" );
  632. }
  633. Math.sigFig = function( x , N ){
  634. if ( ! N ){
  635. N = 3;
  636. }
  637. var p = Math.pow( 10, N - Math.ceil( Math.log( Math.abs(x) ) / Math.log( 10 )) );
  638. return Math.round(x*p)/p;
  639. }
  640. Random = function() {}
  641. // set random seed
  642. Random.srand = function( s ) { _srand( s ); }
  643. // random number 0 <= r < 1
  644. Random.rand = function() { return _rand(); }
  645. // random integer 0 <= r < n
  646. Random.randInt = function( n ) { return Math.floor( Random.rand() * n ); }
  647. Random.setRandomSeed = function( s ) {
  648. s = s || new Date().getTime();
  649. print( "setting random seed: " + s );
  650. Random.srand( s );
  651. }
  652. // generate a random value from the exponential distribution with the specified mean
  653. Random.genExp = function( mean ) {
  654. var r = Random.rand();
  655. if ( r == 0 ) {
  656. r = Random.rand();
  657. if ( r == 0 ) {
  658. r = 0.000001;
  659. }
  660. }
  661. return -Math.log( r ) * mean;
  662. }
  663. /**
  664. * Generate a random value from the normal distribution with specified 'mean' and
  665. * 'standardDeviation'.
  666. */
  667. Random.genNormal = function( mean, standardDeviation ) {
  668. // See http://en.wikipedia.org/wiki/Marsaglia_polar_method
  669. while ( true ) {
  670. var x = ( 2 * Random.rand() ) - 1;
  671. var y = ( 2 * Random.rand() ) - 1;
  672. var s = ( x * x ) + ( y * y );
  673. if ( s > 0 && s < 1 ) {
  674. var standardNormal = x * Math.sqrt( -2 * Math.log( s ) / s );
  675. return mean + ( standardDeviation * standardNormal );
  676. }
  677. }
  678. }
  679. Geo = {};
  680. Geo.distance = function( a , b ){
  681. var ax = null;
  682. var ay = null;
  683. var bx = null;
  684. var by = null;
  685. for ( var key in a ){
  686. if ( ax == null )
  687. ax = a[key];
  688. else if ( ay == null )
  689. ay = a[key];
  690. }
  691. for ( var key in b ){
  692. if ( bx == null )
  693. bx = b[key];
  694. else if ( by == null )
  695. by = b[key];
  696. }
  697. return Math.sqrt( Math.pow( by - ay , 2 ) +
  698. Math.pow( bx - ax , 2 ) );
  699. }
  700. Geo.sphereDistance = function( a , b ){
  701. var ax = null;
  702. var ay = null;
  703. var bx = null;
  704. var by = null;
  705. // TODO swap order of x and y when done on server
  706. for ( var key in a ){
  707. if ( ax == null )
  708. ax = a[key] * (Math.PI/180);
  709. else if ( ay == null )
  710. ay = a[key] * (Math.PI/180);
  711. }
  712. for ( var key in b ){
  713. if ( bx == null )
  714. bx = b[key] * (Math.PI/180);
  715. else if ( by == null )
  716. by = b[key] * (Math.PI/180);
  717. }
  718. var sin_x1=Math.sin(ax), cos_x1=Math.cos(ax);
  719. var sin_y1=Math.sin(ay), cos_y1=Math.cos(ay);
  720. var sin_x2=Math.sin(bx), cos_x2=Math.cos(bx);
  721. var sin_y2=Math.sin(by), cos_y2=Math.cos(by);
  722. var cross_prod =
  723. (cos_y1*cos_x1 * cos_y2*cos_x2) +
  724. (cos_y1*sin_x1 * cos_y2*sin_x2) +
  725. (sin_y1 * sin_y2);
  726. if (cross_prod >= 1 || cross_prod <= -1){
  727. // fun with floats
  728. assert( Math.abs(cross_prod)-1 < 1e-6 );
  729. return cross_prod > 0 ? 0 : Math.PI;
  730. }
  731. return Math.acos(cross_prod);
  732. }
  733. rs = function () { return "try rs.help()"; }
  734. /**
  735. * This method is intended to aid in the writing of tests. It takes a host's address, desired state,
  736. * and replicaset and waits either timeout milliseconds or until that reaches the desired state.
  737. *
  738. * It should be used instead of awaitRSClientHost when there is no MongoS with a connection to the
  739. * replica set.
  740. */
  741. _awaitRSHostViaRSMonitor = function(hostAddr, desiredState, rsName, timeout) {
  742. timeout = timeout || 60 * 1000;
  743. if (desiredState == undefined) {
  744. desiredState = {ok: true};
  745. }
  746. print("Awaiting " + hostAddr + " to be " + tojson(desiredState) + " in " + " rs " + rsName)
  747. var tests = 0;
  748. assert.soon(function() {
  749. var stats = _replMonitorStats(rsName);
  750. if (tests++ % 10 == 0) {
  751. printjson(stats);
  752. }
  753. for (var i=0; i<stats.length; i++) {
  754. var node = stats[i]
  755. printjson(node);
  756. if (node["addr"] !== hostAddr)
  757. continue;
  758. // Check that *all* hostAddr properties match desiredState properties
  759. var stateReached = true;
  760. for(var prop in desiredState) {
  761. if (isObject(desiredState[prop])) {
  762. if (!friendlyEqual(desiredState[prop], node[prop])) {
  763. stateReached = false;
  764. break;
  765. }
  766. }
  767. else if (node[prop] !== desiredState[prop]) {
  768. stateReached = false;
  769. break;
  770. }
  771. }
  772. if (stateReached) {
  773. printjson(stats);
  774. return true;
  775. }
  776. }
  777. return false;
  778. }, "timed out waiting for replica set member: " + hostAddr + " to reach state: " +
  779. tojson(desiredState),
  780. timeout);
  781. }
  782. rs.help = function () {
  783. print("\trs.status() { replSetGetStatus : 1 } checks repl set status");
  784. print("\trs.initiate() { replSetInitiate : null } initiates set with default settings");
  785. print("\trs.initiate(cfg) { replSetInitiate : cfg } initiates set with configuration cfg");
  786. print("\trs.conf() get the current configuration object from local.system.replset");
  787. print("\trs.reconfig(cfg) updates the configuration of a running replica set with cfg (disconnects)");
  788. print("\trs.add(hostportstr) add a new member to the set with default attributes (disconnects)");
  789. print("\trs.add(membercfgobj) add a new member to the set with extra attributes (disconnects)");
  790. print("\trs.addArb(hostportstr) add a new member which is arbiterOnly:true (disconnects)");
  791. print("\trs.stepDown([secs]) step down as primary (momentarily) (disconnects)");
  792. print("\trs.syncFrom(hostportstr) make a secondary to sync from the given member");
  793. print("\trs.freeze(secs) make a node ineligible to become primary for the time specified");
  794. print("\trs.remove(hostportstr) remove a host from the replica set (disconnects)");
  795. print("\trs.slaveOk() shorthand for db.getMongo().setSlaveOk()");
  796. print();
  797. print("\trs.printReplicationInfo() check oplog size and time range");
  798. print("\trs.printSlaveReplicationInfo() check replica set members and replication lag");
  799. print("\tdb.isMaster() check who is primary");
  800. print();
  801. print("\treconfiguration helpers disconnect from the database so the shell will display");
  802. print("\tan error, even if the command succeeds.");
  803. print("\tsee also http://<mongod_host>:28017/_replSet for additional diagnostic info");
  804. }
  805. rs.slaveOk = function (value) { return db.getMongo().setSlaveOk(value); }
  806. rs.status = function () { return db._adminCommand("replSetGetStatus"); }
  807. rs.isMaster = function () { return db.isMaster(); }
  808. rs.initiate = function (c) { return db._adminCommand({ replSetInitiate: c }); }
  809. rs.printSlaveReplicationInfo = function () { return db.printSlaveReplicationInfo() };
  810. rs.printReplicationInfo = function () { return db.printReplicationInfo() };
  811. rs._runCmd = function (c) {
  812. // after the command, catch the disconnect and reconnect if necessary
  813. var res = null;
  814. try {
  815. res = db.adminCommand(c);
  816. }
  817. catch (e) {
  818. if (("" + e).indexOf("error doing query") >= 0) {
  819. // closed connection. reconnect.
  820. db.getLastErrorObj();
  821. var o = db.getLastErrorObj();
  822. if (o.ok) {
  823. print("reconnected to server after rs command (which is normal)");
  824. }
  825. else {
  826. printjson(o);
  827. }
  828. }
  829. else {
  830. print("shell got exception during repl set operation: " + e);
  831. print("in some circumstances, the primary steps down and closes connections on a reconfig");
  832. }
  833. return "";
  834. }
  835. return res;
  836. }
  837. rs.reconfig = function (cfg, options) {
  838. cfg.version = rs.conf().version + 1;
  839. cmd = { replSetReconfig: cfg };
  840. for (var i in options) {
  841. cmd[i] = options[i];
  842. }
  843. return this._runCmd(cmd);
  844. }
  845. rs.add = function (hostport, arb) {
  846. var cfg = hostport;
  847. var local = db.getSisterDB("local");
  848. assert(local.system.replset.count() <= 1, "error: local.system.replset has unexpected contents");
  849. var c = local.system.replset.findOne();
  850. assert(c, "no config object retrievable from local.system.replset");
  851. c.version++;
  852. var max = 0;
  853. for (var i in c.members)
  854. if (c.members[i]._id > max) max = c.members[i]._id;
  855. if (isString(hostport)) {
  856. cfg = { _id: max + 1, host: hostport };
  857. if (arb)
  858. cfg.arbiterOnly = true;
  859. }
  860. if (cfg._id == null){
  861. cfg._id = max+1;
  862. }
  863. c.members.push(cfg);
  864. return this._runCmd({ replSetReconfig: c });
  865. }
  866. rs.syncFrom = function (host) { return db._adminCommand({replSetSyncFrom : host}); };
  867. rs.stepDown = function (secs) { return db._adminCommand({ replSetStepDown:(secs === undefined) ? 60:secs}); }
  868. rs.freeze = function (secs) { return db._adminCommand({replSetFreeze:secs}); }
  869. rs.addArb = function (hn) { return this.add(hn, true); }
  870. rs.conf = function () { return db.getSisterDB("local").system.replset.findOne(); }
  871. rs.config = function () { return rs.conf(); }
  872. rs.remove = function (hn) {
  873. var local = db.getSisterDB("local");
  874. assert(local.system.replset.count() <= 1, "error: local.system.replset has unexpected contents");
  875. var c = local.system.replset.findOne();
  876. assert(c, "no config object retrievable from local.system.replset");
  877. c.version++;
  878. for (var i in c.members) {
  879. if (c.members[i].host == hn) {
  880. c.members.splice(i, 1);
  881. return db._adminCommand({ replSetReconfig : c});
  882. }
  883. }
  884. return "error: couldn't find "+hn+" in "+tojson(c.members);
  885. };
  886. rs.debug = {};
  887. rs.debug.nullLastOpWritten = function(primary, secondary) {
  888. var p = connect(primary+"/local");
  889. var s = connect(secondary+"/local");
  890. s.getMongo().setSlaveOk();
  891. var secondToLast = s.oplog.rs.find().sort({$natural : -1}).limit(1).next();
  892. var last = p.runCommand({findAndModify : "oplog.rs",
  893. query : {ts : {$gt : secondToLast.ts}},
  894. sort : {$natural : 1},
  895. update : {$set : {op : "n"}}});
  896. if (!last.value.o || !last.value.o._id) {
  897. print("couldn't find an _id?");
  898. }
  899. else {
  900. last.value.o = {_id : last.value.o._id};
  901. }
  902. print("nulling out this op:");
  903. printjson(last);
  904. };
  905. rs.debug.getLastOpWritten = function(server) {
  906. var s = db.getSisterDB("local");
  907. if (server) {
  908. s = connect(server+"/local");
  909. }
  910. s.getMongo().setSlaveOk();
  911. return s.oplog.rs.find().sort({$natural : -1}).limit(1).next();
  912. };
  913. help = shellHelper.help = function (x) {
  914. if (x == "mr") {
  915. print("\nSee also http://dochub.mongodb.org/core/mapreduce");
  916. print("\nfunction mapf() {");
  917. print(" // 'this' holds current document to inspect");
  918. print(" emit(key, value);");
  919. print("}");
  920. print("\nfunction reducef(key,value_array) {");
  921. print(" return reduced_value;");
  922. print("}");
  923. print("\ndb.mycollection.mapReduce(mapf, reducef[, options])");
  924. print("\noptions");
  925. print("{[query : <query filter object>]");
  926. print(" [, sort : <sort the query. useful for optimization>]");
  927. print(" [, limit : <number of objects to return from collection>]");
  928. print(" [, out : <output-collection name>]");
  929. print(" [, keeptemp: <true|false>]");
  930. print(" [, finalize : <finalizefunction>]");
  931. print(" [, scope : <object where fields go into javascript global scope >]");
  932. print(" [, verbose : true]}\n");
  933. return;
  934. } else if (x == "connect") {
  935. print("\nNormally one specifies the server on the mongo shell command line. Run mongo --help to see those options.");
  936. print("Additional connections may be opened:\n");
  937. print(" var x = new Mongo('host[:port]');");
  938. print(" var mydb = x.getDB('mydb');");
  939. print(" or");
  940. print(" var mydb = connect('host[:port]/mydb');");
  941. print("\nNote: the REPL prompt only auto-reports getLastError() for the shell command line connection.\n");
  942. return;
  943. }
  944. else if (x == "keys") {
  945. print("Tab completion and command history is available at the command prompt.\n");
  946. print("Some emacs keystrokes are available too:");
  947. print(" Ctrl-A start of line");
  948. print(" Ctrl-E end of line");
  949. print(" Ctrl-K del to end of line");
  950. print("\nMulti-line commands");
  951. print("You can enter a multi line javascript expression. If parens, braces, etc. are not closed, you will see a new line ");
  952. print("beginning with '...' characters. Type the rest of your expression. Press Ctrl-C to abort the data entry if you");
  953. print("get stuck.\n");
  954. }
  955. else if (x == "misc") {
  956. print("\tb = new BinData(subtype,base64str) create a BSON BinData value");
  957. print("\tb.subtype() the BinData subtype (0..255)");
  958. print("\tb.length() length of the BinData data in bytes");
  959. print("\tb.hex() the data as a hex encoded string");
  960. print("\tb.base64() the data as a base 64 encoded string");
  961. print("\tb.toString()");
  962. print();
  963. print("\tb = HexData(subtype,hexstr) create a BSON BinData value from a hex string");
  964. print("\tb = UUID(hexstr) create a BSON BinData value of UUID subtype");
  965. print("\tb = MD5(hexstr) create a BSON BinData value of MD5 subtype");
  966. print("\t\"hexstr\" string, sequence of hex characters (no 0x prefix)");
  967. print();
  968. print("\to = new ObjectId() create a new ObjectId");
  969. print("\to.getTimestamp() return timestamp derived from first 32 bits of the OID");
  970. print("\to.isObjectId");
  971. print("\to.toString()");
  972. print("\to.equals(otherid)");
  973. print();
  974. print("\td = ISODate() like Date() but behaves more intuitively when used");
  975. print("\td = ISODate('YYYY-MM-DD hh:mm:ss') without an explicit \"new \" prefix on construction");
  976. return;
  977. }
  978. else if (x == "admin") {
  979. print("\tls([path]) list files");
  980. print("\tpwd() returns current directory");
  981. print("\tlistFiles([path]) returns file list");
  982. print("\thostname() returns name of this host");
  983. print("\tcat(fname) returns contents of text file as a string");
  984. print("\tremoveFile(f) delete a file or directory");
  985. print("\tload(jsfilename) load and execute a .js file");
  986. print("\trun(program[, args...]) spawn a program and wait for its completion");
  987. print("\trunProgram(program[, args...]) same as run(), above");
  988. print("\tsleep(m) sleep m milliseconds");
  989. print("\tgetMemInfo() diagnostic");
  990. return;
  991. }
  992. else if (x == "test") {
  993. print("\tstartMongodEmpty(args) DELETES DATA DIR and then starts mongod");
  994. print("\t returns a connection to the new server");
  995. print("\tstartMongodTest(port,dir,options)");
  996. print("\t DELETES DATA DIR");
  997. print("\t automatically picks port #s starting at 27000 and increasing");
  998. print("\t or you can specify the port as the first arg");
  999. print("\t dir is /data/db/<port>/ if not specified as the 2nd arg");
  1000. print("\t returns a connection to the new server");
  1001. print("\tresetDbpath(dirpathstr) deletes everything under the dir specified including subdirs");
  1002. print("\tstopMongoProgram(port[, signal])");
  1003. return;
  1004. }
  1005. else if (x == "") {
  1006. print("\t" + "db.help() help on db methods");
  1007. print("\t" + "db.mycoll.help() help on collection methods");
  1008. print("\t" + "sh.help() sharding helpers");
  1009. print("\t" + "rs.help() replica set helpers");
  1010. print("\t" + "help admin administrative help");
  1011. print("\t" + "help connect connecting to a db help");
  1012. print("\t" + "help keys key shortcuts");
  1013. print("\t" + "help misc misc things to know");
  1014. print("\t" + "help mr mapreduce");
  1015. print();
  1016. print("\t" + "show dbs show database names");
  1017. print("\t" + "show collections show collections in current database");
  1018. print("\t" + "show users show users in current database");
  1019. print("\t" + "show profile show most recent system.profile entries with time >= 1ms");
  1020. print("\t" + "show logs show the accessible logger names");
  1021. print("\t" + "show log [name] prints out the last segment of log in memory, 'global' is default");
  1022. print("\t" + "use <db_name> set current database");
  1023. print("\t" + "db.foo.find() list objects in collection foo");
  1024. print("\t" + "db.foo.find( { a : 1 } ) list objects in foo where a == 1");
  1025. print("\t" + "it result of the last line evaluated; use to further iterate");
  1026. print("\t" + "DBQuery.shellBatchSize = x set default number of items to display on shell");
  1027. print("\t" + "exit quit the mongo shell");
  1028. }
  1029. else
  1030. print("unknown help option");
  1031. }