installer.sh 5.25 KB
Newer Older
1 2
#!/bin/bash

3 4 5 6
# This script is run before the box code is switched. This means that we can
# put network related/curl downloads here. If the script fails, the old code
# will continue to run

7 8
set -eu -o pipefail

9 10 11 12 13
if [[ ${EUID} -ne 0 ]]; then
    echo "This script should be run as root." > /dev/stderr
    exit 1
fi

14 15
readonly USER=yellowtent
readonly BOX_SRC_DIR=/home/${USER}/box
16
readonly BASE_DATA_DIR=/home/${USER}
17

18
readonly curl="curl --fail --connect-timeout 20 --retry 10 --retry-delay 2 --max-time 2400"
19
readonly script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
20
readonly box_src_tmp_dir="$(realpath ${script_dir}/..)"
21

Girish Ramakrishnan's avatar
Girish Ramakrishnan committed
22 23 24
readonly ubuntu_version=$(lsb_release -rs)
readonly ubuntu_codename=$(lsb_release -cs)

25
readonly is_update=$(systemctl is-active box && echo "yes" || echo "no")
26

Johannes Zellner's avatar
Johannes Zellner committed
27 28 29
echo "==> installer: updating nginx"
# https://launchpad.net/~nginx
add-apt-repository ppa:nginx/stable --yes
30
apt-get update
31 32
# We need to tell dpkg to use our modified config file always
apt-get install nginx-full -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" --force-yes
Johannes Zellner's avatar
Johannes Zellner committed
33

Girish Ramakrishnan's avatar
Girish Ramakrishnan committed
34
echo "==> installer: updating docker"
35

Girish Ramakrishnan's avatar
Girish Ramakrishnan committed
36 37 38 39 40
if [[ $(docker version --format {{.Client.Version}}) != "18.09.2" ]]; then
    # there are 3 packages for docker - containerd, CLI and the daemon
    $curl -sL "https://download.docker.com/linux/ubuntu/dists/${ubuntu_codename}/pool/stable/amd64/containerd.io_1.2.2-3_amd64.deb" -o /tmp/containerd.deb
    $curl -sL "https://download.docker.com/linux/ubuntu/dists/${ubuntu_codename}/pool/stable/amd64/docker-ce-cli_18.09.2~3-0~ubuntu-${ubuntu_codename}_amd64.deb" -o /tmp/docker-ce-cli.deb
    $curl -sL "https://download.docker.com/linux/ubuntu/dists/${ubuntu_codename}/pool/stable/amd64/docker-ce_18.09.2~3-0~ubuntu-${ubuntu_codename}_amd64.deb" -o /tmp/docker.deb
Girish Ramakrishnan's avatar
Girish Ramakrishnan committed
41

42
    echo "==> installer: Waiting for all dpkg tasks to finish..."
Girish Ramakrishnan's avatar
Girish Ramakrishnan committed
43 44 45 46 47
    while fuser /var/lib/dpkg/lock; do
        sleep 1
    done

    while ! dpkg --force-confold --configure -a; do
48
        echo "==> installer: Failed to fix packages. Retry"
Girish Ramakrishnan's avatar
Girish Ramakrishnan committed
49
        sleep 1
50 51 52 53 54 55
    done

    # the latest docker might need newer packages
    while ! apt update -y; do
        echo "==> installer: Failed to update packages. Retry"
        sleep 1
Girish Ramakrishnan's avatar
Girish Ramakrishnan committed
56 57
    done

Girish Ramakrishnan's avatar
Girish Ramakrishnan committed
58
    while ! apt install -y /tmp/containerd.deb  /tmp/docker-ce-cli.deb /tmp/docker.deb; do
59
        echo "==> installer: Failed to install docker. Retry"
Girish Ramakrishnan's avatar
Girish Ramakrishnan committed
60 61 62
        sleep 1
    done

Girish Ramakrishnan's avatar
Girish Ramakrishnan committed
63
    rm /tmp/containerd.deb /tmp/docker-ce-cli.deb /tmp/docker.deb
Girish Ramakrishnan's avatar
Girish Ramakrishnan committed
64 65
fi

Girish Ramakrishnan's avatar
Girish Ramakrishnan committed
66
echo "==> installer: updating node"
Girish Ramakrishnan's avatar
Girish Ramakrishnan committed
67 68 69 70 71 72
if [[ "$(node --version)" != "v10.18.1" ]]; then
    mkdir -p /usr/local/node-10.18.1
    $curl -sL https://nodejs.org/dist/v10.18.1/node-v10.18.1-linux-x64.tar.gz | tar zxvf - --strip-components=1 -C /usr/local/node-10.18.1
    ln -sf /usr/local/node-10.18.1/bin/node /usr/bin/node
    ln -sf /usr/local/node-10.18.1/bin/npm /usr/bin/npm
    rm -rf /usr/local/node-10.15.1
Girish Ramakrishnan's avatar
Girish Ramakrishnan committed
73 74
fi

Girish Ramakrishnan's avatar
Girish Ramakrishnan committed
75
# this is here (and not in updater.js) because rebuild requires the above node
76
for try in `seq 1 10`; do
Girish Ramakrishnan's avatar
Girish Ramakrishnan committed
77
    # for reasons unknown, the dtrace package will fail. but rebuilding second time will work
78 79 80 81 82

    # 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
83
    echo "==> installer: Failed to rebuild, trying again"
Girish Ramakrishnan's avatar
Girish Ramakrishnan committed
84 85
    sleep 5
done
86

87
if [[ ${try} -eq 10 ]]; then
88
    echo "==> installer: npm rebuild failed, giving up"
89 90 91
    exit 4
fi

92
echo "==> installer: downloading new addon images"
Girish Ramakrishnan's avatar
typo  
Girish Ramakrishnan committed
93
images=$(node -e "var i = require('${box_src_tmp_dir}/src/infra_version.js'); console.log(i.baseImages.map(function (x) { return x.tag; }).join(' '), Object.keys(i.images).map(function (x) { return i.images[x].tag; }).join(' '));")
94 95 96

echo -e "\tPulling docker images: ${images}"
for image in ${images}; do
Girish Ramakrishnan's avatar
Girish Ramakrishnan committed
97 98 99 100 101 102 103 104
    if ! docker pull "${image}"; then           # this pulls the image using the sha256
        echo "==> installer: Could not pull ${image}"
        exit 5
    fi
    if ! docker pull "${image%@sha256:*}"; then  # this will tag the image for readability
        echo "==> installer: Could not pull ${image%@sha256:*}"
        exit 6
   fi
105 106
done

Johannes Zellner's avatar
Johannes Zellner committed
107
echo "==> installer: update cloudron-syslog"
108
CLOUDRON_SYSLOG_DIR=/usr/local/cloudron-syslog
109
CLOUDRON_SYSLOG="${CLOUDRON_SYSLOG_DIR}/bin/cloudron-syslog"
Johannes Zellner's avatar
Johannes Zellner committed
110
CLOUDRON_SYSLOG_VERSION="1.0.3"
111
while [[ ! -f "${CLOUDRON_SYSLOG}" || "$(${CLOUDRON_SYSLOG} --version)" != ${CLOUDRON_SYSLOG_VERSION} ]]; do
112 113
    rm -rf "${CLOUDRON_SYSLOG_DIR}"
    mkdir -p "${CLOUDRON_SYSLOG_DIR}"
Johannes Zellner's avatar
Johannes Zellner committed
114
    if npm install --unsafe-perm -g --prefix "${CLOUDRON_SYSLOG_DIR}" cloudron-syslog@${CLOUDRON_SYSLOG_VERSION}; then break; fi
115 116
    echo "===> installer: Failed to install cloudron-syslog, trying again"
    sleep 5
Girish Ramakrishnan's avatar
Girish Ramakrishnan committed
117
done
Johannes Zellner's avatar
Johannes Zellner committed
118

Girish Ramakrishnan's avatar
Girish Ramakrishnan committed
119
if ! id "${USER}" 2>/dev/null; then
120 121 122
    useradd "${USER}" -m
fi

123
if [[ "${is_update}" == "yes" ]]; then
124 125
    echo "==> installer: stop cloudron.target service for update"
    ${BOX_SRC_DIR}/setup/stop.sh
126 127
fi

128 129 130
# ensure we are not inside the source directory, which we will remove now
cd /root

131
echo "==> installer: switching the box code"
132 133
rm -rf "${BOX_SRC_DIR}"
mv "${box_src_tmp_dir}" "${BOX_SRC_DIR}"
134
chown -R "${USER}:${USER}" "${BOX_SRC_DIR}"
135

136
echo "==> installer: calling box setup script"
137
"${BOX_SRC_DIR}/setup/start.sh"