Module: Unfold

Defined in:
lib/unfold.rb

Defined Under Namespace

Classes: Config

Class Method Summary collapse

Class Method Details

.config(env_arrays) ⇒ Object

the method used in the config file



20
21
22
23
24
25
26
27
28
29
30
# 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) ⇒ Object

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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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_configObject

loads config from config ruby script



52
53
54
# File 'lib/unfold.rb', line 52

def self.read_config
  eval(File.read(File.git_root + CONFIG_RELATIVE))
end

.rollback_to(envi, revision) ⇒ Object



78
79
80
81
82
83
84
85
86
# 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_localObject



88
89
90
91
92
93
94
# 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_remoteObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# 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) ⇒ Object

DRYs up the setup and teardown code



57
58
59
60
61
62
63
64
# 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) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# 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