cloudron-e2e-test/prepareDeployTarget.sh
2017-02-07 12:11:59 +01:00

121 lines
3.5 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 SSH_KEY_DIR="/home/ubuntu/.ssh/"
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-6.9.5
curl -sL https://nodejs.org/dist/v6.9.5/node-v6.9.5-linux-x64.tar.gz | tar zxvf - --strip-components=1 -C /usr/local/node-6.9.5
ln -sf /usr/local/node-6.9.5/bin/node /usr/bin/node
ln -sf /usr/local/node-6.9.5/bin/npm /usr/bin/npm
npm config set prefix /usr/local
# copy ssh certs
mkdir -p "${SSH_KEY_DIR}"
mv /tmp/id_rsa* "${SSH_KEY_DIR}"
chmod 600 ${SSH_KEY_DIR}/id_rsa_*
# 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"
# release tool
rm -rf "${HOME}/release" && mv /tmp/release "${HOME}"
cd "${HOME}/release" && npm install && npm rebuild
# cloudron tool
rm -rf "${HOME}/cloudron-cli" && mv /tmp/cloudron-cli "${HOME}"
cd "${HOME}/cloudron-cli" && npm install && npm rebuild && npm link
# 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