deploy snippets

How to use Vlad the Deployer with git, nginx, mongrel, mongrel_cluster and Rails

Tagged vlad, deployer, deploy, capistrano, nginx, mongrel, mongrel_cluster  Languages ruby

This is a draft...

Installing Vlad the Deployer

gem install vlad

Configuring Vlad the Deployer

Add this to the end of RakeFile:

begin
  require 'rubygems'
  require 'vlad'
  Vlad.load :scm => :git
rescue LoadError => e
  puts "Unable to load Vlad #{e}."
end

Note that we're telling Vlad to use git. This snippet gives you a quick introduction on how to use git with Rails.

Creating the deployment recipe

If you're uncertain what these variables mean, have a look at the docs. This folder is also worth a look, and don't forget to take a peek at the vlad source code.

#
# General configuration
#
set :ssh_flags,             '-p 666'
set :application,           'xxx.com'
set :domain,                '127.0.01'
set :deploy_to,             '/var/www/xxx.com'
set :repository,            '/var/lib/git/repositories/xxx.com/.git/'


#
# Mongrel configuration
#
set :mongrel_clean,         true
set :mongrel_command,       'sudo mongrel_rails'
set :mongrel_group,         'www-data'
set :mongrel_port,          9000
set :mongrel_servers,       3

#set :mongrel_address,       '127.0.0.1'
#set(:mongrel_conf)          { '#{shared_path}/mongrel_cluster.conf' }
#set :mongrel_config_script, nil
#set :mongrel_environment,   'production'
#set :mongrel_log_file,      nil
#set :mongrel_pid_file,      nil
#set :mongrel_prefix,        nil
#set :mongrel_user,          'mongrel'

#
# Customize Vlad to our needs
#
namespace :vlad do
  #
  # Add an after_update hook
  #
  remote_task :update do
    Rake::Task['vlad:after_update'].invoke
  end

  #
  # The after_update hook, which is run after vlad:update
  #
  remote_task :after_update do
  # Link to shared resources, if you have them in .gitignore
  #  run "ln -s #{deploy_to}/shared/system/database.yml #{deploy_to}/current/config/database.yml"
  end

  #
  # Deploys a new version of your application
  #
  remote_task :deploy => [:update, :migrate, :start_app]
end

Setup the server

$ rake vlad:setup

This will create the necessary folders and mongrel_cluster configuration file.

Deploy the application

Now deploy the application with vlad:deploy, which is a custom rake task that we added to the deployment recipe:

$ rake vlad:deploy

Copying your SSH public key to the remote server

Vlad uses ssh for executing commands on the remotely, and rsync for copying the build to your server, which means you'll quickly grow tired of typing your password each time a command is run.

This problem is solved by copying your public SSH keys to the remote server, this snippet explains how to do exactly that.

"updating paths is incompatible with switching branches/forcing" error when using git branches with Vlad

Tagged vlad, git, branch, deploy, revisions, tags  Languages 

I ended up writing this snippet after I found out that deploying a git branch with vlad wasn't as easy as it should've been...

I assumed the Vlad documentation was right, so I put the branch name in the revision variable (located in config/deploy.rb):

set :revision,              'branch_name'

But then I got this error when executing rake vlad:deploy:

git checkout: updating paths is incompatible with switching branches/forcing
Did you intend to checkout 'branch_name' which can not be resolved as commit?

I managed to solve the problem by using the SHA1 hash of the branch instead of the branch name, but that only worked for the current revision:

set :revision,              'd34360870be6536992e6d45bb0aa72eca31e14443'

So after some googling I found this post at scie.nti.st, which made my day because it has the following examples of how to deploy tags and branches with git and Vlad:

# Deploy the latest code, this the default
set :revision, "HEAD" 

# Deploy branch "origin/branch_name"
set :revision, "origin/branch_name"
 
# Deploy tag "1.0"
set :revision, "1.0"

Note that you have to push the branch to the remote server before running rake vlad:deploy:

git push origin branch_name

Elixir Deployment

Tagged deployment, elixir, exrm, deploy, rollback, upgrade, distillery  Languages bash, elixir

Setup

Development environment:

  1. Sign up for a Gitlab account, or use your favorite git server
  2. Create a (private) repository
  3. Create a deploy key that allows read-only access to your private repository (optional, if public)
  4. Commit code to the repository

Pre-requisites

You will need Elixir:

$ sudo apt-get install elixir

Code

Add distillery to the list of dependencies:

def deps do
   ...
   {:distillery, "~> 0.9.9"},
   ...
 end

Run mix release.init and follow the instructions:

$ mix release.init

An example config file has been placed in rel/config.exs, review it,
make edits as needed/desired, and then run `mix release` to build the release

Next, run mix release:

You can run it in one of the following ways:
  Interactive: rel/xxx/bin/xxx console
  Foreground: rel/xxx/bin/xxx foreground
  Daemon: rel/xxx/bin/xxx start

Deployment

Production environment:

  1. Clone project
$ cd /var/www
$ git clone git@gitlab.com:christianhellsten/aktagon-snippets.git
  1. Create a release
$ MIX_ENV=prod mix do deps.get, clean, compile
$ MIX_ENV=prod mix release --verbosity=verbose
  1. Create a monit start script
#
# Run "ps -ef | grep beam, if you get status "Does not exist" from "monit status"
#
check process snippets MATCHING "rel/snippets"
    start program = "/bin/su - deploy -c '/var/www/aktagon-snippets/rel/snippets/bin/snippets start'"
    stop program = "/bin/su - deploy -c '/var/www/aktagon-snippets/rel/snippets/bin/snippets stop'"
  1. Start server
$ sudo monit start snippets

Deploying a new version

First increment the version number. Then perform the hot upgrade:

$ git pull
$ export MIX_ENV=prod
$ mix do compile
$ mix release --upgrade 

$ rel/snippets/bin/snippets upgrade "0.0.2"

Rolling back to a previous version

How to perform hot downgrades:

$ rel/snippets/bin/snippets downgrade "0.0.1"

See https://hexdocs.pm/distillery/getting-started.html

Phoenix

See Using Distillery With Phoenix.

Alternatives