From f113fde981ec7659a4cbe6cb587ecb0471a72b5b Mon Sep 17 00:00:00 2001 From: Joe Uhren Date: Sun, 1 May 2022 15:00:34 -0600 Subject: [PATCH] Add update-explorer script -Added new script to A) grab the latest code form github, B) update all dependencies, and C) run the database initialize function to ensure new changes take effect immidiately -Updated README to explain the new explorer update function --- README.md | 13 +++++++ package.json | 3 +- scripts/update_explorer.js | 78 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 scripts/update_explorer.js diff --git a/README.md b/README.md index 594d116..7bcbad8 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,7 @@ Table of Contents - [What is CORS?](#what-is-cors) - [How to Benefit From Using CORS?](#how-to-benefit-from-using-cors) - [Useful Scripts](#useful-scripts) + - [Update Explorer Script](#update-explorer-script) - [Backup Database Script](#backup-database-script) - [Restore Database Script](#restore-database-script) - [Delete Database Script](#delete-database-script) @@ -454,6 +455,8 @@ A small handful of useful scripts are also included to assist in solving various - `npm run reindex-txcount`: Recalculate the count of transactions stored in `stats.txes` by recounting the txes stored in the mongo database. Rarely needed, but can be useful for debugging or if you notice the main list of transactions is showing the wrong number of entries. If this value is off for some reason, you will not be able to page back to the 1st blocks on the main list of transactions for example. - `npm run reindex-last`: Lookup the last transaction in the mongo database and reset the `stats.last` value to that most recent block index. Rarely needed, but can be useful for debugging. The `stats.last` value is used to remember which block the last sync left off on to resume syncing from the next block. +Also see the [Useful Scripts](#useful-scripts) section for more helpful scripts. + #### Sample Crontab *Example crontab; update index every minute, market data every 2 minutes, peers and masternodes every 5 minutes* @@ -717,6 +720,16 @@ jQuery(document).ready(function($) { ### Useful Scripts +#### Update Explorer Script + +Automatically download and install the newest explorer source code, update out-of-date dependencies and initialize new changes with a single command. In most cases this update script can be safely run while the explorer is actively running to prevent needing to shut down to do updates, but please note that it may be possible for certain updates with large changes to require a reboot to the explorer for all changes to take effect properly. If you notice the explorer having issues after updating, try shutting down and restarting the explorer before seeking support. + +**NOTE:** Only explorer installations that were installed via cloning the source from git can be automatically updated. Be sure to follow the [Quick Install Instructions](#quick-install-instructions) to set up the explorer for optimum use with this update script. + +Update the explorer with the following command: + +`npm run update-explorer` + #### Backup Database Script Make a complete backup of an eIquidus mongo database and save to compressed file. A built-in locking mechanism prevents data from being updated or changed while a backup is in process. Backups can be safely created while the explorer is actively running and/or while the explorer is turned off. The following backup scenarios are supported: diff --git a/package.json b/package.json index 0789910..ffe874d 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,8 @@ "reindex-last": "node ./scripts/sync.js index reindex-last", "create-backup": "node ./scripts/create_backup.js", "restore-backup": "node ./scripts/restore_backup.js", - "delete-database": "node ./scripts/delete_database.js" + "delete-database": "node ./scripts/delete_database.js", + "update-explorer": "node ./scripts/update_explorer.js" }, "dependencies": { "express": ">=4.17.1", diff --git a/scripts/update_explorer.js b/scripts/update_explorer.js new file mode 100644 index 0000000..de03d94 --- /dev/null +++ b/scripts/update_explorer.js @@ -0,0 +1,78 @@ +const { execSync } = require('child_process'); +const fs = require('fs'); +const mongoose = require('mongoose'); + +// exit function used to cleanup before finishing script +function exit(exitCode) { + // disconnect mongo connection + mongoose.disconnect(); + + // exit process + process.exit(exitCode); +} + +var response; + +// check if the .git directory exists +if (fs.existsSync('./.git')) { + // update to newest explorer source + console.log('Downloading newest explorer code.. Please wait..'); + + try { + response = execSync('git pull'); + + // split response string by new line + var splitResponse = (response == null ? '' : response.toString()).split('\n').filter(element => element); + + // check if the response was a single line which indicates it was already up-to-date + if (splitResponse.length == 1) { + // explorer code was already up-to-date + console.log('Explorer code is already up-to-date'); + } else { + console.log(response.toString().trim()); + console.log('Explorer code successfully updated'); + } + } catch(err) { + console.log('Error updating explorer code. Maybe git is not installed globally?'); + } +} else { + console.log('WARNING: Explorer code not cloned from github and cannot be automatically updated!'); + console.log('Skipping explorer code update'); +} + +// update npm modules to latest versions according to package.json rules +console.log('Updating out-of-date explorer packages.. Please wait..'); +execSync('npm update'); + +// check for outdated packages +try { + execSync('npm outdated'); + + // all packages are up-to-date + console.log('All explorer packages are up-to-date'); +} catch (err) { + console.log(`The following packages are still out-of-date:\n${err.stdout.toString().trim()}`); +} + +// load database and settings files after being updated +const db = require('../lib/database'); +const settings = require('../lib/settings'); + +var dbString = 'mongodb://' + settings.dbsettings.user; +dbString = dbString + ':' + settings.dbsettings.password; +dbString = dbString + '@' + settings.dbsettings.address; +dbString = dbString + ':' + settings.dbsettings.port; +dbString = dbString + '/' + settings.dbsettings.database; + +// connect to mongo database +mongoose.connect(dbString, function(err) { + if (err) { + console.log('Error: Unable to connect to database: %s', dbString); + exit(999); + } else { + // initialize the database + db.initialize_data_startup(function() { + exit(0); + }); + } +}); \ No newline at end of file