Skip to content

Commit

Permalink
Writes output to log files, and misc
Browse files Browse the repository at this point in the history
- Writes json output to log files on each run
- Adds User-Agent string to identify importer
- Renames customData option "schema" to "flatten"

RESOLVES OKTA-123670, OKTA-124066
8ac0n: test

OKTA-123670
<<<Jenkins Check-In of Tested SHA: a42415b for [email protected]>>>
Artifact: stormpath-migration
  • Loading branch information
rchild-okta authored and jenkins-okta committed Apr 27, 2017
1 parent 32b3048 commit a52b2b8
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
logs
.vscode
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ $ import-stormpath --stormPathBaseDir /path/to/export/data --oktaBaseUrl https:/

### Optional Args

**--customData (-d)** Strategy for importing Stormpath Account custom data. Defaults to `schema`.
**--customData (-d)** Strategy for importing Stormpath Account custom data. Defaults to `flatten`.

- Options

- `schema` - Add [custom user profile schema properties](http://developer.okta.com/docs/api/resources/schemas.html#user-profile-schema-property-object) for each custom data property. Use this for simple custom data objects.
- `flatten` - Add [custom user profile schema properties](http://developer.okta.com/docs/api/resources/schemas.html#user-profile-schema-property-object) for each custom data property. Use this for simple custom data objects.
- `stringify` - Stringify the Account custom data object into one `customData` [custom user profile schema property](http://developer.okta.com/docs/api/resources/schemas.html#user-profile-schema-property-object). Use this for more complex custom data objects.
- `exclude` - Exclude Stormpath Account custom data from the import

Expand Down
2 changes: 2 additions & 0 deletions bin/migrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const migrateOrganizations = require('../migrators/migrate-organizations');

logger.setLevel(config.logLevel);
logger.info(`Starting import...`);
logger.info(`Writing log output to ${logger.logFile}`);

async function migrate() {
try {
Expand All @@ -28,6 +29,7 @@ async function migrate() {
await migrateApplications();

logger.header('Done');
logger.info(`Wrote log output to ${logger.logFile}`);
console.timeEnd('migrate');
} catch (err) {
logger.error(err);
Expand Down
2 changes: 1 addition & 1 deletion stormpath/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ class Account extends Base {
if (config.isCustomDataStringify) {
customData['customData'] = transform(JSON.stringify(this.customData));
}
else if (config.isCustomDataSchema) {
else if (config.isCustomDataFlatten) {
const skip = ['createdAt', 'modifiedAt', 'href', 'id'];
const flattened = flattenCustomData(this.customData);
const keys = Object.keys(flattened).filter(key => !skip.includes(key));
Expand Down
6 changes: 3 additions & 3 deletions util/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ const config = yargs
description: 'Strategy for importing Account custom data',
required: false,
alias: 'd',
choices: ['schema', 'stringify', 'exclude'],
default: 'schema'
choices: ['flatten', 'stringify', 'exclude'],
default: 'flatten'
},
concurrencyLimit: {
description: 'Max number of concurrent transactions',
Expand Down Expand Up @@ -64,7 +64,7 @@ const config = yargs
})
.argv;

config.isCustomDataSchema = config.customData === 'schema';
config.isCustomDataFlatten = config.customData === 'flatten';
config.isCustomDataStringify = config.customData === 'stringify';
config.isCustomDataExclude = config.customData === 'exclude';

Expand Down
16 changes: 16 additions & 0 deletions util/logger.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const winston = require('winston');
const chalk = require('chalk');
const path = require('path');
const fs = require('fs');
const prettyJson = require('prettyjson');
const columnify = require('columnify');
const ApiError = require('./api-error');
Expand Down Expand Up @@ -67,6 +69,15 @@ function formatLine(title, options) {
return `${date} ${title}${message}\n${prependSpaces(metaStr)}`;
}

// Create logger directory if it does not exist, and generate a unique name
// for this current run.
const logDir = path.resolve(__dirname, '../logs');
if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir);
}
const time = (new Date().toISOString()).replace(/:|\./g, '');
const logFile = `${logDir}/import-${time}.json`;

let indent = 0;

const levels = {
Expand Down Expand Up @@ -131,6 +142,9 @@ const logger = new (winston.Logger)({
}
return formatLine(title, options);
}
}),
new (winston.transports.File)({
filename: logFile
})
]
});
Expand All @@ -147,4 +161,6 @@ logger.group = (title) => {
};
}

logger.logFile = logFile;

module.exports = logger;
18 changes: 14 additions & 4 deletions util/request-scheduler.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
'use strict';

const os = require('os');
const Promise = require('bluebird');
const rp = require('request-promise');
const logger = require('../util/logger');
const config = require('../util/config');
const ConcurrencyPool = require('../util/concurrency-pool');
const logger = require('./logger');
const config = require('./config');
const ConcurrencyPool = require('./concurrency-pool');
const packageJson = require('../package.json');

// The max number of concurrent requests. Note, this is different from the
// concurrencyLimit in the config, which defines the max number of concurrent
Expand Down Expand Up @@ -68,6 +70,13 @@ async function schedule(scheduler, msg, fn) {
}
}

/**
* Constructs user agent based on environment information
*/
function getUserAgent() {
return `stormpath-migration/${packageJson.version} node/${process.versions.node} ${os.platform()}/${os.release()}`;
}

/**
* Class that wraps request-promise with two enhancements:
* 1. Limits the number of concurrent requests that are made at any given time
Expand All @@ -84,7 +93,8 @@ class RequestScheduler {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': `SSWS ${config.oktaApiToken}`
'Authorization': `SSWS ${config.oktaApiToken}`,
'User-Agent': getUserAgent()
},
resolveWithFullResponse: true,
json: true,
Expand Down

0 comments on commit a52b2b8

Please sign in to comment.