module Unfold

Public Class Methods

config(env_arrays) { |config,env| ... } click to toggle source

the method used in the config file

# File lib/unfold.rb, line 20
def self.config(env_arrays)
  return [] if block_given? == false
  configs = []
  env_arrays.each do |env|
    config = Config.new
    config.env = env.to_s
    yield(config,env.to_s)
    configs << config
  end
  return configs
end
post_receive_script(c) click to toggle source

creates a post-receive hook script from an Unfold::Config object

# File lib/unfold.rb, line 33
  def self.post_receive_script(c)
    return %{#!/bin/bash
read oldrev newrev refname
      
deploy() {
  if [ "$refname" = "refs/heads/master" ]; then
    unset GIT_DIR
    cd #{c.remote_destination}
    git pull origin master > /dev/null 2> /dev/null
    echo "\rUNFOLD: Deployed $newrev"
    if [ -f config/unfold_postdeploy.rb ]; then
      bash -lc "source ~/.rvm/scripts/rvm; ruby config/unfold_postdeploy.rb"
    fi
  fi
}
deploy}
  end
read_config() click to toggle source

loads config from config ruby script

# File lib/unfold.rb, line 52
def self.read_config
  eval(File.read(File.git_root + CONFIG_RELATIVE))
end
rollback_to(envi, revision) click to toggle source
# File lib/unfold.rb, line 78
def self.rollback_to(envi, revision)
  Unfold.read_config.each do |c|
    if c.env == envi
      Unfold.ssh c do |ssh, paths| 
        print ssh.exec!("cd #{c.remote_destination}; git reset --hard #{revision}")
      end
    end
  end
end
setup_local() click to toggle source
# File lib/unfold.rb, line 88
def self.setup_local
  # set up the local git repo's remotes
  Unfold.read_config.each do |c|
    repo_path = "~/.unfold/repos/#{c.appname}-#{c.env}.git"
    `git remote add #{c.env} #{c.remote_user}@#{c.remote_host}:#{repo_path}`
  end
end
setup_remote() click to toggle source
# File lib/unfold.rb, line 96
def self.setup_remote
  Unfold.read_config.each do |c|
    Unfold.ssh c do |ssh, paths|
      # create the remote repo
      ssh.exec!("mkdir -p #{c.remote_destination}; mkdir -p #{paths[:repo]}; cd #{paths[:repo]}; git --bare init")
      
      # upload the post-receive hook
      script = StringIO.new(Unfold.post_receive_script(c))
      dest = "#{paths[:repo]}/hooks/post-receive"
      ssh.scp.upload!(script, dest)
      
      # make it executable
      ssh.exec!("chmod 775 #{paths[:repo]}/hooks/post-receive")
      
      # move to the deployment target and locally clone the repo
      ssh.exec!("cd #{c.remote_destination}; git clone #{paths[:repo]} .")
    end
  end
end
ssh(c) { |ssh,{ :repo => repo_path }| ... } click to toggle source

DRYs up the setup and teardown code

# File lib/unfold.rb, line 57
def self.ssh(c)
  return nil if block_given? == false
  Net::SSH.start(c.remote_host, c.remote_user) do |ssh|
    home_path = ssh.exec!("cd; pwd").strip # home directory
    repo_path = "#{home_path}/.unfold/repos/#{c.appname}-#{c.env}.git"
    yield(ssh,{ :repo => repo_path })
  end
end
teardown(local, remote) click to toggle source
# File lib/unfold.rb, line 66
def self.teardown(local, remote)
  Unfold.read_config.each do |c|
    `git remote remove #{c.env}` if local == true
    
    if remote == true
      Unfold.ssh c do |ssh, paths| 
        ssh.exec!("rm -rf #{paths[:repo]}") 
      end 
    end
  end
end