2018-06-01 19:14:59 +00:00
|
|
|
#!/bin/bash
|
|
|
|
#<pre><code>
|
|
|
|
|
2018-06-02 08:25:41 +00:00
|
|
|
# What does this do.. and why?
|
|
|
|
# (and why is it so complicated?)
|
|
|
|
#
|
|
|
|
# What this does
|
|
|
|
#
|
|
|
|
# 1. Sets some vars and asks some questions
|
|
|
|
# 2. Installs everything into a single place
|
|
|
|
# (inculding deps like node.js, with the correct version)
|
|
|
|
# 3. Depending on OS, creates a user for the service
|
|
|
|
# 4. Depending on OS, register with system launcher
|
|
|
|
#
|
|
|
|
# Why
|
|
|
|
#
|
|
|
|
# So that you can get a fully configured, running product,
|
|
|
|
# with zero manual configuration in a matter of seconds -
|
|
|
|
# and have an uninstall that's just as easy.
|
|
|
|
#
|
|
|
|
# Why so complicated?
|
|
|
|
#
|
|
|
|
# To support nuance differences between various versions of
|
|
|
|
# Linux, macOS, and Android, including whether it's being
|
|
|
|
# installed with user privileges, as root, wit a system user
|
2018-06-02 08:58:42 +00:00
|
|
|
# system daemon launcher, etc. Also, this is designed to be
|
|
|
|
# reusable with many apps and services, so it's very variabled...
|
2018-06-01 19:14:59 +00:00
|
|
|
|
|
|
|
set -e
|
|
|
|
set -u
|
|
|
|
|
|
|
|
### http_bash exported by get.sh
|
|
|
|
|
2018-06-28 20:47:32 +00:00
|
|
|
TELEBIT_DEBUG=${TELEBIT_DEBUG:-}
|
|
|
|
|
2018-07-26 22:35:04 +00:00
|
|
|
# NOTE: On OS X logname works from a pipe, but on Linux it does not
|
2018-07-27 07:10:02 +00:00
|
|
|
my_logname=$(who am i </dev/tty | awk '{print $1}')
|
2018-07-26 22:46:57 +00:00
|
|
|
#my_logname=${my_logname:-$(logname)}
|
|
|
|
#my_logname=${my_logname:-$SUDO_USER}
|
|
|
|
if [ -n "$my_logname" ] && [ "$my_logname" != "$(id -u -n)" ]; then
|
2018-06-26 06:26:58 +00:00
|
|
|
echo "WARNING:"
|
|
|
|
echo " You are logged in as '$(logname)' but acting as '$(id -u -n)'."
|
|
|
|
echo " If the installation is not successful please log in as '$(id -u -n)' directly."
|
|
|
|
sleep 3
|
|
|
|
fi
|
|
|
|
|
2018-07-08 01:06:17 +00:00
|
|
|
if [ -n "${TELEBIT_DEBUG:-}" ]; then
|
|
|
|
echo 'TELEBIT_DEBUG='${TELEBIT_DEBUG}
|
|
|
|
fi
|
|
|
|
if [ -n "${TELEBIT_PATH:-}" ]; then
|
|
|
|
echo 'TELEBIT_PATH='${TELEBIT_PATH}
|
|
|
|
fi
|
|
|
|
if [ -n "${TELEBIT_USERSPACE:-}" ]; then
|
|
|
|
echo 'TELEBIT_USERSPACE='${TELEBIT_USERSPACE}
|
|
|
|
fi
|
|
|
|
if [ -n "${TELEBIT_USER:-}" ]; then
|
|
|
|
echo 'TELEBIT_USER='${TELEBIT_USER}
|
|
|
|
fi
|
|
|
|
if [ -n "${TELEBIT_GROUP:-}" ]; then
|
|
|
|
echo 'TELEBIT_GROUP='${TELEBIT_GROUP}
|
|
|
|
fi
|
2018-06-11 18:56:16 +00:00
|
|
|
TELEBIT_VERSION=${TELEBIT_VERSION:-master}
|
2018-06-24 08:36:09 +00:00
|
|
|
TELEBIT_USERSPACE=${TELEBIT_USERSPACE:-no}
|
2018-06-01 19:14:59 +00:00
|
|
|
my_email=${1:-}
|
|
|
|
my_relay=${2:-}
|
|
|
|
my_servernames=${3:-}
|
|
|
|
my_secret=${4:-}
|
2018-06-24 08:36:09 +00:00
|
|
|
|
|
|
|
cur_user="$(id -u -n)"
|
|
|
|
TELEBIT_USER="${TELEBIT_USER:-$cur_user}"
|
|
|
|
|
|
|
|
cur_group="$(id -g -n)"
|
|
|
|
TELEBIT_GROUP="${TELEBIT_GROUP:-$cur_group}"
|
|
|
|
|
2018-06-02 09:57:38 +00:00
|
|
|
my_app_pkg_name="cloud.telebit.remote"
|
2018-06-01 19:14:59 +00:00
|
|
|
my_app="telebit"
|
2018-06-11 18:56:16 +00:00
|
|
|
my_daemon="telebitd"
|
2018-06-01 19:14:59 +00:00
|
|
|
my_bin="telebit.js"
|
|
|
|
my_name="Telebit Remote"
|
|
|
|
my_repo="telebit.js"
|
2018-06-02 08:25:41 +00:00
|
|
|
my_root=${my_root:-} # todo better install script
|
2018-06-23 23:38:57 +00:00
|
|
|
soft_sudo_cmd="sudo"
|
|
|
|
soft_sudo_cmde="sudo "
|
2018-06-05 05:23:59 +00:00
|
|
|
exec 3<>/dev/tty
|
|
|
|
read_cmd="read -u 3"
|
2018-06-02 09:57:38 +00:00
|
|
|
# TODO detect if rsync is available and use rsync -a (more portable)
|
|
|
|
rsync_cmd="cp -pPR"
|
2018-06-01 19:14:59 +00:00
|
|
|
|
2018-06-05 06:46:06 +00:00
|
|
|
set +e
|
2018-06-05 07:01:31 +00:00
|
|
|
my_edit=$(basename "${EDITOR:-}")
|
2018-06-05 06:46:06 +00:00
|
|
|
if [ -z "$my_edit" ]; then
|
2018-06-05 07:01:31 +00:00
|
|
|
my_edit=$(basename "$(type -p edit)")
|
2018-06-05 06:46:06 +00:00
|
|
|
fi
|
|
|
|
if [ -z "$my_edit" ]; then
|
2018-06-05 07:01:31 +00:00
|
|
|
my_edit=$(basename "$(type -p nano)")
|
2018-06-05 06:46:06 +00:00
|
|
|
fi
|
|
|
|
if [ -z "$my_edit" ]; then
|
2018-06-05 07:01:31 +00:00
|
|
|
my_edit=$(basename "$(type -p vim)")
|
|
|
|
fi
|
|
|
|
if [ -z "$my_edit" ]; then
|
|
|
|
my_edit=$(basename "$(type -p vi)")
|
2018-06-05 06:46:06 +00:00
|
|
|
fi
|
2018-06-05 07:02:43 +00:00
|
|
|
if [ -z "$my_edit" ]; then
|
|
|
|
my_edit="nano"
|
|
|
|
fi
|
2018-06-05 06:46:06 +00:00
|
|
|
set -e
|
|
|
|
|
2018-06-05 06:09:24 +00:00
|
|
|
if [ "root" == $(whoami) ] || [ 0 == $(id -u) ]; then
|
2018-06-23 23:38:57 +00:00
|
|
|
soft_sudo_cmd=" "
|
|
|
|
soft_sudo_cmde=""
|
2018-06-05 05:00:09 +00:00
|
|
|
fi
|
|
|
|
|
2018-06-01 19:14:59 +00:00
|
|
|
echo ""
|
|
|
|
|
2018-06-23 23:39:58 +00:00
|
|
|
TELEBIT_REAL_PATH=${TELEBIT_PATH:-}
|
2018-06-24 08:36:09 +00:00
|
|
|
|
|
|
|
if [ $(id -u) -ne 0 ] && [ "$TELEBIT_USER" == "$cur_user" ]; then
|
|
|
|
TELEBIT_USERSPACE="yes"
|
|
|
|
if [ -z "${TELEBIT_REAL_PATH:-}" ]; then
|
|
|
|
TELEBIT_REAL_PATH=$HOME/Applications/$my_app
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
TELEBIT_USERSPACE="no"
|
|
|
|
if [ -z "${TELEBIT_REAL_PATH:-}" ]; then
|
|
|
|
TELEBIT_REAL_PATH=/opt/$my_app
|
|
|
|
fi
|
2018-06-01 19:14:59 +00:00
|
|
|
fi
|
2018-06-24 08:36:09 +00:00
|
|
|
TELEBIT_PATH="$TELEBIT_REAL_PATH"
|
|
|
|
TELEBIT_TMP="$TELEBIT_REAL_PATH"
|
2018-06-28 19:15:06 +00:00
|
|
|
# this works slightly differently between bsd (macOS) and gnu mktemp
|
|
|
|
# bsd requires the Xes for templates while GNU uses them literally
|
|
|
|
my_tmp="$(mktemp -d -t telebit.XXXXXXXX)"
|
2018-06-24 08:36:09 +00:00
|
|
|
#TELEBIT_TMP="$my_tmp/telebit"
|
2018-06-01 19:14:59 +00:00
|
|
|
|
2018-07-08 01:06:17 +00:00
|
|
|
echo "Installing $my_name to '$TELEBIT_REAL_PATH'"
|
2019-05-12 00:22:20 +00:00
|
|
|
# v10.2+ has much needed networking fixes, but breaks ursa.
|
|
|
|
# v9.x has severe networking bugs.
|
|
|
|
# v8.x has working ursa, but requires tls workarounds"
|
|
|
|
# v10.13 seems to work for me locally (new greenlock)
|
|
|
|
NODEJS_VER="${NODEJS_VER:-v10.13}"
|
2018-06-01 19:14:59 +00:00
|
|
|
export NODEJS_VER
|
2018-06-23 23:38:57 +00:00
|
|
|
export NODE_PATH="$TELEBIT_TMP/lib/node_modules"
|
|
|
|
export NPM_CONFIG_PREFIX="$TELEBIT_TMP"
|
|
|
|
# this comes last for security
|
|
|
|
export PATH="$PATH:$TELEBIT_REAL_PATH/bin"
|
2018-06-07 16:43:05 +00:00
|
|
|
sleep 0.25
|
2018-06-23 23:38:57 +00:00
|
|
|
real_sudo_cmd=$soft_sudo_cmd
|
|
|
|
real_sudo_cmde=$soft_sudo_cmde
|
|
|
|
|
|
|
|
set +e
|
2018-06-24 05:19:11 +00:00
|
|
|
mkdir -p $my_tmp "$TELEBIT_REAL_PATH" "$TELEBIT_REAL_PATH/etc" "$TELEBIT_REAL_PATH/var/log" 2>/dev/null && \
|
|
|
|
chown -R $(id -u -n):$(id -g -n) $my_tmp "$TELEBIT_REAL_PATH" 2>/dev/null
|
2018-06-23 23:38:57 +00:00
|
|
|
if [ $? -eq 0 ]; then
|
|
|
|
soft_sudo_cmd=" "
|
|
|
|
soft_sudo_cmde=""
|
|
|
|
else
|
|
|
|
$soft_sudo_cmd mkdir -p $my_tmp "$TELEBIT_REAL_PATH" "$TELEBIT_REAL_PATH/etc" "$TELEBIT_REAL_PATH/var/log"
|
2018-06-24 05:19:11 +00:00
|
|
|
$soft_sudo_cmd chown -R $(id -u -n):$(id -g -n) $my_tmp "$TELEBIT_REAL_PATH"
|
2018-06-23 23:38:57 +00:00
|
|
|
fi
|
|
|
|
set -e
|
2018-06-07 16:43:05 +00:00
|
|
|
|
2018-06-09 14:05:45 +00:00
|
|
|
|
2018-06-28 20:47:32 +00:00
|
|
|
if [ -n "${TELEBIT_DEBUG}" ]; then
|
|
|
|
echo " - installing node.js runtime to '$TELEBIT_REAL_PATH'..."
|
|
|
|
http_bash https://git.coolaj86.com/coolaj86/node-installer.sh/raw/branch/master/install.sh --no-dev-deps
|
|
|
|
else
|
2018-06-28 22:17:41 +00:00
|
|
|
echo -n "."
|
|
|
|
#bash -c 'while true; do echo -n "."; sleep 2; done' 2>/dev/null &
|
|
|
|
#_my_pid=$!
|
2018-06-28 20:47:32 +00:00
|
|
|
http_bash https://git.coolaj86.com/coolaj86/node-installer.sh/raw/branch/master/install.sh --no-dev-deps >/dev/null 2>/dev/null
|
2018-06-28 22:17:41 +00:00
|
|
|
#kill $_my_pid >/dev/null 2>/dev/null
|
2018-06-28 20:47:32 +00:00
|
|
|
fi
|
2018-06-01 19:14:59 +00:00
|
|
|
|
2018-06-23 23:38:57 +00:00
|
|
|
#
|
|
|
|
# TODO create "upgrade" script and run that instead
|
|
|
|
#
|
2018-06-01 19:14:59 +00:00
|
|
|
|
2018-06-24 01:19:12 +00:00
|
|
|
my_node="$TELEBIT_REAL_PATH/bin/node"
|
2018-06-24 05:19:11 +00:00
|
|
|
tmp_node="$TELEBIT_TMP/bin/node"
|
2018-06-23 23:38:57 +00:00
|
|
|
my_npm="$my_node $TELEBIT_TMP/bin/npm"
|
2018-06-24 05:19:11 +00:00
|
|
|
tmp_npm="$tmp_node $TELEBIT_TMP/bin/npm"
|
2018-06-01 19:14:59 +00:00
|
|
|
|
|
|
|
#https://git.coolaj86.com/coolaj86/telebit.js.git
|
|
|
|
#https://git.coolaj86.com/coolaj86/telebit.js/archive/:tree:.tar.gz
|
|
|
|
#https://git.coolaj86.com/coolaj86/telebit.js/archive/:tree:.zip
|
2018-06-02 09:30:13 +00:00
|
|
|
set +e
|
2018-06-01 19:14:59 +00:00
|
|
|
my_unzip=$(type -p unzip)
|
|
|
|
my_tar=$(type -p tar)
|
2018-06-11 18:56:16 +00:00
|
|
|
# TODO extract to temporary directory, configure, copy etc, replace
|
2018-06-01 19:14:59 +00:00
|
|
|
if [ -n "$my_unzip" ]; then
|
2018-06-11 18:56:16 +00:00
|
|
|
rm -f $my_tmp/$my_app-$TELEBIT_VERSION.zip
|
2018-06-28 20:47:32 +00:00
|
|
|
if [ -n "${TELEBIT_DEBUG}" ]; then
|
2018-06-28 21:48:12 +00:00
|
|
|
echo " - installing telebit zip to '$TELEBIT_REAL_PATH'"
|
2018-06-28 20:47:32 +00:00
|
|
|
fi
|
2018-06-28 22:17:41 +00:00
|
|
|
echo -n "."
|
|
|
|
#bash -c 'while true; do echo -n "."; sleep 2; done' 2>/dev/null &
|
|
|
|
#_my_pid=$!
|
2018-06-11 18:56:16 +00:00
|
|
|
http_get https://git.coolaj86.com/coolaj86/$my_repo/archive/$TELEBIT_VERSION.zip $my_tmp/$my_app-$TELEBIT_VERSION.zip
|
2018-06-28 22:17:41 +00:00
|
|
|
#kill $_my_pid >/dev/null 2>/dev/null
|
2018-06-01 19:14:59 +00:00
|
|
|
# -o means overwrite, and there is no option to strip
|
2018-06-23 23:38:57 +00:00
|
|
|
$my_unzip -o $my_tmp/$my_app-$TELEBIT_VERSION.zip -d $my_tmp/ >/dev/null
|
|
|
|
$rsync_cmd $my_tmp/$my_repo/* $TELEBIT_TMP/ > /dev/null
|
|
|
|
rm -rf $my_tmp/$my_repo
|
2018-06-01 19:14:59 +00:00
|
|
|
elif [ -n "$my_tar" ]; then
|
2018-06-11 18:56:16 +00:00
|
|
|
rm -f $my_tmp/$my_app-$TELEBIT_VERSION.tar.gz
|
2018-06-28 20:47:32 +00:00
|
|
|
if [ -n "${TELEBIT_DEBUG}" ]; then
|
2018-06-28 21:48:12 +00:00
|
|
|
echo " - installing telebit tar.gz to '$TELEBIT_REAL_PATH'"
|
2018-06-28 20:47:32 +00:00
|
|
|
fi
|
2018-06-28 22:17:41 +00:00
|
|
|
echo -n "."
|
|
|
|
#bash -c 'while true; do echo -n "."; sleep 2; done' 2>/dev/null &
|
|
|
|
#_my_pid=$!
|
2018-06-11 18:56:16 +00:00
|
|
|
http_get https://git.coolaj86.com/coolaj86/$my_repo/archive/$TELEBIT_VERSION.tar.gz $my_tmp/$my_app-$TELEBIT_VERSION.tar.gz
|
2018-06-28 22:17:41 +00:00
|
|
|
#kill $_my_pid >/dev/null 2>/dev/null
|
2018-06-23 23:38:57 +00:00
|
|
|
$my_tar -xzf $my_tmp/$my_app-$TELEBIT_VERSION.tar.gz --strip 1 -C $TELEBIT_TMP/ >/dev/null
|
2018-06-01 19:14:59 +00:00
|
|
|
else
|
|
|
|
echo "Neither tar nor unzip found. Abort."
|
|
|
|
exit 13
|
|
|
|
fi
|
|
|
|
set -e
|
|
|
|
|
2018-06-24 08:36:09 +00:00
|
|
|
#
|
|
|
|
# TODO create slim packages that contain all the deps on each os and cpu
|
|
|
|
#
|
2018-06-23 23:38:57 +00:00
|
|
|
pushd $TELEBIT_TMP >/dev/null
|
2018-06-28 20:47:32 +00:00
|
|
|
if [ -n "${TELEBIT_DEBUG}" ]; then
|
|
|
|
echo " - installing telebit npm dependencies to '$TELEBIT_REAL_PATH'..."
|
|
|
|
else
|
|
|
|
echo -n "."
|
|
|
|
fi
|
2018-10-08 04:50:11 +00:00
|
|
|
set +e
|
2018-06-28 21:26:07 +00:00
|
|
|
$tmp_npm install >/dev/null 2>/dev/null &
|
2018-06-28 21:18:19 +00:00
|
|
|
tmp_npm_pid=$!
|
2018-06-28 21:23:02 +00:00
|
|
|
while [ -n "$tmp_npm_pid" ]; do
|
2018-06-28 21:18:19 +00:00
|
|
|
sleep 2
|
|
|
|
echo -n "."
|
2018-06-28 21:26:07 +00:00
|
|
|
kill -s 0 $tmp_npm_pid >/dev/null 2>/dev/null || tmp_npm_pid=""
|
2018-06-28 21:18:19 +00:00
|
|
|
done
|
2018-10-08 04:50:11 +00:00
|
|
|
set -e
|
|
|
|
echo -n "."
|
|
|
|
$tmp_npm install >/dev/null 2>/dev/null
|
|
|
|
# ursa is now an entirely optional dependency for key generation
|
|
|
|
# but very much needed on ARM devices
|
|
|
|
$tmp_npm install ursa >/dev/null 2>/dev/null || true
|
2018-06-01 19:14:59 +00:00
|
|
|
popd >/dev/null
|
|
|
|
|
2018-06-28 21:09:24 +00:00
|
|
|
if [ -n "${TELEBIT_DEBUG}" ]; then
|
|
|
|
echo " - configuring telebit..."
|
|
|
|
echo ""
|
|
|
|
fi
|
2018-06-07 16:43:05 +00:00
|
|
|
|
2018-06-24 08:36:09 +00:00
|
|
|
###############################################
|
|
|
|
#
|
|
|
|
# TODO convert to node script
|
|
|
|
#
|
|
|
|
# Now that node is installed and the telebit
|
|
|
|
# packeage is downloaded, everything can be
|
|
|
|
# run from node, except things requiring sudo
|
|
|
|
#
|
|
|
|
###############################################
|
|
|
|
|
2018-06-11 18:56:16 +00:00
|
|
|
# telebit remote
|
2018-06-23 23:38:57 +00:00
|
|
|
echo '#!/bin/bash' > "$TELEBIT_TMP/bin/$my_app"
|
|
|
|
echo "$my_node $TELEBIT_REAL_PATH/bin/$my_bin "'"$@"' >> "$TELEBIT_TMP/bin/$my_app"
|
|
|
|
chmod a+x "$TELEBIT_TMP/bin/$my_app"
|
2018-06-05 06:53:20 +00:00
|
|
|
|
2018-06-11 18:56:16 +00:00
|
|
|
# telebit daemon
|
2018-06-23 23:38:57 +00:00
|
|
|
echo '#!/bin/bash' > "$TELEBIT_TMP/bin/$my_daemon"
|
|
|
|
echo "$my_node $TELEBIT_REAL_PATH/bin/$my_daemon.js daemon "'"$@"' >> "$TELEBIT_TMP/bin/$my_daemon"
|
|
|
|
chmod a+x "$TELEBIT_TMP/bin/$my_daemon"
|
2018-06-11 18:56:16 +00:00
|
|
|
|
2018-06-05 06:53:20 +00:00
|
|
|
# Create uninstall script based on the install script variables
|
2018-06-23 23:38:57 +00:00
|
|
|
cat << EOF > $TELEBIT_TMP/bin/${my_app}_uninstall
|
2018-06-05 06:53:20 +00:00
|
|
|
#!/bin/bash
|
2018-06-24 08:36:09 +00:00
|
|
|
set -x
|
2018-06-05 07:07:32 +00:00
|
|
|
if [ "$(type -p launchctl)" ]; then
|
2018-06-05 06:53:20 +00:00
|
|
|
sudo launchctl unload -w /Library/LaunchDaemons/${my_app_pkg_name}.plist
|
2018-06-24 08:36:09 +00:00
|
|
|
sudo rm -f /Library/LaunchDaemons/${my_app_pkg_name}.plist
|
2018-06-26 05:37:51 +00:00
|
|
|
|
2018-06-26 06:13:12 +00:00
|
|
|
launchctl unload -w $HOME/Library/LaunchAgents/${my_app_pkg_name}.plist
|
|
|
|
rm -f $HOME/Library/LaunchAgents/${my_app_pkg_name}.plist
|
2018-06-05 06:53:20 +00:00
|
|
|
fi
|
2018-06-05 07:07:32 +00:00
|
|
|
if [ "$(type -p systemctl)" ]; then
|
2018-06-26 05:37:51 +00:00
|
|
|
systemctl --user disable $my_app >/dev/null
|
|
|
|
systemctl --user stop $my_app
|
2018-06-26 06:13:12 +00:00
|
|
|
rm -f $HOME/.config/systemd/user/$my_app.service
|
2018-06-26 05:37:51 +00:00
|
|
|
|
|
|
|
sudo systemctl disable $my_app >/dev/null
|
|
|
|
sudo systemctl stop $my_app
|
|
|
|
sudo rm -f /etc/systemd/system/$my_app.service
|
2018-06-05 06:53:20 +00:00
|
|
|
fi
|
2018-06-23 23:38:57 +00:00
|
|
|
sudo rm -rf $TELEBIT_REAL_PATH /usr/local/bin/$my_app
|
|
|
|
sudo rm -rf $TELEBIT_REAL_PATH /usr/local/bin/$my_daemon
|
2018-06-26 06:13:12 +00:00
|
|
|
rm -rf $HOME/.config/$my_app $HOME/.local/share/$my_app
|
2018-06-05 06:53:20 +00:00
|
|
|
EOF
|
2018-06-23 23:38:57 +00:00
|
|
|
chmod a+x $TELEBIT_TMP/bin/${my_app}_uninstall
|
|
|
|
|
|
|
|
#set +e
|
|
|
|
#if type -p setcap >/dev/null 2>&1; then
|
|
|
|
# #echo "Setting permissions to allow $my_app to run on port 80 and port 443 without sudo or root"
|
|
|
|
# echo " > ${real_sudo_cmde}setcap cap_net_bind_service=+ep $TELEBIT_REAL_PATH/bin/node"
|
|
|
|
# $real_sudo_cmd setcap cap_net_bind_service=+ep $TELEBIT_REAL_PATH/bin/node
|
|
|
|
#fi
|
|
|
|
#set -e
|
2018-06-01 19:14:59 +00:00
|
|
|
|
2018-06-24 01:19:12 +00:00
|
|
|
my_skip=""
|
2018-06-02 08:25:41 +00:00
|
|
|
set +e
|
|
|
|
# TODO for macOS https://apple.stackexchange.com/questions/286749/how-to-add-a-user-from-the-command-line-in-macos
|
2018-06-24 01:19:12 +00:00
|
|
|
# TODO do stuff for groups too
|
|
|
|
# TODO add ending $
|
|
|
|
if type -p dscl >/dev/null 2>/dev/null; then
|
2018-06-27 09:21:44 +00:00
|
|
|
if [ -n "$(dscl . list /users | grep ^$TELEBIT_USER)" ] && [ -n "$(dscl . list /groups | grep ^$TELEBIT_GROUP)" ]; then
|
2018-06-24 01:19:12 +00:00
|
|
|
my_skip="yes"
|
|
|
|
fi
|
2018-06-27 09:21:44 +00:00
|
|
|
elif [ -n "$(cat $my_root/etc/passwd | grep $TELEBIT_USER)" ] && [ -n "$(cat $my_root/etc/group | grep $TELEBIT_GROUP)" ]; then
|
2018-06-24 01:19:12 +00:00
|
|
|
my_skip="yes"
|
|
|
|
fi
|
2018-06-24 08:36:09 +00:00
|
|
|
|
2018-06-24 01:19:12 +00:00
|
|
|
if [ -z "$my_skip" ]; then
|
|
|
|
if type -p adduser >/dev/null 2>/dev/null; then
|
2018-06-27 09:21:44 +00:00
|
|
|
$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
|
2018-06-24 01:19:12 +00:00
|
|
|
elif [ -n "$(cat /etc/passwd | grep www-data:)" ]; then
|
|
|
|
# Linux (Ubuntu)
|
2018-06-27 09:21:44 +00:00
|
|
|
TELEBIT_USER=www-data
|
|
|
|
TELEBIT_GROUP=www-data
|
2018-06-24 01:19:12 +00:00
|
|
|
elif [ -n "$(cat /etc/passwd | grep _www:)" ]; then
|
|
|
|
# Mac
|
2018-06-27 09:21:44 +00:00
|
|
|
TELEBIT_USER=_www
|
|
|
|
TELEBIT_GROUP=_www
|
2018-06-24 01:19:12 +00:00
|
|
|
else
|
|
|
|
# Unsure
|
2018-06-27 09:21:44 +00:00
|
|
|
TELEBIT_USER=$(id -u -n) # $(whoami)
|
|
|
|
TELEBIT_GROUP=$(id -g -n)
|
2018-06-02 08:25:41 +00:00
|
|
|
fi
|
2018-06-01 19:14:59 +00:00
|
|
|
fi
|
2018-06-02 08:25:41 +00:00
|
|
|
set -e
|
2018-06-01 19:14:59 +00:00
|
|
|
|
2018-06-27 09:21:44 +00:00
|
|
|
export TELEBIT_USER
|
|
|
|
export TELEBIT_GROUP
|
2018-06-24 01:19:12 +00:00
|
|
|
export TELEBIT_PATH
|
2018-06-28 04:58:43 +00:00
|
|
|
export TELEBIT_CONFIG=$HOME/.config/$my_app/$my_app.yml
|
2018-06-24 08:36:09 +00:00
|
|
|
# TODO check both expected sock paths in client by default
|
|
|
|
if [ "yes" == "$TELEBIT_USERSPACE" ]; then
|
2018-06-26 07:20:54 +00:00
|
|
|
TELEBIT_TMP_CONFIGD=$HOME/.config/$my_app/$my_daemon.yml
|
2018-06-27 09:21:44 +00:00
|
|
|
TELEBITD_CONFIG=$HOME/.config/$my_app/$my_daemon.yml
|
2018-06-28 19:31:39 +00:00
|
|
|
TELEBIT_LOG_DIR=${TELEBIT_LOG_DIR:-$HOME/.local/share/$my_app/var/log/}
|
|
|
|
TELEBIT_SOCK_DIR=${TELEBIT_SOCK_DIR:-$HOME/.local/share/$my_app/var/run/}
|
2018-06-28 04:58:43 +00:00
|
|
|
TELEBIT_SOCK=${TELEBIT_SOCK:-$HOME/.local/share/$my_app/var/run/$my_app.sock}
|
2018-06-24 08:36:09 +00:00
|
|
|
else
|
|
|
|
TELEBIT_TMP_CONFIGD=$TELEBIT_TMP/etc/$my_daemon.yml
|
2018-06-27 09:21:44 +00:00
|
|
|
TELEBITD_CONFIG=$TELEBIT_REAL_PATH/etc/$my_daemon.yml
|
2018-06-28 19:31:39 +00:00
|
|
|
TELEBIT_LOG_DIR=${TELEBIT_LOG_DIR:-$TELEBIT_REAL_PATH/var/log/}
|
|
|
|
TELEBIT_SOCK_DIR=${TELEBIT_SOCK_DIR:-$TELEBIT_REAL_PATH/var/run/}
|
2018-06-28 04:58:43 +00:00
|
|
|
TELEBIT_SOCK=${TELEBIT_SOCK:-$TELEBIT_REAL_PATH/var/run/$my_app.sock}
|
2018-06-24 08:36:09 +00:00
|
|
|
fi
|
2018-06-28 04:58:43 +00:00
|
|
|
export TELEBITD_CONFIG
|
2018-06-27 09:21:44 +00:00
|
|
|
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
|
2018-06-28 19:31:39 +00:00
|
|
|
export TELEBIT_LOG_DIR
|
|
|
|
export TELEBIT_SOCK_DIR
|
2018-06-27 10:24:56 +00:00
|
|
|
export NODE_PATH="$TELEBIT_REAL_PATH/lib/node_modules"
|
|
|
|
export NPM_CONFIG_PREFIX="$TELEBIT_REAL_PATH"
|
2018-06-27 09:21:44 +00:00
|
|
|
|
2018-06-24 01:19:12 +00:00
|
|
|
$my_node $TELEBIT_TMP/usr/share/template-launcher.js
|
|
|
|
|
2018-06-24 08:36:09 +00:00
|
|
|
# TODO don't create this in TMP_PATH if it exists in TELEBIT_REAL_PATH
|
|
|
|
mkdir -p "$(dirname $TELEBIT_TMP_CONFIGD)"
|
2018-06-27 09:21:44 +00:00
|
|
|
if [ ! -e "$TELEBITD_CONFIG" ]; then
|
2018-06-24 08:36:09 +00:00
|
|
|
|
|
|
|
echo "sock: $TELEBIT_SOCK" >> "$TELEBIT_TMP_CONFIGD"
|
2018-06-26 05:37:51 +00:00
|
|
|
echo "root: $TELEBIT_REAL_PATH" >> "$TELEBIT_TMP_CONFIGD"
|
2018-06-24 08:36:09 +00:00
|
|
|
cat $TELEBIT_REAL_PATH/usr/share/$my_daemon.tpl.yml >> "$TELEBIT_TMP_CONFIGD"
|
|
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
mkdir -p "$(dirname $TELEBIT_CONFIG)"
|
|
|
|
if [ ! -e "$TELEBIT_CONFIG" ]; then
|
|
|
|
|
|
|
|
echo "sock: $TELEBIT_SOCK" >> "$TELEBIT_CONFIG"
|
|
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-06-24 01:19:12 +00:00
|
|
|
# TODO
|
|
|
|
# Backup final directory, if it exists
|
|
|
|
# Move everything over to final directory
|
|
|
|
# Restore config files, if they exist
|
|
|
|
# rewrite system service file with real variables
|
|
|
|
|
2018-06-24 08:36:09 +00:00
|
|
|
# This should only affect non-USERSPACE installs
|
2018-06-27 09:21:44 +00:00
|
|
|
#echo "${soft_sudo_cmde}chown -R $TELEBIT_USER '$TELEBIT_REAL_PATH'
|
2018-06-28 19:31:39 +00:00
|
|
|
$soft_sudo_cmd mkdir -p $TELEBIT_LOG_DIR
|
|
|
|
$soft_sudo_cmd mkdir -p $TELEBIT_SOCK_DIR
|
2018-06-27 09:21:44 +00:00
|
|
|
$soft_sudo_cmd chown -R $TELEBIT_USER "$TELEBIT_REAL_PATH"
|
2018-06-24 08:36:09 +00:00
|
|
|
|
2018-06-26 06:13:12 +00:00
|
|
|
# $HOME/.config/systemd/user/
|
2018-06-09 14:19:57 +00:00
|
|
|
# %h/.config/telebit/telebit.yml
|
2018-06-28 21:09:24 +00:00
|
|
|
if [ -n "${TELEBIT_DEBUG}" ]; then
|
|
|
|
echo " - adding $my_app as a system service"
|
|
|
|
fi
|
2018-06-09 14:19:57 +00:00
|
|
|
# TODO detect with type -p
|
|
|
|
my_system_launcher=""
|
2018-06-24 08:36:09 +00:00
|
|
|
my_app_launchd_service=""
|
2018-06-09 14:19:57 +00:00
|
|
|
if [ -d "/Library/LaunchDaemons" ]; then
|
|
|
|
my_system_launcher="launchd"
|
2018-06-24 08:44:24 +00:00
|
|
|
my_sudo_cmde="$real_sudo_cmde"
|
|
|
|
my_sudo_cmd="$real_sudo_cmd"
|
2018-06-09 14:19:57 +00:00
|
|
|
|
2018-06-24 08:36:09 +00:00
|
|
|
|
|
|
|
if [ "yes" == "$TELEBIT_USERSPACE" ]; then
|
2018-06-24 08:44:24 +00:00
|
|
|
my_app_launchd_service_skel="etc/skel/Library/LaunchAgents/${my_app_pkg_name}.plist"
|
2018-06-24 08:36:09 +00:00
|
|
|
my_app_launchd_service="$HOME/Library/LaunchAgents/${my_app_pkg_name}.plist"
|
2018-06-28 21:09:24 +00:00
|
|
|
if [ -n "${TELEBIT_DEBUG}" ]; then
|
|
|
|
echo " > $rsync_cmd $TELEBIT_REAL_PATH/usr/share/dist/$my_app_launchd_service $my_app_launchd_service"
|
|
|
|
fi
|
2018-06-28 09:25:36 +00:00
|
|
|
mkdir -p $HOME/Library/LaunchAgents
|
2018-06-24 08:44:24 +00:00
|
|
|
$rsync_cmd "$TELEBIT_REAL_PATH/usr/share/dist/$my_app_launchd_service_skel" "$my_app_launchd_service"
|
2018-06-24 08:36:09 +00:00
|
|
|
|
2018-06-28 21:09:24 +00:00
|
|
|
if [ -n "${TELEBIT_DEBUG}" ]; then
|
|
|
|
echo " > chown $(id -u -n):$(id -g -n) $my_app_launchd_service"
|
|
|
|
fi
|
2018-06-24 08:36:09 +00:00
|
|
|
chown $(id -u -n):$(id -g -n) "$my_app_launchd_service"
|
2018-06-24 08:44:24 +00:00
|
|
|
my_sudo_cmd=""
|
|
|
|
my_sudo_cmde=""
|
2018-07-01 00:25:54 +00:00
|
|
|
|
2018-07-01 00:27:47 +00:00
|
|
|
if [ -n "${TELEBIT_DEBUG}" ]; then
|
|
|
|
echo " > launchctl unload -w $my_app_launchd_service >/dev/null 2>/dev/null"
|
|
|
|
fi
|
2018-10-08 05:05:32 +00:00
|
|
|
launchctl unload -w "$my_app_launchd_service" >/dev/null 2>/dev/null
|
2018-06-24 08:36:09 +00:00
|
|
|
else
|
2018-06-24 08:44:24 +00:00
|
|
|
my_app_launchd_service_skel="usr/share/dist/Library/LaunchDaemons/${my_app_pkg_name}.plist"
|
2018-06-24 08:36:09 +00:00
|
|
|
my_app_launchd_service="$my_root/Library/LaunchDaemons/${my_app_pkg_name}.plist"
|
|
|
|
echo " > ${real_sudo_cmde}$rsync_cmd $TELEBIT_REAL_PATH/usr/share/dist/$my_app_launchd_service $my_app_launchd_service"
|
2018-06-24 08:44:24 +00:00
|
|
|
$real_sudo_cmd $rsync_cmd "$TELEBIT_REAL_PATH/usr/share/dist/$my_app_launchd_service_skel" "$my_app_launchd_service"
|
2018-06-24 08:36:09 +00:00
|
|
|
|
|
|
|
echo " > ${real_sudo_cmde}chown root:wheel $my_app_launchd_service"
|
|
|
|
$real_sudo_cmd chown root:wheel "$my_app_launchd_service"
|
2018-06-09 14:19:57 +00:00
|
|
|
|
2018-07-01 00:25:54 +00:00
|
|
|
echo " > ${real_sudo_cmde}launchctl unload -w $my_app_launchd_service >/dev/null 2>/dev/null"
|
|
|
|
$real_sudo_cmd launchctl unload -w "$my_app_launchd_service" >/dev/null 2>/dev/null
|
|
|
|
fi
|
2018-06-24 08:44:24 +00:00
|
|
|
|
2018-06-09 14:19:57 +00:00
|
|
|
elif [ -d "$my_root/etc/systemd/system" ]; then
|
|
|
|
my_system_launcher="systemd"
|
2018-06-24 08:36:09 +00:00
|
|
|
|
2018-06-26 05:37:51 +00:00
|
|
|
if [ "yes" == "$TELEBIT_USERSPACE" ]; then
|
2018-06-28 21:18:19 +00:00
|
|
|
if [ -n "${TELEBIT_DEBUG}" ]; then
|
|
|
|
echo " > $rsync_cmd $TELEBIT_REAL_PATH/usr/share/dist/etc/skel/.config/systemd/user/$my_app.service $HOME/.config/systemd/user/$my_app.service"
|
|
|
|
fi
|
2018-06-26 06:13:12 +00:00
|
|
|
mkdir -p $HOME/.config/systemd/user
|
|
|
|
$rsync_cmd "$TELEBIT_REAL_PATH/usr/share/dist/etc/skel/.config/systemd/user/$my_app.service" "$HOME/.config/systemd/user/$my_app.service"
|
2018-06-26 05:37:51 +00:00
|
|
|
else
|
|
|
|
echo " > ${real_sudo_cmde}$rsync_cmd $TELEBIT_REAL_PATH/usr/share/dist/etc/systemd/system/$my_app.service /etc/systemd/system/$my_app.service"
|
|
|
|
$real_sudo_cmd $rsync_cmd "$TELEBIT_REAL_PATH/usr/share/dist/etc/systemd/system/$my_app.service" "/etc/systemd/system/$my_app.service"
|
|
|
|
fi
|
2018-06-09 14:19:57 +00:00
|
|
|
fi
|
|
|
|
|
2018-06-09 14:23:54 +00:00
|
|
|
sleep 1
|
2018-06-09 14:19:57 +00:00
|
|
|
|
2018-06-09 19:12:30 +00:00
|
|
|
###############################
|
|
|
|
# Actually Launch the Service #
|
|
|
|
###############################
|
2018-06-28 21:37:31 +00:00
|
|
|
if [ -n "${TELEBIT_DEBUG}" ]; then
|
|
|
|
echo ""
|
|
|
|
fi
|
2018-06-09 19:12:30 +00:00
|
|
|
if [ "launchd" == "$my_system_launcher" ]; then
|
2018-06-24 08:36:09 +00:00
|
|
|
|
|
|
|
if [ "yes" == "$TELEBIT_USERSPACE" ]; then
|
2018-06-28 20:47:32 +00:00
|
|
|
if [ -n "${TELEBIT_DEBUG}" ]; then
|
|
|
|
echo " > launchctl load -w $my_app_launchd_service"
|
|
|
|
else
|
|
|
|
echo -n "."
|
|
|
|
fi
|
2018-06-24 08:36:09 +00:00
|
|
|
launchctl load -w "$my_app_launchd_service"
|
|
|
|
else
|
|
|
|
echo " > ${real_sudo_cmde}launchctl load -w $my_app_launchd_service"
|
|
|
|
$real_sudo_cmd launchctl load -w "$my_app_launchd_service"
|
|
|
|
fi
|
2018-07-01 00:25:54 +00:00
|
|
|
sleep 2; # give it time to start
|
2018-06-24 08:36:09 +00:00
|
|
|
|
2018-06-26 05:37:51 +00:00
|
|
|
elif [ "systemd" == "$my_system_launcher" ]; then
|
|
|
|
|
|
|
|
if [ "yes" == "$TELEBIT_USERSPACE" ]; then
|
|
|
|
# https://wiki.archlinux.org/index.php/Systemd/User
|
|
|
|
# sudo loginctl enable-linger username
|
|
|
|
|
2018-06-28 20:47:32 +00:00
|
|
|
if [ -n "${TELEBIT_DEBUG}" ]; then
|
|
|
|
echo " > systemctl --user enable $my_app"
|
|
|
|
else
|
|
|
|
echo -n "."
|
|
|
|
fi
|
2018-06-26 07:12:53 +00:00
|
|
|
systemctl --user daemon-reload
|
2018-06-28 23:14:05 +00:00
|
|
|
# enable also puts success output to stderr... why?
|
|
|
|
systemctl --user enable $my_app >/dev/null 2>/dev/null
|
2018-06-26 06:29:53 +00:00
|
|
|
#echo " > systemctl --user enable systemd-tmpfiles-setup.service systemd-tmpfiles-clean.timer"
|
|
|
|
#systemctl --user enable systemd-tmpfiles-setup.service systemd-tmpfiles-clean.timer
|
2018-06-28 20:47:32 +00:00
|
|
|
if [ -n "${TELEBIT_DEBUG}" ]; then
|
|
|
|
echo " > systemctl --user start $my_app"
|
|
|
|
fi
|
|
|
|
systemctl --user stop $my_app >/dev/null 2>/dev/null
|
|
|
|
systemctl --user start $my_app >/dev/null
|
2018-07-01 00:25:54 +00:00
|
|
|
sleep 2; # give it time to start
|
2018-06-28 21:09:24 +00:00
|
|
|
_is_running=$(systemctl --user status --no-pager $my_app 2>/dev/null | grep "active.*running")
|
2018-06-28 21:18:19 +00:00
|
|
|
if [ -z "$_is_running" ]; then
|
2018-06-28 21:09:24 +00:00
|
|
|
echo "Something went wrong:"
|
|
|
|
systemctl --user status --no-pager $my_app
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
echo -n "."
|
2018-06-26 05:37:51 +00:00
|
|
|
else
|
|
|
|
|
2018-06-26 07:12:53 +00:00
|
|
|
$real_sudo_cmd systemctl daemon-reload
|
2018-06-26 05:37:51 +00:00
|
|
|
echo " > ${real_sudo_cmde}systemctl enable $my_app"
|
|
|
|
$real_sudo_cmd systemctl enable $my_app >/dev/null
|
|
|
|
echo " > ${real_sudo_cmde}systemctl start $my_app"
|
2018-06-26 07:12:53 +00:00
|
|
|
$real_sudo_cmd systemctl daemon-reload
|
2018-06-26 05:37:51 +00:00
|
|
|
$real_sudo_cmd systemctl restart $my_app
|
2018-07-01 00:25:54 +00:00
|
|
|
sleep 2; # give it time to start
|
2018-06-26 07:24:12 +00:00
|
|
|
$real_sudo_cmd systemctl status --no-pager $my_app
|
2018-06-26 05:37:51 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
echo "Run the service manually (we couldn't detect your system service to do that automatically):"
|
|
|
|
echo ""
|
2018-06-28 20:47:32 +00:00
|
|
|
echo " $TELEBITD_BIN --config $TELEBITD_CONFIG"
|
|
|
|
echo " ~/$my_app --config $TELEBIT_CONFIG"
|
2018-06-26 05:37:51 +00:00
|
|
|
|
2018-06-09 19:12:30 +00:00
|
|
|
fi
|
2018-06-05 05:02:50 +00:00
|
|
|
|
2018-06-28 21:09:24 +00:00
|
|
|
# NOTE: ln -sf *should* replace an existing link... but sometimes it doesn't, hence rm -f
|
|
|
|
if [ "yes" == "$TELEBIT_USERSPACE" ]; then
|
|
|
|
if [ -n "${TELEBIT_DEBUG}" ]; then
|
|
|
|
echo " > ${real_sudo_cmde}ln -sf $TELEBIT_REAL_PATH/bin/$my_app /usr/local/bin/$my_app"
|
|
|
|
fi
|
|
|
|
rm -f /usr/local/bin/$my_app 2>/dev/null || true
|
|
|
|
ln -sf $TELEBIT_REAL_PATH/bin/$my_app /usr/local/bin/$my_app 2>/dev/null || true
|
|
|
|
else
|
2018-06-28 20:47:32 +00:00
|
|
|
echo " > ${real_sudo_cmde}ln -sf $TELEBIT_REAL_PATH/bin/$my_app /usr/local/bin/$my_app"
|
2018-06-28 21:09:24 +00:00
|
|
|
rm -f /usr/local/bin/$my_app 2>/dev/null || \
|
|
|
|
$real_sudo_cmd rm -f /usr/local/bin/$my_app
|
|
|
|
ln -sf $TELEBIT_REAL_PATH/bin/$my_app /usr/local/bin/$my_app 2>/dev/null || \
|
|
|
|
$real_sudo_cmd ln -sf $TELEBIT_REAL_PATH/bin/$my_app /usr/local/bin/$my_app
|
|
|
|
# telebitd
|
|
|
|
echo " > ${real_sudo_cmde}ln -sf $TELEBIT_REAL_PATH/bin/$my_daemon /usr/local/bin/$my_daemon"
|
|
|
|
rm -f $TELEBIT_REAL_PATH/bin/$my_daemon || $real_sudo_cmd rm -f $TELEBIT_REAL_PATH/bin/$my_daemon
|
|
|
|
ln -sf $TELEBIT_REAL_PATH/bin/$my_daemon /usr/local/bin/$my_daemon || \
|
|
|
|
$real_sudo_cmd ln -sf $TELEBIT_REAL_PATH/bin/$my_daemon /usr/local/bin/$my_daemon
|
2018-06-28 20:47:32 +00:00
|
|
|
fi
|
2018-06-28 19:01:19 +00:00
|
|
|
rm -f $HOME/$my_app; ln -s $TELEBIT_REAL_PATH/bin/$my_app $HOME/
|
2018-06-24 08:36:09 +00:00
|
|
|
|
|
|
|
|
2018-06-28 20:47:32 +00:00
|
|
|
if [ -n "${TELEBIT_DEBUG}" ]; then
|
|
|
|
echo " > telebit init --tty"
|
|
|
|
echo ""
|
|
|
|
fi
|
2018-06-13 18:59:44 +00:00
|
|
|
sleep 0.25
|
2018-06-01 19:14:59 +00:00
|
|
|
|
2018-06-28 21:37:31 +00:00
|
|
|
echo ""
|
2018-06-23 23:38:57 +00:00
|
|
|
$TELEBIT_REAL_PATH/bin/node $TELEBIT_REAL_PATH/bin/telebit.js init --tty
|