Permalink
Browse files

[fix][WIP] basic working order, starting CLI cleanup

  • Loading branch information...
bmeck authored and indexzero committed Sep 27, 2011
1 parent 6f68823 commit bad47f617e184a9e927fcbf46bca2f64e4051a77
View
@@ -1,86 +0,0 @@
-#!/bin/bash
-#
-# initd-example Node init.d
-#
-# chkconfig: 345 80 20
-# description: Node init.d example
-# processname: node
-# pidfile: /var/run/initd-example.pid
-# logfile: /var/log/initd-example.log
-#
-
-# Source function library.
-. /lib/lsb/init-functions
-PATH=$PATH:/usr/local/bin
-
-NAME=foreverd
-pidfile=/var/run/$NAME.pid
-logfile=/var/log/$NAME.log
-forever_dir=/var/local/forever # Forever root directory.
-
-node=node
-forever=`which forever`
-awk=awk
-sed=sed
-
-start() {
- echo "Starting $NAME: "
-
- if [ "$id" = "" ]; then
- # Create the log and pid files, making sure that the target use has access to them
- touch $logfile
- chown $USER $logfile
-
- # Launch the application
- $forever service-start -p $forever_dir
- else
- echo "Instance already running"
- fi
- RETVAL=$?
-}
-
-restart() {
- echo -n "Restarting $NAME: "
- if [ "$id" != "" ]; then
- $forever service-restart -p $forever_dir
- RETVAL=$?
- else
- start
- fi
-}
-
-stop() {
- echo -n "Shutting down $NAME: "
- if [ "$id" != "" ]; then
- $forever service-stop -p $forever_dir
- else
- echo "Instance is not running";
- fi
- RETVAL=$?
-}
-
-getForeverId() {
- local pid=$(pidofproc -p $pidfile)
- $forever list -p $forever_dir | $sed -e 's/\x1b\[[0-9; ]*m//g' | $awk "\$4 == \"$pid]\" { gsub(/[\[\]]/, \"\", \$1); print \$1; }"
-}
-id=$(getForeverId)
-
-case "$1" in
- start)
- start
- ;;
- stop)
- stop
- ;;
- status)
- $forever list -p $forever_dir
- ;;
- restart)
- restart
- ;;
- *)
- echo "Usage: {start|stop|status|restart}"
- exit 1
- ;;
-esac
-exit $RETVAL
View
@@ -225,152 +225,6 @@ cli.clear = function (key) {
});
};
-//
-// ### function service(action)
-// add
-// install
-// start
-// stop
-// restart
-//
-// TODO - guard against user invocation
-//
-cli['service-add'] = function (file, options) {
- //
- // Add descriptor to our service list
- // this is just a json file in $root/services/*.json
- //
- forever.config.set('root', path.join('/var', 'local', 'forever'));
- var options;
- function save() {
- var service = {
- file: file,
- options: options
- };
- options.appendLog = true;
- var filePath = path.join(forever.config.get('root'), 'services', options.uid + '.json');
- fs.writeFileSync(filePath, JSON.stringify(service));
- }
- save();
-}
-cli['service-install'] = function () {
- //
- // Copy the init.d script to the right location
- // TODO Distribution fixes?
- //
- forever.config.set('root', path.join('/var', 'local', 'foreverd'));
- forever.log.info('Creating service environment.');
- var initdPath = path.join('/etc', 'init.d', 'foreverd');
- try {
- fs.mkdirSync(forever.config.get('root'), 0777);
- fs.mkdirSync(path.join(forever.config.get('root'), 'services'), 0777);
- }
- catch (e) {
- if (e.code !== 'EEXIST') {
- forever.log.error('Error creating service directory.');
- forever.log.error(e.message);
- return;
- }
- }
- forever.log.info('Installing init.d script.');
- var script = fs.createReadStream(path.join(__dirname, '..', '..', 'init.d', 'foreverd'));
- var target = fs.createWriteStream(initdPath, {
- flags: 'w',
- mode: 0777
- });
- script.pipe(target);
- script.on('end', function() {
- forever.log.info('Adding init.d script to run levels');
- var directories = fs.readdirSync('/etc');
- directories.forEach(function (directory) {
- var match = directory.match(/^rc(\d+)\.d$/);
- if(match) {
- var kill_or_start = {0:true, 1:true, 6:true}[match[1]] ? 'K' : 'S';
- fs.symlinkSync(initdPath, path.join('/etc',directory,kill_or_start+'20foreverd'));
- }
- });
- });
-}
-cli['service-start'] = function (file, options) {
- //
- // Start all of the scripts
- //
- var pidFilePath = path.join('/var','run', 'foreverd.pid');
- var logFilePath = path.join('/var','log','foreverd');
- process.on('exit', function() {
- try{
- fs.unlinkSync(pidFilePath);
- }
- catch(err) {
- //we are exiting anyway. this may have some noexist error already
- }
- })
- forever.config.set('root', path.join('/var', 'local', 'foreverd'));
- fs.open(logFilePath, 'w+', function (err, logFile) {
- if(err) {
- throw err;
- }
- daemon.start(logFile);
- daemon.lock(pidFilePath);
-
- var serviceFiles = fs.readdirSync(path.join(forever.config.get('root'), 'services'));
- if (serviceFiles.length === 0) {
- forever.log.info('No services found.');
- return;
- }
- var monitors = [];
- serviceFiles.forEach(function (serviceFile, index) {
- var serviceFilePath = path.join(forever.config.get('root'), 'services', serviceFile);
- var service = JSON.parse(fs.readFileSync(serviceFilePath));
- var file = service.file;
- forever.log.info('Starting service for ' + file);
- var options = service.options;
- options.minUptime = 200;
- var monitor = new forever.Monitor(file, options);
- monitor.start();
- monitors.push(monitor);
- if(index === serviceFiles.length - 1) {
- forever.startServer.apply(forever, monitors);
- }
- });
- });
-}
-cli['service-stop'] = function (file, options) {
- //
- // Stop all the scripts
- //
- forever.config.set('root', path.join('/var', 'local', 'foreverd'));
- var serviceFiles = fs.readdirSync(path.join(forever.config.get('root'), 'services'));
- if (serviceFiles.length === 0) {
- forever.log.info('No services found.');
- return;
- }
- serviceFiles.forEach(function (serviceFile) {
- var serviceFilePath = path.join(forever.config.get('root'), 'services', serviceFile);
- var service = JSON.parse(fs.readFileSync(serviceFilePath));
- var file = service.file;
- cli.stop(file);
- });
-}
-cli['service-restart'] = function (file, options) {
- //
- // Restart all the scripts
- //
- forever.config.set('root', path.join('/var', 'local', 'foreverd'));
- var serviceFiles = fs.readdirSync(path.join(forever.config.get('root'), 'services'));
- if (serviceFiles.length === 0) {
- forever.log.info('No services found.');
- return;
- }
-
- serviceFiles.forEach(function (serviceFile) {
- var serviceFilePath = path.join(forever.config.get('root'), 'services', serviceFile);
- var service = JSON.parse(fs.readFileSync(serviceFilePath));
- var file = service.file;
- cli.restart(file);
- });
-}
-
//
// ### function columns (action, value)
// #### @action {string} The subaction to execute
View
@@ -1,3 +1,5 @@
+module.exports = ForeverServiceAdapter;
+
function ForeverServiceAdapter(service) {
this.service = service;
}
@@ -27,12 +27,8 @@ start() {
echo "Starting $NAME: "
if [ "$id" = "" ]; then
- # Create the log and pid files, making sure that the target use has access to them
- touch $logfile
- chown $USER $logfile
-
# Launch the application
- $foreverd -p $forever_dir
+ $foreverd -p $forever_dir run
else
echo "Instance already running"
fi
@@ -42,7 +38,7 @@ start() {
restart() {
echo -n "Restarting $NAME: "
if [ "$id" != "" ]; then
- $foreverd -p $forever_dir
+ $foreverd -p $forever_dir restart
RETVAL=$?
else
start
@@ -52,7 +48,7 @@ restart() {
stop() {
echo -n "Shutting down $NAME: "
if [ "$id" != "" ]; then
- $foreverd -p $forever_dir
+ $foreverd -p $forever_dir stop
else
echo "Instance is not running";
fi
@@ -73,7 +69,7 @@ case "$1" in
stop
;;
status)
- $foreverd list -p $forever_dir
+ $foreverd -p $forever_dir list
;;
restart)
restart
Oops, something went wrong.

0 comments on commit bad47f6

Please sign in to comment.