cloudron-e2e-test/prepareDeployTarget.sh
2015-11-19 10:32:14 -08:00

111 lines
3.2 KiB
Bash

#!/bin/bash
######################################
## This script is run on the server ##
######################################
set -eux -o pipefail
readonly USER=ubuntu
readonly APP_DIR="/home/ubuntu/app"
readonly CERTS_DIR="/home/ubuntu/certs"
readonly APP_REPO_DIR="/home/ubuntu/app.git"
readonly INIT_MASTER_DIR="/tmp/repoMasterSetup"
readonly ECOSYSTEM="/home/ubuntu/ecosystem.json"
readonly SERVER_NAME="<%= serverName %>"
readonly ENV="<%= env %>"
swap_file="/swap"
[[ -f "${swap_file}" ]] && swapoff "${swap_file}"
if [[ "${ENV}" == "dev" ]]; then
fallocate -l 2048m "${swap_file}" # t1.micro has 1GB RAM
elif [[ "${ENV}" == "staging" ]]; then
fallocate -l 2048m "${swap_file}" # t1.micro has 1GB RAM
else
fallocate -l 2048m "${swap_file}" # t1.micro has 1GB RAM
fi
chmod 600 "${swap_file}"
mkswap "${swap_file}"
swapon "${swap_file}"
if ! grep "${swap_file}" /etc/fstab; then
bash -c "echo '${swap_file} none swap sw 0 0' >> /etc/fstab"
fi
# install git
apt-get update
apt-get install -y git
sudo -u ubuntu git config --global user.name e2etest
sudo -u ubuntu git config --global user.email e2etest@cloudron.io
# install node LTS
apt-get install -y python-software-properties python g++ make
mkdir -p /usr/local/node-4.2.2
curl -sL https://nodejs.org/dist/v4.2.2/node-v4.2.2-linux-x64.tar.gz | tar zxvf - --strip-components=1 -C /usr/local/node-4.2.2
ln -sf /usr/local/node-4.2.2/bin/node /usr/bin/node
ln -sf /usr/local/node-4.2.2/bin/npm /usr/bin/npm
npm config set prefix /usr/local
# install tmpreaper (runs everyday and clean tmp)
apt-get install -y tmpreaper
sed -e 's/SHOWWARNING=true/# SHOWWARNING=true/' -i /etc/tmpreaper.conf
# install pm2
npm install -g pm2 pm2-run forever
rm -rf ~/.npm # .npm will get owned by root after "npm install -g"
# installer
rm -rf "${HOME}/release" && mv /tmp/release "${HOME}"
cd "${HOME}/release" && npm install && npm rebuild
# create deploy directories
mkdir -p "${APP_DIR}"
# prepare git repo in case there is not yet one
if [[ ! -d "${APP_REPO_DIR}" ]]; then
mkdir -p "${APP_REPO_DIR}"
cd "${APP_REPO_DIR}"
git init --bare
# create stub master branch
mkdir -p "${INIT_MASTER_DIR}" && cd "${INIT_MASTER_DIR}"
git init
touch stub
git add stub
git commit -m "stub"
git push ${APP_REPO_DIR} master --force
cd && rm -rf "${INIT_MASTER_DIR}"
else
echo "Git repo already exists at ${APP_REPO_DIR}"
fi
# install post-receive hook
cat > "${APP_REPO_DIR}/hooks/post-receive" <<EOF
#!/bin/sh
set -eu
git --work-tree=${APP_DIR} --git-dir=${APP_REPO_DIR} checkout -f
cd ${APP_DIR}
pm2-run --ecosystem ${ECOSYSTEM} --env <%= env %> --cmd "npm prune && npm install --production"
pm2 startOrRestart ${ECOSYSTEM} --env <%= env %>
# save the new status for restart
pm2 save
EOF
chmod +x "${APP_REPO_DIR}/hooks/post-receive"
# create empty ecosystem.json
touch "${ECOSYSTEM}"
# fix ownership
chown -R $USER:$USER /home/$USER
# run pm2 as current user ubuntu to avoid pm2 running as root below
sudo -u $USER pm2 status
# ensure we run the latest version, this might not be the case if this script is ran against a already prepared server
sudo -u $USER pm2 updatePM2
# setup the init scripts
pm2 startup -u $USER