WIP make installable from npm

This commit is contained in:
AJ ONeal 2018-06-27 03:21:44 -06:00
parent e8b3ae59c8
commit d85682ff7f
8 changed files with 306 additions and 77 deletions

View File

@ -6,11 +6,11 @@
<string>Telebit Remote</string>
<key>ProgramArguments</key>
<array>
<string>{TELEBIT_PATH}/bin/node</string>
<string>{TELEBIT_PATH}/bin/telebitd.js</string>
<string>{TELEBIT_NODE}</string>
<string>{TELEBITD_JS}</string>
<string>daemon</string>
<string>--config</string>
<string>{TELEBIT_PATH}/etc/telebitd.yml</string>
<string>{TELEBITD_CONFIG}</string>
</array>
<key>EnvironmentVariables</key>
<dict>

2
usr/share/dist/bin/telebit.tpl vendored Normal file
View File

@ -0,0 +1,2 @@
#!/bin/bash
{TELEBIT_NODE} {TELEBIT_JS} "$@"

2
usr/share/dist/bin/telebitd.tpl vendored Normal file
View File

@ -0,0 +1,2 @@
#!/bin/bash
{TELEBIT_NODE} {TELEBITD_JS} daemon "$@"

View File

@ -7,11 +7,11 @@
<key>ProgramArguments</key>
<array>
<string>{TELEBIT_PATH}/bin/node</string>
<string>{TELEBIT_PATH}/bin/telebitd.js</string>
<string>{TELEBIT_NODE}</string>
<string>{TELEBITD_JS}</string>
<string>daemon</string>
<string>--config</string>
<string>{TELEBIT_CONFIGD}</string>
<string>{TELEBITD_CONFIG}</string>
</array>
<key>EnvironmentVariables</key>

View File

@ -0,0 +1,180 @@
'use strict';
//var fs = require('fs');
var os = require('os');
var mkdirp = require('mkdirp');
var exec = require('child_process').exec;
var path = require('path');
module.exports.install = function (things) {
things = things || {};
// in some future version we can take this file out
// and accept process.env from things
var installLauncher = require('./install-launcher');
// Right now this is just for npm install -g and npx
if (things.env) {
things.env.PATH = things.env.PATH || process.env.PATH;
}
var execOpts = { windowsHide: true, env: things.env || process.env };
var userspace = (!things.telebitUser || (things.telebitUser === os.userInfo().username)) ? true : false;
var telebitRoot = path.join(__dirname, '../..');
var telebitBinTpl = path.join(telebitRoot, 'dist/bin/telebit.tpl');
var vars = {
telebitPath: telebitRoot
, telebitUser: os.userInfo().username
, telebitGroup: (/^darwin/i.test(os.platform()) ? 'staff' : os.userInfo().username)
, telebitRwDirs: [
path.resolve(__dirname, '../..')
, path.join(os.homedir(), '.config/telebit')
, path.join(os.homedir(), '.local/share/telebit')
].join(' ')
, telebitNode: path.join(telebitRoot, 'bin/node')
, telebitNpm: path.join(telebitRoot, 'bin/npm')
, telebitBin: path.join(telebitRoot, 'bin/telebit')
, telebitdBin: path.join(telebitRoot, 'bin/telebitd')
, telebitBinJs: path.join(telebitRoot, 'bin/telebit.js')
, telebitdBinJs: path.join(telebitRoot, 'bin/telebitd.js')
, telebitConfig: path.join(os.homedir(), '.config/telebit/telebit.yml')
, telebitdConfig: path.join(os.homedir(), '.config/telebit/telebitd.yml')
};
var launchers = {
'launchctl': function () {
var launcher = path.join(os.homedir(), 'Library/LaunchAgents/cloud.telebit.remote.plist');
try {
mkdirp.sync(path.join(os.homedir(), 'Library/LaunchAgents'));
mkdirp.sync(path.join(telebitRoot, 'bin'));
installLauncher({
file: {
tpl: telebitBinTpl
, launcher: path.join(telebitRoot, 'bin/telebit')
}
, vars: vars
}, function (err) {
if (err) { console.error(err); }
installLauncher({
file: {
tpl: path.join(__dirname, 'dist/etc/skel/Library/LaunchAgents/cloud.telebit.remote.plist.tpl')
, launcher: launcher
}
, vars: vars
}, function (err) {
if (err) { console.error(err); }
var launcherstr = (userspace ? "sudo " : "") + "launchctl ";
exec(launcherstr + "unload -w " + launcher, execOpts, function (err, stdout, stderr) {
console.log(stdout);
if (stderr) {
console.error(stderr);
}
exec(launcherstr + "load -w " + launcher, execOpts, function (err, stdout, stderr) {
console.log(stdout);
if (stderr) {
console.error(stderr);
}
});
});
});
});
} catch(e) {
}
}
, 'systemctl': function () {
var launcher = path.join(os.homedir(), '.config/systemd/user/telebit.service');
try {
mkdirp.sync(path.join(os.homedir(), '.config/systemd/user'));
installLauncher({
file: {
tpl: path.join(__dirname, 'dist/etc/skel/.config/systemd/user/telebit.service.tpl')
, launcher: launcher
}
, vars: vars
}, function () {
var launcherstr = "systemctl " + (userspace ? "--user " : "");
exec(launcherstr + "daemon-reload", execOpts, function (err, stdout, stderr) {
console.log(stdout);
if (stderr) { console.error(stderr); }
exec(launcherstr + "enable " + launcher, execOpts, function (err, stdout, stderr) {
console.log(stdout);
if (stderr) { console.error(stderr); }
exec(launcherstr + "restart " + launcher, execOpts, function (err, stdout, stderr) {
console.log(stdout);
if (stderr) { console.error(stderr); }
});
});
});
});
} catch(e) {
}
}
, 'reg.exe': function () {
vars.telebitNode += '.exe';
var cmd = 'reg.exe add "HKCU\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"'
+ ' /V "Telebit" /t REG_SZ /D '
+ '"' + process.argv[0] + ' /c ' // something like C:\\Program Files (x64)\nodejs\node.exe
+ [ path.join(__dirname, 'bin/telebitd.js')
, 'daemon'
, '--config'
, path.join(os.homedir(), '.config/telebit/telebitd.yml')
].join(' ')
+ '" /F'
;
exec(cmd, execOpts, function (err, stdout, stderr) {
console.log(stdout);
if (stderr) {
console.error(stderr);
}
run(err, 'launchctl');
});
}
};
function run(err, launcher) {
if (err) {
console.error("No luck with '" + launcher + "'");
return;
}
if (!launchers[launcher]) {
launchers[launcher]();
return;
}
}
// 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())) {
exec('type -p launchctl', execOpts, function (err, stdout, stderr) {
console.log(stdout);
if (stderr) {
console.error(stderr);
}
run(err, 'launchctl');
});
} else {
exec('type -p systemctlctl', execOpts, function (err, stdout, stderr) {
console.log(stdout);
if (stderr) {
console.error(stderr);
}
run(err, 'launchctl');
});
}
} 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
exec('where reg.exe', execOpts, function (err, stdout, stderr) {
console.log(stdout);
if (stderr) {
console.error(stderr);
}
run(err, 'reg.exe');
});
}
};

View File

@ -47,11 +47,9 @@ my_secret=${4:-}
cur_user="$(id -u -n)"
TELEBIT_USER="${TELEBIT_USER:-$cur_user}"
my_user="$TELEBIT_USER"
cur_group="$(id -g -n)"
TELEBIT_GROUP="${TELEBIT_GROUP:-$cur_group}"
my_group="$TELEBIT_GROUP"
my_app_pkg_name="cloud.telebit.remote"
my_app="telebit"
@ -253,53 +251,61 @@ set +e
# TODO do stuff for groups too
# TODO add ending $
if type -p dscl >/dev/null 2>/dev/null; then
if [ -n "$(dscl . list /users | grep ^$my_user)" ] && [ -n "$(dscl . list /groups | grep ^$my_group)" ]; then
if [ -n "$(dscl . list /users | grep ^$TELEBIT_USER)" ] && [ -n "$(dscl . list /groups | grep ^$TELEBIT_GROUP)" ]; then
my_skip="yes"
fi
elif [ -n "$(cat $my_root/etc/passwd | grep $my_user)" ] && [ -n "$(cat $my_root/etc/group | grep $my_group)" ]; then
elif [ -n "$(cat $my_root/etc/passwd | grep $TELEBIT_USER)" ] && [ -n "$(cat $my_root/etc/group | grep $TELEBIT_GROUP)" ]; then
my_skip="yes"
fi
if [ -z "$my_skip" ]; then
if type -p adduser >/dev/null 2>/dev/null; then
$real_sudo_cmd adduser --home $TELEBIT_REAL_PATH --gecos '' --disabled-password $my_user >/dev/null 2>&1
#my_user=$my_app_name
my_group=$my_user
$real_sudo_cmd adduser --home $TELEBIT_REAL_PATH --gecos '' --disabled-password $TELEBIT_USER >/dev/null 2>&1
#TELEBIT_USER=$my_app_name
TELEBIT_GROUP=$TELEBIT_USER
elif [ -n "$(cat /etc/passwd | grep www-data:)" ]; then
# Linux (Ubuntu)
my_user=www-data
my_group=www-data
TELEBIT_USER=www-data
TELEBIT_GROUP=www-data
elif [ -n "$(cat /etc/passwd | grep _www:)" ]; then
# Mac
my_user=_www
my_group=_www
TELEBIT_USER=_www
TELEBIT_GROUP=_www
else
# Unsure
my_user=$(id -u -n) # $(whoami)
my_group=$(id -g -n)
TELEBIT_USER=$(id -u -n) # $(whoami)
TELEBIT_GROUP=$(id -g -n)
fi
fi
set -e
export TELEBIT_USER=$my_user
export TELEBIT_GROUP=$my_group
export TELEBIT_USER
export TELEBIT_GROUP
export TELEBIT_PATH
TELEBIT_CONFIG=$HOME/.config/$my_app/$my_app.yml
# TODO check both expected sock paths in client by default
if [ "yes" == "$TELEBIT_USERSPACE" ]; then
TELEBIT_TMP_CONFIGD=$HOME/.config/$my_app/$my_daemon.yml
TELEBIT_CONFIGD=$HOME/.config/$my_app/$my_daemon.yml
TELEBITD_CONFIG=$HOME/.config/$my_app/$my_daemon.yml
TELEBIT_SOCK=$HOME/.local/share/$my_app/var/run/$my_app.sock
else
TELEBIT_TMP_CONFIGD=$TELEBIT_TMP/etc/$my_daemon.yml
TELEBIT_CONFIGD=$TELEBIT_REAL_PATH/etc/$my_daemon.yml
TELEBITD_CONFIG=$TELEBIT_REAL_PATH/etc/$my_daemon.yml
TELEBIT_SOCK=$TELEBIT_REAL_PATH/var/run/$my_app.sock
fi
export TELEBIT_SOCK
export TELEBIT_NODE=$TELEBIT_REAL_PATH/bin/node
export TELEBIT_NPM=$TELEBIT_REAL_PATH/bin/npm
export TELEBIT_BIN=$TELEBIT_REAL_PATH/bin/telebit
export TELEBITD_BIN=$TELEBIT_REAL_PATH/bin/telebitd
export TELEBIT_JS=$TELEBIT_REAL_PATH/bin/telebit.js
export TELEBITD_JS=$TELEBIT_REAL_PATH/bin/telebitd.js
$my_node $TELEBIT_TMP/usr/share/template-launcher.js
# TODO don't create this in TMP_PATH if it exists in TELEBIT_REAL_PATH
mkdir -p "$(dirname $TELEBIT_TMP_CONFIGD)"
if [ ! -e "$TELEBIT_CONFIGD" ]; then
if [ ! -e "$TELEBITD_CONFIG" ]; then
echo "sock: $TELEBIT_SOCK" >> "$TELEBIT_TMP_CONFIGD"
echo "root: $TELEBIT_REAL_PATH" >> "$TELEBIT_TMP_CONFIGD"
@ -323,8 +329,8 @@ fi
# rewrite system service file with real variables
# This should only affect non-USERSPACE installs
#echo "${soft_sudo_cmde}chown -R $my_user '$TELEBIT_REAL_PATH'
$soft_sudo_cmd chown -R $my_user "$TELEBIT_REAL_PATH"
#echo "${soft_sudo_cmde}chown -R $TELEBIT_USER '$TELEBIT_REAL_PATH'
$soft_sudo_cmd chown -R $TELEBIT_USER "$TELEBIT_REAL_PATH"
# $HOME/.config/systemd/user/
# %h/.config/telebit/telebit.yml
@ -427,7 +433,7 @@ else
echo "Run the service manually (we couldn't detect your system service to do that automatically):"
echo ""
echo " $my_daemon --config $TELEBIT_CONFIGD"
echo " $my_daemon --config $TELEBITD_CONFIG"
echo " $my_app --config $TELEBIT_CONFIG"
fi

View File

@ -11,28 +11,28 @@ if [ "systemd" == "$my_system_launcher" ]; then
my_stopper="${real_sudo_cmde}systemctl stop $my_app"
echo "Edit the config and restart, if desired:"
echo ""
echo " ${real_sudo_cmde}$my_edit $TELEBIT_CONFIGD"
echo " ${real_sudo_cmde}$my_edit $TELEBITD_CONFIG"
echo " ${real_sudo_cmde}systemctl restart $my_app"
echo ""
echo "Or disabled the service and start manually:"
echo ""
echo " ${real_sudo_cmde}systemctl stop $my_app"
echo " ${real_sudo_cmde}systemctl disable $my_app"
echo " $my_daemon --config $TELEBIT_CONFIGD"
echo " $my_daemon --config $TELEBITD_CONFIG"
elif [ "launchd" == "$my_system_launcher" ]; then
my_stopper="${real_sudo_cmde}launchctl unload $my_app_launchd_service"
echo "Edit the config and restart, if desired:"
echo ""
echo " ${real_sudo_cmde}$my_edit $TELEBIT_CONFIGD"
echo " ${real_sudo_cmde}$my_edit $TELEBITD_CONFIG"
echo " ${real_sudo_cmde}launchctl unload $my_app_launchd_service"
echo " ${real_sudo_cmde}launchctl load -w $my_app_launchd_service"
echo ""
echo "Or disabled the service and start manually:"
echo ""
echo " ${real_sudo_cmde}launchctl unload -w $my_app_launchd_service"
echo " $my_daemon --config $TELEBIT_CONFIGD"
echo " $my_daemon --config $TELEBITD_CONFIG"
else
@ -40,7 +40,7 @@ else
echo ""
echo "Run the service manually (we couldn't detect your system service to do that automatically):"
echo ""
echo " $my_daemon --config $TELEBIT_CONFIGD"
echo " $my_daemon --config $TELEBITD_CONFIG"
echo " $my_app --config $TELEBIT_CONFIG"
fi

View File

@ -4,40 +4,79 @@ var path = require('path');
var fs = require('fs');
var os = require('os');
module.exports = function (opts, fn) {
// TODO make async version
try {
module.exports.sync(opts);
} catch(e) {
if (fn) { fn(e); }
}
if (fn) { fn(null); }
};
module.exports.sync = function (opts) {
var f = opts.file;
var vars = opts.vars;
var text = fs.readFileSync(f.tpl, 'utf8')
.replace(/{TELEBIT_PATH}/g, vars.telebitPath || '{TELEBIT_PATH}')
.replace(/{TELEBIT_NODE}/g, vars.telebitNode || '{TELEBIT_NODE}')
.replace(/{TELEBIT_NPM}/g, vars.telebitNpm || '{TELEBIT_NPM}')
.replace(/{TELEBIT_BIN}/g, vars.telebitBin || '{TELEBIT_BIN}')
.replace(/{TELEBITD_BIN}/g, vars.telebitdBin || '{TELEBITD_BIN}')
.replace(/{TELEBIT_JS}/g, vars.telebitJs || '{TELEBIT_JS}')
.replace(/{TELEBITD_JS}/g, vars.telebitdJs || '{TELEBITD_JS}')
.replace(/{TELEBIT_USER}/g, vars.telebitUser || '{TELEBIT_USER}')
.replace(/{TELEBIT_GROUP}/g, vars.telebitGroup || '{TELEBIT_GROUP}')
.replace(/{TELEBIT_RW_DIRS}/g, vars.telebitRwDirs || '{TELEBIT_RW_DIRS}')
.replace(/{TELEBIT_CONFIG}/g, vars.telebitConfig || '{TELEBIT_CONFIG}')
.replace(/{TELEBITD_CONFIG}/g, vars.telebitdConfig || '{TELEBITD_CONFIG}')
;
fs.writeFileSync(f.launcher, text, 'utf8');
};
function run() {
var files = [
[ (process.env.TELEBIT_SERVICE_TPL || path.join(__dirname, 'dist/etc/systemd/system/telebit.service.tpl'))
, (process.env.TELEBIT_SERVICE || path.join(__dirname, 'dist/etc/systemd/system/telebit.service'))
]
, [ (process.env.TELEBIT_USER_SERVICE_TPL || path.join(__dirname, 'dist/etc/skel/.config/systemd/user/telebit.service.tpl'))
, (process.env.TELEBIT_USER_SERVICE || path.join(__dirname, 'dist/etc/skel/.config/systemd/user/telebit.service'))
]
, [ (process.env.TELEBIT_PLIST_TPL || path.join(__dirname, 'dist/Library/LaunchDaemons/cloud.telebit.remote.plist.tpl'))
,(process.env.TELEBIT_PLIST || path.join(__dirname, 'dist/Library/LaunchDaemons/cloud.telebit.remote.plist'))
]
, [ (process.env.TELEBIT_USER_PLIST_TPL || path.join(__dirname, 'dist/etc/skel/Library/LaunchAgents/cloud.telebit.remote.plist.tpl'))
,(process.env.TELEBIT_USER_PLIST || path.join(__dirname, 'dist/etc/skel/Library/LaunchAgents/cloud.telebit.remote.plist'))
]
{ tpl: (process.env.TELEBIT_SERVICE_TPL || path.join(__dirname, 'dist/etc/systemd/system/telebit.service.tpl'))
, launcher: (process.env.TELEBIT_SERVICE || path.join(__dirname, 'dist/etc/systemd/system/telebit.service'))
}
, { tpl: (process.env.TELEBIT_USER_SERVICE_TPL || path.join(__dirname, 'dist/etc/skel/.config/systemd/user/telebit.service.tpl'))
, launcher: (process.env.TELEBIT_USER_SERVICE || path.join(__dirname, 'dist/etc/skel/.config/systemd/user/telebit.service'))
}
, { tpl: (process.env.TELEBIT_PLIST_TPL || path.join(__dirname, 'dist/Library/LaunchDaemons/cloud.telebit.remote.plist.tpl'))
, launcher: (process.env.TELEBIT_PLIST || path.join(__dirname, 'dist/Library/LaunchDaemons/cloud.telebit.remote.plist'))
}
, { tpl: (process.env.TELEBIT_USER_PLIST_TPL || path.join(__dirname, 'dist/etc/skel/Library/LaunchAgents/cloud.telebit.remote.plist.tpl'))
, launcher: (process.env.TELEBIT_USER_PLIST || path.join(__dirname, 'dist/etc/skel/Library/LaunchAgents/cloud.telebit.remote.plist'))
}
];
files.forEach(function (f) {
var telebitRoot = path.resolve(__dirname, '../..');
var vars = {
telebitPath: process.env.TELEBIT_PATH || path.resolve(__dirname, '../..')
telebitPath: process.env.TELEBIT_PATH || telebitRoot
, telebitNode: process.env.TELEBIT_NODE || 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')
, telebitdBin: process.env.TELEBITD_BIN || path.resolve(telebitRoot, 'bin/telebitd')
, telebitJs: process.env.TELEBIT_JS || path.resolve(telebitRoot, 'bin/telebit.js')
, telebitdJs: process.env.TELEBITD_JS || path.resolve(telebitRoot, 'bin/telebitd.js')
, telebitRwDirs: [
(process.env.TELEBIT_PATH || path.resolve(__dirname, '../..'))
, path.join(os.homedir(), '.config/telebit')
, path.join(os.homedir(), '.local/share/telebit')
, ].join(' ')
].join(' ')
, telebitUser: process.env.TELEBIT_USER || 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')
, telebitConfigd: process.env.TELEBIT_CONFIGD || path.join(os.homedir(), '.config/telebit/telebitd.yml')
, telebitdConfig: process.env.TELEBITD_CONFIG || path.join(os.homedir(), '.config/telebit/telebitd.yml')
};
files.forEach(function (f) {
var text = fs.readFileSync(f[0], 'utf8')
.replace(/{TELEBIT_PATH}/g, vars.telebitPath)
.replace(/{TELEBIT_USER}/g, vars.telebitUser)
.replace(/{TELEBIT_GROUP}/g, vars.telebitGroup)
.replace(/{TELEBIT_RW_DIRS}/g, vars.telebitRwDirs)
.replace(/{TELEBIT_CONFIG}/g, vars.telebitConfig)
.replace(/{TELEBIT_CONFIGD}/g, vars.telebitConfigd)
;
fs.writeFileSync(f[1], text, 'utf8');
module.exports({
file: f
, vars: vars
});
});
}
if (module === require.main) {
run();
}