Created
July 13, 2016 19:56
-
-
Save wparad/897907f8dbfce3818aea3380e3103eea to your computer and use it in GitHub Desktop.
Nodejs install linux service using update-rc.d
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
'use strict'; | |
/* | |
These command steps are run on the remote application server after the VM has been created. | |
*/ | |
var commander = require('commander'); | |
var exec = require('child_process').spawnSync; | |
var fs = require('fs'); | |
var https = require('https'); | |
var mustache = require('mustache'); | |
var path = require('path'); | |
var spawn = require('child_process').spawn; | |
var package_metadata = require('../package.json'); | |
var version = package_metadata.version; | |
var name = package_metadata.name; | |
var CWD = path.resolve(__dirname, '..'); | |
commander.version(version); | |
commander | |
.command('info') | |
.description('Get deployment information.') | |
.action(function() { | |
if(__dirname.match(/lib\/node_modules\/[^\/]+/)) { | |
console.log('Filename: ' + __filename); | |
console.log('Directory: ' + CWD); | |
} | |
}); | |
var init_script = path.join('/etc/init.d/', name); | |
commander | |
.command('install') | |
.description('Create init scripts and start service.') | |
.action(function() { | |
if(fs.existsSync(init_script)) { | |
console.log('Stopping current running instance.'); | |
exec(init_script, ['stop']); | |
} | |
console.log('Creating init scripts %s (%s)', name, version); | |
var template = path.join(CWD, 'bin', 'init.d.template'); | |
var output = mustache.render(fs.readFileSync(template, {encoding: 'utf8'}), {application_name: name}); | |
fs.writeFileSync(init_script, output); | |
fs.chmodSync(init_script, '0755'); | |
console.log('Adding service to startup'); | |
exec('update-rc.d', ['-f', name, 'defaults']); | |
var certs = path.join(CWD, 'certs'); | |
if(!fs.existsSync(certs)) { fs.mkdirSync(certs); } | |
fs.writeFileSync(path.join(certs, 'key.pem'), fs.readFileSync('/opt/infra/pki/wildcard.key')); | |
fs.writeFileSync(path.join(certs, 'cert.pem'), fs.readFileSync('/opt/infra/pki/wildcard.pem')); | |
console.log('Starting the service.'); | |
var service_start = spawn(init_script, ['start'], {detached: true, stdio: 'ignore'}); | |
service_start.unref(); | |
console.log(''); | |
}); | |
commander | |
.command('uninstall') | |
.description('Create init scripts and start service.') | |
.action(function() { | |
console.log('Removing %s (%s)', name, version); | |
console.log('Stopping service.'); | |
exec(init_script, ['stop']); | |
console.log('Removing service startup.'); | |
exec('update-rc.d', ['-f', name, 'remove']); | |
console.log('Removing init scripts.'); | |
fs.unlinkSync(init_script); | |
console.log(''); | |
}); | |
commander.on('*', function() { | |
console.log('Unknown Command: ' + commander.args.join(' ')); | |
commander.help(); | |
process.exit(0); | |
}); | |
commander.parse(process.argv[2] ? process.argv : process.argv.concat(['install'])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment