Files
cloudron-box/scripts/installer.sh
T

77 lines
2.3 KiB
Bash
Raw Normal View History

2015-08-04 16:29:49 -07:00
#!/bin/bash
set -eu -o pipefail
2016-11-01 15:13:20 +01:00
if [[ ${EUID} -ne 0 ]]; then
echo "This script should be run as root." > /dev/stderr
exit 1
fi
readonly USER=yellowtent
readonly BOX_SRC_DIR=/home/${USER}/box
readonly CLOUDRON_CONF=/home/yellowtent/configs/cloudron.conf
2015-08-04 16:29:49 -07:00
readonly script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly box_src_tmp_dir="$(realpath ${script_dir}/..)"
2015-08-04 16:29:49 -07:00
readonly is_update=$([[ -f "${CLOUDRON_CONF}" ]] && echo "yes" || echo "no")
2015-08-04 16:29:49 -07:00
# create a provision file for testing. %q escapes args. %q is reused as much as necessary to satisfy $@
(echo -e "#!/bin/bash\n"; printf "%q " "${script_dir}/installer.sh" "$@") > /root/provision.sh
chmod +x /root/provision.sh
2015-08-04 16:29:49 -07:00
arg_data=""
args=$(getopt -o "" -l "data:,data-file:" -n "$0" -- "$@")
2015-08-04 16:29:49 -07:00
eval set -- "${args}"
while true; do
case "$1" in
2016-12-28 09:46:04 -08:00
--data) arg_data="$2"; shift 2;;
--data-file) arg_data=$(cat $2); shift 2;;
2015-08-04 16:29:49 -07:00
--) break;;
*) echo "Unknown option $1"; exit 1;;
esac
done
2016-11-01 17:01:16 +01:00
for try in `seq 1 10`; do
2016-01-14 10:48:44 -08:00
# for reasons unknown, the dtrace package will fail. but rebuilding second time will work
2016-11-01 16:57:43 +01:00
# We need --unsafe-perm as we run as root and the folder is owned by root,
# however by default npm drops privileges for npm rebuild
# https://docs.npmjs.com/misc/config#unsafe-perm
if cd "${box_src_tmp_dir}" && npm rebuild --unsafe-perm; then break; fi
2016-01-14 10:48:44 -08:00
echo "Failed to rebuild, trying again"
sleep 5
done
2015-08-04 16:29:49 -07:00
2016-11-01 17:01:16 +01:00
if [[ ${try} -eq 10 ]]; then
echo "npm rebuild failed"
exit 4
fi
2016-12-26 12:23:09 -08:00
if ! id "${USER}" 2>/dev/null; then
useradd "${USER}" -m
fi
2015-08-04 16:29:49 -07:00
if [[ "${is_update}" == "yes" ]]; then
echo "Setting up update splash screen"
"${box_src_tmp_dir}/setup/splashpage.sh" --data "${arg_data}" # show splash from new code
${BOX_SRC_DIR}/setup/stop.sh # stop the old code
fi
# ensure we are not inside the source directory, which we will remove now
cd /root
2016-12-28 09:54:30 -08:00
echo "==> installer: switching the box code"
2015-08-04 16:29:49 -07:00
rm -rf "${BOX_SRC_DIR}"
mv "${box_src_tmp_dir}" "${BOX_SRC_DIR}"
chown -R "${USER}:${USER}" "${BOX_SRC_DIR}"
2015-08-04 16:29:49 -07:00
# create a start file for testing. %q escapes args
(echo -e "#!/bin/bash\n"; printf "%q " "${BOX_SRC_DIR}/setup/start.sh" --data "${arg_data}") > /home/yellowtent/setup_start.sh
chmod +x /home/yellowtent/setup_start.sh
2016-12-28 09:54:30 -08:00
echo "==> installer: calling box setup script"
2015-08-04 16:29:49 -07:00
"${BOX_SRC_DIR}/setup/start.sh" --data "${arg_data}"