walnut.js/install.sh

288 lines
8.7 KiB
Bash
Raw Normal View History

2016-03-28 23:50:36 +00:00
#!/bin/bash
2017-05-09 16:27:47 +00:00
set -e
set -u
# something or other about android and tmux using PREFIX
#: "${PREFIX:=''}"
MY_ROOT=""
if [ -z "${PREFIX-}" ]; then
MY_ROOT=""
else
MY_ROOT="$PREFIX"
fi
# Not every platform has or needs sudo, gotta save them O(1)s...
sudo_cmd=""
2017-05-09 16:29:29 +00:00
((EUID)) && [[ -z "${ANDROID_ROOT-}" ]] && sudo_cmd="sudo"
2016-04-09 23:14:00 +00:00
2017-05-06 09:33:40 +00:00
###############################
# #
# http_get #
# boilerplate for curl / wget #
# #
###############################
# See https://git.daplie.com/Daplie/daplie-snippets/blob/master/bash/http-get.sh
2017-05-09 18:42:24 +00:00
http_curl_opts="-fsSL"
http_wget_opts="--quiet"
http_bin=""
2017-05-06 09:33:40 +00:00
http_opts=""
http_out=""
2017-05-09 18:42:24 +00:00
detect_http_bin()
2017-05-06 09:33:40 +00:00
{
if type -p curl >/dev/null 2>&1; then
2017-05-09 18:42:24 +00:00
http_bin="curl"
http_opts="$http_curl_opts"
2017-05-06 09:33:40 +00:00
http_out="-o"
2017-05-09 18:42:24 +00:00
#curl -fsSL "$url" -o "$PREFIX/tmp/$pkg"
2017-05-06 09:33:40 +00:00
elif type -p wget >/dev/null 2>&1; then
2017-05-09 18:42:24 +00:00
http_bin="wget"
http_opts="$http_wget_opts"
2017-05-06 09:33:40 +00:00
http_out="-O"
2017-05-09 18:42:24 +00:00
#wget --quiet "$url" -O "$PREFIX/tmp/$pkg"
else
echo "Aborted, could not find curl or wget"
return 7
fi
}
http_get()
{
if [ -e "$1" ]; then
rsync -a "$1" "$2"
elif type -p curl >/dev/null 2>&1; then
$http_bin $http_curl_opts $http_out "$2" "$1"
elif type -p wget >/dev/null 2>&1; then
$http_bin $http_wget_opts $http_out "$2" "$1"
2017-05-06 09:33:40 +00:00
else
echo "Aborted, could not find curl or wget"
return 7
fi
}
dap_dl()
{
2017-05-09 18:42:24 +00:00
http_get "$1" "$2"
2017-05-06 09:33:40 +00:00
}
dap_dl_bash()
{
dap_url=$1
#dap_args=$2
2017-05-25 18:26:14 +00:00
rm -rf /tmp/dap-tmp-runner.sh
$http_bin $http_opts $http_out /tmp/dap-tmp-runner.sh "$dap_url"; bash /tmp/dap-tmp-runner.sh; rm /tmp/dap-tmp-runner.sh
2017-05-06 09:33:40 +00:00
}
2017-05-09 18:42:24 +00:00
detect_http_bin
2017-05-06 09:33:40 +00:00
## END HTTP_GET ##
mvdir_backward_compat()
{
old_dir=$1
new_dir=$2
# The symlink has already been set up, so no need to do anything.
if [ -L $old_dir ] && [ $(readlink $old_dir) == "$new_dir" ]; then
return 0
fi
if [ -d $old_dir ]; then
if [ $(ls $old_dir | wc -l) -gt 0 ]; then
mv ${old_dir}/* ${new_dir}/
fi
rm -r ${old_dir}
#rmdir ${old_dir}
fi
ln -snf $new_dir $old_dir
}
2017-05-06 09:33:40 +00:00
2017-05-09 16:27:47 +00:00
###################
# #
# Install service #
# #
###################
2017-05-06 09:33:40 +00:00
install_for_systemd()
{
echo ""
echo "Installing as systemd service"
echo ""
2017-05-09 16:27:47 +00:00
mkdir -p $(dirname "$my_app_dir/$my_app_systemd_service")
dap_dl "$installer_base/$my_app_systemd_service" "$my_app_dir/$my_app_systemd_service"
$sudo_cmd mv "$my_app_dir/$my_app_systemd_service" "$MY_ROOT/$my_app_systemd_service"
$sudo_cmd chown -R root:root "$MY_ROOT/$my_app_systemd_service"
$sudo_cmd chmod 644 "$MY_ROOT/$my_app_systemd_service"
mkdir -p $(dirname "$my_app_dir/$my_app_systemd_tmpfiles")
dap_dl "$installer_base/$my_app_systemd_tmpfiles" "$my_app_dir/$my_app_systemd_tmpfiles"
$sudo_cmd mv "$my_app_dir/$my_app_systemd_tmpfiles" "$MY_ROOT/$my_app_systemd_tmpfiles"
$sudo_cmd chown -R root:root "$MY_ROOT/$my_app_systemd_tmpfiles"
$sudo_cmd chmod 644 "$MY_ROOT/$my_app_systemd_tmpfiles"
2017-05-06 09:33:40 +00:00
$sudo_cmd systemctl stop "${my_app_name}.service" >/dev/null 2>/dev/null
$sudo_cmd systemctl daemon-reload
$sudo_cmd systemctl start "${my_app_name}.service"
$sudo_cmd systemctl enable "${my_app_name}.service"
2017-05-09 16:27:47 +00:00
echo "$my_app_name started with systemctl, check its status like so"
echo " $sudo_cmd systemctl status $my_app_name"
2017-05-15 18:16:10 +00:00
echo " $sudo_cmd journalctl -xe -u $my_app_name"
2017-05-06 09:33:40 +00:00
}
install_for_launchd()
{
echo ""
echo "Installing as launchd service"
echo ""
# See http://www.launchd.info/
2017-05-09 16:27:47 +00:00
mkdir -p $(dirname "$my_app_dir/$my_app_launchd_service")
2017-05-06 09:33:40 +00:00
dap_dl "$installer_base/$my_app_launchd_service" "$my_app_dir/$my_app_launchd_service"
2017-05-09 16:27:47 +00:00
$sudo_cmd mv "$my_app_dir/$my_app_launchd_service" "$MY_ROOT/$my_app_launchd_service"
$sudo_cmd chown root:wheel "$MY_ROOT/$my_app_launchd_service"
$sudo_cmd chmod 0644 "$MY_ROOT/$my_app_launchd_service"
$sudo_cmd launchctl unload -w "$MY_ROOT/$my_app_launchd_service" >/dev/null 2>/dev/null
$sudo_cmd launchctl load -w "$MY_ROOT/$my_app_launchd_service"
2017-05-06 09:33:40 +00:00
echo "$my_app_name started with launchd"
}
2017-05-09 16:27:47 +00:00
install_etc_config()
2017-05-06 09:33:40 +00:00
{
2017-05-25 18:57:24 +00:00
#echo "install etc config $MY_ROOT / $my_app_etc_config"
2017-05-09 16:27:47 +00:00
if [ ! -e "$MY_ROOT/$my_app_etc_config" ]; then
$sudo_cmd mkdir -p $(dirname "$MY_ROOT/$my_app_etc_config")
mkdir -p $(dirname "$my_app_dir/$my_app_etc_config")
dap_dl "$installer_base/$my_app_etc_config" "$my_app_dir/$my_app_etc_config"
$sudo_cmd mv "$my_app_dir/$my_app_etc_config" "$MY_ROOT/$my_app_etc_config"
fi
2017-05-06 09:33:40 +00:00
2017-05-25 17:43:51 +00:00
$sudo_cmd chown -R www-data:www-data $(dirname "$MY_ROOT/$my_app_etc_config") || true
$sudo_cmd chown -R _www:_www $(dirname "$MY_ROOT/$my_app_etc_config") || true
2017-05-09 16:27:47 +00:00
$sudo_cmd chmod 775 $(dirname "$MY_ROOT/$my_app_etc_config")
$sudo_cmd chmod 664 "$MY_ROOT/$my_app_etc_config"
2017-05-06 09:33:40 +00:00
}
install_service()
{
2017-05-09 16:27:47 +00:00
install_etc_config
2017-05-25 18:57:24 +00:00
#echo "install service"
2017-05-09 16:27:47 +00:00
2017-05-06 09:33:40 +00:00
installable=""
2017-05-09 16:27:47 +00:00
if [ -d "$MY_ROOT/etc/systemd/system" ]; then
2017-05-06 09:33:40 +00:00
install_for_systemd
installable="true"
fi
if [ -d "/Library/LaunchDaemons" ]; then
install_for_launchd
installable="true"
fi
if [ -z "$installable" ]; then
echo ""
echo "Unknown system service init type. You must install as a system service manually."
echo '(please file a bug with the output of "uname -a")'
echo ""
fi
2017-05-09 18:42:24 +00:00
echo ""
2017-05-06 09:33:40 +00:00
}
2017-05-09 16:27:47 +00:00
## END SERVICE_INSTALL ##
# Create dirs, set perms
create_skeleton()
{
$sudo_cmd mkdir -p /srv/www
$sudo_cmd mkdir -p /var/log/$my_app_name
$sudo_cmd mkdir -p /etc/$my_app_name
$sudo_cmd mkdir -p /var/$my_app_name
$sudo_cmd mkdir -p /srv/$my_app_name
$sudo_cmd mkdir -p /opt/$my_app_name
}
# Unistall
install_uninstaller()
{
2017-05-25 18:57:24 +00:00
#echo "install uninstaller"
2017-05-09 16:27:47 +00:00
dap_dl "https://git.daplie.com/Daplie/walnut.js/raw/master/uninstall.sh" "./walnut-uninstall"
$sudo_cmd chmod 755 "./walnut-uninstall"
$sudo_cmd chown root:root "./walnut-uninstall"
$sudo_cmd mv "./walnut-uninstall" "/usr/local/bin/uninstall-walnut"
}
# Dependencies
export NODE_PATH=/opt/walnut/lib/node_modules
export NPM_CONFIG_PREFIX=/opt/walnut
2017-06-15 00:54:07 +00:00
$sudo_cmd mkdir -p $NODE_PATH
$sudo_cmd chown -R $(whoami) /opt/walnut
2017-05-09 16:27:47 +00:00
dap_dl_bash "https://git.daplie.com/coolaj86/node-install-script/raw/master/setup-min.sh"
# Install
2017-05-31 00:55:32 +00:00
# npm install -g 'git+https://git@git.daplie.com/Daplie/walnut.js.git#v1'
2017-05-09 16:27:47 +00:00
my_app_name=walnut
my_app_pkg_name=com.daplie.walnut.web
my_app_dir=$(mktemp -d)
2017-05-09 18:42:24 +00:00
#installer_base="https://git.daplie.com/Daplie/walnut.js/raw/master/dist"
2017-05-25 18:57:24 +00:00
#installer_base="$( dirname "${BASH_SOURCE[0]}" )/dist"
installer_base="/srv/walnut/core/dist"
2017-05-09 16:27:47 +00:00
my_app_etc_config="etc/${my_app_name}/${my_app_name}.yml"
my_app_systemd_service="etc/systemd/system/${my_app_name}.service"
my_app_systemd_tmpfiles="etc/tmpfiles.d/${my_app_name}.conf"
my_app_launchd_service="Library/LaunchDaemons/${my_app_pkg_name}.plist"
# Install
2017-05-09 18:42:24 +00:00
install_my_app()
{
2017-06-14 23:14:27 +00:00
# This function shouldn't need to use $sudo_cmd because it is called immediately after
# /srv/walnut is chown-ed and we only mess with things in that directory.
2017-05-09 18:42:24 +00:00
#git clone git@git.daplie.com:Daplie/walnut.js.git
#git clone https://git.daplie.com/Daplie/walnut.js.git /srv/walnut/core
mkdir -p /srv/walnut/{core,lib,var,etc,config,node_modules}
2017-05-09 18:42:24 +00:00
rm -rf /srv/walnut/core/node_modules
ln -sf ../node_modules /srv/walnut/core/node_modules
2017-06-14 23:14:27 +00:00
mkdir -p /srv/walnut/var/sites
mkdir -p /srv/walnut/etc/org.oauth3.consumer
mkdir -p /srv/walnut/etc/org.oauth3.provider
mkdir -p /srv/walnut/etc/client-api-grants
mkdir -p /srv/walnut/packages/{rest,api,pages,services}
2017-06-12 22:06:18 +00:00
# backwards compat
mvdir_backward_compat /srv/walnut/packages/client-api-grants /srv/walnut/etc/client-api-grants
mvdir_backward_compat /srv/walnut/packages/sites /srv/walnut/var/sites
2017-05-09 18:42:24 +00:00
pushd /srv/walnut/core
/opt/walnut/bin/npm install
2017-05-09 18:42:24 +00:00
popd
}
2017-05-09 16:27:47 +00:00
2017-06-14 23:14:27 +00:00
$sudo_cmd mkdir -p /srv/walnut
$sudo_cmd chown -R $(whoami) /srv/walnut
2017-05-25 18:26:14 +00:00
2017-05-09 18:42:24 +00:00
install_my_app
2017-05-09 16:27:47 +00:00
create_skeleton
install_uninstaller
2017-05-06 09:33:40 +00:00
install_service
2017-05-25 18:26:14 +00:00
2017-06-15 00:54:07 +00:00
$sudo_cmd chown -R www-data:www-data /opt/walnut || true
$sudo_cmd chown -R _www:_www /opt/walnut || true
2017-06-14 23:14:27 +00:00
$sudo_cmd chown -R www-data:www-data /srv/walnut || true
$sudo_cmd chown -R _www:_www /srv/walnut || true
$sudo_cmd chmod -R ug+rwX /srv/walnut
$sudo_cmd chmod -R ug+rwX /opt/walnut
# +s sets the setuid/setgid bit, which when set on directories makes it so anything
# created inside the directory maintains the same user/group (depending on the bits
# set). Any directory created within a directory with those bits set will also have
# those bits set. When setuid or setgid bits are set on a file however it means that
# if the file is executed it will run with the permissions of the user/group no matter
# who actually runs it (see the ping executable for example).
# I'm not sure that all systems actually support the use of these bits.
find /srv/walnut -type d -exec $sudo_cmd chmod ug+s {} \; || true
find /opt/walnut -type d -exec $sudo_cmd chmod ug+s {} \; || true