From da25dd0d9c79b3281a15b0ecc2b4b1bf67ee372b Mon Sep 17 00:00:00 2001 From: Devin Howard Date: Mon, 28 Mar 2016 10:38:10 +0800 Subject: [PATCH] add production deploy/update steps (see issue #518) --- doc/production/first-deploy.md | 108 +++++++++++++++++++++++++++++++++ doc/production/pull-changes.md | 35 +++++++++++ 2 files changed, 143 insertions(+) create mode 100644 doc/production/first-deploy.md create mode 100644 doc/production/pull-changes.md diff --git a/doc/production/first-deploy.md b/doc/production/first-deploy.md new file mode 100644 index 00000000..194fb75c --- /dev/null +++ b/doc/production/first-deploy.md @@ -0,0 +1,108 @@ +#### Install passenger (e.g. Ubuntu Trusty like this:) + + sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 \ + --recv-keys 561F9B9CAC40B2F7 + sudo apt-get install -y apt-transport-https ca-certificates + + # Add APT repository + sudo sh -c 'echo deb https://oss-binaries.phusionpassenger.com/apt/passenger trusty main > /etc/apt/sources.list.d/passenger.list' + sudo apt-get update + + # Install Passenger + Nginx (try apache if you would rather not change + # nginx) + sudo apt-get install -y nginx-extras passenger + +#### Setup Postgres + + sudo apt-get install postgresql-9.4 #specify version!! + sudo -u postgres psql + postgres=# CREATE USER metamaps WITH PASSWORD 'mycoolpassword' CREATEDB; + postgres=# CREATE DATABASE metamap002_production OWNER metamaps; + postgres=# \q + +#### Install system-wide rvm: + + sudo gpg --keyserver hkp://keys.gnupg.net \ + --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 + \curl -sSL https://get.rvm.io | sudo bash -s stable + echo "gem: --no-ri --no-rdoc" | sudo tee -a /etc/gemrc + +#### Create user and setup gemsets for that user + + sudo adduser --disabled-password metamaps + sudo adduser metamaps rvm + +#### Clone github repo and install gems + + sudo su - metamaps + rvm user gemsets + git clone https://github.com/metamaps/metamaps_gen002 \ + --branch instance/mycoolinstance + rvm install $(cat metamaps_gen002/.ruby-version) #ensure ruby is installed + cd metamaps_gen002 + gem install bundle + bundle install + +#### Connect rails database + +Run this in the metamaps_gen002 directory, still as metamaps: + + cp .example-env .env + nano .env # fill in DB_* values, and realtime server at least. Ctrl+X to save/exit. + export RAILS_ENV=production + rake db:setup #create, load schema, seed + +Now set up nginx - config stored on Linode, including relevant environment +variables. + +Get an SSL certificate and encrypt it for the realtime video. + +#### And finally + + passenger-config restart-app /home/metamaps/metamaps_gen002 + +If this command fails, it may be helpful for debugging to run a test server to +see what problems show up: + + RAILS_ENV=production rails server + +#### Realtime server: + + sudo aptitude install nodejs npm + sudo ln -s /usr/bin/nodejs /usr/bin/node + sudo npm install -g forever + (crontab -u metamaps -l 2>/dev/null; echo "@reboot $(which forever) --append -l /home/metamaps/logs/forever.realtime.log start /home/metamaps/metamaps_gen002/realtime/realtime-server.js") | crontab -u metamaps - + + cd /home/metamaps/metamaps_gen002/realtime + npm install + mkdir -p /home/metamaps/logs + forever --append -l /home/metamaps/logs/forever.realtime.log \ + start /home/metamaps/metamaps_gen002/realtime/realtime-server.js + +#### Upstart service for delayed_worker: + +Put the following code into `/etc/init/metamaps_delayed_worker.conf`: + + description "Delayed Jobs Worker for Metamaps" + + start on runlevel [2345] + stop on runlevel [!2345] + + setuid metamaps + setgid metamaps + chdir /home/metamaps/metamaps_gen002 + + env HOME=/home/metamaps + env PATH="/usr/local/rvm/gems/ruby-2.1.3@metamaps_gen002/bin:/usr/local/rvm/gems/ruby-2.1.3@global/bin:/usr/local/rvm/rubies/ruby-2.1.3/bin:/usr/local/rvm/bin:/usr/local/bin:/usr/bin:/bin" + env GEM_PATH="/usr/local/rvm/gems/ruby-2.1.3@metamaps_gen002:/usr/local/rvm/gems/ruby-2.1.3@global" + env RAILS_ENV="production" + + respawn + respawn limit 3 30 + + exec bundle exec rake jobs:work + +Then start the service and check the last ten lines of the log file to make sure it's running OK: + + sudo service metamaps_delayed_job start + tail /var/log/upstart/metamaps_delayed_job.log diff --git a/doc/production/pull-changes.md b/doc/production/pull-changes.md new file mode 100644 index 00000000..1586e6e0 --- /dev/null +++ b/doc/production/pull-changes.md @@ -0,0 +1,35 @@ +## How to pull changes from github to an instance: + +These are the steps we use to pull production code to our server. Feel free to adapt them to your own use. We are using an Ubuntu 14.04 server. + +First, run one of these two code blocks. The first is if you've merged the code into the instance/mycoolinstance already. The second is if you'd like to make the merge on the server. + +EITHER THIS + + git checkout instance/mycoolinstance + git fetch origin/instance/mycoolinstance + git reset --hard origin/instance/mycoolinstance + +OR + + git checkout instance/mycoolinstance + git fetch origin master + git merge origin/master + +Now that you have the code, run these commands: + + export RAILS_ENV=production + source .env + + bundle install + rake db:migrate + rake assets:precompile + rake perms:fix + passenger-config restart-app . + + cd realtime + npm install + forever list #find the uid, e.g. xQKv + forever restart xQKv + + sudo service metamaps_delayed_job restart