Skip to content

Instantly share code, notes, and snippets.

@amitaibu
Created October 31, 2016 18:16
Show Gist options
  • Save amitaibu/53ca77342b3544849b16e029aa4a5221 to your computer and use it in GitHub Desktop.
Save amitaibu/53ca77342b3544849b16e029aa4a5221 to your computer and use it in GitHub Desktop.
#! /bin/bash
gcloud compute instances create live-instance13-primary \
--machine-type=n1-standard-1 \
--image=ubuntu-14-04 \
--metadata-from-file startup-script=startup-script.sh \
--zone us-central1-f \
--tags http-server
gcloud compute instances create live-instance13-secondary \
--machine-type=n1-standard-1 \
--image=ubuntu-14-04 \
--metadata-from-file startup-script=startup-script.sh \
--zone us-central1-f \
--tags http-server
#! /bin/bash
# Install logging monitor and configure it to pickup application logs
# [START logging]
set -x
curl -s "https://storage.googleapis.com/signals-agents/logging/google-fluentd-install.sh" | bash
cat >/etc/google-fluentd/config.d/nodeapp.conf << EOF
<source>
type tail
format json
path /opt/app/request.log
pos_file /var/tmp/fluentd.nodeapp-request.pos
tag nodeapp-request
</source>
<source>
type tail
format json
path /opt/app/error.log
pos_file /var/tmp/fluentd.nodeapp-error.pos
tag nodeapp-error
</source>
<source>
type tail
format json
path /opt/app/general.log
pos_file /var/tmp/fluentd.nodeapp-general.pos
tag nodeapp-general
</source>
EOF
service google-fluentd restart &
# [END logging]
# Add nodejs repository (NodeSource)
curl -sL https://deb.nodesource.com/setup | bash -
# Install dependencies from apt
apt-get install -y git nodejs build-essential
# Install Docker
wget -qO- https://get.docker.com/ | sh
# Get the source code
git clone https://github.com/shoov/shoov-nodejs.git /opt/app
cd /opt/app
git checkout tags/v0.1.8
# Add config
cat >/opt/app/config.json << EOF
{
"debug": false,
"backend_url": "https://example.com",
"loggly_token": "LOGGLY-TOKEN",
"loggly_subdomain": "LOGGLY_SUBDOMAIN",
"loggly_uuid": "shoov-nodejs-live",
"php_ci_docker_image": "shoov/php-ci:v0.0.3",
"pr_docker_image": "shoov/github-pr:v0.0.1",
"selenium_docker_image": "elgalu/selenium@sha256:dc7568c79355b6bde63706165b07f3c22e64e5749e12ab3591e5160776e09b1b",
"vnc_passowrd": "VNC-PASSWORD",
"docker_startup_timeout": 30,
"docker_run_timeout": 1200
}
EOF
# Create a nodeapp user. The application will run as this user.
useradd -m -d /home/nodeapp nodeapp
chown -R nodeapp:nodeapp /opt/app
# Install app dependencies
npm install
# Prevent "Attempt to unlock" error on "npm install"
chown -R nodeapp ~/.npm
chown -R nodeapp /usr/local/lib/node_modules
# Use Docker as "nodeapp" user
usermod -aG docker nodeapp
# Pull docker images
docker pull shoov/php-ci:v0.0.3
docker pull elgalu/selenium@sha256:dc7568c79355b6bde63706165b07f3c22e64e5749e12ab3591e5160776e09b1b
npm install -g pm2
# @todo: Install and configure log rotation
# pm2 install pm2-logrotate
# pm2 set pm2-logrotate:max_size 10M
# pm2 startup ubuntu
# su -c "chmod +x /etc/init.d/pm2-init.sh && update-rc.d pm2-init.sh defaults"
sudo -H -u nodeapp bash -c 'PORT="8080" pm2 start bin/www --name=shoov-nodejs; pm2 show 0'
# Verify Docker is installed
docker
# Application should now be running under pm2
# [END startup]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment