Skip to content
This repository was archived by the owner on Oct 5, 2020. It is now read-only.

Commit bc40d1a

Browse files
committed
Final changes
1 parent 9573883 commit bc40d1a

File tree

14 files changed

+231
-175
lines changed

14 files changed

+231
-175
lines changed

app/templates/gulpfile.js

Lines changed: 157 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ var _ = require('lodash');
1616
var $ = require('gulp-load-plugins')({lazy: true});
1717
/* jshint ignore:end */
1818

19-
var _s = require('underscore.string');
19+
var _s = require('underscore.string'),
20+
q = require('q'),
21+
spawn = require('child_process').spawn;
2022

2123
/**
2224
* yargs variables can be passed in to alter the behavior, when present.
@@ -520,118 +522,144 @@ function clean(files) {
520522
*/
521523
function init(env, done) {
522524
if (fs.existsSync(env + '.json')) {
523-
log(env + '.json already exists!');
525+
log('NOTE: ' + env + '.json already exists, change manually if needed.');
526+
if (fs.existsSync('deploy/' + env + '.properties')) {
527+
log('NOTE: deploy/' + env + '.properties already exists, change manually too.');
528+
} else {
529+
log('WARN: deploy/' + env + '.properties is missing!');
530+
}
524531
done();
525532
} else {
526533
//copy from slushfile - config gulp - with modifications to use config instead
527534
var inquirer = require('inquirer');
528535

529-
var properties = fs.readFileSync('deploy/build.properties', { encoding: 'utf8' });
530-
531-
// capture app-name entered earlier
532-
var appNameMatch = /^app\-name=(.*)$/m;
533-
var matches = appNameMatch.exec(properties);
534-
var appName = matches[1];
535-
536-
var prompts = [
537-
{type: 'list', name: 'mlVersion', message: 'MarkLogic version?', choices: ['8','7', '6', '5'], default: 0},
538-
{type: 'input', name: 'marklogicHost', message: 'MarkLogic Host?', default: 'localhost'},
539-
{type: 'input', name: 'marklogicAdminUser', message: 'MarkLogic Admin User?', default: 'admin'},
540-
{type: 'input', name: 'marklogicAdminPass', message: 'Note: consider keeping the following blank, ' +
541-
'you will be asked to enter it at appropriate commands.\n? MarkLogic Admin Password?', default: ''},
542-
{type: 'input', name: 'appPort', message: 'MarkLogic App/Rest port?', default: 8040},
543-
{type: 'input', name: 'xccPort', message: 'XCC port?', default:8041, when: function(answers) {
544-
return answers.mlVersion < 8;
545-
}},
546-
{type: 'input', name: 'nodePort', message: 'Node app port?', default: 9070},
547-
{type: 'list', name: 'guestAccess', message: 'Allow anonymous users to search data?', choices: ['false', 'true'], default: 0},
548-
{type: 'list', name: 'readOnlyAccess', message: 'Disallow proxying update requests?', choices: ['false', 'true'], default: 0},
549-
{type: 'list', name: 'appUsersOnly', message: 'Only allow access to users created for this app? Note: disallows admin users.', choices: ['false', 'true'], default: 0}
550-
];
551-
552-
if (typeof appName === 'undefined') {
553-
prompts.unshift(
554-
{type: 'input', name: 'name', message: 'Name for the app?'});
555-
}
536+
run('./ml', [env, 'info', '--format=json']).then(function(output) {
537+
var localJson = fs.existsSync('local.json') ? JSON.parse(fs.readFileSync('local.json', 'utf8')) : {};
538+
539+
var localAppName = localJson['app-name'];
540+
var localMlVersion = localJson['ml-version'];
541+
var localMlHost = localJson['ml-host'];
542+
var localMlAdminUser = localJson['ml-admin-user'];
543+
var localMlAppUser = localJson['ml-app-user'];
544+
var localMlAppPass = localJson['ml-app-pass'];
545+
var localMlHttpPort = localJson['ml-http-port'];
546+
var localMlXccPort = localJson['ml-xcc-port'];
547+
var localNodePort = localJson['node-port'] || 9070;
548+
var localGuestAccess = ['false', 'true'].indexOf(localJson['guest-access']);
549+
var localDisallowUpdates = ['false', 'true'].indexOf(localJson['disallow-updates']);
550+
var localAppUsersOnly = ['false', 'true'].indexOf(localJson['appusers-only']);
551+
552+
var properties = JSON.parse(output).properties || {};
553+
554+
var mlVersion = ['8','7', '6', '5'].indexOf(localMlVersion || properties['ml.server-version'] || '8');
555+
var marklogicHost = properties['ml.' + env + '-server'] || localMlHost || 'localhost';
556+
var marklogicAdminUser = properties['ml.user'] || localMlAdminUser || 'admin';
557+
var appName = properties['ml.app-name'] || localAppName;
558+
var appUserName = properties['ml.default-user'] || localMlAppUser;
559+
var appUserPass = properties['ml.appuser-password'] || localMlAppPass;
560+
var appPort = localMlHttpPort || properties['ml.app-port'] || 8040;
561+
var xccPort = localMlXccPort || properties['ml.xcc-port'] || 8041;
562+
563+
var prompts = [
564+
{type: 'list', name: 'mlVersion', message: 'MarkLogic version?', choices: ['8','7', '6', '5'], default: mlVersion > 0 ? mlVersion : 0 },
565+
{type: 'input', name: 'marklogicHost', message: 'MarkLogic Host?', default: marklogicHost},
566+
{type: 'input', name: 'marklogicAdminUser', message: 'MarkLogic Admin User?', default: marklogicAdminUser},
567+
{type: 'input', name: 'marklogicAdminPass', message: 'Note: consider keeping the following blank, ' +
568+
'you will be asked to enter it at appropriate commands.\n? MarkLogic Admin Password?', default: ''},
569+
{type: 'input', name: 'appPort', message: 'MarkLogic App/Rest port?', default: appPort},
570+
{type: 'input', name: 'xccPort', message: 'XCC port?', default: xccPort, when: function(answers) {
571+
return answers.mlVersion < 8;
572+
}},
573+
{type: 'input', name: 'nodePort', message: 'Node app port?', default: localNodePort},
574+
{type: 'list', name: 'guestAccess', message: 'Allow anonymous users to search data?', choices: ['false', 'true'], default: localGuestAccess > 0 ? localGuestAccess : 0},
575+
{type: 'list', name: 'disallowUpdates', message: 'Disallow proxying update requests?', choices: ['false', 'true'], default: localDisallowUpdates > 0 ? localDisallowUpdates : 0},
576+
{type: 'list', name: 'appUsersOnly', message: 'Only allow access to users created for this app? Note: disallows admin users.', choices: ['false', 'true'], default: localAppUsersOnly > 0 ? localAppUsersOnly : 0}
577+
];
556578

557-
inquirer.prompt(prompts, function (settings) {
558579
if (typeof appName === 'undefined') {
559-
settings.nameDashed = _s.slugify(settings.name);
560-
} else {
561-
settings.nameDashed = _s.slugify(appName);
580+
prompts.unshift(
581+
{type: 'input', name: 'name', message: 'Name for the app?'});
562582
}
563583

564-
try {
565-
var configJSON = {};
566-
configJSON['app-name'] = settings.nameDashed;
567-
configJSON['ml-version'] = settings.mlVersion;
568-
configJSON['ml-host'] = settings.marklogicHost;
569-
configJSON['ml-admin-user'] = settings.marklogicAdminUser;
570-
configJSON['ml-admin-pass'] = settings.marklogicAdminPass;
571-
configJSON['ml-app-user'] = settings.nameDashed + '-user';
572-
configJSON['ml-app-pass'] = settings.appuserPassword;
573-
configJSON['ml-http-port'] = settings.appPort;
574-
575-
if (settings.mlVersion < 8) {
576-
configJSON['ml-xcc-port'] = settings.xccPort;
584+
inquirer.prompt(prompts, function (settings) {
585+
if (typeof appName === 'undefined') {
586+
settings.nameDashed = _s.slugify(settings.name);
587+
} else {
588+
settings.nameDashed = _s.slugify(appName);
577589
}
578590

579-
configJSON['node-port'] = settings.nodePort;
580-
configJSON['guest-access'] = settings.guestAccess;
581-
configJSON['readonly-access'] = settings.readOnlyAccess;
582-
configJSON['appusers-only'] = settings.appUsersOnly;
583-
584-
var configString = JSON.stringify(configJSON, null, 2) + '\n';
585-
fs.writeFileSync(env + '.json', configString, { encoding: 'utf8' });
586-
log('Created ' + env + '.json.');
591+
try {
592+
var configJSON = {};
593+
configJSON['app-name'] = settings.nameDashed;
594+
configJSON['ml-version'] = settings.mlVersion;
595+
configJSON['ml-host'] = settings.marklogicHost;
596+
configJSON['ml-admin-user'] = settings.marklogicAdminUser;
597+
configJSON['ml-admin-pass'] = settings.marklogicAdminPass;
598+
configJSON['ml-app-user'] = appUserName || (settings.nameDashed + '-user');
599+
configJSON['ml-app-pass'] = appUserPass || '';
600+
configJSON['ml-http-port'] = settings.appPort;
587601

588-
if (fs.existsSync('deploy/' + env + '.properties')) {
589-
log('deploy/' + env + '.properties already exists!');
590-
} else {
591-
var envProperties = '#################################################################\n' +
592-
'# This file contains overrides to values in build.properties\n' +
593-
'# These only affect your local environment and should not be checked in\n' +
594-
'#################################################################\n' +
595-
'\n' +
596-
'server-version=' + settings.mlVersion + '\n' +
597-
'\n' +
598-
'#\n' +
599-
'# The ports used by your application\n' +
600-
'#\n' +
601-
'app-port=' + settings.appPort + '\n';
602602
if (settings.mlVersion < 8) {
603-
envProperties += 'xcc-port=' + settings.xccPort + '\n';
604-
}
605-
else
606-
{
607-
envProperties += '# Taking advantage of not needing a XCC Port for ML8\n' +
608-
'xcc-port=${app-port}\n' +
609-
'install-xcc=false\n';
603+
configJSON['ml-xcc-port'] = settings.xccPort;
610604
}
611605

612-
envProperties += '\n' +
613-
'#\n' +
614-
'# the uris or IP addresses of your servers\n' +
615-
'# WARNING: if you are running these scripts on WINDOWS you may need to change localhost to 127.0.0.1\n' +
616-
'# There have been reported issues with dns resolution when localhost wasn\'t in the hosts file.\n' +
617-
'#\n' +
618-
env + '-server=' + settings.marklogicHost + '\n' +
619-
'content-forests-per-host=3\n' +
620-
'\n' +
621-
'#\n' +
622-
'# Admin username/password that will exist on the local/dev/prod servers\n' +
623-
'#\n' +
624-
'user=' + settings.marklogicAdminUser + '\n' +
625-
'password=' + settings.marklogicAdminPass + '\n';
626-
627-
fs.writeFileSync('deploy/' + env + '.properties', envProperties, {encoding: 'utf8'});
628-
log('Created deploy/' + env + '.properties.');
606+
configJSON['node-port'] = settings.nodePort;
607+
configJSON['guest-access'] = settings.guestAccess;
608+
configJSON['disallow-updates'] = settings.disallowUpdates;
609+
configJSON['appusers-only'] = settings.appUsersOnly;
610+
611+
var configString = JSON.stringify(configJSON, null, 2) + '\n';
612+
fs.writeFileSync(env + '.json', configString, { encoding: 'utf8' });
613+
log('Created ' + env + '.json.');
614+
615+
if (fs.existsSync('deploy/' + env + '.properties')) {
616+
log('NOTE: deploy/' + env + '.properties already exists, change manually please!');
617+
} else {
618+
var envProperties = '#################################################################\n' +
619+
'# This file contains overrides to values in build.properties\n' +
620+
'# These only affect your local environment and should not be checked in\n' +
621+
'#################################################################\n' +
622+
'\n' +
623+
'server-version=' + settings.mlVersion + '\n' +
624+
'\n' +
625+
'#\n' +
626+
'# The ports used by your application\n' +
627+
'#\n' +
628+
'app-port=' + settings.appPort + '\n';
629+
if (settings.mlVersion < 8) {
630+
envProperties += 'xcc-port=' + settings.xccPort + '\n';
631+
}
632+
else
633+
{
634+
envProperties += '# Taking advantage of not needing a XCC Port for ML8\n' +
635+
'xcc-port=${app-port}\n' +
636+
'install-xcc=false\n';
637+
}
638+
639+
envProperties += '\n' +
640+
'#\n' +
641+
'# the uris or IP addresses of your servers\n' +
642+
'# WARNING: if you are running these scripts on WINDOWS you may need to change localhost to 127.0.0.1\n' +
643+
'# There have been reported issues with dns resolution when localhost wasn\'t in the hosts file.\n' +
644+
'#\n' +
645+
env + '-server=' + settings.marklogicHost + '\n' +
646+
'content-forests-per-host=3\n' +
647+
'\n' +
648+
'#\n' +
649+
'# Admin username/password that will exist on the local/dev/prod servers\n' +
650+
'#\n' +
651+
'user=' + settings.marklogicAdminUser + '\n' +
652+
'password=' + settings.marklogicAdminPass + '\n';
653+
654+
fs.writeFileSync('deploy/' + env + '.properties', envProperties, {encoding: 'utf8'});
655+
log('Created deploy/' + env + '.properties.');
656+
}
657+
done();
658+
} catch (e) {
659+
log('Failed to write ' + env + ' config files: ' + e.message);
660+
done();
629661
}
630-
done();
631-
} catch (e) {
632-
log('Failed to write ' + env + ' config files: ' + e.message);
633-
done();
634-
}
662+
});
635663
});
636664
}
637665
}
@@ -926,4 +954,36 @@ function notify(options) {
926954
notifier.notify(notifyOptions);
927955
}
928956

957+
function run(cmd, args, verbose) {
958+
var d = q.defer();
959+
var output = '';
960+
961+
console.log('Spawning ' + cmd + ' ' + args.join(' '));
962+
var child = spawn(cmd, args, {
963+
stdio: [
964+
0, // Use parents stdin for child
965+
'pipe', // Pipe child's stdout to parent (default)
966+
'pipe' // Pipe child's stderr to parent (default)
967+
]
968+
});
969+
970+
child.on('close', function() {
971+
console.log('done running ' + cmd);
972+
d.resolve(output);
973+
});
974+
975+
child.stdout.on('data', function (chunk) {
976+
if (verbose) {
977+
console.log(chunk.toString());
978+
}
979+
output += chunk.toString();
980+
});
981+
982+
child.stderr.on('data', function (data) {
983+
console.log(data.toString());
984+
});
985+
986+
return d.promise;
987+
}
988+
929989
module.exports = gulp;

app/templates/node-server/proxy.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ router.put('*', function(req, res) {
3535
// For PUT requests, require authentication
3636
if (req.session.user === undefined) {
3737
res.status(401).send('Unauthorized');
38-
} else if (options.readOnlyAccess || (req.path === '/v1/documents' &&
38+
} else if (options.disallowUpdates || (req.path === '/v1/documents' &&
3939
req.query.uri.match('/api/users/') &&
4040
req.query.uri.match(new RegExp('/api/users/[^(' + req.session.user.username + ')]+.json')))) {
4141
// The user is trying to PUT to a profile document other than his/her own. Not allowed.
@@ -62,7 +62,7 @@ router.post('*', function(req, res) {
6262
noCache(res);
6363
if (req.session.user === undefined) {
6464
res.status(401).send('Unauthorized');
65-
} else if (options.readOnlyAccess) {
65+
} else if (options.disallowUpdates) {
6666
res.status(403).send('Forbidden');
6767
} else {
6868
proxy(req, res);
@@ -74,7 +74,7 @@ router.delete('*', function(req, res) {
7474
noCache(res);
7575
if (req.session.user === undefined) {
7676
res.status(401).send('Unauthorized');
77-
} else if (options.readOnlyAccess) {
77+
} else if (options.disallowUpdates) {
7878
res.status(403).send('Forbidden');
7979
} else {
8080
proxy(req, res);

0 commit comments

Comments
 (0)