Skip to content

Instantly share code, notes, and snippets.

@nicoandmee
Created March 26, 2019 03:09
Show Gist options
  • Save nicoandmee/cbad69c2e6ae1b7a87bfd24a6df4565b to your computer and use it in GitHub Desktop.
Save nicoandmee/cbad69c2e6ae1b7a87bfd24a6df4565b to your computer and use it in GitHub Desktop.
puppeteer on ubuntu linux ami (xvfb setup) + cloudwatch metric for autoscaling
#!/bin/bash
export HOME=/home/ubuntu
export PM2_HOME=$HOME/.pm2
export NODE_ENV=production
export NPM_TOKEN="TOKEN"
export LOCAL_HOSTNAME=$(curl http://169.254.169.254/latest/meta-data/local-hostname)
export PUBLIC_HOSTNAME=$(curl http://169.254.169.254/latest/meta-data/public-hostname)
TIMEZONE=$(echo "America/Chicago
America/New_York
America/Los_Angeles" | shuf -n 1)
echo "Setting timezone $TIMEZONE"
timedatectl set-timezone $TIMEZONE
sleep 5
cat <<EOF > ~/.npmrc
//registry.npmjs.org/:_authToken=\${NPM_TOKEN}
EOF
curl -sL https://deb.nodesource.com/setup_9.x | bash -
apt-get update
apt-get install -y libopencv-dev libpoppler-qt5-dev libcairo2-dev libjpeg-dev libpango1.0-dev libgif-dev build-essential g++ tesseract-ocr nodejs \
gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 \
libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 \
libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 \
libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 \
ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget libnss3-tools \
libdatetime-perl redis-server htop jq xvfb
# setup xvfb init.d service
cat <<EOF > /etc/systemd/system/xvfb.service
[Unit]
Description=Virtual Frame Buffer X Server
After=network.target
[Service]
ExecStart=/usr/bin/Xvfb :99 -screen 0 1x1x24 -ac +extension GLX +render -noreset
[Install]
WantedBy=multi-user.target
EOF
systemctl enable xvfb.service
systemctl start xvfb.service
export XVFB_DISPLAY=:99
cd $HOME
cat <<EOF > check-version-update.sh
#!/bin/sh
LOCKFILE=/tmp/check-version-update.lock
if [ -e \${LOCKFILE} ] && kill -0 \`cat \${LOCKFILE}\`; then
echo "already running"
exit
fi
# make sure the lockfile is removed when we exit and then claim it
trap "rm -f \${LOCKFILE}; exit" INT TERM EXIT
echo \$\$ > \${LOCKFILE}
export HOME=$HOME
export PM2_HOME=$PM2_HOME
export NPM_TOKEN=$NPM_TOKEN
LATEST_VERSION=\$(npm view @username/yourpackage dist-tags.latest 2>/dev/null)
CURRENT_VERSION=\$(npm ls @username/yourpackage --global --json --depth=0 | jq '.dependencies."@username/yourpackage".version' --raw-output 2>/dev/null)
echo "Current: \$CURRENT_VERSION"
echo "Latest: \$LATEST_VERSION"
if [ \$CURRENT_VERSION = \$LATEST_VERSION ]; then
echo "current version is latest. skipping update sequence"
exit 0
fi
npmInstallLatestVersion() {
echo 'npm install'
sudo -E npm i --unsafe-perm -g @username/yourpackage@\$LATEST_VERSION || { echo 'failed npm install'; return 1; }
}
npmCacheClean() {
echo 'npm cache clean'
sudo -E npm cache clean --force || { echo 'failed npm cache clean'; return 1; }
}
rmRfyourpackage() {
echo 'rm -rf yourpackage'
sudo rm -rf /usr/lib/node_modules/@username/yourpackage || { echo 'failed rm -rf yourpackage'; return 1; }
}
npmInstallLatestVersion || (npmCacheClean && rmRfyourpackage && npmInstallLatestVersion)
echo "Reloading PM2"
sudo -E pm2 reload all
rm -f \${LOCKFILE}
EOF
chmod +x check-version-update.sh
npm i --silent --unsafe-perm -g @username/yourpackage@latest pm2@latest
pm2 install pm2-logrotate
pm2 set pm2-logrotate:retain 1
pm2 start -i 12 --node-args="--harmony" bots
cd $HOME
git clone -q https://github.com/nicoandmee/aws-scripts-mon
echo "* * * * * ubuntu cd $HOME/aws-scripts-mon && ./mon-put-instance-data.pl --count-process --from-cron --aggregated=only --auto-scaling=only" >> /etc/crontab
echo "* * * * * root cd $HOME && ./check-version-update.sh" >> /etc/crontab
echo "* * * * * root killall --older-than 60m chrome" >> /etc/crontab
echo "* * * * * root cd $HOME && ./start.sh" >> /etc/crontab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment