node-based install launcher tested on macOS

This commit is contained in:
AJ ONeal 2018-06-27 04:24:56 -06:00
parent d85682ff7f
commit 5eb4cdefb7
6 changed files with 81 additions and 49 deletions

View File

@ -408,6 +408,7 @@ function serveControlsHelper() {
// mask is so that processes owned by other users // mask is so that processes owned by other users
// can speak to this process, which is probably root-owned // can speak to this process, which is probably root-owned
var oldUmask = process.umask(0x0000); var oldUmask = process.umask(0x0000);
require('mkdirp').sync(path.resolve(state._ipc.path, '..'));
controlServer.listen({ controlServer.listen({
path: state._ipc.path path: state._ipc.path
, writableAll: true , writableAll: true

View File

@ -17,9 +17,9 @@
<key>TELEBIT_PATH</key> <key>TELEBIT_PATH</key>
<string>{TELEBIT_PATH}</string> <string>{TELEBIT_PATH}</string>
<key>NODE_PATH</key> <key>NODE_PATH</key>
<string>{TELEBIT_PATH}/lib/node_modules</string> <string>{NODE_PATH}</string>
<key>NPM_CONFIG_PREFIX</key> <key>NPM_CONFIG_PREFIX</key>
<string>{TELEBIT_PATH}</string> <string>{NPM_CONFIG_PREFIX}</string>
</dict> </dict>
<key>UserName</key> <key>UserName</key>

View File

@ -19,9 +19,9 @@
<key>TELEBIT_PATH</key> <key>TELEBIT_PATH</key>
<string>{TELEBIT_PATH}</string> <string>{TELEBIT_PATH}</string>
<key>NODE_PATH</key> <key>NODE_PATH</key>
<string>{TELEBIT_PATH}/lib/node_modules</string> <string>{NODE_PATH}</string>
<key>NPM_CONFIG_PREFIX</key> <key>NPM_CONFIG_PREFIX</key>
<string>{TELEBIT_PATH}</string> <string>{NPM_CONFIG_PREFIX}</string>
</dict> </dict>
<!-- <!--

View File

@ -10,7 +10,7 @@ module.exports.install = function (things) {
things = things || {}; things = things || {};
// in some future version we can take this file out // in some future version we can take this file out
// and accept process.env from things // and accept process.env from things
var installLauncher = require('./install-launcher'); var installLauncher = require('./template-launcher');
// Right now this is just for npm install -g and npx // Right now this is just for npm install -g and npx
if (things.env) { if (things.env) {
@ -19,7 +19,7 @@ module.exports.install = function (things) {
var execOpts = { windowsHide: true, env: things.env || process.env }; var execOpts = { windowsHide: true, env: things.env || process.env };
var userspace = (!things.telebitUser || (things.telebitUser === os.userInfo().username)) ? true : false; var userspace = (!things.telebitUser || (things.telebitUser === os.userInfo().username)) ? true : false;
var telebitRoot = path.join(__dirname, '../..'); var telebitRoot = path.join(__dirname, '../..');
var telebitBinTpl = path.join(telebitRoot, 'dist/bin/telebit.tpl'); var telebitBinTpl = path.join(telebitRoot, 'usr/share/dist/bin/telebit.tpl');
var vars = { var vars = {
telebitPath: telebitRoot telebitPath: telebitRoot
, telebitUser: os.userInfo().username , telebitUser: os.userInfo().username
@ -28,54 +28,62 @@ module.exports.install = function (things) {
path.resolve(__dirname, '../..') path.resolve(__dirname, '../..')
, path.join(os.homedir(), '.config/telebit') , path.join(os.homedir(), '.config/telebit')
, path.join(os.homedir(), '.local/share/telebit') , path.join(os.homedir(), '.local/share/telebit')
].join(' ') ]
, telebitNode: path.join(telebitRoot, 'bin/node') , telebitNode: (process.argv[0]||'').replace(/\.exe/i, '') // path.join(telebitRoot, 'bin/node')
, telebitNpm: path.join(telebitRoot, 'bin/npm')
, telebitBin: path.join(telebitRoot, 'bin/telebit') , telebitBin: path.join(telebitRoot, 'bin/telebit')
, telebitdBin: path.join(telebitRoot, 'bin/telebitd') , telebitdBin: path.join(telebitRoot, 'bin/telebitd')
, telebitBinJs: path.join(telebitRoot, 'bin/telebit.js') , telebitJs: path.join(telebitRoot, 'bin/telebit.js')
, telebitdBinJs: path.join(telebitRoot, 'bin/telebitd.js') , telebitdJs: path.join(telebitRoot, 'bin/telebitd.js')
, telebitConfig: path.join(os.homedir(), '.config/telebit/telebit.yml') , telebitConfig: path.join(os.homedir(), '.config/telebit/telebit.yml')
, telebitdConfig: path.join(os.homedir(), '.config/telebit/telebitd.yml') , telebitdConfig: path.join(os.homedir(), '.config/telebit/telebitd.yml')
}; };
vars.telebitNpm = path.resolve(vars.telebitNode, '../npm');
vars.nodePath = path.resolve(vars.telebitNode, '../lib/node_modules');
vars.npmConfigPrefix = path.resolve(vars.telebitNode, '..');
if (-1 === vars.telebitRwDirs.indexOf(vars.npmConfigPrefix)) {
vars.telebitRwDirs.push(vars.npmConfigPrefix);
}
vars.telebitRwDirs = vars.telebitRwDirs.join(' ');
var launchers = { var launchers = {
'launchctl': function () { 'launchctl': function () {
var launcher = path.join(os.homedir(), 'Library/LaunchAgents/cloud.telebit.remote.plist'); var launcher = path.join(os.homedir(), 'Library/LaunchAgents/cloud.telebit.remote.plist');
try { try {
mkdirp.sync(path.join(os.homedir(), 'Library/LaunchAgents')); mkdirp.sync(path.join(os.homedir(), 'Library/LaunchAgents'));
mkdirp.sync(path.join(telebitRoot, 'bin')); mkdirp.sync(path.join(telebitRoot, 'bin'));
installLauncher({ installLauncher.sync({
file: { file: {
tpl: telebitBinTpl tpl: telebitBinTpl
, launcher: path.join(telebitRoot, 'bin/telebit') , launcher: path.join(telebitRoot, 'bin/telebit')
} }
, vars: vars , vars: vars
}, function (err) { });
installLauncher({
file: {
tpl: path.join(telebitRoot, 'usr/share/dist/etc/skel/Library/LaunchAgents/cloud.telebit.remote.plist.tpl')
, launcher: launcher
}
, vars: vars
});
var launcherstr = (userspace ? "" : "sudo ") + "launchctl ";
exec(launcherstr + "unload -w " + launcher, execOpts, function (err, stdout, stderr) {
if (err) { console.error(err); } if (err) { console.error(err); }
installLauncher({ console.log((stdout||'').trim());
file: { if (stderr) {
tpl: path.join(__dirname, 'dist/etc/skel/Library/LaunchAgents/cloud.telebit.remote.plist.tpl') console.error(stderr);
, launcher: launcher }
} console.log('unload worked?');
, vars: vars exec(launcherstr + "load -w " + launcher, execOpts, function (err, stdout, stderr) {
}, function (err) {
if (err) { console.error(err); } if (err) { console.error(err); }
var launcherstr = (userspace ? "sudo " : "") + "launchctl "; console.log((stdout||'').trim());
exec(launcherstr + "unload -w " + launcher, execOpts, function (err, stdout, stderr) { if (stderr) {
console.log(stdout); console.error(stderr);
if (stderr) { }
console.error(stderr); console.log('load worked?');
}
exec(launcherstr + "load -w " + launcher, execOpts, function (err, stdout, stderr) {
console.log(stdout);
if (stderr) {
console.error(stderr);
}
});
});
}); });
}); });
} catch(e) { } catch(e) {
console.error("'" + launcher + "' error:");
console.error(e);
} }
} }
, 'systemctl': function () { , 'systemctl': function () {
@ -84,26 +92,31 @@ module.exports.install = function (things) {
mkdirp.sync(path.join(os.homedir(), '.config/systemd/user')); mkdirp.sync(path.join(os.homedir(), '.config/systemd/user'));
installLauncher({ installLauncher({
file: { file: {
tpl: path.join(__dirname, 'dist/etc/skel/.config/systemd/user/telebit.service.tpl') tpl: path.join(telebitRoot, 'usr/share/dist/etc/skel/.config/systemd/user/telebit.service.tpl')
, launcher: launcher , launcher: launcher
} }
, vars: vars , vars: vars
}, function () { }, function () {
var launcherstr = "systemctl " + (userspace ? "--user " : ""); var launcherstr = (userspace ? "" : "sudo ") + "systemctl " + (userspace ? "--user " : "");
exec(launcherstr + "daemon-reload", execOpts, function (err, stdout, stderr) { exec(launcherstr + "daemon-reload", execOpts, function (err, stdout, stderr) {
console.log(stdout); if (err) { console.error(err); }
console.log((stdout||'').trim());
if (stderr) { console.error(stderr); } if (stderr) { console.error(stderr); }
exec(launcherstr + "enable " + launcher, execOpts, function (err, stdout, stderr) { exec(launcherstr + "enable " + launcher, execOpts, function (err, stdout, stderr) {
console.log(stdout); if (err) { console.error(err); }
console.log((stdout||'').trim());
if (stderr) { console.error(stderr); } if (stderr) { console.error(stderr); }
exec(launcherstr + "restart " + launcher, execOpts, function (err, stdout, stderr) { exec(launcherstr + "restart " + launcher, execOpts, function (err, stdout, stderr) {
console.log(stdout); if (err) { console.error(err); }
console.log((stdout||'').trim());
if (stderr) { console.error(stderr); } if (stderr) { console.error(stderr); }
}); });
}); });
}); });
}); });
} catch(e) { } catch(e) {
console.error("'" + launcher + "' error:");
console.error(e);
} }
} }
, 'reg.exe': function () { , 'reg.exe': function () {
@ -119,7 +132,7 @@ module.exports.install = function (things) {
+ '" /F' + '" /F'
; ;
exec(cmd, execOpts, function (err, stdout, stderr) { exec(cmd, execOpts, function (err, stdout, stderr) {
console.log(stdout); console.log((stdout||'').trim());
if (stderr) { if (stderr) {
console.error(stderr); console.error(stderr);
} }
@ -131,33 +144,37 @@ module.exports.install = function (things) {
function run(err, launcher) { function run(err, launcher) {
if (err) { if (err) {
console.error("No luck with '" + launcher + "'"); console.error("No luck with '" + launcher + "'");
console.error(err);
return; return;
} }
if (!launchers[launcher]) { if (launchers[launcher]) {
console.log('Launching with launcher ' + launcher);
launchers[launcher](); launchers[launcher]();
return; return;
} else {
console.error("No launcher handler for '" + launcher+ "'");
} }
} }
// could have used "command-exists" but I'm trying to stay low-dependency // could have used "command-exists" but I'm trying to stay low-dependency
// os.platform(), os.type() // os.platform(), os.type()
if (!/^win/i.test(os.platform())) { if (!/^win/i.test(os.platform())) {
if (!/^darwin/i.test(os.platform())) { if (/^darwin/i.test(os.platform())) {
exec('type -p launchctl', execOpts, function (err, stdout, stderr) { exec('type -p launchctl', execOpts, function (err, stdout, stderr) {
console.log(stdout); console.log((stdout||'').trim());
if (stderr) { if (stderr) {
console.error(stderr); console.error(stderr);
} }
run(err, 'launchctl'); run(err, 'launchctl');
}); });
} else { } else {
exec('type -p systemctlctl', execOpts, function (err, stdout, stderr) { exec('type -p systemctl', execOpts, function (err, stdout, stderr) {
console.log(stdout); console.log((stdout||'').trim());
if (stderr) { if (stderr) {
console.error(stderr); console.error(stderr);
} }
run(err, 'launchctl'); run(err, 'systemctl');
}); });
} }
} else { } else {
@ -170,7 +187,7 @@ module.exports.install = function (things) {
// utils.elevate // utils.elevate
// https://github.com/CatalystCode/windows-registry-node // https://github.com/CatalystCode/windows-registry-node
exec('where reg.exe', execOpts, function (err, stdout, stderr) { exec('where reg.exe', execOpts, function (err, stdout, stderr) {
console.log(stdout); console.log((stdout||'').trim());
if (stderr) { if (stderr) {
console.error(stderr); console.error(stderr);
} }
@ -178,3 +195,7 @@ module.exports.install = function (things) {
}); });
} }
}; };
if (module === require.main) {
module.exports.install({});
}

View File

@ -300,6 +300,8 @@ export TELEBIT_BIN=$TELEBIT_REAL_PATH/bin/telebit
export TELEBITD_BIN=$TELEBIT_REAL_PATH/bin/telebitd export TELEBITD_BIN=$TELEBIT_REAL_PATH/bin/telebitd
export TELEBIT_JS=$TELEBIT_REAL_PATH/bin/telebit.js export TELEBIT_JS=$TELEBIT_REAL_PATH/bin/telebit.js
export TELEBITD_JS=$TELEBIT_REAL_PATH/bin/telebitd.js export TELEBITD_JS=$TELEBIT_REAL_PATH/bin/telebitd.js
export NODE_PATH="$TELEBIT_REAL_PATH/lib/node_modules"
export NPM_CONFIG_PREFIX="$TELEBIT_REAL_PATH"
$my_node $TELEBIT_TMP/usr/share/template-launcher.js $my_node $TELEBIT_TMP/usr/share/template-launcher.js

View File

@ -20,6 +20,8 @@ module.exports.sync = function (opts) {
var text = fs.readFileSync(f.tpl, 'utf8') var text = fs.readFileSync(f.tpl, 'utf8')
.replace(/{TELEBIT_PATH}/g, vars.telebitPath || '{TELEBIT_PATH}') .replace(/{TELEBIT_PATH}/g, vars.telebitPath || '{TELEBIT_PATH}')
.replace(/{TELEBIT_NODE}/g, vars.telebitNode || '{TELEBIT_NODE}') .replace(/{TELEBIT_NODE}/g, vars.telebitNode || '{TELEBIT_NODE}')
.replace(/{NODE_PATH}/g, vars.nodePath || '{NODE_PATH}')
.replace(/{NPM_CONFIG_PREFIX}/g, vars.npmConfigPrefix || '{NPM_CONFIG_PREFIX}')
.replace(/{TELEBIT_NPM}/g, vars.telebitNpm || '{TELEBIT_NPM}') .replace(/{TELEBIT_NPM}/g, vars.telebitNpm || '{TELEBIT_NPM}')
.replace(/{TELEBIT_BIN}/g, vars.telebitBin || '{TELEBIT_BIN}') .replace(/{TELEBIT_BIN}/g, vars.telebitBin || '{TELEBIT_BIN}')
.replace(/{TELEBITD_BIN}/g, vars.telebitdBin || '{TELEBITD_BIN}') .replace(/{TELEBITD_BIN}/g, vars.telebitdBin || '{TELEBITD_BIN}')
@ -54,8 +56,7 @@ function run() {
var telebitRoot = path.resolve(__dirname, '../..'); var telebitRoot = path.resolve(__dirname, '../..');
var vars = { var vars = {
telebitPath: process.env.TELEBIT_PATH || telebitRoot telebitPath: process.env.TELEBIT_PATH || telebitRoot
, telebitNode: process.env.TELEBIT_NODE || path.resolve(telebitRoot, 'bin/node') , telebitNode: process.env.TELEBIT_NODE || process.argv[0] || path.resolve(telebitRoot, 'bin/node')
, telebitNpm: process.env.TELEBIT_NPM || path.resolve(telebitRoot, 'bin/npm')
, telebitBin: process.env.TELEBIT_BIN || path.resolve(telebitRoot, 'bin/telebit') , telebitBin: process.env.TELEBIT_BIN || path.resolve(telebitRoot, 'bin/telebit')
, telebitdBin: process.env.TELEBITD_BIN || path.resolve(telebitRoot, 'bin/telebitd') , telebitdBin: process.env.TELEBITD_BIN || path.resolve(telebitRoot, 'bin/telebitd')
, telebitJs: process.env.TELEBIT_JS || path.resolve(telebitRoot, 'bin/telebit.js') , telebitJs: process.env.TELEBIT_JS || path.resolve(telebitRoot, 'bin/telebit.js')
@ -64,12 +65,19 @@ function run() {
(process.env.TELEBIT_PATH || path.resolve(__dirname, '../..')) (process.env.TELEBIT_PATH || path.resolve(__dirname, '../..'))
, path.join(os.homedir(), '.config/telebit') , path.join(os.homedir(), '.config/telebit')
, path.join(os.homedir(), '.local/share/telebit') , path.join(os.homedir(), '.local/share/telebit')
].join(' ') ]
, telebitUser: process.env.TELEBIT_USER || os.userInfo().username , telebitUser: process.env.TELEBIT_USER || os.userInfo().username
, telebitGroup: process.env.TELEBIT_GROUP || ('darwin' === os.platform() ? 'staff' : os.userInfo().username) , telebitGroup: process.env.TELEBIT_GROUP || ('darwin' === os.platform() ? 'staff' : os.userInfo().username)
, telebitConfig: process.env.TELEBIT_CONFIG || path.join(os.homedir(), '.config/telebit/telebit.yml') , telebitConfig: process.env.TELEBIT_CONFIG || path.join(os.homedir(), '.config/telebit/telebit.yml')
, telebitdConfig: process.env.TELEBITD_CONFIG || path.join(os.homedir(), '.config/telebit/telebitd.yml') , telebitdConfig: process.env.TELEBITD_CONFIG || path.join(os.homedir(), '.config/telebit/telebitd.yml')
}; };
vars.telebitNpm = process.env.TELEBIT_NPM || path.resolve(vars.telebitNode, '../npm');
vars.nodePath = process.env.NODE_PATH || path.resolve(vars.telebitNode, '../lib/node_modules');
vars.npmConfigPrefix = process.env.NPM_CONFIG_PREFIX || path.resolve(vars.telebitNode, '..');
if (-1 === vars.telebitRwDirs.indexOf(vars.npmConfigPrefix)) {
vars.telebitRwDirs.push(vars.npmConfigPrefix);
}
vars.telebitRwDirs = vars.telebitRwDirs.join(' ');
module.exports({ module.exports({
file: f file: f
, vars: vars , vars: vars