telebit-relay.js/installer/get.sh

231 lines
7.2 KiB
Bash
Raw Normal View History

2018-05-23 11:12:39 +00:00
#!/bin/bash
2018-05-24 09:19:25 +00:00
#<pre><code>
2018-05-23 11:12:39 +00:00
# This is a 3 step process
# 1. First we need to figure out whether to use wget or curl for fetching remote files
# 2. Next we need to figure out whether to use unzip or tar for downloading releases
# 3. We need to actually install the stuff
set -e
set -u
###############################
# #
# http_get #
# boilerplate for curl / wget #
# #
###############################
# See https://git.coolaj86.com/coolaj86/snippets/blob/master/bash/http-get.sh
_my_http_get=""
_my_http_opts=""
_my_http_out=""
detect_http_get()
{
set +e
if type -p curl >/dev/null 2>&1; then
_my_http_get="curl"
_my_http_opts="-fsSL"
_my_http_out="-o"
elif type -p wget >/dev/null 2>&1; then
_my_http_get="wget"
_my_http_opts="--quiet"
_my_http_out="-O"
else
echo "Aborted, could not find curl or wget"
return 7
fi
set -e
}
http_get()
{
$_my_http_get $_my_http_opts $_my_http_out "$2" "$1"
touch "$2"
}
http_bash()
{
_http_url=$1
my_args=${2:-}
rm -rf my-tmp-runner.sh
$_my_http_get $_my_http_opts $_my_http_out my-tmp-runner.sh "$_http_url"; bash my-tmp-runner.sh $my_args; rm my-tmp-runner.sh
}
detect_http_get
###############################
## END HTTP_GET ##
###############################
2018-05-24 22:40:50 +00:00
my_email=${1:-}
2018-05-25 09:02:04 +00:00
my_servername=${2:-}
2018-05-25 00:38:57 +00:00
my_secret=""
2018-05-24 09:19:25 +00:00
my_user="telebit"
2018-06-11 17:24:57 +00:00
my_app="telebit-relay"
my_bin="telebit-relay.js"
2018-05-23 11:12:39 +00:00
my_name="Telebit Relay"
2018-06-11 17:24:57 +00:00
my_repo="telebit-relay.js"
2018-05-23 11:12:39 +00:00
2018-05-24 22:40:50 +00:00
if [ -z "${my_email}" ]; then
echo ""
echo ""
echo "Telebit uses Greenlock for free automated ssl through Let's Encrypt."
echo ""
echo "To accept the Terms of Service for Telebit, Greenlock and Let's Encrypt,"
echo "please enter your email."
echo ""
2018-05-25 00:55:11 +00:00
read -p "email: " my_email
2018-05-25 01:08:38 +00:00
echo ""
2018-05-26 07:47:20 +00:00
# UX - just want a smooth transition
2018-05-26 08:07:49 +00:00
sleep 0.5
2018-05-24 22:40:50 +00:00
fi
2018-05-25 09:02:04 +00:00
if [ -z "${my_servername}" ]; then
echo "What is the domain of this server (for admin interface)?"
echo ""
2018-06-11 17:24:57 +00:00
read -p "domain (ex: telebit-relay.example.com): " my_servername
2018-05-25 09:02:04 +00:00
echo ""
2018-05-26 07:47:20 +00:00
# UX - just want a smooth transition
2018-05-26 08:07:49 +00:00
sleep 0.5
2018-05-25 09:02:04 +00:00
fi
2018-05-26 08:07:49 +00:00
echo ""
2018-05-25 09:02:04 +00:00
2018-06-11 17:24:57 +00:00
if [ -z "${TELEBIT_RELAY_PATH:-}" ]; then
echo 'TELEBIT_RELAY_PATH="'${TELEBIT_RELAY_PATH:-}'"'
TELEBIT_RELAY_PATH=/opt/$my_app
2018-05-23 11:12:39 +00:00
fi
2018-06-11 17:24:57 +00:00
echo "Installing $my_name to '$TELEBIT_RELAY_PATH'"
2018-05-23 11:12:39 +00:00
2018-06-11 17:24:57 +00:00
echo "Installing node.js dependencies into $TELEBIT_RELAY_PATH"
2018-05-25 00:38:57 +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"
2018-05-25 09:02:04 +00:00
NODEJS_VER="${NODEJS_VER:-v10}"
export NODEJS_VER
2018-06-11 17:24:57 +00:00
export NODE_PATH="$TELEBIT_RELAY_PATH/lib/node_modules"
export NPM_CONFIG_PREFIX="$TELEBIT_RELAY_PATH"
export PATH="$TELEBIT_RELAY_PATH/bin:$PATH"
2018-05-23 11:12:39 +00:00
sleep 1
http_bash https://git.coolaj86.com/coolaj86/node-installer.sh/raw/branch/master/install.sh --no-dev-deps >/dev/null 2>/dev/null
my_tree="master"
2018-06-11 17:24:57 +00:00
my_node="$TELEBIT_RELAY_PATH/bin/node"
2018-05-25 00:55:11 +00:00
my_secret=$($my_node -e "console.info(crypto.randomBytes(16).toString('hex'))")
2018-06-11 17:24:57 +00:00
my_npm="$my_node $TELEBIT_RELAY_PATH/bin/npm"
my_tmp="$TELEBIT_RELAY_PATH/tmp"
2018-05-23 11:12:39 +00:00
mkdir -p $my_tmp
2018-06-11 17:24:57 +00:00
echo "sudo mkdir -p '$TELEBIT_RELAY_PATH'"
sudo mkdir -p "$TELEBIT_RELAY_PATH"
echo "sudo mkdir -p '/opt/$my_app/etc'"
sudo mkdir -p "/opt/$my_app/etc/"
2018-05-25 00:38:57 +00:00
2018-05-23 11:12:39 +00:00
set +e
2018-06-11 17:24:57 +00:00
#https://git.coolaj86.com/coolaj86/telebit-relay.js.git
#https://git.coolaj86.com/coolaj86/telebit-relay.js/archive/:tree:.tar.gz
#https://git.coolaj86.com/coolaj86/telebit-relay.js/archive/:tree:.zip
2018-05-23 11:12:39 +00:00
my_unzip=$(type -p unzip)
my_tar=$(type -p tar)
if [ -n "$my_unzip" ]; then
rm -f $my_tmp/$my_app-$my_tree.zip
http_get https://git.coolaj86.com/coolaj86/$my_repo/archive/$my_tree.zip $my_tmp/$my_app-$my_tree.zip
# -o means overwrite, and there is no option to strip
2018-06-11 17:24:57 +00:00
$my_unzip -o $my_tmp/$my_app-$my_tree.zip -d $TELEBIT_RELAY_PATH/ > /dev/null 2>&1
cp -ar $TELEBIT_RELAY_PATH/$my_repo/* $TELEBIT_RELAY_PATH/ > /dev/null
rm -rf $TELEBIT_RELAY_PATH/$my_bin
2018-05-23 11:12:39 +00:00
elif [ -n "$my_tar" ]; then
rm -f $my_tmp/$my_app-$my_tree.tar.gz
http_get https://git.coolaj86.com/coolaj86/$my_repo/archive/$my_tree.tar.gz $my_tmp/$my_app-$my_tree.tar.gz
ls -lah $my_tmp/$my_app-$my_tree.tar.gz
2018-06-11 17:24:57 +00:00
$my_tar -xzf $my_tmp/$my_app-$my_tree.tar.gz --strip 1 -C $TELEBIT_RELAY_PATH/
2018-05-23 11:12:39 +00:00
else
echo "Neither tar nor unzip found. Abort."
exit 13
fi
set -e
2018-06-11 17:24:57 +00:00
pushd $TELEBIT_RELAY_PATH >/dev/null
2018-05-23 11:12:39 +00:00
$my_npm install >/dev/null 2>/dev/null
popd >/dev/null
2018-06-11 17:24:57 +00:00
cat << EOF > $TELEBIT_RELAY_PATH/bin/$my_app
2018-05-23 11:12:39 +00:00
#!/bin/bash
2018-06-11 17:24:57 +00:00
$my_node $TELEBIT_RELAY_PATH/bin/$my_bin
2018-05-23 11:12:39 +00:00
EOF
2018-06-11 17:24:57 +00:00
chmod a+x $TELEBIT_RELAY_PATH/bin/$my_app
echo "sudo ln -sf $TELEBIT_RELAY_PATH/bin/$my_app /usr/local/bin/$my_app"
sudo ln -sf $TELEBIT_RELAY_PATH/bin/$my_app /usr/local/bin/$my_app
2018-05-24 09:19:25 +00:00
set +e
if type -p setcap >/dev/null 2>&1; then
2018-05-25 01:08:38 +00:00
#echo "Setting permissions to allow $my_app to run on port 80 and port 443 without sudo or root"
2018-06-11 17:24:57 +00:00
echo "sudo setcap cap_net_bind_service=+ep $TELEBIT_RELAY_PATH/bin/node"
sudo setcap cap_net_bind_service=+ep $TELEBIT_RELAY_PATH/bin/node
2018-05-24 09:19:25 +00:00
fi
set -e
if [ -z "$(cat /etc/passwd | grep $my_user)" ]; then
2018-06-11 17:24:57 +00:00
echo "sudo adduser --home $TELEBIT_RELAY_PATH --gecos '' --disabled-password $my_user"
sudo adduser --home $TELEBIT_RELAY_PATH --gecos '' --disabled-password $my_user >/dev/null 2>&1
2018-05-24 09:19:25 +00:00
fi
2018-06-11 17:24:57 +00:00
if [ ! -f "/opt/$my_app/etc/$my_app.yml" ]; then
2018-05-25 01:08:38 +00:00
echo "### Creating config file from template. sudo may be required"
2018-06-11 17:24:57 +00:00
#echo "sudo rsync -a examples/$my_app.yml /opt/$my_app/etc/$my_app.yml"
sudo bash -c "echo 'email: $my_email' >> /opt/$my_app/etc/$my_app.yml"
sudo bash -c "echo 'secret: $my_secret' >> /opt/$my_app/etc/$my_app.yml"
sudo bash -c "echo 'servernames: [ $my_servername ]' >> /opt/$my_app/etc/$my_app.yml"
sudo bash -c "cat examples/$my_app.yml.tpl >> /opt/$my_app/etc/$my_app.yml"
2018-05-25 00:38:57 +00:00
fi
2018-05-24 09:26:14 +00:00
2018-06-11 17:24:57 +00:00
echo "sudo chown -R $my_user '$TELEBIT_RELAY_PATH' '/opt/$my_app/etc'"
sudo chown -R $my_user "$TELEBIT_RELAY_PATH" "/opt/$my_app/etc"
2018-05-25 00:55:11 +00:00
echo "### Adding $my_app is a system service"
2018-06-11 17:24:57 +00:00
echo "sudo rsync -a $TELEBIT_RELAY_PATH/dist/etc/systemd/system/$my_app.service /etc/systemd/system/$my_app.service"
sudo rsync -a $TELEBIT_RELAY_PATH/dist/etc/systemd/system/$my_app.service /etc/systemd/system/$my_app.service
2018-05-25 00:55:11 +00:00
sudo systemctl daemon-reload
echo "sudo systemctl enable $my_app"
sudo systemctl enable $my_app
echo "sudo systemctl start $my_app"
2018-05-25 01:08:38 +00:00
sudo systemctl restart $my_app
2018-05-25 00:55:11 +00:00
2018-05-25 01:08:38 +00:00
sleep 1
echo ""
echo ""
2018-05-23 11:12:39 +00:00
echo ""
2018-05-25 01:08:38 +00:00
echo "=============================================="
echo " Privacy Settings in Config"
echo "=============================================="
2018-05-23 11:12:39 +00:00
echo ""
2018-06-11 17:24:57 +00:00
echo "The example config file /opt/$my_app/etc/$my_app.yml opts-in to"
2018-05-25 00:38:57 +00:00
echo "contributing telemetrics and receiving infrequent relevant updates"
2018-05-24 22:40:50 +00:00
echo "(probably once per quarter or less) such as important notes on"
2018-05-25 00:38:57 +00:00
echo "a new release, an important API change, etc. No spam."
2018-05-24 22:40:50 +00:00
echo ""
echo "Please edit the config file to meet your needs before starting."
echo ""
2018-05-25 01:08:38 +00:00
sleep 2
2018-05-24 22:40:50 +00:00
echo ""
echo ""
2018-05-25 01:08:38 +00:00
echo "=============================================="
2018-05-25 00:38:57 +00:00
echo "Installed successfully. Last steps:"
2018-05-25 01:08:38 +00:00
echo "=============================================="
2018-05-25 00:38:57 +00:00
echo ""
2018-05-25 01:08:38 +00:00
echo "Edit the config and restart, if desired:"
2018-05-25 00:38:57 +00:00
echo ""
2018-06-11 17:24:57 +00:00
echo " sudo vim /opt/$my_app/etc/$my_app.yml"
2018-05-25 01:08:38 +00:00
echo " sudo systemctl restart $my_app"
2018-05-25 00:38:57 +00:00
echo ""
2018-05-25 01:08:38 +00:00
echo "Or disabled the service and start manually:"
2018-05-23 11:12:39 +00:00
echo ""
2018-05-25 01:08:38 +00:00
echo " sudo systemctl stop $my_app"
echo " sudo systemctl disable $my_app"
2018-06-11 17:24:57 +00:00
echo " $my_app --config /opt/$my_app/etc/$my_app.yml"
2018-05-23 11:12:39 +00:00
echo ""
2018-05-25 01:08:38 +00:00
sleep 1