cloudron-e2e-test/prepareDeployTarget.sh

104 lines
3.0 KiB
Bash
Raw Normal View History

2015-07-23 19:02:16 +00:00
#!/bin/bash
######################################
## This script is run on the server ##
######################################
set -eux -o pipefail
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}" ]] && sudo swapoff "${swap_file}"
if [[ "${ENV}" == "dev" ]]; then
sudo fallocate -l 2048m "${swap_file}" # t1.micro has 1GB RAM
elif [[ "${ENV}" == "staging" ]]; then
sudo fallocate -l 2048m "${swap_file}" # t1.micro has 1GB RAM
else
sudo fallocate -l 2048m "${swap_file}" # t1.micro has 1GB RAM
fi
sudo chmod 600 "${swap_file}"
sudo mkswap "${swap_file}"
sudo swapon "${swap_file}"
if ! grep "${swap_file}" /etc/fstab; then
sudo bash -c "echo '${swap_file} none swap sw 0 0' >> /etc/fstab"
fi
# install git
sudo apt-get update
sudo apt-get install -y git
# install node v0.12 https://github.com/nodesource/distributions
sudo apt-get install -y python-software-properties python g++ make
curl -sL https://deb.nodesource.com/setup_0.12 | sudo bash -
sudo apt-get install -y nodejs
# copy over ssh keys
sudo mv "/tmp/id_rsa" "${HOME}/.ssh/id_rsa"
sudo mv "/tmp/id_rsa.pub" "${HOME}/.ssh/id_rsa.pub"
echo -e "Host *.cloudron.me\n StrictHostKeyChecking no" > ${HOME}/.ssh/config
# install tmpreaper (runs everyday and clean tmp)
sudo apt-get install -y tmpreaper
sudo sed -e 's/SHOWWARNING=true/# SHOWWARNING=true/' -i /etc/tmpreaper.conf
# install pm2
sudo npm install -g pm2 pm2-run forever
sudo rm -rf ~/.npm # .npm will get owned by root after "sudo npm install -g"
# 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}"
# run pm2 as current user ubuntu to avoid pm2 running as root below
pm2 status
# ensure we run the latest version, this might not be the case if this script is ran against a already prepared server
pm2 updatePM2
# setup the init scripts
sudo pm2 startup -u ubuntu