terraform-digitalocean-spoke/spoke-app-provision

158 lines
4.2 KiB
Plaintext
Raw Permalink Normal View History

2020-02-10 03:28:45 +00:00
#!/usr/bin/env bash
set -o errexit
set -o pipefail
main() {
local target="${1:-system0}"
"_run_${target}"
}
_run_system0() {
set -o xtrace
2020-02-12 01:02:22 +00:00
cd /tmp
2020-02-10 03:28:45 +00:00
sudo swapon --show | if ! grep -q /swap; then
sudo fallocate -l 8G /swap
sudo chmod 600 /swap
sudo mkswap -L swap /swap
sudo swapon /swap
fi
if ! grep -q ^LABEL=swap /etc/fstab &>/dev/null; then
echo 'LABEL=swap none swap sw 0 0' | sudo tee -a /etc/fstab
fi
sudo sysctl vm.swappiness=10
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf
sudo sysctl vm.vfs_cache_pressure=50
echo 'vm.vfs_cache_pressure=50' |
sudo tee /etc/sysctl.d/99-cache-pressure.conf
sudo apt-get update -y
sudo apt-get install -y \
build-essential \
ca-certificates \
curl \
git \
gnupg \
2020-02-12 01:02:22 +00:00
nginx-full \
2020-02-10 03:28:45 +00:00
redis
2020-02-12 01:02:22 +00:00
dpkg --get-selections || true |
if ! grep -qE '^postgresql-client-13.+install$'; then
2020-02-12 01:02:22 +00:00
echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" |
sudo tee /etc/apt/sources.list.d/pgdg.list &>/dev/null
curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update -y
sudo apt-get install -y postgresql-13 postgresql-client-13
2020-02-12 01:02:22 +00:00
fi
pg_ctlcluster 13 main start
2020-02-12 01:02:22 +00:00
sudo -H -u postgres bash <<PGSETUP
set -o allexport
2020-03-05 01:46:02 +00:00
# shellcheck source=/dev/null
2020-02-12 01:02:22 +00:00
source /tmp/app.env
createuser spoke || true
createdb --owner=spoke spoke || true
psql -c "GRANT ALL ON DATABASE spoke TO spoke"
set +o xtrace
psql -c "ALTER USER spoke WITH PASSWORD '\${DB_PASSWORD}';"
PGSETUP
if ! command -v yarn; then
curl -fsSL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" |
sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt-get update -y
sudo apt-get install -y --no-install-recommends yarn
fi
2020-02-10 03:28:45 +00:00
if ! getent passwd spoke; then
sudo useradd --create-home --comment 'Spoke app' spoke
fi
sudo chsh -s /bin/bash spoke
sudo chown -R spoke:spoke /home/spoke
mv -v /tmp/spoke.crt /home/spoke/spoke.crt
mv -v /tmp/spoke.key /home/spoke/spoke.key
chmod 0600 /home/spoke/spoke.crt /home/spoke/spoke.key
chown spoke /home/spoke/spoke.crt /home/spoke/spoke.key
cp -v /tmp/nginx-sites-default.conf /etc/nginx/sites-available/default
if [[ -s /tmp/nginx-sites-default-override.conf ]]; then
cp -v /tmp/nginx-sites-default-override.conf /etc/nginx/sites-available/default
fi
ln -svf /etc/nginx/sites-available/default /etc/nginx/sites-enabled/default
sha1sum /etc/nginx/sites-available/default
systemctl restart nginx
2020-02-10 03:28:45 +00:00
}
_run_system1() {
set -o xtrace
sudo cp -v /tmp/spoke.service /etc/systemd/system/spoke.service
sudo systemctl enable spoke
sudo systemctl stop spoke || true
sudo systemctl start spoke || true
}
_run_spoke0() {
set -o xtrace
set -o allexport
2020-03-05 01:46:02 +00:00
# shellcheck source=/dev/null
source /tmp/app.env
set +o allexport
2020-02-10 03:28:45 +00:00
git --version
if [[ ! -d /home/spoke/app/.git ]]; then
git clone https://github.com/MoveOnOrg/Spoke.git /home/spoke/app
fi
2020-02-10 03:28:45 +00:00
cd /home/spoke/app
2020-03-05 02:17:18 +00:00
git fetch
git checkout -qf "${TERRAFORM_SPOKE_VERSION}"
2020-02-10 03:28:45 +00:00
cp -v /tmp/spoke-app-run /home/spoke/spoke-app-run
chmod +x /home/spoke/spoke-app-run
if ! command -v nvm; then
curl -fsSL \
https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
2020-02-10 03:28:45 +00:00
set +o errexit
set +o xtrace
2020-03-05 01:46:02 +00:00
# shellcheck source=/dev/null
2020-02-10 03:28:45 +00:00
source ~/.nvm/nvm.sh
set -o xtrace
set -o errexit
fi
nvm --version 2>/dev/null
nvm install 2>/dev/null
nvm use 2>/dev/null
2020-02-12 01:02:22 +00:00
cp -v /tmp/app.env /home/spoke/app/.env
2020-02-10 03:28:45 +00:00
sha1sum /home/spoke/app/.env
yarn --version
yarn install --ignore-scripts --non-interactive --frozen-lockfile
local git_head
git_head="$(cat .git/HEAD || true)"
local yarn_prod_build_ref
yarn_prod_build_ref="$(
cat /home/spoke/yarn_prod_build_ref 2>/dev/null || true
)"
if [[ "${git_head}" == "${yarn_prod_build_ref}" ]]; then
echo "skipping yarn run prod-build"
return
fi
yarn run prod-build
rm -rf ./node_modules
yarn install --production --ignore-scripts
2020-02-12 01:02:22 +00:00
echo "${git_head}" >/home/spoke/yarn_prod_build_ref
2020-02-10 03:28:45 +00:00
}
main "${@}"