tooltwist
Version:
Tooltwist Command Line Interface
238 lines (205 loc) • 6.35 kB
JavaScript
var Log = require('./log'),
Config = require('./config'),
Templates = require('./templates'),
stdio = require('stdio'),
FipPairing = require('./fipPairing');
var IMAGE_DIR = '.tooltwist/image';
/**
* Create a default config file for the Designer.
*/
exports.createDefaultConfigFile = function() {
Log.debug('initDesigner.createConfigFile()');
console.log()
console.log('\tYour server must be set up with a user named \'tooltwist\' and have')
console.log('\tthe standard files structure installed, including serverKit and the')
console.log('\tstartup scripts installed.')
console.log();
stdio.question('Is your server configured as described?', ['y', 'n'], function (err, answer) {
if (err || answer.toLowerCase() != 'y') {
console.log()
console.log('\tNo problem. Come back and run this command when it\'s ready.')
console.log()
process.exit(1)
}
askAboutFIP();
});
}
/**
* Ask whether FIP pairing should be attempted now.
*/
function askAboutFIP(){
console.log()
console.log('\tFIP (File Installation Protocol) will be used to install files')
console.log('\ton your server. This can be set up manually, but it\'s simpler')
console.log('\tto let this command do it. In order to do so, you must have SSH')
console.log('\tkeys set up to allow access to the server without a password.')
console.log()
stdio.question('Do you have SSH keys set up for the server?', ['y', 'n'], function (err, answer) {
if (err) {
console.log('Error: ' + err);
process.exit(1)
}
if (answer.toLowerCase() === 'y') {
// Pair up FIP
Log.always();
Log.always('\tGreat. I\'ll try to set pair this machine and your server.');
Log.always();
askHostname(true);
} else {
// Don't set up FIP.
console.log()
console.log('\tOk, no worries. A .fip-source file will be created on this machine,')
console.log('\tbut you will need to create a .fip-destination file on your server')
console.log('\tand put matching entries in both files.')
console.log();
askHostname(false);
}
});
}
/**
* Ask the hostname
*/
function askHostname(pairFip) {
stdio.question('What is the hostname or IP address of the server?', function (err, host) {
if (err || host === '') {
console.log()
console.log('\tSorry, you must enter host details. Cannot proceed.');
console.log()
process.exit(1)
}
console.log('host is ' + host)
askFipPort(pairFip, host);
});
}
function askFipPort(pairFip, host){
stdio.question('What port does FIP use on the remote server (default=39393)?', function (err, fipPort) {
if (err) {
console.log()
console.log('\tSorry, you must enter SSH port details. Cannot proceed.');
console.log()
process.exit(1)
}
if (fipPort === '') {
fipPort = 39393;
} else {
try {
fipPort = parseInt(fipPort)
} catch (e) {
console.log()
console.log('\tSorry, the FIP port must be a number', e);
console.log()
process.exit(1)
}
}
console.log('fipPort is ' + fipPort)
if (err) {
console.log('Error: ' + err);
process.exit(1)
}
if (pairFip) {
askSshUser(host, fipPort);
} else {
createConfig(host, fipPort);
}
});
}
function askSshUser(host, fipPort) {
stdio.question('What username is used on the server (default=tooltwist)?', function (err, sshUser) {
if (err) {
console.log()
console.log('\tSorry, you must enter a username. Cannot proceed.');
console.log()
process.exit(1)
}
if (sshUser === '') {
sshUser = 'tooltwist';
}
console.log('username is ' + sshUser)
askSshPort(host, fipPort, sshUser);
});
}
function askSshPort(host, fipPort, sshUser) {
stdio.question('What port does SSH use on the server (default=22)?', function (err, sshPort) {
if (err) {
console.log()
console.log('\tSorry, you must enter SSH port details. Cannot proceed.');
console.log()
process.exit(1)
}
if (sshPort === '') {
sshPort = 22;
} else {
try {
sshPort = parseInt(sshPort)
} catch (e) {
console.log()
console.log('\tSorry, an SSH port must be a number', e);
console.log()
process.exit(1)
}
}
console.log('sshPort is ' + sshPort)
askServerHome(host, fipPort, sshUser, sshPort);
});
}
function askServerHome(host, fipPort, sshUser, sshPort) {
var defaultServerHome = '/home/' + sshUser + '/server';
stdio.question('What directory will we deploy to on the server (default=' + defaultServerHome + ')?', function (err, serverHome) {
if (err) {
console.log()
console.log('\tSorry, you must enter a username. Cannot proceed.');
console.log()
process.exit(1)
}
if (serverHome === '') {
serverHome = defaultServerHome;
}
console.log('serverHome is ' + serverHome)
// Pair FIP with the remote server
FipPairing.pairWithServer(IMAGE_DIR, host, sshUser, sshPort, serverHome, function(err, wasPaired){
Log.debug('Pairing result is ', err, wasPaired)
if ( !wasPaired) {
Log.error()
Log.error('\tNOTE: Pairing was not successful.')
Log.error('\tThe config file HAS NOT been created.')
Log.error()
process.exit(1);
}
Log.log()
Log.log('\tThe server is now paired with this machine.')
Log.log()
createConfig(host, fipPort, sshUser, sshPort, serverHome);
})
});
}
function createConfig(host, fipPort, sshUser, sshPort, serverHome) {
// Use the template to create the file
var from = __dirname + "/../templates/config/deploy-tooltwist.js"
Log.debug('from=' + from)
var data = {
MODE: 'deploy',
HOST: host,
FIP_PORT: fipPort,
SSH_USER: sshUser,
SSH_PORT: sshPort,
SERVER_HOME: serverHome
};
var creatingExampleTemplate = false;
Templates.copy(from, Config.CONFIG_FILE, creatingExampleTemplate, data);
// Display a nice message.
Log.always();
Log.always('\tSpecification file \'' + Config.CONFIG_FILE + '\' has been created with a default');
Log.always('\tconfiguration that will build, generate and deploy the demo web design')
Log.always('\tproject \'ttdemo\' to server ' + host);
Log.always()
Log.always('\tModify this file to suit your needs, then run:');
Log.always();
Log.always('\t $ tooltwist deploy')
Log.always();
Log.always('\tFor debugging purposes you can also run:')
Log.always();
Log.always('\t $ tooltwist designer')
Log.always();
// All done
process.exit(0)
}