telebit.js/usr/share/install-launcher.js

296 lines
11 KiB
JavaScript
Raw Normal View History

2018-06-27 09:21:44 +00:00
'use strict';
//var fs = require('fs');
var os = require('os');
var mkdirp = require('mkdirp');
var exec = require('child_process').exec;
var path = require('path');
2018-06-28 03:35:14 +00:00
var Launcher = module.exports;
Launcher.install = function (things, fn) {
if (!fn) { fn = function (err) { if (err) { console.error(err); } }; }
2018-06-27 09:21:44 +00:00
things = things || {};
// in some future version we can take this file out
// and accept process.env from things
var installLauncher = require('./template-launcher');
2018-06-27 09:21:44 +00:00
// Right now this is just for npm install -g and npx
if (things.env) {
things.env.PATH = things.env.PATH || process.env.PATH;
2018-06-28 03:35:14 +00:00
} else {
things.env = process.env;
2018-06-27 09:21:44 +00:00
}
2018-06-28 03:35:14 +00:00
things.argv = things.argv || process.argv;
things._execOpts = { windowsHide: true, env: things.env };
2018-06-27 09:21:44 +00:00
var telebitRoot = path.join(__dirname, '../..');
var vars = {
telebitPath: telebitRoot
, telebitUser: os.userInfo().username
, telebitGroup: (/^darwin/i.test(os.platform()) ? 'staff' : os.userInfo().username)
, telebitRwDirs: [
2018-06-28 07:53:03 +00:00
telebitRoot
2018-06-27 09:21:44 +00:00
, path.join(os.homedir(), '.config/telebit')
, path.join(os.homedir(), '.local/share/telebit')
]
2018-06-28 03:35:14 +00:00
, telebitNode: (things.argv[0]||'').replace(/\.exe/i, '') // path.join(telebitRoot, 'bin/node')
2018-06-27 09:21:44 +00:00
, telebitBin: path.join(telebitRoot, 'bin/telebit')
, telebitdBin: path.join(telebitRoot, 'bin/telebitd')
, telebitJs: path.join(telebitRoot, 'bin/telebit.js')
, telebitdJs: path.join(telebitRoot, 'bin/telebitd.js')
2018-06-27 09:21:44 +00:00
, telebitConfig: path.join(os.homedir(), '.config/telebit/telebit.yml')
, telebitdConfig: path.join(os.homedir(), '.config/telebit/telebitd.yml')
, TELEBIT_LOG_DIR: path.join(os.homedir(), '.local/share/telebit/var/log')
2018-06-27 09:21:44 +00:00
};
2018-06-28 03:35:14 +00:00
vars.telebitBinTpl = path.join(telebitRoot, 'usr/share/dist/bin/telebit.tpl');
vars.telebitNpm = path.resolve(vars.telebitNode, '../npm');
vars.nodePath = path.resolve(vars.telebitNode, '../lib/node_modules');
vars.npmConfigPrefix = path.resolve(vars.telebitNode, '..');
2018-06-28 03:35:14 +00:00
vars.userspace = (!things.telebitUser || (things.telebitUser === os.userInfo().username)) ? true : false;
if (-1 === vars.telebitRwDirs.indexOf(vars.npmConfigPrefix)) {
vars.telebitRwDirs.push(vars.npmConfigPrefix);
}
vars.telebitRwDirs = vars.telebitRwDirs.join(' ');
2018-06-28 03:35:14 +00:00
function getError(err, stderr) {
if (err) { return err; }
if (stderr) {
err = new Error(stderr);
err.code = 'ELAUNCHER';
return err;
}
}
2018-06-27 09:21:44 +00:00
var launchers = {
2018-06-28 03:35:14 +00:00
'node': function () {
var fs = require('fs');
var spawn = require('child_process').spawn;
var logpath = path.join(os.homedir(), '.local/share/telebit/var/log');
try {
mkdirp.sync(logpath);
} catch(e) {
if (fn) { fn(e); return; }
return;
}
var stdout = fs.openSync(path.join(logpath, 'info.log'), 'a');
var stderr = fs.openSync(path.join(logpath, 'error.log'), 'a');
2018-06-28 07:53:03 +00:00
var killed = 0;
2018-06-28 03:35:14 +00:00
var err;
var subprocess = spawn(
vars.telebitNode
2018-06-28 07:53:03 +00:00
, [ path.join(telebitRoot, 'bin/telebitd.js')
2018-06-28 03:35:14 +00:00
, 'daemon'
, '--config'
, vars.telebitdConfig
]
, { detached: true
, stdio: [ 'ignore', stdout, stderr ]
}
);
subprocess.unref();
subprocess.on('error', function (_err) {
err = _err;
2018-06-28 07:53:03 +00:00
killed += 1;
2018-06-28 03:35:14 +00:00
});
subprocess.on('exit', function (code, signal) {
if (!err) { err = new Error('' + code + ' ' + signal + ' failure to launch'); }
2018-06-28 07:53:03 +00:00
killed += 1;
2018-06-28 03:35:14 +00:00
});
setTimeout(function () {
2018-06-28 07:53:03 +00:00
if (fn) { fn(null); return; }
2018-06-28 03:35:14 +00:00
}, 1 * 1000);
return;
}
, 'launchctl': function () {
2018-06-27 09:21:44 +00:00
var launcher = path.join(os.homedir(), 'Library/LaunchAgents/cloud.telebit.remote.plist');
try {
mkdirp.sync(path.join(os.homedir(), 'Library/LaunchAgents'));
2018-06-28 03:35:14 +00:00
mkdirp.sync(path.join(vars.telebitPath, 'bin'));
mkdirp.sync(vars.TELEBIT_LOG_DIR);
installLauncher.sync({
2018-06-28 03:35:14 +00:00
file: {
tpl: vars.telebitBinTpl
, launcher: path.join(vars.telebitPath, 'bin/telebit')
2018-06-27 09:21:44 +00:00
}
, vars: vars
});
installLauncher({
file: {
2018-06-28 03:35:14 +00:00
tpl: path.join(vars.telebitPath, 'usr/share/dist/etc/skel/Library/LaunchAgents/cloud.telebit.remote.plist.tpl')
, launcher: launcher
}
2018-06-28 03:35:14 +00:00
, vars: vars
});
var launcherstr = (vars.userspace ? "" : "sudo ") + "launchctl ";
var execstr = launcherstr + "unload -w " + launcher;
exec(execstr, things._execOpts, function (/*err, stdout, stderr*/) {
// we probably only need to skip the stderr (saying that it can't stop something that isn't started)
//err = getError(err, stderr);
//if (err) { fn(err); return; }
2018-06-28 03:35:14 +00:00
//console.log((stdout||'').trim());
//console.log('unload worked?');
execstr = launcherstr + "load -w " + launcher;
exec(execstr, things._execOpts, function (err, stdout, stderr) {
2018-06-28 03:35:14 +00:00
err = getError(err, stderr);
if (err) { fn(err); return; }
//console.log((stdout||'').trim());
//console.log('load worked?');
fn(null);
2018-06-27 09:21:44 +00:00
});
});
} catch(e) {
console.error("'" + launcher + "' error:");
console.error(e);
2018-06-28 03:35:14 +00:00
if (fn) { fn(e); return; }
2018-06-27 09:21:44 +00:00
}
}
2018-06-28 03:35:14 +00:00
, 'systemctl': function () {
2018-06-27 09:21:44 +00:00
var launcher = path.join(os.homedir(), '.config/systemd/user/telebit.service');
2018-06-28 07:53:03 +00:00
var launchername = 'telebit.service';
2018-06-27 09:21:44 +00:00
try {
mkdirp.sync(path.join(os.homedir(), '.config/systemd/user'));
installLauncher({
file: {
2018-06-28 03:35:14 +00:00
tpl: path.join(vars.telebitPath, 'usr/share/dist/etc/skel/.config/systemd/user/telebit.service.tpl')
2018-06-27 09:21:44 +00:00
, launcher: launcher
}
, vars: vars
}, function () {
2018-06-28 07:53:03 +00:00
// IMPORTANT
// It's a dangerous to go alone, take this:
// SYSTEMD_LOG_LEVEL=debug journalctl -xef --user-unit=telebit
// (makes debugging systemd issues not "easy" per se, but possible)
2018-06-28 03:35:14 +00:00
var launcherstr = (vars.userspace ? "" : "sudo ") + "systemctl " + (vars.userspace ? "--user " : "");
2018-06-28 08:01:43 +00:00
var execstr = launcherstr + "daemon-reload";
exec(execstr, things._execOpts, function (err, stdout, stderr) {
2018-06-28 03:35:14 +00:00
err = getError(err, stderr);
if (err) { fn(err); return; }
//console.log((stdout||'').trim());
2018-06-28 08:01:43 +00:00
var execstr = launcherstr + "enable " + launchername;
exec(execstr, things._execOpts, function (err, stdout, stderr) {
2018-06-28 03:35:14 +00:00
err = getError(err, stderr);
if (err) { fn(err); return; }
//console.log((stdout||'').trim());
2018-06-28 08:01:43 +00:00
var execstr = launcherstr + "restart " + launchername;
exec(execstr, things._execOpts, function (err, stdout, stderr) {
2018-06-28 03:35:14 +00:00
err = getError(err, stderr);
if (err) { fn(err); return; }
//console.log((stdout||'').trim());
2018-06-28 08:01:43 +00:00
setTimeout(function () {
var execstr = launcherstr + "status " + launchername;
exec(execstr, things._execOpts, function (err, stdout, stderr) {
err = getError(err, stderr);
if (err) { fn(err); return; }
if (!/active.*running/i.test(stdout)) {
err = new Error("systemd failed to start '" + launchername + "'");
}
if (err) { fn(err); return; }
//console.log((stdout||'').trim());
fn(null);
});
}, 1.25 * 1000);
2018-06-27 09:21:44 +00:00
});
});
});
});
} catch(e) {
console.error("'" + launcher + "' error:");
console.error(e);
2018-06-28 03:35:14 +00:00
if (fn) { fn(e); return; }
2018-06-27 09:21:44 +00:00
}
}
, 'reg.exe': function () {
2018-06-28 03:35:14 +00:00
if (!vars.userspace) {
console.warn("sysetm-level, privileged services are not yet supported on windows");
}
2018-06-27 09:21:44 +00:00
vars.telebitNode += '.exe';
var cmd = 'reg.exe add "HKCU\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"'
+ ' /V "Telebit" /t REG_SZ /D '
2018-06-28 03:35:14 +00:00
+ '"' + things.argv[0] + ' /c ' // something like C:\\Program Files (x64)\nodejs\node.exe
2018-06-27 09:21:44 +00:00
+ [ path.join(__dirname, 'bin/telebitd.js')
, 'daemon'
, '--config'
, path.join(os.homedir(), '.config/telebit/telebitd.yml')
].join(' ')
+ '" /F'
;
2018-06-28 03:35:14 +00:00
exec(cmd, things._execOpts, function (err, stdout, stderr) {
err = getError(err, stderr);
if (err) { fn(err); return; }
//console.log((stdout||'').trim());
fn(null);
});
2018-06-27 09:21:44 +00:00
}
};
function run(err, launcher) {
if (err) {
2018-06-28 03:35:14 +00:00
console.error("No luck with '" + launcher + "', trying a child process instead...");
console.error(err);
2018-06-28 03:35:14 +00:00
launcher = 'node';
2018-06-27 09:21:44 +00:00
}
if (launchers[launcher]) {
2018-06-28 03:35:14 +00:00
// console.log('Launching with launcher ' + launcher);
2018-06-27 09:21:44 +00:00
launchers[launcher]();
return;
} else {
console.error("No launcher handler for '" + launcher+ "'");
2018-06-27 09:21:44 +00:00
}
}
2018-06-28 03:35:14 +00:00
if (things.launcher) {
if ('string' === typeof things.launcher) {
run(null, things.launcher);
return;
}
if ('function' === typeof things.launcher) {
things._vars = vars;
things._userspace = vars.userspace;
things.launcher(things);
return;
}
}
2018-06-27 09:21:44 +00:00
// could have used "command-exists" but I'm trying to stay low-dependency
// os.platform(), os.type()
if (!/^win/i.test(os.platform())) {
if (/^darwin/i.test(os.platform())) {
2018-06-28 07:53:03 +00:00
exec('command -v launchctl', things._execOpts, function (err, stdout, stderr) {
2018-06-28 03:35:14 +00:00
err = getError(err, stderr);
2018-06-27 09:21:44 +00:00
run(err, 'launchctl');
2018-06-28 03:35:14 +00:00
});
2018-06-27 09:21:44 +00:00
} else {
2018-06-28 07:53:03 +00:00
exec('command -v systemctl', things._execOpts, function (err, stdout, stderr) {
2018-06-28 03:35:14 +00:00
err = getError(err, stderr);
run(err, 'systemctl');
2018-06-28 03:35:14 +00:00
});
2018-06-27 09:21:44 +00:00
}
} else {
// https://stackoverflow.com/questions/17908789/how-to-add-an-item-to-registry-to-run-at-startup-without-uac
// wininit? regedit? SCM?
// REG ADD "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" /V "My App" /t REG_SZ /F /D "C:\MyAppPath\MyApp.exe"
// https://www.microsoft.com/developerblog/2015/11/09/reading-and-writing-to-the-windows-registry-in-process-from-node-js/
// https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/reg-add
// https://social.msdn.microsoft.com/Forums/en-US/5b318f44-281e-4098-8dee-3ba8435fa391/add-registry-key-for-autostart-of-app-in-ice?forum=quebectools
// utils.elevate
// https://github.com/CatalystCode/windows-registry-node
2018-06-28 03:35:14 +00:00
exec('where reg.exe', things._execOpts, function (err, stdout, stderr) {
console.log((stdout||'').trim());
2018-06-27 09:21:44 +00:00
if (stderr) {
console.error(stderr);
}
run(err, 'reg.exe');
});
}
};
if (module === require.main) {
2018-06-28 03:35:14 +00:00
module.exports.install({
argv: process.argv
, env: process.env
}, function (err) {
if (err) { console.error(err); return; }
console.log("Telebit launched, or so it seems.");
});
}